You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
4.3 KiB

1 month ago
/*
Sog
2016 by zouwei
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sog;
namespace Chat
{
public class PlayerTableOp : BaseReloadableService
{
//数据表,需要外部传入,保证reload不出问题
private PlayerTable m_table;
public override int GetServiceType()
{
return ChatServiceType.PlayerTableOp;
}
//销毁的时候置空
public override void Dispose()
{
m_table = null;
}
//构造的时候外部传入table对象
public PlayerTableOp(PlayerTable table)
{
m_table = table;
}
public PlayerOnChat GetPlayerOnChat(long sessionID)
{
PlayerOnChat player;
m_table.m_sessionsTable.TryGetValue(sessionID, out player);
return player;
}
public PlayerOnChat GetPlayerOnChatByUID(long useUID)
{
if(m_table.m_uidTable.ContainsKey(useUID))
{
return m_table.m_uidTable[useUID];
}
return null;
//PlayerOnChat player = m_table.m_sessionsTable.FirstOrDefault(p => p.Value.UserID == useUID).Value;
//if (player == null || player.UserID != useUID)
//{
// return null;
//}
//return player;
}
public int GetPlayerOnChatCount()
{
return m_table.m_sessionsTable.Count;
}
public List<long> GetAllPlayer()
{
return m_table.m_sessionsTable.Keys.ToList();
}
public List<long> GetAllSameLogicWorldPlayer(int logicWorld)
{
List<long> sessionIDList = new List<long>();
foreach (var keyValue in m_table.m_sessionsTable)
{
if (keyValue.Value.LogicWorldID == logicWorld)
{
sessionIDList.Add(keyValue.Key);
}
}
return sessionIDList;
}
public List<long> GetAllSameRealmIDPlayer(PlayerOnChat player)
{
int realmID = player.RealmID;
if (realmID <= 0)
{
return null;
}
List<long> sessionIDList = new List<long>();
foreach (var keyValue in m_table.m_sessionsTable)
{
if (keyValue.Value.RealmID == realmID)
{
sessionIDList.Add(keyValue.Key);
}
}
return sessionIDList;
}
private List<int> GetRealmInBigRealm(int realmId)
{
var allBigRealm = ChatServerUtils.GetChatServerData().m_bigRealmDict;
foreach(var bigRealm in allBigRealm.Values)
{
if(bigRealm.Contains(realmId))
{
return bigRealm;
}
}
return null;
}
public List<long> GetAllSameBigRealmIDPlayer(PlayerOnChat player)
{
var BigRealmList = GetRealmInBigRealm(player.RealmID);
if(BigRealmList == null)
{
return null;
}
List<long> sessionIDList = new List<long>();
foreach (var keyValue in m_table.m_sessionsTable)
{
if (BigRealmList.Contains(keyValue.Value.RealmID))
{
sessionIDList.Add(keyValue.Key);
}
}
return sessionIDList;
}
//不允许重复添加
public void AddPlayerSession(long sessionID, PlayerOnChat player)
{
if (m_table.m_sessionsTable.ContainsKey(sessionID))
{
TraceLog.Error("player session {0} already exist, add failed,please remove it first!", sessionID);
return;
}
m_table.m_sessionsTable.Add(sessionID, player);
}
public void RemoveSession(long sessionID)
{
TraceLog.Trace("delete player session {0} ", sessionID);
if (m_table.m_sessionsTable.ContainsKey(sessionID))
{
RemoveUid(m_table.m_sessionsTable[sessionID].UserID);
m_table.m_sessionsTable.Remove(sessionID);
}
}
public void AddPlayerUid(PlayerOnChat player)
{
if(player == null || player.UserID == 0)
{
TraceLog.Error("player uid == 0 or player is null!");
return;
}
long uid = player.UserID;
if (m_table.m_uidTable.ContainsKey(uid))
{
TraceLog.Trace("player uid {0} already exist, add failed,please remove it first!", uid);
return;
}
m_table.m_uidTable.Add(uid, player);
}
public void RemoveUid(long uid)
{
TraceLog.Trace("delete player uid {0} ", uid);
if (m_table.m_uidTable.ContainsKey(uid))
{
m_table.m_uidTable.Remove(uid);
}
}
}
}