def query_summary(self, gid, mi, request): # 新增设备, 新增用户, 活跃用户, (新)付费玩家, (新)用户付费, 充值次数 start = mi.get_param('start') end = mi.get_param('end') start_day = Time.str_to_datetime(start, '%Y-%m-%d') end_day = Time.str_to_datetime(end, '%Y-%m-%d') mo = MsgPack(0) while start_day <= end_day: fmt = Time.datetime_to_str(start_day, '%Y-%m-%d') kvs = Context.Stat.get_day_data(gid, fmt) channel_info = {} for k, v in kvs.iteritems(): if (k.endswith('.new.device.count') or k.endswith('.new.user.count') or k.endswith('.login.user.count') or k.endswith('.new.pay.user.count') or k.endswith('.new.pay.user.pay_total') or k.endswith('.pay.user.count') or k.endswith('.pay.user.pay_total') or k.endswith('.user.pay.times')): channel, key = k.split('.', 1) if channel not in channel_info: channel_info[channel] = {} channel_info[channel][key] = int(v) mo.set_param(fmt, channel_info) start_day = Time.next_days(start_day) return mo
def on_create_user(cls, uid, gid): super(FishAccount, cls).on_create_user(uid, gid) # 发放一级礼包 conf = Context.Configure.get_game_item_json(gid, 'exp.level.reward') rewards_info = FishProps.issue_rewards(uid, gid, conf[0], 'exp.upgrade') rewards_info = FishProps.convert_reward(rewards_info) mo = MsgPack(Message.FISH_MSG_EXP_UPGRADE | Message.ID_NTF) mo.set_param('exp', 0) mo.set_param('lv', 1) mo.set_param('df', [1, [0, conf[1]]]) mo.update_param(rewards_info) Context.GData.send_to_connect(uid, mo) # new user carrying pipe_args = [] for k in ('chip', 'diamond', 'coupon'): if k in rewards_info: pipe_args.append('login.carrying.volume.%s' % k) pipe_args.append(rewards_info[k]) if 'chip' in rewards_info: pipe_args.append('carrying.volume.chip') pipe_args.append(rewards_info['chip']) if pipe_args: Context.Stat.mincr_daily_data(gid, *pipe_args)
def on_join_table(self, gid, msg): ack = MsgPack(Message.MSG_SYS_JOIN_TABLE | Message.ID_ACK) tid = msg.get_param('tableId') self._info('player req to join table', tid) self.offline = False if self.tid > 0 and self.tid != tid: self.offline = True return ack.set_error(Enum.join_table_failed_multi) registry = Context.get_module(gid, 'registry') table = registry.create_table(gid, tid) if not table: self.offline = True return ack.set_error(Enum.join_table_failed_id) result = table.join_table(self.uid) if result != 0: self.offline = True return ack.set_error(result) self.table = table self.online_ts = Time.current_ts() sid = msg.get_param('seatId') if sid is not None: return self.on_sit_down(msg) return result
def deliverOrder(self, mi, request): gid = mi.get_param('gameId') order_list = mi.get_param('orders') if not Entity.logined(request): return MsgPack.Error(0, Const.E_NOT_LOGIN, Const.ES_NOT_LOGIN) uid = request.getSession().userId orders = [] for orderId in order_list: orderInfo = self.getOrderInfo(orderId) if not orderInfo: continue userId = int(orderInfo['userId']) gameId = int(orderInfo['gameId']) state = int(orderInfo['state']) if userId != uid: Context.Log.warn('userId not match', userId, uid, orderId) continue if gameId != gid: Context.Log.warn('gameId not match', gameId, gid, orderId) continue if state == self.state_create: create_ts = Time.str_to_timestamp(orderInfo['createTime'], '%Y-%m-%d %X.%f') now_ts = Time.current_ts() if now_ts - create_ts > 3600: state = self.state_timeout orders.append({'id': orderId, 'state': state}) return MsgPack(0, {'orders': orders})
def on_get_config(self, uid, gid, mi): which = mi.get_param('which') if isinstance(which, (str, unicode)): which = [which] mo = MsgPack(Message.MSG_SYS_CONFIG | Message.ID_ACK) for name in which: if name == 'vip': conf = self.get_vip_config(uid, gid) elif name == 'shop': conf = self.get_shop_config(uid, gid) elif name == 'raffle': conf = self.get_raffle_config(uid, gid) elif name == 'props': conf = FishProps.get_props_config(gid) elif name == 'unlock': conf = self.get_unlock_config(uid, gid) elif name == 'barrel': conf = self.get_barrel_config(uid, gid) elif name == 'exchange': conf = self.get_exchange_config(uid, gid) elif name == 'benefit': conf = self.get_benefit_config(uid, gid) elif name == 'html': conf = self.get_html_config(uid, gid) elif name == 'exp': conf = self.get_exp_config(uid, gid) elif name == 'upbrrel': conf = self.get_upbrrel_config(uid, gid) else: continue mo.set_param(name, conf) return mo
def loginByUserName(self, mi, request): param = self.getParam(mi, 'userName', 'passwd') if not Entity.checkUserName(param['userName']): return MsgPack.Error(0, self.error_invalid_username, self.desc_invalid_username) if not Entity.checkPassword(param['passwd']): return MsgPack.Error(0, self.error_invalid_passwd, self.desc_invalid_passwd) idType = Const.IDTYPE_USERNAME userId = Account.getUserIDByUserName(param['userName'], idType) if not userId: return MsgPack.Error(0, self.error_user_not_exist, self.desc_user_not_exist) userInfo = Account.getUserInfo(userId) if not userInfo['userName']: return MsgPack.Error(0, self.error_user_not_exist, self.desc_user_not_exist) # 进行密码比较 strMd5Pass = Entity.encodePassword(param['userName'], param['passwd']) if strMd5Pass != userInfo['token']: return MsgPack.Error(0, self.error_pwd_not_right, self.desc_pwd_not_right) return self.getLoginInfo(request, 0, userId, param['gameId'], param, userInfo, True)
def __pub_raffle_led(self, uid, gid, level, reward_info): if 'reward' in reward_info: reward = reward_info['reward'] name = None if 'chip' in reward: Context.Data.hincr_game(uid, gid, 'chip_pool', -reward['chip']) elif 'coupon' in reward: name = u'%d鱼券' % reward['coupon'] elif 'diamond' in reward: name = u'%d钻石' % reward['diamond'] elif 'props' in reward: props = reward['props'] for one in props: name = FishProps.get_props_desc(one['id']) break if name: nick = Context.Data.get_attr(uid, 'nick') if nick: led = u'恭喜%s玩家,在%s中抽中%s' % (nick.decode('utf-8'), level, name) mo = MsgPack(Message.MSG_SYS_LED | Message.ID_NTF) mo.set_param('game', { 'list': [led], 'ts': Time.current_ts() }) Context.GData.broadcast_to_system(mo)
def updateUserInfo(self, mi, request): nick = mi.get_param('nick') avatar = mi.get_param('avatar') if not Entity.logined(request): return MsgPack.Error(0, Const.E_NOT_LOGIN, Const.ES_NOT_LOGIN) uid = request.getSession().userId kvs = {} if nick: if not Entity.checkNick(nick): return MsgPack.Error(0, 1, 'param nick invalid') if Context.KeywordFilter.isContains(nick): return MsgPack.Error(0, 2, 'key word filter') kvs['nick'] = nick if avatar: if avatar in Const.AVATAR_MAN: kvs['avatar'] = avatar kvs['sex'] = Const.SEX_MAN elif avatar in Const.AVATAR_WOMAN: kvs['avatar'] = avatar kvs['sex'] = Const.SEX_WOMAN else: return MsgPack.Error(0, 3, 'param avatar invalid') if kvs: Account.updateUserInfo(uid, **kvs) return MsgPack(0, kvs)
def query_diamond_produce(self, gid, mi, request): start = mi.get_param('start') end = mi.get_param('end') start_day = Time.str_to_datetime(start, '%Y-%m-%d') end_day = Time.str_to_datetime(end, '%Y-%m-%d') mo = MsgPack(0) while start_day <= end_day: fmt = Time.datetime_to_str(start_day, '%Y-%m-%d') kvs = Context.Stat.get_day_data(gid, fmt) _kvs = {} total, task_total, fall_total = 0, 0, 0 for k, v in kvs.iteritems(): if k.startswith('in.diamond.'): if k.startswith('in.diamond.task.reward.'): task_total += int(v) elif k.startswith('in.diamond.fish.fall.'): fall_total += int(v) else: _kvs[k] = int(v) total += int(v) _kvs['in.diamond.task.reward'] = task_total _kvs['in.diamond.fish.fall'] = fall_total _kvs['in.diamond.buy.product'] = int(kvs.get('in.diamond.buy.product', 0)) _kvs['total'] = total mo.set_param(fmt, _kvs) start_day = Time.next_days(start_day) return mo
def query_pay_detail(self, gid, mi, request): conf = Context.Configure.get_game_item_json(gid, 'product.config') pids = [] for pid in conf.iterkeys(): pids.append('product_' + pid) start = mi.get_param('start') end = mi.get_param('end') start_day = Time.str_to_datetime(start, '%Y-%m-%d') end_day = Time.str_to_datetime(end, '%Y-%m-%d') mo = MsgPack(0) while start_day <= end_day: fmt = Time.datetime_to_str(start_day, '%Y-%m-%d') timess = Context.Stat.get_day_data(gid, fmt, *pids) kvs = {'product_100646': 0, 'product_100710': 0} for k, v in zip(pids, timess): pid = k.replace('product_', '') if pid in ('100646', '100647', '100648', '100649', '100650', '100651', '100652', '100653', '100654', '100655'): kvs['product_100646'] += Tool.to_int(v, 0) elif pid in ('100710', '100711', '100712', '100713', '100714', '100715', '100716'): kvs['product_100710'] += Tool.to_int(v, 0) else: kvs[k] = Tool.to_int(v, 0) mo.set_param(fmt, kvs) start_day = Time.next_days(start_day) return mo
def query_chip_produce(self, gid, mi, request): start = mi.get_param('start') end = mi.get_param('end') start_day = Time.str_to_datetime(start, '%Y-%m-%d') end_day = Time.str_to_datetime(end, '%Y-%m-%d') mo = MsgPack(0) while start_day <= end_day: fmt = Time.datetime_to_str(start_day, '%Y-%m-%d') kvs = Context.Stat.get_day_data(gid, fmt) task_total, catch_total = 0, 0 _kvs = {} for k, v in kvs.iteritems(): if k.startswith('in.chip.'): if k.startswith('in.chip.task.reward.'): task_total += int(v) elif k.startswith('in.chip.catch.fish.'): catch_total += int(v) else: _kvs[k] = int(v) _kvs['in.chip.task.reward'] = task_total _kvs['in.chip.catch.fish'] = catch_total _kvs['in.chip.buy.product'] = int(kvs.get('in.chip.buy.product', 0)) mo.set_param(fmt, _kvs) start_day = Time.next_days(start_day) return mo
def query_diamond_consume(self, gid, mi, request): start = mi.get_param('start') end = mi.get_param('end') start_day = Time.str_to_datetime(start, '%Y-%m-%d') end_day = Time.str_to_datetime(end, '%Y-%m-%d') mo = MsgPack(0) while start_day <= end_day: fmt = Time.datetime_to_str(start_day, '%Y-%m-%d') kvs = Context.Stat.get_day_data(gid, fmt) _kvs, total = {}, 0 for k, v in kvs.iteritems(): if k.startswith('out.diamond.'): if k.startswith('out.diamond.inner.buy.'): k = 'out.diamond.buy.' + k[-3:] elif k.startswith('out.diamond.table.buy.'): k = 'out.diamond.buy.' + k[-3:] if k in _kvs: _kvs[k] += int(v) else: _kvs[k] = int(v) total += int(v) _kvs['total'] = total mo.set_param(fmt, _kvs) start_day = Time.next_days(start_day) return mo
def query_chip_consume(self, gid, mi, request): room_types = (201, 202, 203) mini_games = (10002, 10003) fields = ['out.chip.attack'] for room_type in room_types: fields.append('out.chip.game.shot.bullet.%d' % room_type) for game in mini_games: fields.append('out.chip.game.%d' % game) start = mi.get_param('start') end = mi.get_param('end') start_day = Time.str_to_datetime(start, '%Y-%m-%d') end_day = Time.str_to_datetime(end, '%Y-%m-%d') mo = MsgPack(0) while start_day <= end_day: fmt = Time.datetime_to_str(start_day, '%Y-%m-%d') chips = Context.Stat.get_day_data(gid, fmt, *fields) attack = Tool.to_int(chips[0], 0) info = [] for chip in chips[1:]: if chip: info.append(int(chip)) else: info.append(0) info[2] += attack mo.set_param(fmt, info) start_day = Time.next_days(start_day) return mo
def query_chip_pump(self, gid, mi, request): room_types = (201, 202, 203) fields = [] for room_type in room_types: fields.append('out.chip.pump.%d' % room_type) fields.append('out.chip.buff.pump.%d' % room_type) fields.append('out.chip.red.pump.%d' % room_type) start = mi.get_param('start') end = mi.get_param('end') start_day = Time.str_to_datetime(start, '%Y-%m-%d') end_day = Time.str_to_datetime(end, '%Y-%m-%d') mo = MsgPack(0) while start_day <= end_day: fmt = Time.datetime_to_str(start_day, '%Y-%m-%d') chips = Context.Stat.get_day_data(gid, fmt, *fields) info = [] for i in range(0, len(chips), 3): total = 0 for j in range(3): if chips[i + j]: total += int(chips[i + j]) info.append(total) mo.set_param(fmt, info) start_day = Time.next_days(start_day) return mo
def gm_reward_chip(self, gid, mi, request): uid = mi.get_param('userId') if not Context.UserAttr.check_exist(uid, gid): return MsgPack.Error(0, 1, 'not exist') chip = mi.get_param('chip') real, final = Context.UserAttr.incr_chip(uid, gid, chip, 'gm.reward') if real != chip: MsgPack.Error(0, 1, 'not enough') return MsgPack(0, {'chip': final, 'delta': real})
def disable_user(self, gid, mi, request): uid = mi.get_param('userId') if not Context.UserAttr.check_exist(uid, gid): return MsgPack.Error(0, 1, 'not exist') disable = mi.get_param('disable') if disable: Context.RedisMix.set_add('game.%d.disable.user' % gid, uid) else: Context.RedisMix.set_rem('game.%d.disable.user' % gid, uid) return MsgPack(0)
def on_room_list(self, uid, gid, mi): room_config = Context.Configure.get_room_config(gid) if not room_config: Context.Log.error(uid, 'req room list, but no config fetch') return False conf = Context.copy_json_obj(room_config) mo = MsgPack(Message.MSG_SYS_ROOM_LIST | Message.ID_ACK) mo.set_param('room_list', conf) return Context.GData.send_to_connect(uid, mo)
def gm_reward_diamond(self, gid, mi, request): uid = mi.get_param('userId') if not Context.UserAttr.check_exist(uid, gid): return MsgPack.Error(0, 1, 'not exist') diamond = mi.get_param('diamond') real, final = Context.UserAttr.incr_diamond(uid, gid, diamond, 'gm.reward') if real != diamond: MsgPack.Error(0, 1, 'not enough') return MsgPack(0, {'diamond': final, 'delta': real})
def gm_exchange_phone(self, gid, mi, request): uid = mi.get_param('userId') seq = mi.get_param('seq') state = Context.RedisCluster.hash_get_int(uid, 'history:%d:%d' % (gid, uid), seq) if state is None: return MsgPack.Error(0, 1, 'error seq') if state == 1: return MsgPack.Error(0, 2, 'already exchange') Context.RedisCluster.hash_set(uid, 'history:%d:%d' % (gid, uid), seq, 1) return MsgPack(0)
def on_bind_game(self, cmd, raw): mi = MsgPack.unpack(cmd, raw) gid = mi.get_param('gameId') if gid > 0: room = mi.get_param('room') self.connection.bind_game(gid, room) mo = MsgPack(Message.MSG_SYS_BIND_GAME | Message.ID_ACK) mo.set_param('gameId', gid) Context.GData.send_to_client(self.connection.userId, mo, self.connection) return True
def setTimeout(self, second, param, *args, **kwargs): self.type = self.TIMER_ONCE self.second = second msg = MsgPack(Message.MSG_INNER_TIMER, param) uid = msg.get_param('userId') gid = msg.get_param('gameId') msg = MsgLine(msg.pack(), gid, target=uid).pack() task = self.tasklet(Message.to_inner(Message.MSG_INNER_TIMER), msg, None).run self.timer = TaskManager.call_later(self.__timeout, second, msg, task, *args, **kwargs) return True
def upgradeByMobile(self, mi, request): gid = mi.get_param('gameId') nick = mi.get_param('nick', '') mobile = mi.get_param('mobile', '') passwd = mi.get_param('passwd', '') verifyCode = mi.get_param('verifyCode', '') if not Entity.logined(request): return MsgPack.Error(0, Const.E_NOT_LOGIN, Const.ES_NOT_LOGIN) if not Entity.checkMobile(mobile): return MsgPack.Error(0, 1, 'mobile invalid') if not Entity.checkPassword(passwd): return MsgPack.Error(0, 2, 'password invalid') if not Entity.checkNick(nick): return MsgPack.Error(0, 6, 'nick invalid') userId = request.getSession().userId # 先判断是不是游客 userInfo = Account.getUserInfo(userId) if int(userInfo['idType']) != Const.IDTYPE_GUEST: return MsgPack.Error(0, 3, 'not guest') # 先查表,判断用户存不存在 if Account.getUserIDByUserName(mobile, Const.IDTYPE_MOBILE): return MsgPack.Error(0, 4, 'mobile exist') if not Mobile.checkVerifyCode(gid, mobile, verifyCode): return MsgPack.Error(0, 5, 'verifycode not right') # nick 唯一 if not Context.RedisMix.hash_setnx('game.%d.unique.nick' % gid, nick, 0): return MsgPack.Error(0, 7, 'nick not unique') if not Account.createUserName(Const.IDTYPE_MOBILE, mobile, userId): Context.RedisMix.hash_del('game.%d.unique.nick' % gid, nick) return MsgPack.Error(0, Const.E_BAD_REDIS, Const.ES_BAD_REDIS) if not Account.deleteUserName(userInfo['userName'], Const.IDTYPE_GUEST): Context.Log.error(userId, 'DeleteUserName failed,userName:'******'userName']) Context.RedisMix.hash_set('game.%d.unique.nick' % gid, nick, userId) Account.updateUserInfo(userId, userName=mobile, nick=nick, idType=Const.IDTYPE_MOBILE, token=Entity.encodePassword(mobile, passwd)) return MsgPack(0)
def gm_account_block(self, gid, mi, request): uid = mi.get_param('userId') odds = mi.get_param('odds') if odds is None: Context.Data.del_game_attrs(uid, gid, 'block') else: if not Context.UserAttr.check_exist(uid, gid): return MsgPack.Error(0, 1, 'not exist') if odds <= 0 or odds > 1: return MsgPack.Error(0, 2, 'odds limit (0, 1]') Context.Data.set_game_attr(uid, gid, 'block', odds) return MsgPack(0)
def gm_notice_global(self, gid, mi, request): led = mi.get_param('led') start = mi.get_param('start') end = mi.get_param('end') now_ts = Time.current_ts() if now_ts > end: return MsgPack(0) Context.RedisCache.hash_mset('global.notice', 'led', led, 'start', start, 'end', end) if now_ts >= start: self._do_notice(led, end) return MsgPack(0) TaskManager.set_timeout(self.do_notice, start-now_ts, led, end) return MsgPack(0)
def loginByAccessToken(self, mi, request): param = self.getParam(mi, 'accessToken') if len(param['accessToken']) <= 1: return MsgPack.Error(0, self.error_invalid_access_token, self.desc_invalid_access_token) try: decrptData = Entity.decrypt(param['accessToken']) decoded = Context.json_loads(decrptData) except Exception, e: return MsgPack.Error(0, self.error_invalid_access_token, self.desc_invalid_access_token)
def on_leave_table(self, msg): self._info('player req to leave table', self.tid) ack = MsgPack(Message.MSG_SYS_LEAVE_TABLE | Message.ID_ACK) table = Context.GData.online_table.get(self.tid) if not table: self._error('not exists the table', self.tid) return ack.set_error(Enum.leave_table_failed_id) result = table.leave_table(self.uid) if result != 0: return ack.set_error(result) return result
def freeze_user(self, gid, mi, request): uid = mi.get_param('userId') if not Context.UserAttr.check_exist(uid, gid): return MsgPack.Error(0, 1, 'not exist') days = mi.get_param('days') mo = MsgPack(0) if days is None: Context.RedisMix.hash_del('game.%d.freeze.user' % gid, uid) else: end_ts = Time.today_start_ts() + days * 3600 * 24 Context.RedisMix.hash_set('game.%d.freeze.user' % gid, uid, end_ts) mo.set_param('end_ts', end_ts) return mo
def on_sit_down(self, msg): sid = msg.get_param('seatId') self._info('player req to sit table %d at %d' % (self.tid, sid)) ack = MsgPack(Message.MSG_SYS_SIT_DOWN | Message.ID_ACK) table = Context.GData.online_table.get(self.tid) if not table: self._error('not exists the table', self.tid) return ack.set_error(Enum.sit_down_failed_id) result = table.sit_down(self.uid, sid) if result != 0: return ack.set_error(result) return result
def on_up_barrel(self, uid, gid, mi): # 强化万倍炮 # up type 1 石头 2 精华 mo = MsgPack(Message.MSG_SYS_UP_BARREL | Message.ID_ACK) up_type = mi.get_param('up_ty') conf = Context.Configure.get_game_item_json(gid, 'barrel.unlock.config') if not conf: return mo.set_error(1, 'system error') next_level = Context.Data.get_game_attr_int(uid, gid, 'barrel_level', 1) + 1 # if next_level > len(conf): if next_level <= 36 or next_level > 54: return mo.set_error(2, 'level error') level_conf = conf[next_level - 1] diamond_count = level_conf['diamond'] real, final = Context.UserAttr.incr_diamond(uid, gid, -diamond_count, 'up.barrel') if real != -diamond_count: return mo.set_error(3, 'lack diamond') if up_type == 1: count = -level_conf['stone'] if not FishProps.mincr_props(uid, gid, 'on_up_barrel', 215, count, 216, count, 217, count, 218, count): Context.UserAttr.incr_diamond(uid, gid, diamond_count, 'up.barrel.error') return mo.set_error(3, 'lack stone') res, gem = self.do_up_barrel(level_conf) if res: Context.Data.set_game_attr(uid, gid, 'barrel_level', next_level) else: FishProps.mincr_props(uid, gid, 'on_up_barrel.fail_reurn', 219, gem) mo.set_param('num', gem) elif up_type == 2: count = -level_conf['stone'] count_gem = -level_conf['gem'] if not FishProps.mincr_props(uid, gid, 'on_up_barrel', 215, count, 216, count, 217, count, 218, count, 219, count_gem): Context.UserAttr.incr_diamond(uid, gid, diamond_count, 'up.barrel.error') return mo.set_error(3, 'lack item') Context.Data.set_game_attr(uid, gid, 'barrel_level', next_level) else: return mo.set_error(4, 'type error') return mo
def on_use_props(self, uid, gid, mi): _id = mi.get_param('id') _count = mi.get_param('count') mo = MsgPack(Message.MSG_SYS_USE_PROPS | Message.ID_ACK) if _id not in [ FishProps.PROP_EGG_BRONZE, FishProps.PROP_EGG_SILVER, FishProps.PROP_EGG_GOLD, FishProps.PROP_EGG_COLOR ]: return mo.set_error(1, 'can not use') if not isinstance(_count, int) or _count <= 0: return mo.set_error(2, 'count error') conf = FishProps.get_config_by_id(gid, _id) if not conf: Context.Log.error('not found props:', uid, gid, _id, _count) return mo.set_error(4, 'not found props') real, final = FishProps.incr_props(uid, gid, _id, -_count, 'entity.use') if real != -_count: return mo.set_error(3, 'not enough') if _count == 1: reward = conf['content'] else: reward = FishProps.merge_reward(*[conf['content']] * _count) reward = Context.copy_json_obj(reward) reward = self.deal_reward(reward) reward = FishProps.issue_rewards(uid, gid, reward, 'entity.use') reward = FishProps.convert_reward(reward) mo.update_param(reward) return mo