def sync_gs_dirty_attributes(queue, loop=True): _len = queue.qsize() if _len > 0: if loop: _times = min(_len, MAX_SYNC_CS_CNT_PER_LOOP) else: _times = _len i = 0 dirty_attributes = [] while i < _times: i += 1 try: attr = queue.get_nowait() _data = attr.syncDirtyAttrToCS() if _data: dirty_attributes.append(_data) except Queue.Empty: break except Exception as e: log.error('Exp3029882 e:', e) if dirty_attributes: cs_call('cs_sync_user_attribute', (dirty_attributes, )) log.info( 'End sync, total attributes: {0}, left dirty attributes length: {1}.' .format(_times, queue.qsize())) if loop: reactor.callLater(SYNC_CS_INTERVAL, sync_gs_dirty_attributes, queue) else: log.warn('End sync, dirty attributes length {0}, loop:{1}.'.format( queue.qsize(), loop))
def syncToCS(self): if not self.__dirty: return if hasattr(self, 'update_time'): self.update_time = int(time()) dirty_data = {} for fn in self.__dirty_fields: dirty_data[fn] = self.__dict__[fn] cs_call('cs_sync_user_data', (self.__cid, self.__name, dirty_data)) self.clean_dirty()
def friend_rand_list(p, req): ''' 随机玩家列表 ''' cid, [nick_name] = req user = g_UserMgr.getUser( cid ) if not user: log.error('Can not find user. cid: {0}.'.format( cid )) defer.returnValue( CONNECTION_LOSE ) if nick_name: # 搜索指定玩家 try: _dataset = yield cs_call("cs_search_nick_name", [cid, nick_name]) defer.returnValue( _dataset ) except Exception as e: log.error('cs_search_nick_name error. e: {0}'.format( e )) defer.returnValue( [] ) except_cids = user.friends except_cids.append( cid ) # 随机的20为推荐玩家, 需要过滤掉自己和自己的好友 rand_users = [] users = g_UserMgr.getUserByLevel(user.level-5, user.level+5, except_cids) if len(users) >= FRIEND_RAND_MAX_COUNT: rand_users = random.sample(users, FRIEND_RAND_MAX_COUNT) else: users = g_UserMgr.getUserByLevel(user.level-10, user.level+10, except_cids) if len(users) >= FRIEND_RAND_MAX_COUNT: rand_users = random.sample(users, FRIEND_RAND_MAX_COUNT) else: users = g_UserMgr.getUserByLevel(0, 1000, except_cids) if len(users) >= FRIEND_RAND_MAX_COUNT: rand_users = random.sample(users, FRIEND_RAND_MAX_COUNT) else: rand_users = users rand_info = [_u.friend_info() for _u in rand_users] totals = len(rand_users) if totals >= FRIEND_RAND_MAX_COUNT: defer.returnValue( rand_info ) # 查询db 获取离线玩家的基本信息 try: for _u in rand_users: except_cids.append( _u.cid ) offline_info = yield cs_call("cs_offline_rand_friends", [cid, FRIEND_RAND_MAX_COUNT-totals, user.level, except_cids]) rand_info.extend( offline_info ) defer.returnValue( rand_info ) except Exception as e: log.error('cs_offline_rand_friends error. e: {0}'.format( e )) defer.returnValue( [] )
def syncToCS(self): if not self.__dirty: #log.info('For Test. no data need sync to cs.') return #log.info('For Test. GSAttribute::syncToCS, self.__dirty_fields:', self.__dirty_fields) if hasattr(self, 'update_time'): self.update_time = datetime.now() dirty_data = {} for fn in self.__dirty_fields: dirty_data[fn] = self.__dict__[fn] cs_call('cs_sync_user_attribute', ([(self.__cid, self.__name, self.__row_id, dirty_data)], )) self.clean_dirty()
def load(cid, table): attribs = {} try: res_err, table_data = yield cs_call('cs_load_table_data', (cid, table)) #log.info('For Test. res_err: {0}, length: {1}.'.format( res_err, len(table_data) )) if res_err: log.error( 'load table({0}) data error. table_data: {1}.'.format( table, table_data)) defer.returnValue(attribs) _table_fields = TABLE_FIELDS[table][0] for attrib_id, row in table_data.iteritems(): attr = GSAttribute(cid, table, attrib_id) attr.updateGSAttribute(False, **dict(zip(_table_fields, row))) if attr.attrib_id not in attribs: attribs[attr.attrib_id] = attr else: log.error('Exception table data. attrib_id: {0}.'.format( attr.attrib_id)) defer.returnValue(attribs) except Exception as e: log.error('Exception raise. e: {0}.'.format(e)) raise e
def gs_offline_login(cid): user = g_UserMgr.getUser(cid, True) if user: defer.returnValue( user ) alliance_info = 0, '', 0 try: res_err = yield cs_call("cs_character_login", [cid]) # get alliance info alliance_info = yield alli_call('get_alliance_info', [cid]) except Exception as e: log.error('cs_character_login error. e: {0}'.format( e )) defer.returnValue( None ) if res_err[0]: log.error('Get char data error. cid: {0}, res_err: {1}.'.format(cid, res_err)) defer.returnValue( None ) user = g_UserMgr.loginUser( res_err[1], True ) user.offline_flag = True if alliance_info[0] > 0: user.alliance_id = alliance_info[0] user.alliance_name = alliance_info[1] defer.returnValue( user )
def gs_gm_login(p, req): ''' gm login gameserver as offline login @param: query_flag-查询标志位 ''' cid, query_flag = req user = g_UserMgr.getUser(cid, True) if user: defer.returnValue( NO_ERROR ) try: res_err = yield cs_call("cs_character_login", [cid]) except Exception as e: log.error('gm cs_character_login error. e: {0}'.format( e )) defer.returnValue( UNKNOWN_ERROR ) if res_err[0]: log.error('Get char data error. cid: {0}, res_err: {1}.'.format(cid, res_err)) defer.returnValue( res_err[0] ) user = g_UserMgr.loginUser( res_err[1], flag=True ) user.offline_flag = True yield user.fellow_mgr.value_list # 查询玩家离线登陆后 5min下线 if query_flag: reactor.callLater(SESSION_LOGOUT_REAL, gs_offline_logout, cid) defer.returnValue( NO_ERROR )
def gs_logout(p, req): cid = req user = g_UserMgr.getUser(cid) if not user: log.warn('User had logout. cid: {0}.'.format( cid )) defer.returnValue( UNKNOWN_ERROR ) # 有离线登陆的玩家 if user.offline_num > 0: log.warn('User had offline login, could not logout. cid: {0}.'.format( cid )) # 更新标志位为离线登陆标志 user.offline_flag = True defer.returnValue( UNKNOWN_ERROR ) if worldBoss.running: worldBoss.remove_attacker(cid) try: res = yield cs_call('cs_character_logout', cid) except: log.warn('Some exp raise in gs_logout. cid: {0}.'.format( cid )) defer.returnValue( UNKNOWN_ERROR ) else: g_UserMgr.logoutUser(cid) log.debug('user logout sucess. cid: {0}.'.format( cid )) defer.returnValue( res )
def sync_gs_dirty_attributes(loops=True): _len = g_DirtyGSAtrributeQueue.qsize() if _len > 0: if loops: _times = min(_len, MAX_SYNC_CS_CNT_PER_LOOP) else: _times = _len i = 0 dirty_attributes = [] while i < _times: i += 1 try: attr = g_DirtyGSAtrributeQueue.get_nowait() _data = attr.syncDirtyAttrToCS() if _data: dirty_attributes.append( _data ) except Queue.Empty: break except Exception as e: log.error('Exp3029882 e:', e) if dirty_attributes: yield cs_call('cs_sync_user_data', dirty_attributes) log.info('End sync, total attributes: {0}, left dirty attributes length: {1}.'.format( _times, g_DirtyGSAtrributeQueue.qsize())) if loops: loop.call_later(SYNC_CS_INTERVAL, sync_gs_dirty_attributes) else: log.warn('End sync, dirty attributes length {0}, loop:{1}.'.format(g_DirtyGSAtrributeQueue.qsize(), loops))
def gs_create_char(p, req): lead_id, nick_name, account, login_type, platform_id, version, sid = req res_err = [UNKNOWN_ERROR, None] try: res_err = yield cs_call("cs_new_character", (lead_id, nick_name, account, sid)) except Exception as e: log.error('cs_new_character raise. e: {0}.'.format( e )) defer.returnValue(res_err) if res_err[0]: log.error('New char data error.') defer.returnValue( res_err ) user = g_UserMgr.loginUser( res_err[1] ) #yield user.sync_camp_to_redis() # new major fellow # args: fellow_id, is_major, camp_id, on_troop yield user.fellow_mgr.create_table_data( lead_id, 1, 1, FELLOW_FORMATION_ORDER[0]+1 ) # a gift to new character yield user.fellow_mgr.create_table_data( 22071, 0, 0, 0 ) yield user.bag_treasureshard_mgr.create_table_data( 12, 41001, 1, int(time()) ) yield user.bag_treasureshard_mgr.create_table_data( 12, 41003, 1, int(time()) ) try: device = login_type.split('/') if len(device) < 3: device = ['', '', ''] except Exception, e: log.error(e) device = ['', '', '']
def friend_list(p, req): ''' 获取好友列表 ''' cid, [index] = req user = g_UserMgr.getUser( cid ) if not user: log.error('Can not find user. cid: {0}.'.format( cid )) defer.returnValue( CONNECTION_LOSE ) add_douzhan = yield user.goodwill_mgr.check_achieve() totals = len(user.friends) #成就 yield user.achievement_mgr.update_achievement_status( ACHIEVEMENT_QUEST_ID_20, totals ) friend_cids = user.friends[:index+20] if (not friend_cids): defer.returnValue( [index, totals, [], 0, GET_DOUZHAN_DAILY_LIMIT + add_douzhan] ) # 给好友赠送斗战点的信息 gift_cids = [] gift_data = yield redis.hget(HASH_FRIENDS_SEND_DOUZHAN, cid) if gift_data: gift_data = loads(gift_data) dt_now = datetime.now() dt_last = datetime.fromtimestamp( gift_data[0] ) if dt_last.date() == dt_now.date(): gift_cids = gift_data[1] friend_info = [] offline_cids = [] for _cid in friend_cids: online_user = g_UserMgr.getUser( _cid ) if online_user: _info = online_user.friend_info() _info.append(1) # 0-离线, 1-在线 _info.append( 1 if _cid in gift_cids else 0 ) # 0-未赠送, 1-已赠送 # 在线好友排序 ?? friend_info.append( _info ) else: offline_cids.append( _cid ) # 查询db 获取offline_cids的基本信息 try: if offline_cids: dataset = yield cs_call("cs_offline_friends", [cid, offline_cids]) if dataset: for _info in dataset: _info = list( _info ) _info.append(0) # 0-离线, 1-在线 _info.append( 1 if _info[0] in gift_cids else 0 ) # 0-未赠送, 1-已赠送 friend_info.append( _info ) except Exception as e: log.error('cs_offline_friends error. e: {0}'.format( e )) left_count, gift_data = yield get_friend_douzhan_status(cid, add_douzhan) defer.returnValue( [index, totals, friend_info, len(gift_data), left_count] )
def gs_login(p, req): cid, = req data = yield cs_call('cs_character_login', cid) if data[0]: raise gen.Return(data) g_UserMgr.login(data[1]) f = yield execute("hget", "HASH_EAT_PEACH_REWARD", 51651) raise gen.Return("login succeed!")
def gs_load_table_data(cid, table): try: res_err, table_data = yield cs_call('cs_load_table_data', (cid, table)) if res_err: log.error('Load table data error. cid: {0}, table: {1}.'.format( cid, table )) defer.returnValue( None ) defer.returnValue( table_data ) except Exception as e: log.error( 'Exception raise. e: {0}.'.format( e )) defer.returnValue(res_err)
def new(self, **kwargs): try: res_err, create_data = yield cs_call('cs_new_attrib', (self.__cid, self.__name, kwargs)) if res_err: log.error('create table({0}) data error. create_data: {1}.'.format( self.__name, create_data )) raise gen.Return(res_err) self.updateGSAttribute(False, **dict(zip(self.__fields, create_data)) ) raise gen.Return(res_err) except Exception as e: log.error( 'Exception raise. e: {0}.'.format( e )) raise gen.Return(UNKNOWN_ERROR)
def gs_load_table_data(cid, table): try: res_err, table_data = yield cs_call('cs_load_table_data', (cid, table)) if res_err: log.error('Load table data error. cid: {0}, table: {1}.'.format( cid, table)) defer.returnValue(None) defer.returnValue(table_data) except Exception as e: log.error('Exception raise. e: {0}.'.format(e)) defer.returnValue(res_err)
def createItemViaCS(self, item_type, item_id, add_count):#Return item inst if succeed, else return None try: res_create = yield cs_call('cs_create_item', (self.__cid, item_type, item_id, add_count)) except Exception as e: log.exception() defer.returnValue(None) err, new_item_data = res_create gs_att = GSAttribute(self.__cid, self._tbl_name, new_item_data['id'], self._item_field_set) gs_att.updateGSAttribute(False, new_item_data) new_item = GSBagItem(gs_att) defer.returnValue(new_item)
def gs_create_table_data(*args, **kwargs): cid, table = args try: res_err, create_data = yield cs_call('cs_new_attrib', (cid, table, False, kwargs)) except Exception as e: log.error( 'Exception raise. e: {0}.'.format( e )) defer.returnValue(res_err) if res_err: log.error('Create table data error. cid: {0}, table: {1}.'.format( cid, table )) defer.returnValue( None ) else: defer.returnValue( create_data )
def gs_kickout_user(p, req): cid, = req user = g_UserMgr.getUser(cid) if not user: defer.returnValue( NO_ERROR ) try: user.syncGameDataToCS() yield cs_call('cs_character_logout', cid) g_UserMgr.kickoutUser(cid) except: log.exception() defer.returnValue( UNKNOWN_ERROR )
def gs_create_table_data(*args, **kwargs): cid, table = args try: res_err, create_data = yield cs_call('cs_new_attrib', (cid, table, False, kwargs)) except Exception as e: log.error('Exception raise. e: {0}.'.format(e)) defer.returnValue(res_err) if res_err: log.error('Create table data error. cid: {0}, table: {1}.'.format( cid, table)) defer.returnValue(None) else: defer.returnValue(create_data)
def createItemViaCS( self, item_type, item_id, add_count): #Return item inst if succeed, else return None try: res_create = yield cs_call( 'cs_create_item', (self.__cid, item_type, item_id, add_count)) except Exception as e: log.exception() defer.returnValue(None) err, new_item_data = res_create gs_att = GSAttribute(self.__cid, self._tbl_name, new_item_data['id'], self._item_field_set) gs_att.updateGSAttribute(False, new_item_data) new_item = GSBagItem(gs_att) defer.returnValue(new_item)
def new(self, **kwargs): try: res_err, create_data = yield cs_call( 'cs_new_attrib', (self.__cid, self.__name, True, kwargs)) if res_err: log.error( 'create table({0}) data error. create_data: {1}.'.format( self.__name, create_data)) defer.returnValue(res_err) self.updateGSAttribute(False, **dict(zip(self.__fields, create_data))) defer.returnValue(res_err) except Exception as e: log.error('Exception raise. e: {0}.'.format(e)) defer.returnValue(UNKNOWN_ERROR)
def load(cid, table): attribs = {} try: res_err, table_data = yield cs_call('cs_load_table_data', (cid, table)) if res_err: log.error('load table({0}) data error. table_data: {1}.'.format( table, table_data )) raise gen.Return(attribs) _table_fields = EntityDescription[table][2] for row in table_data: attr = GSAttribute(cid, table, row[0]) attr.updateGSAttribute( False, **dict(zip(_table_fields, row)) ) if attr.ID not in attribs: attribs[attr.ID] = attr else: log.error('Exception table data. attrib_id: {0}.'.format( attr.ID )) raise gen.Return(attribs) except Exception as e: log.error( 'Exception raise. e: {0}.'.format( e )) raise e
def gs_offline_logout(cid): user = g_UserMgr.getUser(cid) if not user: defer.returnValue( UNKNOWN_ERROR ) # 最后一个离线登陆时才做logou处理 flag = user.logoutOffline() if not flag: defer.returnValue( UNKNOWN_ERROR ) # 该玩家已正常登陆, 不做任何处理 if not user.offline_flag: defer.returnValue( UNKNOWN_ERROR ) try: res = yield cs_call('cs_character_logout', cid) except: log.error('Some exp raise in gs_logout') defer.returnValue( UNKNOWN_ERROR ) else: g_UserMgr.logoutUser(cid) log.debug('gs_offline_logout end. cid: ', cid) defer.returnValue( res )
except Exception, e: log.error(e) user_host = '' user = g_UserMgr.getUser(cid) if user: user.offline_flag = False char_data = user.base_att_value res_err = [NO_ERROR, char_data, version_conf] yield check_grant_items(cid, user) log.warn('gameserver has user old data.') syslogger(LOG_LOGIN, cid, cid, user_host, ver, user.level, user.vip_level) defer.returnValue( res_err ) try: res_err = yield cs_call("cs_character_login", [cid]) except Exception as e: log.error('cs_character_login error. e: {0}'.format( e )) defer.returnValue( res_err ) if res_err[0]: log.error('Get char data error. detail:', res_err) defer.returnValue( res_err ) user = g_UserMgr.loginUser( res_err[1] ) user.offline_flag = False yield check_grant_items(cid, user) syslogger(LOG_LOGIN, cid, cid, user_host, ver, user.level, user.vip_level) defer.returnValue( [NO_ERROR, user.base_att_value, version_conf] )