def get_total_degree(self, uid, gid): task_list = Context.Daily.get_daily_data(uid, gid, 'task.list') if not task_list: return 0 task_list = Context.json_loads(task_list) degree = 0 for task in task_list: if task['type'] == 1: count = Context.Daily.get_daily_data(uid, gid, 'fish.' + str(task['fish_type'])) task['count'] = Tool.to_int(count, 0) elif task['type'] == 2: count = Context.Daily.get_daily_data(uid, gid, 'class.boss') task['count'] = Tool.to_int(count, 0) elif task['type'] == 3: count = Context.Daily.get_daily_data(uid, gid, 'class.bonus') task['count'] = Tool.to_int(count, 0) elif task['type'] == 11: count = Context.Daily.get_daily_data(uid, gid, 'win.chip') task['count'] = Tool.to_int(count, 0) elif task['type'] == 21: degree += task['degree'] if 'count' in task and task['count'] >= task['total']: task['done'] = 1 degree += task['degree'] return degree
def onLed(self, uid, gid, mi): mo = MsgPack(Message.MSG_SYS_LED | Message.ID_ACK) action = mi.get_param('action', 'get') if action == 'get': last_ts = mi.get_param('last_ts', 0) led_list = Context.RedisCache.list_range('global.led.list', 0, 9) _list, ts = [], 0 for led in led_list: led = Context.json_loads(led) if led['ts'] > last_ts: _list.append(led['led']) ts = led['ts'] if _list: mo.set_param('global', {'ts': ts, 'list': _list}) led_list = Context.RedisCache.list_range('game.%d.led.list' % gid, 0, 9) _list, ts = [], 0 for led in led_list: led = Context.json_loads(led) if led['ts'] > last_ts: _list.append(led['led']) ts = led['ts'] if _list: mo.set_param('game', {'ts': ts, 'list': _list}) elif action == 'put': conf = Context.Configure.get_game_item_json(gid, 'led.config') if not conf or not conf.get('enable'): mo.set_error(101, 'led not available') else: msg = mi.get_param('msg') cost = conf['cost'] real, final = Context.UserAttr.incr_diamond( uid, gid, -cost, 'led.put') if real != -cost: mo.set_error(102, 'no enough diamond') else: led = Context.json_dumps({ 'led': msg, 'ts': Time.current_ts() }) Context.RedisCache.list_lpush('game.%d.led.list' % gid, led) return Context.GData.send_to_connect(uid, mo)
def getLoginInfo(cls, request, cmd, uid, gid, param, userInfo, freshAccessToken, openid=None, open_token=None): session = Algorithm.md5_encode(Time.asctime() + request.getClientIP() + userInfo['userName']) Account.setUserToken(uid, gid, session) conn_server = Context.json_loads( Context.RedisCache.get('connect.server')) internet = conn_server[uid % len(conn_server)] dictInfo = { "session": session, "userId": uid, "sex": int(userInfo['sex']), "nick": userInfo['nick'], "avatar": userInfo['avatar'], "host": internet["domain"], "port": internet["port"], } if openid: dictInfo['openid'] = openid if open_token: dictInfo['open_token'] = open_token if freshAccessToken: data = '{"uid":%d,"ct":%d}' % (uid, Time.current_ts()) accessToken = Entity.encrypt(data) Account.updateUserInfo(uid, accessToken=accessToken) dictInfo['accessToken'] = accessToken kvs = { 'session_platform': param['platform'] or 'android', 'session_channel': param['channel'] or 'jiyu', 'session_ver': param['releaseVer'] or '1.0.1' } Context.Data.set_game_attrs_dict(uid, gid, kvs) Context.Log.report('user.login:', [uid, gid, kvs]) # 登录成功设置session值 session = request.getSession() session.setLogined(uid) return MsgPack(cmd, dictInfo)
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_consume_task(self, uid, gid, mi): _id = mi.get_param('id') mo = MsgPack(Message.MSG_SYS_CONSUME_TASK | Message.ID_ACK) task_list = Context.Daily.get_daily_data(uid, gid, 'task.list') if not task_list: return mo.set_error(1, 'no task') task_list = Context.json_loads(task_list) degree = 0 for task in task_list: if task['type'] == 1: count = Context.Daily.get_daily_data(uid, gid, 'fish.' + str(task['fish_type'])) task['count'] = Tool.to_int(count, 0) elif task['type'] == 2: count = Context.Daily.get_daily_data(uid, gid, 'class.boss') task['count'] = Tool.to_int(count, 0) elif task['type'] == 3: count = Context.Daily.get_daily_data(uid, gid, 'class.bonus') task['count'] = Tool.to_int(count, 0) elif task['type'] == 11: count = Context.Daily.get_daily_data(uid, gid, 'win.chip') task['count'] = Tool.to_int(count, 0) elif task['type'] == 21: degree += task['degree'] if 'count' in task and task['count'] >= task['total']: task['done'] = 1 degree += task['degree'] conf = Context.Configure.get_game_item_json(gid, 'task.config') for i, reward in enumerate(conf['reward']): if i == _id: if degree < reward['degree']: return mo.set_error(2, 'not done') state = Context.Daily.incr_daily_data(uid, gid, 'task.reward.%d' % _id, 1) if state > 1: return mo.set_error(3, 'received') reward = FishProps.issue_rewards(uid, gid, reward['reward'], 'task.reward.%d' % _id) _reward = FishProps.convert_reward(reward) mo.update_param(_reward) break else: mo.set_error(4, 'error id') return mo
def __get_history_exchange_list(self, uid, gid): _list = [] all_history = Context.RedisCluster.hash_getall(uid, 'history:%d:%d' % (gid, uid)) if all_history: keys = all_history.keys() values = Context.RedisMix.hash_mget('game.%d.exchange.record' % gid, *keys) for k, v in zip(keys, values): v = Context.json_loads(v) if v and v['type'] == 'exchange': _r = { 'ts': v['ts'], 'desc': v['desc'], 'cost': v['cost'], 'state': int(all_history[k]) } if 'phone' in v: _r['phone'] = v['phone'] _list.append(_r) return _list
def query_history_phone(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') uid_seq_map, all_seq_list = {}, [] while start_day <= end_day: fmt = Time.datetime_to_str(start_day, '%Y-%m-%d') records = Context.RedisStat.hash_getall('history:%d:%s' % (gid, fmt)) for seq, uid in records.iteritems(): if uid not in uid_seq_map: uid_seq_map[uid] = [] uid_seq_map[uid].append(int(seq)) all_seq_list.append(int(seq)) start_day = Time.next_days(start_day) _list = [] if all_seq_list: seq_record_map = Context.RedisMix.hash_mget_as_dict('game.%d.exchange.record' % gid, *all_seq_list) for uid, seq_list in uid_seq_map.iteritems(): states = Context.RedisCluster.hash_mget(uid, 'history:%d:%s' % (gid, uid), *seq_list) for seq, state in zip(seq_list, states): if state is not None and seq in seq_record_map: record = Context.json_loads(seq_record_map[seq]) if record['type'] == 'exchange' and record['to'] == 'phone': record = { 'uid': int(uid), 'ts': record['ts'], 'count': record['count'], 'phone': record['phone'], 'state': int(state), 'seq': seq, } _list.append(record) mo = MsgPack(0) mo.set_param('exchange', _list) return mo
def action_push_config(params): """推送配置""" data = File.read_file(params['redis.output']) j = Context.json_loads(data) for item in j: Context.RedisConfig.execute(*item)
def on_task_list(self, uid, gid, mi): conf = Context.Configure.get_game_item_json(gid, 'task.config') task_list = Context.Daily.get_daily_data(uid, gid, 'task.list') if task_list: task_list = Context.json_loads(task_list) else: what_day = Time.weekday(today=True) task_types = conf['daily'][what_day] task_map = {} for task in conf['task']: task_map[task['type']] = task total_degree, task_list = 0, [] for i, task_type in enumerate(task_types): task = {'id': i, 'type': task_type} if task_type == 2: # boss task['total'] = random.randint(*task_map[task_type]['range']) task['desc'] = task_map[task_type]['desc'] task['degree'] = task_map[task_type]['degree'] elif task_type == 3: task['total'] = random.randint(*task_map[task_type]['range']) task['desc'] = task_map[task_type]['desc'] task['degree'] = task_map[task_type]['degree'] elif task_type == 11: barrel_level = Context.Data.get_game_attr_int(uid, gid, 'barrel_level', 1) task['total'] = random.randint(*task_map[task_type]['range']) * barrel_level task['desc'] = task_map[task_type]['desc'] task['degree'] = task_map[task_type]['degree'] elif task_type == 21: task['desc'] = task_map[task_type]['desc'] task['degree'] = task_map[task_type]['degree'] elif task_type == 31: task['desc'] = task_map[task_type]['desc'] task['degree'] = task_map[task_type]['degree'] else: break total_degree += task['degree'] task_list.append(task) if total_degree < conf['total_degree']: fish_config = Context.Configure.get_game_item_json(gid, 'fish.201.config') fish_config = Context.copy_json_obj(fish_config) while total_degree < conf['total_degree']: # 普通鱼填充, 直到达到最大活跃值 task_type = 1 task = {'id': len(task_list), 'type': task_type} index = random.randrange(0, len(fish_config['common'])) fish = fish_config['common'][index] del fish_config['common'][index] task['total'] = random.randint(*task_map[task_type]['range']) task['desc'] = task_map[task_type]['desc'] task['fish_type'] = fish['type'] if isinstance(task_map[task_type]['degree'], list): for rg in task_map[task_type]['degree']: if task['total'] >= rg[0]: task['degree'] = rg[1] break else: task['degree'] = task_map[task_type]['degree'][-1][1] else: task['degree'] = task_map[task_type]['degree'] total_degree += task['degree'] task_list.insert(0, task) Context.Daily.set_daily_data(uid, gid, 'task.list', Context.json_dumps(task_list)) rewards, degree = [], 0 for task in task_list: if task['type'] == 1: count = Context.Daily.get_daily_data(uid, gid, 'fish.' + str(task['fish_type'])) task['count'] = Tool.to_int(count, 0) elif task['type'] == 2: count = Context.Daily.get_daily_data(uid, gid, 'class.boss') task['count'] = Tool.to_int(count, 0) elif task['type'] == 3: count = Context.Daily.get_daily_data(uid, gid, 'class.bonus') task['count'] = Tool.to_int(count, 0) elif task['type'] == 11: count = Context.Daily.get_daily_data(uid, gid, 'win.chip') task['count'] = Tool.to_int(count, 0) elif task['type'] == 21: task['done'] = 1 degree += task['degree'] if 'count' in task and task['count'] >= task['total']: task['done'] = 1 degree += task['degree'] # 处理奖励 _ids = range(len(conf['reward'])) for i, _id in enumerate(_ids): _ids[i] = 'task.reward.%d' % _id _states = Context.Daily.get_daily_data(uid, gid, *_ids) for _state, reward in zip(_states, conf['reward']): _state = 1 if _state else 0 _reward = FishProps.convert_reward(reward['reward']) rewards.append({'degree': reward['degree'], 'state': _state, 'reward': _reward}) mo = MsgPack(Message.MSG_SYS_TASK_LIST | Message.ID_ACK) mo.set_param('tasks', task_list) mo.set_param('reward', rewards) mo.set_param('degree', degree) return mo