def gm_add_money(self, pid, coin1, coin2=0, coin3=0, is_set=False, vip=False): """ 增加金钱(coin1, coin2=0, coin3=0) """ p = self._get_player(pid) if p: p.add_coin(aCoin1=coin1, aCoin2=coin2, aCoin3=coin3, is_set=is_set, vip=vip, log_type=COIN_ADD_GM) p.pack_msg_data(coin=1, send=1) else: str_coin1, str_coin2, str_coin3 = 'coin1', 'coin2', 'coin3' cols = [str_coin1, str_coin2, str_coin3] val = PlayerData.get_values(pid, cols) coin1 = val[str_coin1] + coin1 coin2 = val[str_coin2] + coin2 coin3 = val[str_coin3] + coin3 PlayerData.update_values( pid, dict(coin1=coin1, coin2=coin2, coin3=coin3)) self.log(u'增加银币成功,角色当前(银币,元宝,绑元宝)数:(%s, %s, %s)', coin1, coin2, coin3)
def gm_scene_enter(self, pid, mapId): p = self._get_player(pid) if p: p.scene_enter(mapId) else: PlayerData.update_values(pid, dict(mapId=int(mapId))) self.log(u'进入地图成功')
def gm_unforbid_chat(self, pid): """ 取消禁言 """ p = self._get_player(pid) if p: p.unforbid_chat() else: PlayerData.update_values(pid, dict(fbChat=0)) self.log(u'取消禁言成功')
def gm_set_level(self, pid, level): """ 调等级 """ player = Game.rpc_player_mgr.get_rpc_player(pid) if player: player.upgrade(level) else: PlayerData.update_values(pid, dict(level=level)) self.log(u'更发等级成功')
def gm_unforbid_login(self, pid): """ 解禁登录 """ p = self._get_player(pid) if p: p.unforbid_login() else: PlayerData.update_values(pid, dict(fbLogin=0)) self.log(u'取消禁止登录成功')
def gm_forbid_chat(self, pid, times): """ 禁言到某某时间 """ p = self._get_player(pid) if p: p.forbid_chat(times) else: PlayerData.update_values(pid, dict(fbChat=int(time.time()) + times)) self.log(u'禁言成功')
def gm_forbid_login(self, pid, times): """ 禁止登录 """ p = self._get_player(pid) if p: p.forbid_login(times) else: PlayerData.update_values(pid, dict(fbLogin=int(time.time()) + times)) self.log(u'禁止登录成功')
def gm_del_player(self, pid): """ 删除玩家,不删除数据,只是将数据和user断开联系 """ p = self._get_player(pid) if not p: uid = 'uid' tmp_player = PlayerData.get_values(pid, None) PlayerData.update_values(pid, dict(uid=-abs(tmp_player[uid]))) else: p.data.uid = -abs(p.data.uid) self.log(u'修改角色(%d)成功', pid)
def _reward(self): """ 世界boss战发放奖励并 """ rank_data = sorted(self.pid_hurts.iteritems(), key=lambda x: x[1], reverse=True) #活动奖励广播 self.boss_mgr.safe_pub(MSG_REWARD_WORLDBOSS, rank_data, self.max_hp) pid_levels = PlayerData.get_players_levels(self.pid_hurts.keys()) res_rw_mail = Game.res_mgr.reward_mails.get(RW_MAIL_BOSS) ranks_data = {} p_ranks = [] start_type = self.start_type for index, (pid, hurts) in enumerate(rank_data): rank = index + 1 key = (WORLD_BOSS, rank) rs_items = self._get_reward(key, hurts, pid_levels[pid]) #邮件通知玩家 content = res_rw_mail.content % dict(rank=rank) Game.mail_mgr.send_mails(pid, MAIL_REWARD, res_rw_mail.title, RW_MAIL_BOSS, rs_items, param=content) self.boss_mgr.safe_pub(MSG_REWARD_WORLDITEMS, pid, start_type, rs_items) ranks_data[pid] = (rank, hurts) p_ranks.append(pid) self.boss_mgr.safe_pub(MSG_WBOSS_RANK, ranks_data, p_ranks)
def get_player_infos(self, pids, CBE=0): """ 获取玩家信息 返回: onlines, {pid:(name, rid, level, CBE)} """ onlines = self.get_online_ids(pids) rs = {} #等级 if CBE: levels = self.exec_players_func(onlines, _get_level_CBE, has_result=1, _pickle=True) else: levels = self.exec_players_func(onlines, _get_level, has_result=1, _pickle=True) off_ids = set(pids).difference(levels.keys()) if off_ids: off_lvs = PlayerData.get_players_levels(off_ids, CBE=CBE) if off_lvs: levels.update(off_lvs) for pid in pids: n_rid = self.get_name_by_id(pid, rid=1) if not n_rid: continue if CBE: lv, cbe = levels.get(pid, (1, 0)) rs[pid] = (n_rid[0], n_rid[1], lv, cbe) else: rs[pid] = (n_rid[0], n_rid[1], levels.get(pid, 1)) return onlines, rs
def message(self): """ 获取需要的信息 """ #获取对应等级范围内对应玩家的id while not self.stoped: self.mapid_pos = {} self.levels_players = {} for levels in self.LEVELS.itervalues(): cols = ['pos', 'mapId', 'id'] players = PlayerData.get_level_pvalue(levels[0], levels[1], cols, 100) if not players: continue while players: player = random.choice(players) players.remove(player) levels_players = self.levels_players.setdefault( tuple(levels), []) levels_players.append(player) if self.COUNT < len(levels_players): break p_pos = player['pos'] mid = player['mapId'] pid = player['id'] poses = self.mapid_pos.setdefault(mid, [[], []]) if p_pos and p_pos not in poses[PLAYER_POS_INDEX]: poses[PLAYER_POS_INDEX].append(p_pos) self._get_npc_pos(mid, pid, poses) sleep(10 * 60)
def message(self): """ 获取需要的信息 """ #获取对应等级范围内对应玩家的id while not self.stoped: self.mapid_pos = {} self.levels_players = {} for levels in self.LEVELS.itervalues(): cols = ['pos', 'mapId', 'id'] players = PlayerData.get_level_pvalue(levels[0], levels[1], cols, 100) if not players: continue while players: player = random.choice(players) players.remove(player) levels_players = self.levels_players.setdefault(tuple(levels), []) levels_players.append(player) if self.COUNT < len(levels_players): break p_pos = player['pos'] mid = player['mapId'] pid = player['id'] poses = self.mapid_pos.setdefault(mid, [[],[]]) if p_pos and p_pos not in poses[PLAYER_POS_INDEX]: poses[PLAYER_POS_INDEX].append(p_pos) self._get_npc_pos(mid, pid, poses) sleep(10*60)
def gm_add_ally_members(self, a_name, p_name, num): """添加同盟成员的数目""" if p_name: return self._add_member_by_p_name(a_name, p_name) try: rpc_store = self.player._game.rpc_store ally_mgr = Game.instance.rpc_ally_mgr sum_num = 0 #通过GM命令加入同盟的总数 lev_in_num = PlayerData.count({FN_PLAYER_LEVEL: { FOP_GTE: 1 }}) #达到等级条件的总数 us_list = [] #本次使用到了的pid #每个段的起始和结束pid[(s1,e1), (s2,e2))] section_list = [(i * SECTION, (i + 1) * SECTION) for i in xrange(lev_in_num / SECTION)] start_time = time.time() shuffle(section_list) if not ally_mgr.set_gm_ally_by_name(a_name): self.log(u'公会名字不存在') return for start_pid, end_pid in section_list: us_list.append((start_pid, end_pid)) for pid in xrange(start_pid, end_pid): if sum_num >= num: self.log(u'申请添加%s个满足等级条件的%s个实际添加%s个成员', num, lev_in_num, sum_num) ally_mgr.clear_gm_ally() return if not rpc_store.has("player", pid): log.info("table player has no pid:%s", pid) continue sum_num += ally_mgr.gm_add_ally(pid) except: log.log_except()
def _reward(self): """ 发放奖励 """ log.info(u'竞技场排名:%s', self.ranks[:500]) #log.info('arena_ranks:%s', self.pid2ranks) mail_mgr = Game.mail_mgr rw_mail = Game.res_mgr.reward_mails.get(RW_MAIL_ARENA) levels = {} lv_count = 30 #批量读取等级 global arena_level ranks = self.ranks[:] log.info('arena_reward:%s', len(ranks)) for index, pid in enumerate(ranks): rank = index + 1 pids = ranks[index:index + lv_count] if pid not in levels: #批量获取等级 self._get_rivals(pids) rs = PlayerData.get_players_levels(pids) if rs: levels.update(rs) if pid not in levels: #玩家不存在 continue rs, items, rid = self.get_reward(rank, levels[pid]) if not rs: break #content = rw_mail.content % dict(name=self.get_name(pid), rank=rank) content = rw_mail.content % dict(rank=rank) mail_mgr.send_mails(pid, MAIL_REWARD, rw_mail.title, RW_MAIL_ARENA, items, param=content, rid=rid)
def new_player(self, uid, name, rid): """ 创建玩家对象,防止重名 """ with self._new_player_lock: if self.get_id_by_name(name) is not None: return pid, data = PlayerData.new_player(uid, name, rid) self._add_name_id(pid, name, rid) return data
def gm_get(self, name): """ 获取角色gm类 """ pid = PlayerData.name_to_id(name) p = self.player._game.player_mgr.get_player(pid) if not p: self.log(u'获取玩家(%s)失败', name) else: return GMPlayer(self, p)
def get_id_by_name(self, name): """ 根据名称获取对应玩家id """ try: return self.name2pids[name] except KeyError: pid_rid = PlayerData.name_to_id(name) if pid_rid is None: return self._add_name_id(pid_rid[0], name, pid_rid[1]) return pid_rid[0]
def _node_list(self, op_l): online_pids = self._mgr._game.rpc_player_mgr.get_online_ids(op_l) r_l = [] for pid in op_l: t_dict = PlayerData.get_values(pid, ['rid', 'name', 'level'], self._mgr._game.rpc_store) if not t_dict: continue if pid in online_pids: t_dict['st'] = 1 else: t_dict['st'] = 0 r_l.append(t_dict) return r_l
def _auto_start(self): global arena_auto_start, arena_level sleep_times = 60 * 5 log.info('arena auto_start(%s) running', arena_auto_start) while 1: sleep(sleep_times) c = PlayerData.count({FN_PLAYER_LEVEL: {FOP_GTE: arena_level}}) if c >= arena_auto_start: log.info('arena auto start:%d', c) self.init() self.is_start = True self.start() self._auto_start_task = None return
def members(self, is_other=False): """ 获取自己同盟某页成员""" onls = [] dis_onls = [] pids = self.get_member_pids() online_pids = self._mgr.rpc_player_mgr.get_online_ids(pids) p_dicts = dict( (data['id'], data) for data in PlayerData.get_players_values( pids, ['name', 'level', 'tLogout'], store=self.rpc_store)) for assist_data in self.members_list: try: t_dit = dict() pid = assist_data.pid if pid not in p_dicts: #玩家不存在 log.error(u'同盟(%s)玩家(%s)不存在', self.data.name, pid) continue p_dic = p_dicts[pid] t_dit['pid'] = pid t_dit['n'] = p_dic['name'] t_dit['lv'] = p_dic['level'] t_dit['duty'] = assist_data.duty t_dit['pvp'] = 2000 pvp = self._mgr._game.rpc_arena_mgr.get_rank(pid) if pvp: t_dit['pvp'] = pvp #是不是查看其他的同盟 是就不加载exp和dt dis_onls.append(t_dit) if not is_other: t_dit['exp'] = assist_data.exp if pid in online_pids: t_dit['dt'] = 0 dis_onls.remove(t_dit) onls.append(t_dit) else: t_dit['dt'] = p_dic['tLogout'] except: log.error( "PlayerData.get_values is None pid:%s in ally.py func->members", assist_data.pid) if onls or dis_onls: onls = sor_member(is_other, True, onls) dis_onls = sor_member(is_other, False, dis_onls) mems = onls mems.extend(dis_onls) return mems
def get_name_by_id(self, pid, rid=0): """ 查询pid, rid: 1 一起查询主配将id return: name, name, rid """ try: if rid: return self.pid2names[pid] return self.pid2names[pid][0] except KeyError: name_rid = PlayerData.id_to_name(pid) if name_rid is None: return self._add_name_id(pid, name_rid[0], name_rid[1]) if rid: return name_rid return name_rid[0]
def _reward(self): """ 同盟boss战发放奖励并 """ a_level = Game.rpc_ally_mgr.ally_level_by_aid(self.data.aid) pid_levels = PlayerData.get_players_levels(self.pid_hurts.keys()) for rank, pid in enumerate(self.rank_pids): key = (ALLY_BOSS, a_level, rank+1) hurt = self.pid_hurts.get(pid) rs_items = self._get_reward(key, hurt, pid_levels[pid], a_level) #邮件通知玩家 Game.mail_mgr.send_mails(pid, MAIL_REWARD, language.ALLY_BOSS_REWARD, RW_MAIL_ABOSS, rs_items) for pid, hurt in self.pid_hurts.iteritems(): if pid in self.rank_pids: continue key = (ALLY_BOSS, a_level) rs_items = self._get_reward(key, hurt, pid_levels[pid], a_level) #邮件通知玩家 Game.mail_mgr.send_mails(pid, MAIL_REWARD, language.ALLY_BOSS_REWARD, RW_MAIL_ABOSS, rs_items)
def members(self, is_other = False): """ 获取自己同盟某页成员""" onls = [] dis_onls = [] pids = self.get_member_pids() online_pids = self._mgr.rpc_player_mgr.get_online_ids(pids) p_dicts= dict((data['id'], data) for data in PlayerData.get_players_values( pids, ['name', 'level', 'tLogout'], store=self.rpc_store)) for assist_data in self.members_list: try: t_dit = dict() pid = assist_data.pid if pid not in p_dicts:#玩家不存在 log.error(u'同盟(%s)玩家(%s)不存在', self.data.name, pid) continue p_dic = p_dicts[pid] t_dit['pid'] = pid t_dit['n'] = p_dic['name'] t_dit['lv'] = p_dic['level'] t_dit['duty'] = assist_data.duty t_dit['pvp'] = 2000 pvp = self._mgr._game.rpc_arena_mgr.get_rank(pid) if pvp: t_dit['pvp'] = pvp #是不是查看其他的同盟 是就不加载exp和dt dis_onls.append(t_dit) if not is_other: t_dit['exp'] = assist_data.exp if pid in online_pids: t_dit['dt'] = 0 dis_onls.remove(t_dit) onls.append(t_dit) else: t_dit['dt'] = p_dic['tLogout'] except: log.error("PlayerData.get_values is None pid:%s in ally.py func->members", assist_data.pid) if onls or dis_onls: onls = sor_member(is_other, True, onls) dis_onls = sor_member(is_other, False, dis_onls) mems = onls mems.extend(dis_onls) return mems
def _reward(self): """ 世界boss战发放奖励并 """ rank_data = sorted(self.pid_hurts.iteritems(), key=lambda x:x[1], reverse=True) #活动奖励广播 self.boss_mgr.safe_pub(MSG_REWARD_WORLDBOSS, rank_data, self.max_hp) pid_levels = PlayerData.get_players_levels(self.pid_hurts.keys()) res_rw_mail = Game.res_mgr.reward_mails.get(RW_MAIL_BOSS) p_ranks = {} for index, (pid, hurts) in enumerate(rank_data): rank = index + 1 key = (WORLD_BOSS, rank) rs_items = self._get_reward(key, hurts, pid_levels[pid]) #邮件通知玩家 content = res_rw_mail.content % dict(rank=rank) Game.mail_mgr.send_mails(pid, MAIL_REWARD, res_rw_mail.title, RW_MAIL_BOSS, rs_items, param=content) self.boss_mgr.safe_pub(MSG_REWARD_WORLDITEMS, pid, rs_items) p_ranks[str(pid)] = (rank, hurts) self.boss_mgr.safe_pub(MSG_WBOSS_RANK, p_ranks)
def get_bot(self, bid): assert is_bot(bid), ValueError('bid(%s) is not bot' % bid) try: return self.bots[bid] except KeyError: #从数据库中随机选择某等级区间的玩家作为基础数据 key = 'arena_bot_pids' pids = Game.gcaches.get(key) if not pids: pids = PlayerData.get_level_pids(arena_level, arena_level + 10) Game.gcaches.set(key, pids, timeout=60 * 5) pid = random.choice(pids) bot = Player.load_player(pid) data = bot.data data.id = bid data.name = Game.res_mgr.get_random_name() bot._bot_rival = (data.name, data.rid, data.level, bot.play_attr.CBE) self.bots[bid] = bot return bot
def init(self): """ 初始化竞技场数据:根据30级以上的玩家战斗力排名,得到竞技场排名 """ log.warn(u"初始化竞技场") self.clear() #删除记录 Game.rpc_store.deletes(TN_ARENA_RANKS) Game.rpc_store.deletes(TN_P_ARENA) #Game.rpc_store.insert(TN_ARENA_RANK, {FN_ID:0, FN_NEXT:END}) #根据30级以上玩家战斗力排名 CBEs = [(i[FN_P_ATTR_PID], i.get(FN_P_ATTR_CBE, index)) for index, i in enumerate(PlayerAttr.get_CBE_ranks())] levels = PlayerData.get_players_levels(None) #插入机器人 for i in xrange(self.BOT_RANK): self.update_rank(END, END - (i + 1)) #真实玩家 for pid, CBE in CBEs: if pid not in levels or levels[pid] < arena_level: continue self.update_rank(END, pid)
def get_user_detail_by_playername(self, name, *argv, **kw): """ 返回玩家详细信息 """ return PlayerData.userinfo_from_name(name, *argv, **kw)
def get_player_detail(self, pids, cols): """ 返回玩家详细信息 """ return PlayerData.get_players_values(pids, cols)
def gm_change_uid(self, pid, uid): PlayerData.update_values(pid, dict(uid=uid)) self.log(u'更发UID成功')
def get_tokens(self, pids): from game.player.player import UserData, PlayerData uids = PlayerData.get_uids(pids) return UserData.user_tokens(uids)
def gm_player_func(self, pid, index): funcs = 'funcs' tmp_func = PlayerData.get_values(pid, None) tmp_func = tmp_func[funcs] | (1L << index) PlayerData.update_values(pid, dict(funcs=tmp_func)) self.log(u'解锁成功')
def gm_del_offline_player(self, pid): """ 删除角色 """ uid = 'uid' tmp_player = PlayerData.get_values(pid, None) PlayerData.update_values(pid, dict(uid=-tmp_player[uid])) self.log(u'修改角色(%d)成功', pid)
def gm_del(self, name): """ 删除指定名称的玩家数据 """ pid = PlayerData.name_to_id(name) self.gm_del_pid(pid)
def gm_get_pid_by_name(self, name): """ 通过角色名获取pid """ pid = PlayerData.name_to_id(name) self.log(u'玩家PID:%s', pid[0])