def get_account_id(uuid): sql_result = util.GetOneRecordInfo('tb_account', dict(uuid=uuid)) if sql_result is not None: # print sql_result return sql_result['id'] else: data = dict(uuid=uuid, last_login=0, create_time=time.time()) insert_result = util.InsertIntoDB('tb_account', data) if insert_result: sql_result = util.GetOneRecordInfo('tb_account', dict(uuid=uuid)) return sql_result['id'] return 0
def __user_register(account_name='', account_password='', device_id='', is_tourist=True): # todo check account name password sql_result = util.GetOneRecordInfo(USER_TABLE_NAME, dict(account_name=account_name)) if sql_result: return json.dumps( dict(result=False, result_no=1, message='account name is exist')) if is_tourist: global USER_TABLE_MAX USER_TABLE_MAX += 1 account_name = 'tourist%s' % USER_TABLE_MAX account_password = TOURIST_PWD user_data = dict(id=get_uuid(), account_name=account_name, account_password=account_password, device_id=device_id, last_login=0, create_time=int(time.time())) insert_result = util.InsertIntoDB(USER_TABLE_NAME, user_data) if insert_result: return json.dumps( dict(result=True, result_no=0, account_name=account_name, account_password=account_password, passport=user_data.get('uuid'))) return json.dumps(dict(result=False, result_no=-1, message='error'))
def user_bind(): user_name = request.args.get('name') user_pwd = request.args.get('pwd') tourist_id = request.args.get('tid') logger.info('bind name:%s pwd:%s %s', user_name, user_pwd, tourist_id) if not user_name.isalnum(): return json.dumps( dict(result=False, result_no=-2, message='error name')) if len(user_name) > 32: return json.dumps( dict(result=False, result_no=-3, message='error name len')) if len(user_pwd) > 32: return json.dumps( dict(result=False, result_no=-4, message='error pwd len')) get_result = util.GetOneRecordInfo( USER_TABLE_NAME, dict(account_name=tourist_id, account_password=TOURIST_PWD)) if get_result is None: return json.dumps( dict(result=False, message='account name or password error!')) result = util.UpdateWithDict( USER_TABLE_NAME, dict(account_name=user_name, account_password=user_pwd, device_id=''), dict(id=get_result['id'])) logger.info('bind result:%s', result) return json.dumps(dict(result=result))
def find_friend_request_1107(data, player): request = friend_pb2.FindFriendRequest() request.ParseFromString(data) response = friend_pb2.FindFriendResponse() response.id = 0 response.nickname = 'none' response.atk = 111 response.hero_no = 11 response.gift = datetime.datetime.now().day if request.id_or_nickname.isdigit(): player_data = tb_character_info.getObjData(request.id_or_nickname) else: prere = dict(nickname=request.id_or_nickname) player_data = util.GetOneRecordInfo('tb_character_info', prere) if player_data: response.id = player_data.get('id') response.nickname = player_data.get('nickname') # 添加好友主将的属性 _with_battle_info(response, player_data.get('id')) return response.SerializePartialToString()
def __user_login(account_name='', account_password=''): get_result = util.GetOneRecordInfo( USER_TABLE_NAME, dict(account_name=account_name, account_password=account_password)) logger.info(get_result) if get_result is None: return json.dumps( dict(result=False, message='account name or password error!')) if get_result['id'] not in account_login_cache: account_login_cache.append(get_result['id']) return json.dumps(dict(result=True, passport=get_result['id']))
def query_touristid(): device_id = request.args.get('deviceid') get_result = util.GetOneRecordInfo(USER_TABLE_NAME, dict(device_id=device_id)) if get_result is None: return json.dumps(dict(result=False, message='query tourist fail!')) logger.info('tourist query :%s', get_result) return json.dumps( dict(result=True, account_name=get_result['account_name'], account_password=get_result['account_password']))
def pvp_player_info_request_1504(data, player): request = pvp_rank_pb2.PvpPlayerInfoRequest() request.ParseFromString(data) record = util.GetOneRecordInfo(PVP_TABLE_NAME, dict(id=request.player_rank), ['slots']) if record: response = record.get('slots') response = cPickle.loads(response) return response.SerializeToString() else: logger.error('can not find player rank:%s', request.player_rank) return None
def getObj(self, pk): '''根据主键,可以获得mmode对象的实例.\n >>> m = madmin.getObj(1) ''' mm = MMode(self._name + ':%s' % pk, self._pk) if not mm.IsEffective(): return None if mm.get('data'): return mm props = {self._pk: pk} record = util.GetOneRecordInfo(self._name, props) if not record: return None mm.loads(record) mm.insert() return mm
def __user_register(account_name='', account_password=''): # todo check account name password # sql_result = select_execute(USER_TABLE_NAME, dict(account_name=account_name)) sql_result = util.GetOneRecordInfo(USER_TABLE_NAME, dict(account_name=account_name)) if sql_result: return json.dumps( dict(result=False, result_no=1, message='account name is exist')) user_data = dict(id=get_uuid(), account_name=account_name, account_password=account_password, last_login=0, create_time=int(time.time())) insert_result = util.InsertIntoDB(USER_TABLE_NAME, user_data) if insert_result: return json.dumps( dict(result=True, result_no=0, passport=user_data.get('uuid'))) return json.dumps(dict(result=False, result_no=-1, message='error'))
def getObjData(self, pk): '''根据主键,可以获得mmode对象的实例的数据.\n >>> m = madmin.getObjData(1) ''' # print pk,"pk++++++++++++++" mm = MMode(self._name + ':%s' % pk, self._pk) if not mm.IsEffective(): return None data = mm.get('data') if mm.get('data'): return data props = {self._pk: pk} record = util.GetOneRecordInfo(self._name, props) if not record: return None mm.loads(record) mm.insert() return record
def nickname_create_5(request_proto, player): argument = CreatePlayerRequest() argument.ParseFromString(request_proto) nickname = argument.nickname response = CommonResponse() match = re.search(u'[\uD800-\uDBFF][\uDC00-\uDFFF]', nickname) if match: response.result = False response.result_no = 1 logger.info('not support emoji') return response.SerializeToString() if trie_tree.check.replace_bad_word(nickname) != nickname: response.result = False response.result_no = 1 return response.SerializeToString() # 判断昵称是否重复 sql_result = util.GetOneRecordInfo('tb_character_info', dict(nickname=nickname)) if sql_result: response.result = False response.result_no = 1 return response.SerializeToString() character_obj = tb_character_info.getObj(player.base_info.id) if not character_obj: response.result_no = 2 return response.SerializeToString() character_obj.update('nickname', nickname) # 加入聊天 remote_gate.login_chat_remote(player.dynamic_id, player.base_info.id, player.guild.g_id, nickname) response.result = True return response.SerializeToString()
def pvp_player_rank_refresh_request(data, player): response = pvp_rank_pb2.PlayerRankResponse() prere = dict(character_id=player.base_info.id) record = util.GetOneRecordInfo(PVP_TABLE_NAME, prere, ['id']) if not record: cur_rank = 300 else: cur_rank = record.get('id') ranks = [] for v in arena_fight_config.values(): play_rank = v.get('play_rank') if cur_rank in range(play_rank[0], play_rank[1] + 1): para = dict(k=cur_rank) choose_fields = eval(v.get('choose'), para) logger.info('cur:%s choose:%s', cur_rank, choose_fields) for x, y, c in choose_fields: for _ in range(c): r = random.randint(int(x), int(y)) ranks.append(r) break if not ranks: raise Exception('rank field error!') caret = ',' prere = 'id in (%s)' % caret.join(str(_) for _ in ranks) logger.info('prere:%s', prere) columns = ['id', 'nickname', 'level', 'ap', 'hero_ids'] records = util.GetSomeRecordInfo(PVP_TABLE_NAME, prere, columns) for record in records: rank_item = response.rank_items.add() rank_item.level = record.get('level') rank_item.nickname = record.get('nickname') rank_item.rank = record.get('id') rank_item.ap = record.get('ap') hero_ids = cPickle.loads(record.get('hero_ids')) rank_item.hero_ids.extend([_ for _ in hero_ids]) return response.SerializeToString()
def is_new_character(self): character_info = tb_character_info.getObj(self._pid) logger.debug('is_new_character,pid:%s', self._pid) if not character_info.exists(): pwere = dict(id=self._pid) result = util.GetOneRecordInfo(CHARACTER_TABLE_NAME, pwere) if result: logger.info('loads player in redis:%s-%s-%s', self._pid, len(result.get('base_info')), result['base_info']) character_info.hmset(cPickle.loads(result['base_info'])) equipments = cPickle.loads(result['equipments']) if equipments: character_info.getObj('equipments').hmset(equipments) mails = cPickle.loads(result['mails']) if mails: character_info.getObj('mails').hmset(mails) heroes = cPickle.loads(result['heroes']) if heroes: character_info.getObj('heroes').hmset(heroes) tb_character_info.sadd('all', self._pid) return not character_info.exists()
def pvp_player_rank_request_1502(data, player): response = pvp_rank_pb2.PlayerRankResponse() prere = dict(character_id=player.base_info.id) record = util.GetOneRecordInfo(PVP_TABLE_NAME, prere, ['id']) if not record: return pvp_player_rank_refresh_request(data, player) cur_rank = record.get('id') columns = ['id', 'nickname', 'level', 'ap', 'hero_ids'] prere = 'id>=%s and id<=%s' % (cur_rank - 9, cur_rank + 1) records = util.GetSomeRecordInfo(PVP_TABLE_NAME, prere, columns) for record in records: rank_item = response.rank_items.add() rank_item.level = record.get('level') rank_item.nickname = record.get('nickname') rank_item.rank = record.get('id') rank_item.ap = record.get('ap') hero_ids = cPickle.loads(record.get('hero_ids')) rank_item.hero_ids.extend([_ for _ in hero_ids]) return response.SerializeToString()
def get_user_info(args): if args['search_type'] == '1': character_obj = tb_character_info.getObj(args['search_value']) isexist = character_obj.exists() elif args['search_type'] == '2': nickname_obj = tb_character_info.getObj('nickname') isexist = nickname_obj.hexists(args['search_value']) pid = nickname_obj.hget(args['search_value']) character_obj = tb_character_info.getObj(pid) elif args['search_type'] == '3': uuid = args['search_value'] sql_result = util.GetOneRecordInfo('tb_account', dict(uuid=uuid)) if sql_result is not None: pid = sql_result['id'] character_obj = tb_character_info.getObj(pid) isexist = character_obj.exists() else: return {'success': 0, 'message': 3} else: return {'success': 0, 'message': 1} if not isexist: return {'success': 0, 'message': 2} character_info = character_obj.hmget([ 'nickname', 'attackPoint', 'heads', 'upgrade_time', 'level', 'id', 'exp', 'vip_level', 'register_time', 'upgrade_time', 'guild_id', 'position', 'finances', 'recharge_accumulation', 'gen_balance' ]) finances = character_info['finances'] if character_info['guild_id'] == 'no': position = 0 guild_name = '' guild_id = '' else: guild_id = character_info['guild_id'] guild_obj = tb_guild_info.getObj(guild_id) if guild_obj.exists(): guild_info = guild_obj.hmget(['name']) position = character_info['position'] guild_name = guild_info['name'] else: position = 0 guild_name = '' return { 'success': 1, 'message': { 'uid': character_info['id'], 'nickname': character_info['nickname'], 'level': character_info['level'], 'exp': character_info['exp'], 'vip_level': character_info['vip_level'], 'register_time': character_info['register_time'], 'recently_login_time': character_info['upgrade_time'], 'guild_id': guild_id, 'guild_name': guild_name, 'gold': finances[2], 'gold_all_cost': finances[9], 'gold_all_recharge': character_info['recharge_accumulation'], 'gold_all_send': character_info['gen_balance'], 'position': position } }
def check_mem_db(delta): """同步内存数据到数据库 """ new_character_ids = tb_character_info.smem('new') new_character_ids = filter(lambda x: x >= 10000, new_character_ids) for char_id in new_character_ids: character_obj = tb_character_info.getObj(char_id) character_info = character_obj.hgetall() character_equipments = character_obj.getObj('equipments').hgetall() character_mails = character_obj.getObj('mails').hgetall() character_heroes = character_obj.getObj('heroes').hgetall() user_data = dict(id=char_id, base_info=cPickle.dumps(character_info), heroes=cPickle.dumps(character_heroes), mails=cPickle.dumps(character_mails), equipments=cPickle.dumps(character_equipments)) insert_result = util.InsertIntoDB(CHARACTER_TABLE_NAME, user_data) if not insert_result: logger.error('insert id:%s error', char_id) result = util.GetOneRecordInfo(CHARACTER_TABLE_NAME, dict(id=char_id), ['id']) if result: tb_character_info.sadd('all', char_id) tb_character_info.srem('new', char_id) else: tb_character_info.sadd('all', char_id) tb_character_info.srem('new', char_id) logger.info('new character:%s', char_id) break global ALL_CHARACTER_IDS if not ALL_CHARACTER_IDS: ALL_CHARACTER_IDS = tb_character_info.smem('all') for char_id in ALL_CHARACTER_IDS: character_obj = tb_character_info.getObj(char_id) character_info = character_obj.hgetall() character_equipments = character_obj.getObj('equipments').hgetall() character_mails = character_obj.getObj('mails').hgetall() character_heroes = character_obj.getObj('heroes').hgetall() user_data = dict(base_info=cPickle.dumps(character_info), heroes=cPickle.dumps(character_heroes), mails=cPickle.dumps(character_mails), equipments=cPickle.dumps(character_equipments)) pwere = dict(id=char_id) result = util.GetOneRecordInfo(CHARACTER_TABLE_NAME, pwere, ['id']) if not result: logger.error('all insert id:%s error', char_id) tb_character_info.sadd('new', char_id) else: result = util.UpdateWithDict(CHARACTER_TABLE_NAME, user_data, pwere) if not result: logger.error('update id:%s error', char_id) # else: # logger.info('save character:%s', char_id) ALL_CHARACTER_IDS.remove(char_id) break if not new_character_ids and not ALL_CHARACTER_IDS: reactor.callLater(delta * 10, check_mem_db, delta) else: reactor.callLater(delta, check_mem_db, delta)
def pvp_fight_request_1505(data, player): request = pvp_rank_pb2.PvpFightRequest() request.ParseFromString(data) __skill = request.skill __best_skill = player.line_up_component.get_skill_id_by_unpar(__skill) __skill_level = 0 if __skill in player.line_up_component.unpars: __skill_level = player.line_up_component.unpars[request.skill] else: logger.error('error skill level:%s,%s', request.skill, player.line_up_component.unpars.keys()) line_up = {} # {hero_id:pos} for line in request.lineup: if not line.hero_id: continue line_up[line.hero_id] = line.pos player.line_up_component.line_up_order = line_up player.line_up_component.save_data() prere = dict(character_id=player.base_info.id) record = util.GetOneRecordInfo(PVP_TABLE_NAME, prere, ['id']) before_player_rank = 0 if record: before_player_rank = record.get('id') refresh_rank_data(player, before_player_rank, __skill, __skill_level) if before_player_rank == request.challenge_rank: logger.error('cant not fight self') return False prere = dict(id=request.challenge_rank) record = util.GetOneRecordInfo(PVP_TABLE_NAME, prere, ['character_id', 'nickname', 'level', 'ap', 'best_skill', 'unpar_skill', 'unpar_skill_level', 'units', 'slots', 'hero_ids']) blue_units = record.get('units') # print "blue_units:", blue_units blue_units = cPickle.loads(blue_units) # print "blue_units:", blue_units red_units = player.fight_cache_component.red_unit player.fight_cache_component.awake_hero_units(blue_units) player.fight_cache_component.awake_hero_units(red_units) process = BattlePVPProcess(red_units, __best_skill, player.level.level, blue_units, record.get('best_skill', 0), record.get('level', 1)) fight_result = process.process() logger.debug("fight result:%s" % fight_result) if fight_result: logger.debug("fight result:True:%s:%s", before_player_rank, request.challenge_rank) if before_player_rank != 0: if request.challenge_rank < before_player_rank: prere = dict(id=before_player_rank) result = util.UpdateWithDict(PVP_TABLE_NAME, record, prere) logger.info('update result:%s', result) refresh_rank_data(player, request.challenge_rank, __skill, __skill_level) else: refresh_rank_data(player, request.challenge_rank, __skill, __skill_level) else: logger.debug("fight result:False") response = pvp_rank_pb2.PvpFightResponse() response.res.result = True for slot_no, red_unit in red_units.items(): if not red_unit: continue red_add = response.red.add() assemble(red_add, red_unit) for slot_no, blue_unit in blue_units.items(): if not blue_unit: continue blue_add = response.blue.add() assemble(blue_add, blue_unit) response.red_skill = __skill response.red_skill_level = __skill_level response.blue_skill = record.get('unpar_skill') response.blue_skill_level = record.get('unpar_skill_level') lively_event = CountEvent.create_event(EventType.SPORTS, 1, ifadd=True) tstatus = player.tasks.check_inter(lively_event) if tstatus: task_data = task_status(player) remote_gate.push_object_remote(1234, task_data, [player.dynamic_id]) return response.SerializeToString()