using Sog; using System.Collections.Generic; using System.Linq; using ProtoCSStruct; namespace World { //先放在World,之后看看需求是否移动到Friend public class RoleCacheInfoSvc : IServerDataObj { //Key:playerUid , value:m_RoleCacheStructList pos index public Dictionary m_selfIDMap; private static StructMemoryCache m_RoleCacheStructList; public RoleCacheInfoSvc() { m_selfIDMap = new Dictionary(); m_RoleCacheStructList = new StructMemoryCache((int)RoleCacheDef.MaxRoleCacheCount); } public bool RemoveOneRoleCacheByUid(long playerUid) { if (m_selfIDMap.TryGetValue(playerUid, out long pos)) { m_selfIDMap.Remove(playerUid); m_RoleCacheStructList.Free(pos); return true; } return false; } /// /// 增加一个空的,具体数据设置自己接返回值然后处理 /// /// /// public ref OneRoleCacheStruct AddOneRoleCacheByUid(long playerUid) { if (m_selfIDMap.ContainsKey(playerUid)) { return ref GetOneRoleCacheStructByUid(playerUid); } //满了的话,默认移除掉m_selfIDMap的第一个 if (m_RoleCacheStructList.m_freeQueue.Count == 0) { RemoveOneRoleCacheByUid(m_selfIDMap.FirstOrDefault().Key); } //移除失败的话,再尝试移除最后一个 if (m_RoleCacheStructList.m_freeQueue.Count == 0) { RemoveOneRoleCacheByUid(m_selfIDMap.LastOrDefault().Key); } //还是不行直接返回吧 if (m_RoleCacheStructList.m_freeQueue.Count == 0) { return ref m_RoleCacheStructList.GetByIndex(0); ; } m_selfIDMap.Add(playerUid, m_RoleCacheStructList.m_freeQueue.Peek()); ref var one = ref m_RoleCacheStructList.Malloc(); one.LastAccessTime = 0; one.showInfo.Clear(); one.sysData.Clear(); return ref one; } public ref OneRoleCacheStruct GetOneRoleCacheStructByUid(long playerUid) { if (m_selfIDMap.TryGetValue(playerUid, out long pos)) { return ref m_RoleCacheStructList.GetByIndex(pos); } return ref m_RoleCacheStructList.GetByIndex(0); } public bool RoleCacheIsFull() { //还剩11个空间的时候就认为满了,可以删除一些 if (m_selfIDMap.Count + 11 >= (int) RoleCacheDef.MaxRoleCacheCount) { return true; } return false; } public override int GetDataType() { return WorldDataObjType.RoleCacheInfo; } } }