Exemplo n.º 1
0
    def dice_chip_in_new(self, user_id, user_channel_id, coin_id, bet_amount,
                         user_dice):
        change_type_31 = '31'  # 扣款
        change_type_32 = '32'  # 返还
        change_type_33 = '33'  # 中奖
        with MysqlTools().session_scope() as session:
            # 查询用户资产
            account_service = AccountService()
            user_info = account_service.get_inner_user_account_by_token(
                session, user_id, coin_id)
            if isinstance(user_info, int):
                if user_info == 20001:
                    return self.return_error(20001)

            balance = get_decimal(user_info.get("balance"),
                                  digits=8,
                                  decimal_type="down")
            # 账户余额 < 需要下注金额
            bet_number = get_decimal(bet_amount, digits=4, decimal_type="down")
            if balance < bet_number:
                return self.return_error(60005)
            dice_serial = dice_generate_phase()
            dice_time = get_timestamp()
            dice_part_in_model = DiceParticipateInModel(
                dice_serial=dice_serial,
                user_id=user_id,
                user_dice=user_dice,
                channel=user_channel_id,
                bet_number=bet_number,
                bet_token=coin_id,
                reward_token=coin_id,
                reward_quantity=bet_number,
                dice_timestamp=dice_time)
            session.add(dice_part_in_model)
            session.flush()
            # 提交扣款申请
            dice_part_id = dice_part_in_model._id
            result = account_service.do_bet(
                session, user_id, coin_id, bet_number,
                'dice_bet' + str(dice_part_in_model._id), 1, dice_serial,
                user_channel_id, change_type_31)
            if result != 0:
                session.rollback()
                raise_logger("do_bet result" + str(result), "game_bet_in",
                             "info")
            session.commit()
        return {
            'user_dice':
            user_dice,
            'dice_timestamp':
            timestamp_to_str(dice_time, format_style="%Y-%m-%d %H:%M:%S"),
            'dice_id':
            str(dice_part_id),
            'dice_serial':
            dice_serial
        }
Exemplo n.º 2
0
    def bet_in(self, dic):
        id = dic.get("game_instance_id", "")  # 项目id
        user_id = dic.get("user_id", "")  # 用户id
        user_channel_id = dic.get("user_channel_id", "")  # 用户下注渠道id
        conin_id = dic.get("conin_id", "")  # 投入币种id
        bet_amount = dic.get("bet_amount", "")  # 投注数量
        merge_id = dic.get("merge_id", -1)  # 合并投注id
        # transaction_password = dic.get("transaction_password", "")  # 交易密码

        with MysqlTools().session_scope() as session:
            # 查询项目
            model = session.query(GameDigitalInstanceModel).filter(
                GameDigitalInstanceModel._id == id).with_for_update().first()
            if model is None:
                return self.return_error(60001)
            if model.status == 1:
                return self.return_error(60002)
            if model.status == 2:
                return self.return_error(60003)
            if model.status == 0:
                can_bet = model.need - model.bet_number
                if can_bet <= 0:
                    return self.return_error(60002)
                if int(bet_amount) > can_bet:
                    return self.return_error(60004)

            if model.support_token >= 0 and int(
                    conin_id) != model.support_token and int(conin_id) != int(
                        _COIN_ID_EXP):
                return self.return_error(60008)

            # 查询币价 币种实际需要金额
            exchange_rate = get_exchange_rate(int(conin_id))

            # (投注数量 * 投注单位) / 投注币种兑换usdt比例
            bet_money = (int(bet_amount) *
                         model.bet_unit) / exchange_rate['price']

            # 体验金相关逻辑
            if int(conin_id) == int(_COIN_ID_EXP):
                # 检查本次投注体验金使用数量 (允许范围: 1~10)
                if int(bet_amount) < 1 or int(bet_amount) > 10:
                    self.return_error(60015)
                # 体验金使用额度
                exp = int(model.experience / 100 * model.need)
                # 检查项目体验金占比剩余额度以及个人当天最大体验金使用额度(每天最多用10个)
                limit = self.check_instance_exp_limit(session, user_id, id)
                instance_limit = limit['instance_limit']
                user_limit = limit['user_limit']
                if user_limit > 10 - int(bet_amount):
                    self.return_error(60017)
                if instance_limit >= exp:
                    self.return_error(60016)

            # 查询用户资产
            account_service = AccountService()
            user_info = account_service.get_inner_user_account_by_token(
                session, user_id, conin_id)
            if isinstance(user_info, int):
                if user_info == 20001:
                    return self.return_error(20001)

            balance = get_decimal(user_info.get("balance"),
                                  digits=8,
                                  decimal_type="down")

            # 账户余额 < 需要下注金额
            if balance < bet_money:
                return self.return_error(60005)

            # 验证交易密码
            # if account_service.check_pay_password(user_id, transaction_password) is False:
            #     return self.return_error(35024)
            # raise_logger("begin", "game_bet_in", "info")
            # 获取下注编号
            game_numbers = session.query(GameNumbersSetModel).filter(
                GameNumbersSetModel.game_serial ==
                model.game_serial).with_for_update().all()
            numbers = []
            for num in game_numbers:
                numbers.append(num.number)
            numbers = random.sample(numbers, int(bet_amount))
            # raise_logger("get game_numbers", "game_bet_in", "info")
            if isinstance(numbers, list):
                nick_name = account_service.get_inner_user_account_info(
                    session, user_id).get("nick_name")
                try:
                    # 添加参与记录
                    participate_in_model = ParticipateInModel(
                        game_instance_id=model._id,
                        template_id=model.template_id,
                        game_serial=model.game_serial,
                        game_title=model.game_title,
                        release_type=model.release_type,
                        bet_token=model.bet_token,
                        bet_unit=model.bet_unit,
                        user_id=user_id,
                        nick_name=nick_name,
                        channel=user_channel_id,
                        bet_number=int(bet_amount),
                        pay_token=int(conin_id),
                        pay_number=get_decimal(bet_money,
                                               digits=8,
                                               decimal_type="down"),
                        award_numbers=json.dumps(numbers),
                        user_type=0,
                        merge_id=merge_id)
                    session.add(participate_in_model)
                    session.flush()
                    # raise_logger("add participate_in_model", "game_bet_in", "info")
                    # 提交扣款申请
                    result = account_service.do_bet(
                        session, user_id, conin_id,
                        get_decimal(bet_money, digits=8, decimal_type="down"),
                        str(participate_in_model._id), int(bet_amount),
                        model.game_serial, user_channel_id)
                    # raise_logger("do_bet success", "game_bet_in", "info")
                    if isinstance(result, int):
                        if result == 0:
                            # 修改项目状态
                            game_service = GameService()
                            # raise_logger("modifyDigitalInstance begin", "game_bet_in", "info")
                            if game_service.modifyDigitalInstance(
                                    model, {
                                        "game_id": id,
                                        "add_bet_number": int(bet_amount),
                                        "full_load_time":
                                        participate_in_model.created_at,
                                        "add_people": 1
                                    }) is False:
                                session.rollback()
                                raise_logger("modifyDigitalInstance err",
                                             "game_bet_in", "info")
                                return self.return_error(80005)
                            # raise_logger("modifyDigitalInstance succ", "game_bet_in", "info")
                            try:
                                created_at_str = str(
                                    participate_in_model.created_at)
                                update_at_str = str(
                                    participate_in_model.update_at)
                                deleted_at_str = str(
                                    participate_in_model.deleted_at)
                                deleted_str = str(participate_in_model.deleted)
                                if BlockChainInfoService(
                                ).insert_block_chain_info(
                                        user_id,
                                        participate_in_model.game_instance_id,
                                        1,
                                    {
                                        "id":
                                        participate_in_model._id,
                                        "created_at":
                                        created_at_str[0:19],
                                        "update_at":
                                        update_at_str[0:19],
                                        "deleted_at":
                                        deleted_at_str[0:19],
                                        "deleted":
                                        deleted_str[0:19],
                                        "game_instance_id":
                                        str(participate_in_model.
                                            game_instance_id),
                                        "template_id":
                                        participate_in_model.template_id,
                                        "game_serial":
                                        participate_in_model.game_serial,
                                        "bet_token":
                                        participate_in_model.bet_token,
                                        "user_id":
                                        participate_in_model.user_id,
                                        "nick_name":
                                        participate_in_model.nick_name,
                                        "channel":
                                        participate_in_model.channel,
                                        "pay_token":
                                        participate_in_model.pay_token,
                                        "bet_number":
                                        participate_in_model.bet_number,
                                        "pay_number":
                                        str(participate_in_model.pay_number),
                                        "award_numbers":
                                        participate_in_model.award_numbers,
                                        "user_type":
                                        participate_in_model.user_type,
                                    }):
                                    participate_in_model.chain_status = 1
                                    # raise_logger("insert_block succ", "game_bet_in", "info")
                                else:
                                    raise_logger("insert_block err",
                                                 "game_bet_in", "info")
                            except Exception as e:
                                session.rollback()
                                raise_logger(e, "game_bet_in", "info")
                            # raise_logger("pri del GameNumbersSetModel", "game_bet_in", "info")
                            for i in numbers:
                                # raise_logger("pri del GameNumbersSetModel", "game_bet_in", "info")
                                # game_number_filters = {"game_serial": model.game_serial, "number": i}
                                session.query(GameNumbersSetModel). \
                                    filter(GameNumbersSetModel.game_serial == model.game_serial,
                                           GameNumbersSetModel.number == i).delete()
                                # number_filter.time_stamp = 1
                                # session.delete(num_filters)
                            # raise_logger("GameNumbersSetModel delete succ", "game_bet_in", "info")

                            # # 检测游戏是否售罄需要申请开奖
                            # instance = session.query(GameDigitalInstanceModel).filter(
                            #     GameDigitalInstanceModel._id == id).first()
                            if int(model.status) == 1:
                                add_result = WalletEosService().lottery_adduce(
                                    id, get_timestamp(model.full_load_time))
                                if not add_result:
                                    raise_logger("lottery_adduce fail",
                                                 "game_bet_in", "info")

                            session.commit()
                            # raise_logger("commit succ", "game_bet_in", "info")
                            try:
                                # # 提交验证是否需要上线新项目
                                game_service.automatic_release(
                                    session, model.template_id)
                            except Exception as e:
                                raise_logger(e, "game_bet_in", "info")

                            can_merge = False
                            if int(
                                    bet_amount
                            ) >= model.merge_threshold and merge_id == -1:
                                can_merge = True
                            # raise_logger("pri return", "game_bet_in", "info")
                            return {
                                'numbers': numbers,
                                'can_merge': can_merge,
                                'part_in_id': participate_in_model._id
                            }

                        else:
                            session.rollback()
                            return self.return_error(60009)
                    else:
                        session.rollback()
                        return self.return_error(60010)

                except Exception as e:
                    raise_logger(e, "game_bet_in", "info")
                    session.rollback()
                    return self.return_error(60011)

                if isinstance(numbers, int):
                    session.rollback()
                    return self.return_error(60012)

            else:
                session.rollback()
                return self.return_error(60007)
Exemplo n.º 3
0
    def bet_in(self, dic):
        change_type_41 = '41'  # 即时开中奖
        change_type_42 = '42'  # 即时开投注
        id = dic.get("game_instance_id", "")  # 项目id
        user_id = dic.get("user_id", "")  # 用户id
        user_channel_id = dic.get("user_channel_id", "")  # 用户下注渠道id
        conin_id = dic.get("coin_id", "")  # 投入币种id
        bet_amount = dic.get("bet_amount", "")  # 投注数量
        bet_start = dic.get("bet_start", 0)
        bet_end = dic.get("bet_end", 0)
        faker = True
        # merge_id = dic.get("merge_id", -1)  # 合并投注id
        # transaction_password = dic.get("transaction_password", "")  # 交易密码
        with MysqlTools().session_scope() as session:
            # 查询项目
            model = session.query(InstantGameInstanceModel).filter(
                InstantGameInstanceModel._id == id).first()
            template = session.query(InstantGameTemplateModel). \
                order_by(InstantGameTemplateModel._id.desc()).first()
            if model is None:
                return self.return_error(60001)
            if model.status == 1:
                return self.return_error(60002)
            if model.status == 2:
                return self.return_error(60003)
            if model.status == 0:
                can_bet = model.need - model.bet_number
                if can_bet <= 0:
                    return self.return_error(60002)
                if int(bet_amount) > can_bet:
                    return self.return_error(60004)

            if model.support_token >= 0 and int(
                    conin_id) != model.support_token and int(conin_id) != int(
                        _COIN_ID_EXP):
                return self.return_error(60008)

            if int(bet_amount) > model.need * (model.max_bet_ratio / 100):
                return self.return_error(40025)

            # 查询币价 币种实际需要金额
            exchange_rate = get_exchange_rate(int(conin_id))

            # (投注数量 * 投注单位) / 投注币种兑换usdt比例
            bet_money = (int(bet_amount) *
                         model.bet_unit) / exchange_rate['price']

            # 体验金相关逻辑
            if int(conin_id) == int(_COIN_ID_EXP):
                # 检查本次投注体验金使用数量 (允许范围: 1~10)
                if int(bet_amount) < 1 or int(bet_amount) > 10:
                    self.return_error(60015)
                # 体验金使用额度
                exp = int(model.experience / 100 * model.need)
                # 检查项目体验金占比剩余额度以及个人当天最大体验金使用额度(每天最多用10个)
                limit = self.check_instance_exp_limit(session, user_id,
                                                      model.game_serial)
                # instance_limit = limit['instance_limit']
                user_limit = limit['user_limit']
                total_pay_number = limit['total_pay_number']
                if user_limit > 10 - int(bet_amount):
                    self.return_error(60017)
                # if instance_limit >= exp:
                #     self.return_error(60016)
                if total_pay_number >= int(
                        model.need) * model.reward_quantity * (
                            (100 + template.exceeded_ratio) / 100):
                    model.full_load_time = datetime.datetime.utcnow()
                    faker = False
            else:
                limit = self.check_instance_exp_limit(session, user_id,
                                                      model.game_serial)
                total_pay_number = limit['total_pay_number']
                raise_logger("总下注金额" + "USDT:" + str(total_pay_number),
                             "game_publish_lottery", "info")
                if total_pay_number >= int(
                        model.need) * model.reward_quantity * (
                            (100 + template.exceeded_ratio) / 100):
                    model.full_load_time = datetime.datetime.utcnow()
                    faker = False

            # 查询用户资产
            account_service = AccountService()
            user_info = account_service.get_inner_user_account_by_token(
                session, user_id, conin_id)
            if isinstance(user_info, int):
                if user_info == 20001:
                    return self.return_error(20001)

            balance = get_decimal(user_info.get("balance"),
                                  digits=8,
                                  decimal_type="down")

            # 账户余额 < 需要下注金额
            if balance < bet_money:
                return self.return_error(60005)

            # 获取下注编号
            numbers = []
            user_lottery_num_list = []
            i = int(bet_amount)
            # start_num = random.randint(1, model.need 0- int(bet_amount))
            bet_start = int(bet_start)
            bet_end = int(bet_end)
            user_lottery_num_list.append(bet_start)
            user_lottery_num_list.append(bet_end)
            while i > 0:
                numbers.append(bet_start)
                bet_start += 1
                i -= 1
            if isinstance(numbers, list):
                nick_name = account_service.get_inner_user_account_info(
                    session, user_id).get("nick_name")
                # 添加参与记录
                participate_in_model = ParticipateInModel(
                    game_instance_id=0,
                    template_id=model.template_id,
                    game_serial=model.game_serial,
                    game_title=model.game_title,
                    release_type=model.release_type,
                    bet_token=model.bet_token,
                    bet_unit=model.bet_unit,
                    user_id=user_id,
                    nick_name=nick_name,
                    channel=user_channel_id,
                    bet_number=int(bet_amount),
                    pay_token=int(conin_id),
                    pay_number=get_decimal(bet_money,
                                           digits=8,
                                           decimal_type="down"),
                    award_numbers=json.dumps(numbers),
                    user_type=0,
                    merge_id=-1)
                session.add(participate_in_model)
                session.flush()
                # raise_logger("add participate_in_model", "game_bet_in", "info")
                # 提交扣款申请
                result = account_service.do_bet(
                    session, user_id, conin_id,
                    get_decimal(bet_money, digits=8, decimal_type="down"),
                    str(participate_in_model._id), int(bet_amount),
                    model.game_serial, user_channel_id, change_type_42)
                # raise_logger("do_bet success", "game_bet_in", "info")
                if isinstance(result, int):
                    if result == 0:
                        instant_game_service = InstantGameModelService()
                        # print('user_lottery_num_list-=-=', user_lottery_num_list)
                        # print('model.need-=-=', model.need)
                        # print('faker-=-=', faker)
                        lottery_result = WalletEosService().\
                            lottery_instant_adduce(get_timestamp(), user_lottery_num_list, model.need, faker)
                        # print('lottery_result-=-=', lottery_result)
                        if not any(lottery_result):
                            raise_logger("lottery_result empty", "game_bet_in",
                                         "info")
                            session.rollback()
                            return self.return_error(40026)
                        # 开奖
                        hash_numbers = lottery_result['block_hash']
                        raise_logger(
                            "项目:" + str(id) + "   中奖区块号:" + str(hash_numbers),
                            "game_publish_lottery", "info")
                        if hash_numbers == "":
                            raise_logger("开奖异常:80001", "game_publish_lottery",
                                         "info")
                            return False
                        # prize_number = int(hash_numbers, 16) % model.need + 1
                        prize_number = lottery_result['lottery_num']
                        raise_logger(
                            "项目:" + str(id) + "    中奖号码:" + str(prize_number),
                            "game_publish_lottery", "info")
                        participate_in_model.win_number = prize_number
                        participate_in_model.block_no = lottery_result[
                            'block_num']
                        participate_in_model.bet_hash = hash_numbers
                        participate_in_model.received_time = lottery_result[
                            'timestamp']
                        # 中奖
                        is_win = False
                        if prize_number in numbers:
                            is_win = True
                            participate_in_model.result = 1
                            model.lottery_time = datetime.datetime.utcnow()
                            instant_game_service.automatic_release(
                                session, model.template_id)
                            do_win = account_service.do_win(
                                session, user_id, model.reward_token,
                                model.reward_quantity,
                                str(participate_in_model._id), int(bet_amount),
                                model.game_serial, change_type_41)
                            if isinstance(do_win, int) is False:
                                raise_logger("分钱失败" + "user:"******"game_publish_lottery", "info")
                                session.rollback()
                                return False
                            if do_win != 0:
                                raise_logger("分钱失败" + "user:"******"game_publish_lottery", "info")
                                session.rollback()
                                return False

                        session.commit()

                        return {
                            'is_win': is_win,
                            'prize_number': prize_number,
                            'part_in_id': participate_in_model._id
                        }
                    else:
                        session.rollback()
                        return self.return_error(60009)
                else:
                    session.rollback()
                    return self.return_error(60010)
            else:
                session.rollback()
                return self.return_error(60007)
Exemplo n.º 4
0
    def dice_chip_in_fast(self, user_id, user_channel_id, coin_id, bet_amount,
                          user_dice):
        change_type_31 = '31'  # 扣款
        change_type_32 = '32'  # 返还
        change_type_33 = '33'  # 中奖
        with MysqlTools().session_scope() as session:
            # 查询用户资产
            account_service = AccountService()
            user_info = account_service.get_inner_user_account_by_token(
                session, user_id, coin_id)
            if isinstance(user_info, int):
                if user_info == 20001:
                    return self.return_error(20001)

            balance = get_decimal(user_info.get("balance"),
                                  digits=8,
                                  decimal_type="down")
            # 账户余额 < 需要下注金额
            bet_number = get_decimal(bet_amount, digits=4, decimal_type="down")
            if balance < bet_number:
                return self.return_error(60005)
            dice_serial = dice_generate_phase()
            # 获取最新eos区块,计算开奖区块高度
            dice_time = get_timestamp()
            ec = TokenNodeConfModel.get_eos_node_script(script_unit=_THREE_S)
            latest_eos_block_info = ec.http_get_latest_block()
            # print('latest_eos_block_info=-=-=', latest_eos_block_info)
            block_no = latest_eos_block_info['block_num']

            dice_part_in_model = DiceParticipateInModel(
                dice_serial=dice_serial,
                user_id=user_id,
                user_dice=user_dice,
                channel=user_channel_id,
                bet_number=bet_number,
                bet_token=coin_id,
                reward_token=coin_id,
                reward_quantity=bet_number,
                dice_timestamp=dice_time,
                block_no=block_no)
            session.add(dice_part_in_model)
            session.flush()
            # 提交扣款申请
            dice_part_id = dice_part_in_model._id
            result = account_service.do_bet(
                session, user_id, coin_id, bet_number,
                'dice_bet' + str(dice_part_in_model._id), 1, dice_serial,
                user_channel_id, change_type_31)
            if result != 0:
                session.rollback()
                raise_logger("do_bet result" + str(result), "game_bet_in",
                             "info")

            # -------------------------------- 开奖 -------------------------------- #
            block_timestamp = latest_eos_block_info['timestamp']
            block_hash = latest_eos_block_info['id']
            dice_result = -1
            banker_dice = int(block_hash, 16) % 3  # 0石头 1剪刀 2布
            user_dice = int(user_dice)
            if banker_dice == user_dice:
                dice_result = 1  # 0用户胜 1平局 2庄家胜 -1未知
            else:
                if banker_dice == 0 and user_dice == 1:
                    dice_result = 2
                elif banker_dice == 0 and user_dice == 2:
                    dice_result = 0
                elif banker_dice == 1 and user_dice == 0:
                    dice_result = 0
                elif banker_dice == 1 and user_dice == 2:
                    dice_result = 2
                elif banker_dice == 2 and user_dice == 0:
                    dice_result = 2
                elif banker_dice == 2 and user_dice == 1:
                    dice_result = 0
            dice_part_in_model.banker_dice = banker_dice
            dice_part_in_model.dice_result = dice_result
            dice_part_in_model.block_hash = block_hash
            dice_part_in_model.block_timestamp = block_timestamp
            # session.flush()
            change_type = change_type_32

            # ------------ 中奖控制 ------------ #
            winning_control = 0
            if dice_result in [0, 1]:
                win_dice_num = session.query(func.count(DiceParticipateInModel._id)). \
                    filter(DiceParticipateInModel.user_id == user_id,
                           DiceParticipateInModel.dice_result == 0,
                           func.date_format(DiceParticipateInModel.created_at, "%Y-%m-%d") ==
                           func.date_format(get_utc_now(), "%Y-%m-%d")).first()
                if win_dice_num[0] >= decimal.Decimal(500):
                    raise_logger(
                        "dice winning control userid = " + str(user_id),
                        "game_bet_in", "info")
                    dice_part_in_model.banker_dice = user_dice
                    dice_part_in_model.dice_result = 1
                    dice_result = 1
                    winning_control = 1
            # ------------ 中奖控制结束 ------------ #

            if dice_result in [0, 1]:
                reward_quantity = 0
                dice_config = session.query(DiceConfigModel). \
                    filter(DiceConfigModel.support_token_id == dice_part_in_model.bet_token).first()
                if dice_config is None:
                    return self.return_error(40022)
                if dice_result == 0:
                    change_type = change_type_33
                    # 用户获胜 中奖金额为投注金额+奖励金额(投注金额=奖励金额) 扣除手续费
                    reward_quantity = (dice_part_in_model.reward_quantity +
                                       dice_part_in_model.bet_number) * (
                                           100 -
                                           dice_config.handling_fee) / 100
                elif dice_result == 1:
                    change_type = change_type_32
                    # 平局 中奖金额为投注金额
                    reward_quantity = dice_part_in_model.bet_number
                result = account_service.do_win(
                    session, dice_part_in_model.user_id,
                    dice_part_in_model.reward_token, reward_quantity,
                    'dice_win' + str(dice_part_in_model._id),
                    dice_part_in_model.user_dice,
                    dice_part_in_model.dice_serial, change_type)
                if isinstance(result, int) is False:
                    raise_logger(
                        "dice分钱失败" + "user:"******"game_publish_lottery", "info")
                    session.rollback()
                    return False
                if result != 0:
                    raise_logger(
                        "dice分钱失败" + "user:"******"game_publish_lottery", "info")
                    session.rollback()
                    return False
            # -------------------------------- 开奖结束 -------------------------------- #

            session.commit()
            if winning_control == 1:
                return self.return_error(40023)
        return {
            'dice_result':
            dice_result,
            'banker_dice':
            banker_dice,
            'user_dice':
            user_dice,
            'dice_timestamp':
            timestamp_to_str(dice_time, format_style="%Y-%m-%d %H:%M:%S"),
            'dice_id':
            str(dice_part_id),
            'dice_serial':
            dice_serial,
            'reward_token':
            self.get_coin_name(coin_id),
            'reward_quantity':
            decimal_to_str(
                str((bet_number + bet_number) * decimal.Decimal(0.99)), 6)
        }
Exemplo n.º 5
0
    def dice_chip_in(self, user_id, user_channel_id, coin_id, bet_amount,
                     user_dice):
        with MysqlTools().session_scope() as session:
            # 查询用户资产
            account_service = AccountService()
            user_info = account_service.get_inner_user_account_by_token(
                session, user_id, coin_id)
            if isinstance(user_info, int):
                if user_info == 20001:
                    return self.return_error(20001)

            balance = get_decimal(user_info.get("balance"),
                                  digits=8,
                                  decimal_type="down")
            # 账户余额 < 需要下注金额
            bet_number = get_decimal(bet_amount, digits=4, decimal_type="down")
            if balance < bet_number:
                return self.return_error(60005)
            dice_serial = dice_generate_phase()
            block_no = ''
            # 获取最新eos区块,计算开奖区块高度
            dice_time = get_timestamp()
            latest_eos_block_info = session.query(SyncEosModel). \
                order_by(SyncEosModel._id.desc()).first()
            latest_eos_block_no = latest_eos_block_info.block_num
            latest_eos_block_time = latest_eos_block_info.time_stamp_decimal
            block_delay = math.ceil(
                (dice_time - latest_eos_block_time) / decimal.Decimal(0.5))
            block_no = latest_eos_block_no + block_delay + 1
            # raise_logger("最新区块高度=" + str(latest_eos_block_no) + "时间:" + get_utc_now().strftime(
            #     "%Y-%m-%d %H:%M:%S.%f"), "game_bet_in", "info")
            # raise_logger("最新区块时间=" + str(latest_eos_block_time) + "时间:" + get_utc_now().strftime(
            #     "%Y-%m-%d %H:%M:%S.%f"), "game_bet_in", "info")
            # raise_logger("时间延迟=" + str(block_delay) + "时间:" + get_utc_now().strftime(
            #     "%Y-%m-%d %H:%M:%S.%f"), "game_bet_in", "info")
            dice_part_in_model = DiceParticipateInModel(
                dice_serial=dice_serial,
                user_id=user_id,
                user_dice=user_dice,
                channel=user_channel_id,
                bet_number=bet_number,
                bet_token=coin_id,
                reward_token=coin_id,
                reward_quantity=bet_number,
                dice_timestamp=dice_time,
                block_no=block_no)
            session.add(dice_part_in_model)
            session.flush()
            # raise_logger("添加dice参与记录成功, id= " + str(dice_part_in_model._id) + "时间:" + get_utc_now().strftime(
            #     "%Y-%m-%d %H:%M:%S.%f"), "game_bet_in", "info")
            # 提交扣款申请
            dice_part_id = dice_part_in_model._id
            result = account_service.do_bet(
                session, user_id, coin_id, bet_number,
                'dice_bet' + str(dice_part_in_model._id), 1, dice_serial,
                user_channel_id)
            if result != 0:
                session.rollback()
                raise_logger("do_bet result" + str(result), "game_bet_in",
                             "info")
            # raise_logger("提交扣款申请成功 " + "时间:" + get_utc_now().strftime(
            #     "%Y-%m-%d %H:%M:%S.%f"), "game_bet_in", "info")
            session.commit()
        return {
            # 'result': True,
            'dice_timestamp':
            timestamp_to_str(dice_time, format_style="%Y-%m-%d %H:%M:%S"),
            'dice_id':
            dice_part_id,
            'dice_serial':
            dice_serial
        }