def recharge_strategy(player_token, value): value = decimal.Decimal(value) activate = KeyValue.get_value(Keys.RECHARGE_TACTICS_ACTIVATE.value) if activate != str(True): return info = KeyValue.get_value(Keys.RECHARGE_TACTICS_INFO.value) if info is None or info == {}: return info = json.loads(info) now_time = int(time.strftime("%Y%m%d%H%M%S")) reward = 0 if int(info['range'][0]) < now_time < int(info['range'][1]): if info['method'] == "INTERVAL": # 区间 for v in info['value']: if decimal.Decimal(v['min']) <= value < decimal.Decimal(v['max']): reward = v['reward'] break if info['method'] == "FIXED": # 固定值 for v in info['value']: if decimal.Decimal(v['recharge_amount']) == value: reward = v['reward_amount'] break if info['method'] == "PERCENTAGE": # 百分比 x = decimal.Decimal(info['value']) / 100 reward = value * x if reward == 0: return if info['type'] == RechargeType.GOLD.value: Player.update_player_gold(player_token, reward) elif info['type'] == RechargeType.INTEGRAL.value: Player.update_player_integral(player_token, reward)
def pig_open(player_id, token): """ 开奖 :return: gold """ gold = random.randint(1, 50) Player.update_player_integral(token, -500) ok, player = Player.update_player_gold(token, gold, gold) value = int(KeyValue.get_value(Keys.GOLDEN_PIG.value)) KeyValue.update_value(Keys.GOLDEN_PIG.value, (value - 1)) PigRecord.create(player_id=player_id, gold=gold) update_player_info(player) return gold
def create_or_update(player_id, **params): """ 创建或者修改签到 :param player_id: 玩家id :param params: 签到参数 :return: 奖励 """ SignRecord.create_or_update(player_id, **params) SignReward.find_by_day(params['total']) reward = SignReward.find_by_day(params['total']) if reward.reward_type == RewardType.GOLD.value: Player.update_gold_by_id(player_id, reward.reward_value) return {'type': reward.reward_type, 'value': reward.reward_value}
def find_by_token(token): """ 根据token获取用户 :param token: :return: """ return Player.find_by_token(token)
def reset_token(token): """ 重置Token :param token: token :return: 成功 Boolean """ return Player.reset_token(token)
def auth_token(token): """ 验证Token :param token: :return: 成功 Boolean """ return Player.check_token(token)
def create_mail(**params): """ 创建邮件 :param params: Map :return: """ # 创建邮件 ok, mail = Mail.create(**params) if not ok: print('创建失败') # 群发还是私发 if params['mail_type'] == MailType.SYSTEM_MAIL.value: players = Player.all_player_id() else: players = Player.find_by_serial(params['player_ids']) MailPlayer.create(mail.id, players)
def login(phone, password): """ 登陆验证 :param phone: 手机号 :param password: 密码 :return: 是否成功, 玩家对象 """ ok, obj = Player.authenticate(phone, md5(password)) return ok, response_obj(obj)
def reset_password(phone, new_password): """ 重置密码 :param phone: 手机号 :param new_password: 新密码 :return: """ ok, obj = Player.reset_password(phone, md5(new_password)) return ok, response_obj(obj)
def update_player_gold(token, value, lock_gold=0): """ 更新玩家金币 :param lock_gold: :param token: 玩家Token :param value: 变动金额 :return: Boolean """ return Player.update_player_gold(token, value, lock_gold)
def check(**params): if params['allow'] == RechargeStatusEnum.SUCCEED.value: ok, obj = RechargeRecord.check_recharge(**params) ok, player = Player.update_player_gold(obj.player.token, obj.pay_money) recharge_strategy(obj.player.token, obj.pay_money) return ok, player elif params['allow'] == RechargeStatusEnum.FAILED.value: ok, _ = RechargeRecord.check_recharge(**params) return ok, None return False, None
def registered(phone, password, code): """ 注册 :param phone: 手机号 :param password: 密码 :param code: 邀请注册码 :return: 是否成功, 玩家对象 """ serial = str(int(time.time())) ok, obj = Player.create(serial, md5(password), phone, code) return ok, response_obj(obj)
def check_player_gold(token, value): """ 检查玩家金币 :param token: 玩家Token :param value: 变动金额 :return: Boolean """ ok, gold = Player.get_player_gold(token) if not ok or decimal.Decimal(value) > gold: return False return True
def search(params): """ 查询玩家列表 :param params: :return: """ result = Player.search(params) result['ls'] = [{ 'name': d['name'], 'serial': d['serial'], 'phone': d['phone'], 'gold': d['gold'], 'status': d['status'] } for d in result['ls']] return result
def bet(channel_name, **params): """ @api {post} /ReqBet 游戏下注接口 @apiVersion 1.0.0 @apiName ReqBet @apiGroup WebSocket-Receive @apiSuccessExample {json} 通知样例: { "event": "ReqBet", "data": { "bet_amount": 200, "dice_type": "SUM_THREE" } } """ log.info('*****************************************') log.info(params) player_token = get_player_token(channel_name) hall_id = get_player_hall(player_token) log.info(player_token) log.info(hall_id) event = NoticeEvent.GameBetErrorNotice.value ok, hall = find_by_id(hall_id) if not ok: log.info('未查询到大厅') # bet_error(channel_name, hall.tag) return create_result( hall.tag, event, { 'code': ServerCode.CODE41001.value, 'msg': ServerMsg.CODE41001.value, 'gold': params['bet_amount'] }) if hall.stage != HallStage.BetStage.value: log.info("大厅当前阶段【%s】当前阶段不允许下注" % hall.stage) bet_error(channel_name, hall.tag) return create_result( hall.tag, event, { 'code': ServerCode.CODE41002.value, 'msg': ServerMsg.CODE41002.value, 'gold': params['bet_amount'] }) form = BetForm(params) if not form.is_valid(): log.info(form.errors) return create_result( hall.tag, event, { 'code': ServerCode.CODE41006.value, 'msg': ServerMsg.CODE41006.value, 'gold': params['bet_amount'] }) # bet_error(channel_name, hall_tag) ok, player = Player.find_by_token(player_token) if not ok: log.info('未查询到玩家') return create_result( hall.tag, event, { 'code': ServerCode.CODE41003.value, 'msg': ServerMsg.CODE41003.value, 'gold': params['bet_amount'] }) # bet_error(channel_name, hall_tag) # 获取大厅ID hall_id = get_player_hall(player_token) sequence = get_hall_new_sequence(hall_id, hall.tag) # 检查下注金额 if not check_value(hall_id, params['bet_amount']): log.info('下注金额不正确') return create_result( hall.tag, event, { 'code': ServerCode.CODE41004.value, 'msg': ServerMsg.CODE41004.value, 'gold': params['bet_amount'] }) # bet_error(channel_name, hall_tag) # 检查下注类型 ok, odds = check_bet(hall_id, params['dice_type']) if not ok: log.info('下注类型不正确') return create_result( hall.tag, event, { 'code': ServerCode.CODE41005.value, 'msg': ServerMsg.CODE41005.value, 'gold': params['bet_amount'] }) # bet_error(channel_name, hall_tag) if Decimal(params['bet_amount']) > player.gold: log.info('玩家金额不足') return create_result( hall.tag, event, { 'code': ServerCode.CODE41007.value, 'msg': ServerMsg.CODE41007.value, 'gold': 0 }) # 计算下注奖金 bonus = Decimal(params['bet_amount']) * Decimal(odds) # 扣除玩家金币 update_player_gold(player.token, -params['bet_amount']) # 创建下注记录 BetRecord.create(player.id, sequence, params['bet_amount'], params['dice_type'], bonus) return None
def process_request(request): if '/api' in request.path: # 客户端校验 if request.META.get('HTTP_AUTHORIZATION', '').startswith('Token'): if not hasattr(request, 'user') or request.user.is_anonymous(): try: key = request.META.get('HTTP_AUTHORIZATION').split( ' ')[1] payload = token.decode(key) if payload is None: return JsonResponse({ 'code': ResultCode.CODE_40001.value, 'message': ResultMsg.MSG_40001.value }) db_token = KeyValue.get_token(payload['user']['id']) if db_token is None: return JsonResponse({ 'code': ResultCode.CODE_40001.value, 'message': ResultMsg.MSG_40001.value }) if key != db_token.value: return JsonResponse({ 'code': ResultCode.CODE_40001.value, 'message': ResultMsg.MSG_40001.value }) pk = payload['user']['id'] user = User.objects.get(id=pk) KeyValue.get_token(user.id) request.user = request._cached_user = user except User.DoesNotExist: return JsonResponse({ 'code': ResultCode.CODE_40001.value, 'message': ResultMsg.MSG_40001.value }) except IndexError: return JsonResponse({ 'code': ResultCode.CODE_40001.value, 'message': ResultMsg.MSG_40001.value }) else: return JsonResponse({ 'code': ResultCode.CODE_40001.value, 'message': ResultMsg.MSG_40001.value }) elif '/three' in request.path: # 客户端校验 if request.META.get('HTTP_AUTHORIZATION', '').startswith('Token'): if not hasattr(request, 'user') or request.user.is_anonymous(): try: key = request.META.get('HTTP_AUTHORIZATION').split( ' ')[1] pass ok, player = Player.find_by_token(key) if not ok: return JsonResponse({ 'code': ResultCode.CODE_40001.value, 'message': ResultMsg.MSG_40001.value }) request.player = player except Player.DoesNotExist: return JsonResponse({ 'code': ResultCode.CODE_40001.value, 'message': ResultMsg.MSG_40001.value }) except IndexError: return JsonResponse({ 'code': ResultCode.CODE_40001.value, 'message': ResultMsg.MSG_40001.value }) else: return JsonResponse({ 'code': ResultCode.CODE_40001.value, 'message': ResultMsg.MSG_40001.value })