def incrby(cls, uid, gid, rank_name, score, cache=None, field='score'): key = 'rank:%d:%s' % (gid, rank_name) score = Context.RedisMix.zset_incrby(key, uid, score) if cache: cache[field] = score Context.RedisMix.hash_set(key + ':cache', uid, Context.json_dumps(cache)) return int(score)
def on_exchange(self, uid, gid, mi): mo = MsgPack(Message.MSG_SYS_EXCHANGE | Message.ID_ACK) _id = mi.get_param('id') if not isinstance(_id, int): return mo.set_error(1, 'error param') conf = Context.Configure.get_game_item_json(gid, 'exchange.config') if _id >= len(conf): return mo.set_error(1, 'error id') info = conf[_id] to_type = info['type'] if to_type not in ('diamond', 'props', 'phone'): raise Exception( str(to_type) + '<----error type, please check config') real, final = Context.UserAttr.incr_coupon(uid, gid, -info['cost'], 'exchange.' + to_type) if real != -info['cost']: return mo.set_error(2, 'not enough') mo.set_param('coupon', final) record = { 'uid': uid, 'type': 'exchange', 'ts': Time.current_ts(), 'from': 'coupon', 'to': to_type, 'cost': info['cost'], 'count': info['count'], 'desc': info['desc'] } if info['type'] == 'diamond': # 兑换钻石 real, final = Context.UserAttr.incr_diamond( uid, gid, info['count'], 'exchange.diamond') mo.set_param('diamond', final) state = 1 elif info['type'] == 'props': # 兑换道具 real, final = FishProps.incr_props(uid, gid, info['id'], info['count'], 'exchange.props') mo.set_param('id', info['id']) mo.set_param('count', final) state = 1 record['id'] = info['id'] elif info['type'] == 'phone': state = 0 record['phone'] = mi.get_param('phone') else: raise Exception('something error, please check config') seq_num = Context.RedisMix.hash_incrby('game.%d.info.hash' % gid, 'exchange.history.seq', 1) Context.RedisCluster.hash_set(uid, 'history:%d:%d' % (gid, uid), seq_num, state) record = Context.json_dumps(record) Context.RedisMix.hash_mset('game.%d.exchange.record' % gid, seq_num, record) fmt = Time.current_time('%Y-%m-%d') Context.RedisStat.hash_set('history:%d:%s' % (gid, fmt), seq_num, uid) return mo
def __add_config_item(cls, key, attr, value): if key not in cls._ALL_CONFIG: cls._ALL_CONFIG[key] = {} cls._ALL_CONFIG[key][attr] = value if isinstance(value, (tuple, list, dict)): value = Context.json_dumps(value) cls._ALL_CMD.append(['HSET', key, attr, value])
def gm_push_led(self, gid, mi, request): msg = mi.get_param('msg') if not msg: # 清除led Context.RedisCache.delete('game.%d.led.list' % gid) else: led = Context.json_dumps({'led': msg, 'ts': Time.current_ts()}) Context.RedisCache.list_lpush('game.%d.led.list' % gid, led) return MsgPack(0)
def save_config(cls, fdir, fname): cls.add_global_config('game.list', cls._ROOM_MAP.keys()) for gameId, item in cls._ROOM_MAP.iteritems(): cls.add_game_config(gameId, 'room.map', item) roomtype = item.keys() cls.add_game_config(gameId, 'room.type', sorted(roomtype)) gamelist = set() for _, servers in item.iteritems(): gamelist.update(servers) data = Context.json_dumps(cls._ALL_CMD) File.write_file(fdir, fname, data)
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 global_lock(cls, uid, gid, expire_at): data = Context.json_dumps({'gid': gid, 'ts': expire_at}) Context.RedisCache.hash_set('global.playing.lock', uid, data)
def game_push_config(params): action_push_config(params) Context.RedisCache.set('connect.server', Context.json_dumps(params['connect.server']))
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