/* Sog 游戏基础库 2016 by zouwei */ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; namespace Game { public class PlayerTableOp : BaseReloadableService { //数据表,需要外部传入,保证reload不出问题 public PlayerTable Table { get; private set; } public override int GetServiceType() { return GameServiceType.PlayerTableOp; } //销毁的时候置空 public override void Dispose() { Table = null; } //构造的时候外部传入table对象 public PlayerTableOp(PlayerTable table) { Table = table; } //public PlayerTable Table { public get { return m_table; }; private set; } public PlayerSession GetPlayerSession(long sessionID) { PlayerSession session; Table.m_sessionsTable.TryGetValue(sessionID, out session); return session; } public PlayerSession GetPlayerSessionByUid(long uid) { PlayerOnGame player = null; PlayerSession playerSession = null; Table.m_uidTable.TryGetValue(uid, out player); if (player != null) { Table.m_sessionsTable.TryGetValue(player.SessionID, out playerSession); } return playerSession; } //不允许重复添加 public void AddPlayerSession(long sessionID, PlayerSession playerSession) { if (Table.m_sessionsTable.ContainsKey(sessionID)) { TraceLog.Trace("PlayerTableOp player session {0} already exist, add failed,please remove it first!", sessionID); return; } Table.m_sessionsTable.Add(sessionID, playerSession); } public void RemoveSession(long sessionID) { TraceLog.Trace("PlayerTableOp delete player session {0} ", sessionID); if (Table.m_sessionsTable.ContainsKey(sessionID)) { Table.m_sessionsTable.Remove(sessionID); } } public int GetOnlinePlayer() { return Table.m_sessionsTable.Count; } public PlayerOnGame GetPlayerByUid(long uid) { PlayerOnGame player; Table.m_uidTable.TryGetValue(uid, out player); return player; } public void AddPlayer(PlayerOnGame player) { if (Table.m_uidTable.ContainsKey(player.UserID)) { player.Error("PlayerTableOp.AddPlayer player uid {0} already exist, add failed,please remove it first!" , player.UserID); return; } Table.m_uidTable.Add(player.UserID, player); } public void RemovePlayer(PlayerOnGame player) { //防止逻辑写错,这里需要处理一下 if (player.SessionID != 0) { player.Error("PlayerTableOp.RemovePlayer player uid {0} exist session id {1},can not remove!" , player.UserID, player.SessionID); return; } if (!Table.m_uidTable.ContainsKey(player.UserID)) { player.Error("PlayerTableOp.RemovePlayer player uid {0} not in table, remove failed,please remove it first!" , player.UserID); return; } Table.m_uidTable.Remove(player.UserID); } public Dictionary> GetAllPlayer() { Dictionary> allPlayers = new Dictionary>(); foreach (var keyValue in Table.m_sessionsTable) { List gataList; if (allPlayers.ContainsKey(keyValue.Value.GateServerID)) { gataList = allPlayers[keyValue.Value.GateServerID]; } else { gataList = new List(); allPlayers.Add(keyValue.Value.GateServerID, gataList); } gataList.Add(keyValue.Key); } return allPlayers; } public Dictionary > GetSameRealmIDPlayer(int realmId) { Dictionary> allPlayers = new Dictionary>(); foreach (var keyValue in Table.m_sessionsTable) { if(keyValue.Value.RealmID != realmId) { continue; } List gataList; if (allPlayers.ContainsKey(keyValue.Value.GateServerID)) { gataList = allPlayers[keyValue.Value.GateServerID]; } else { gataList = new List(); allPlayers.Add(keyValue.Value.GateServerID, gataList); } gataList.Add(keyValue.Key); } return allPlayers; } } }