using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using ProtoCSStruct; namespace Friend { class FriendOp { #region 通过玩家保存的 对应的数组索引 获取数据 /// /// 通过数组下标index获取selfuid用户里面的指定FriendStranger /// /// /// /// ref DBFriendStrangerOneStruct public static unsafe ref DBFriendStrangerOneStruct GetFriendStrangerByIndex(ref FriendCacheInfoStruct infoStruct, int index) { if (index >= infoStruct.FriendOpData.StrangerCount || index < 0) { return ref FriendInfoCache.m_cacheStructFriendStranger.GetByIndex(0); } long posIndex = infoStruct.FriendOpData.StrangerList[index]; return ref FriendInfoCache.m_cacheStructFriendStranger.GetByIndex(posIndex); } /// /// 通关玩家UID 和 数组下标 获取指定其中一个好友 /// /// /// /// public static unsafe ref DBFriendOneStruct GetFriendOneByIndex(ref FriendCacheInfoStruct infoStruct, int index) { if (index >= infoStruct.FriendList.iCount || index < 0) { return ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(0); } long posIndex = infoStruct.FriendList.DBFriendOneID[index]; return ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(posIndex); } /// /// 通过数组下标index获取selfuid用户里面的指定chat /// /// /// /// ref DBFriendChatOneStruct public static unsafe ref DBFriendChatOneStruct GetFriendChatByIndex(ref FriendCacheInfoStruct infoStruct, int index) { if (index >= infoStruct.FriendOpData.ChatCount || index < 0) { return ref FriendInfoCache.m_cacheStructFriendChat.GetByIndex(0); } long posIndex = infoStruct.FriendOpData.ChatList[index]; return ref FriendInfoCache.m_cacheStructFriendChat.GetByIndex(posIndex); } /// /// 通过数组下标index获取selfuid用户里面的指定Op /// /// /// /// ref DBFriendOpDataStruct public static unsafe ref DBFriendOneOpStruct GetFriendOpByIndex(ref FriendCacheInfoStruct infoStruct, int index) { if (index >= infoStruct.FriendOpData.OpCount || index < 0) { return ref FriendInfoCache.m_cacheStructFriendOneOp.GetByIndex(0); } long posIndex = infoStruct.FriendOpData.OpList[index]; return ref FriendInfoCache.m_cacheStructFriendOneOp.GetByIndex(posIndex); } #endregion #region 通过玩家UID获取 指定数据 /// /// 通过玩家UID获取Friendinfo /// /// /// public static unsafe ref FriendCacheInfoStruct GetFriendInfoByUid(long selfUid) { FriendInfoCache.m_selfIDMap.TryGetValue(selfUid, out long posIndex); return ref FriendInfoCache.m_cacheStructFriendInfo.GetByIndex(posIndex); } /// /// 获取我的好友中,指定UID的好友 /// /// /// /// public static unsafe ref DBFriendOneStruct GetFriendOneByUid(long selfUid, long Uid) { ref FriendCacheInfoStruct selfInfoStruct = ref FriendOp.GetFriendInfoByUid(selfUid); if (selfInfoStruct.IsNull()) { return ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(0); } for (int i = 0; i < selfInfoStruct.FriendList.iCount; i++) { ref var oneStruct = ref GetFriendOneByIndex(ref selfInfoStruct, i); if (oneStruct.oneFriend.Uid == Uid) { return ref oneStruct; } } return ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(0); } public static unsafe ref DBFriendStrangerOneStruct GetStrangerByUid(long selfUid, long StrangerUid) { ref FriendCacheInfoStruct info =ref GetFriendInfoByUid(selfUid); if (info.IsNull()) { return ref FriendInfoCache.m_cacheStructFriendStranger.GetByIndex(0); } for (int i = 0; i < info.FriendOpData.StrangerCount; i++) { ref DBFriendStrangerOneStruct one =ref GetFriendStrangerByIndex(ref info, i); if (one.IsNull()) { continue; } ref var friendone = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(one.DBFriendOneID); if (friendone.oneFriend.Uid == StrangerUid) { return ref one; } } return ref FriendInfoCache.m_cacheStructFriendStranger.GetByIndex(0); } /// /// 找到其中一个操作,比如其他玩家邀请加我为好友 /// /// /// /// public static unsafe ref DBFriendOneOpStruct GetFriendOpByUid(long selfUid, long opUid, int opType) { ref FriendCacheInfoStruct info = ref GetFriendInfoByUid(selfUid); for (int i = 0; i < info.FriendOpData.OpCount; i++) { ref DBFriendOneOpStruct one =ref GetFriendOpByIndex(ref info, i); if (one.IsNull()) { continue; } ref var friendone = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(one.DBFriendOneID); if (friendone.oneFriend.Uid == opUid && one.OpType == opType) { return ref one; } } return ref FriendInfoCache.m_cacheStructFriendOneOp.GetByIndex(0); } #endregion #region 删除玩家的好友相关信息 /// /// 删除一个好友通过好友UID /// /// /// public static unsafe void DoDeleteFriendList(ref FriendCacheInfoStruct infoStruct, long Uid) { if (infoStruct.IsNull()) { return; } int arrayIndex = -1; for (int i = 0; i < infoStruct.FriendList.iCount; i++) { ref var oneStruct = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(infoStruct.FriendList.DBFriendOneID[i]); if (oneStruct.oneFriend.Uid == Uid) { arrayIndex = i; DoFreeFriendOneByIndex(infoStruct.FriendList.DBFriendOneID[i]); break; } } if (arrayIndex >= 0) { //1:首先把最后一个的关联缓存索引赋值给删除的这个(好友前端排序就好了,所以这里的顺序不重要) infoStruct.FriendList.DBFriendOneID[arrayIndex] = infoStruct.FriendList.DBFriendOneID[infoStruct.FriendList.iCount - 1]; //2:然后将最后一个置0 infoStruct.FriendList.DBFriendOneID[infoStruct.FriendList.iCount - 1] = 0; //3:好友数量 -- infoStruct.FriendList.iCount--; infoStruct.SetNeedSave(true); } } /// /// 通过索引删除一个好友操作 /// /// /// public static unsafe void DoDeleteFriendOP(ref FriendCacheInfoStruct infoStruct, long posIndex) { ref DBFriendOneOpStruct opStruct = ref FriendInfoCache.m_cacheStructFriendOneOp.GetByIndex(posIndex); if (opStruct.IsNull()) { return; } int arrayIndex = -1; for (int i = 0; i < infoStruct.FriendOpData.OpCount; i++) { if (infoStruct.FriendOpData.OpList[i] == posIndex) { arrayIndex = i; DoFreeFriendOPByIndex(posIndex); break; } } if (arrayIndex >= 0) { //1:首先把最后一个的关联缓存索引赋值给删除的这个(好友前端排序就好了,所以这里的顺序不重要) infoStruct.FriendOpData.OpList[arrayIndex] = infoStruct.FriendOpData.OpList[infoStruct.FriendOpData.OpCount - 1]; //2:然后将最后一个置0 infoStruct.FriendOpData.OpList[infoStruct.FriendOpData.OpCount - 1] = 0; //3:数量 -- infoStruct.FriendOpData.OpCount--; infoStruct.SetNeedSave(true); } } public static unsafe void DoDeleteFriendOp(ref FriendCacheInfoStruct selfInfo, long opUid, int opType) { int arrayIndex = -1; for (int i = 0; i < selfInfo.FriendOpData.OpCount; i++) { ref DBFriendOneOpStruct friendOpStruct = ref FriendInfoCache.m_cacheStructFriendOneOp.GetByIndex(selfInfo.FriendOpData.OpList[i]); ref var oneFriend = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(friendOpStruct.DBFriendOneID); if (!oneFriend.IsNull() && oneFriend.oneFriend.Uid == opUid && friendOpStruct.OpType == opType) { arrayIndex = i; DoFreeFriendOPByIndex(friendOpStruct.GetObjectID()); break; } } if (arrayIndex >= 0) { selfInfo.FriendOpData.OpList[arrayIndex] = selfInfo.FriendOpData.OpList[selfInfo.FriendOpData.OpCount - 1]; selfInfo.FriendOpData.OpList[selfInfo.FriendOpData.OpCount - 1] = 0; selfInfo.FriendOpData.OpCount--; selfInfo.SetNeedSave(true); } } public static unsafe void DoDeleteFriendOpByType(ref FriendCacheInfoStruct selfInfo, int opType) { for (int i = selfInfo.FriendOpData.OpCount - 1; i >= 0; i--) { long posIndex = selfInfo.FriendOpData.OpList[i]; ref DBFriendOneOpStruct oneOp = ref FriendInfoCache.m_cacheStructFriendOneOp.GetByIndex(posIndex); if (oneOp.IsNull()) { continue; } if (oneOp.OpType == opType) { DoFreeFriendOPByIndex(posIndex); selfInfo.FriendOpData.OpList[i] = selfInfo.FriendOpData.OpList[selfInfo.FriendOpData.OpCount - 1]; selfInfo.FriendOpData.OpList[selfInfo.FriendOpData.OpCount - 1] = 0; selfInfo.FriendOpData.OpCount--; selfInfo.SetNeedSave(true); } } } /// /// 通过数组下标删除(说明:聊天这个顺序好像有作用,聊天消息满了从第一个开始删除的,这个之后改下看看) /// /// /// public static unsafe void DoDeleteFriendChat(ref FriendCacheInfoStruct infoStruct, int arrayIndex) { if (arrayIndex < 0 || arrayIndex >= infoStruct.FriendOpData.ChatCount) { return; } DoFreeChatListByIndex(infoStruct.FriendOpData.ChatList[arrayIndex]); //聊天的保留顺序 for (int i = arrayIndex + 1; i < infoStruct.FriendOpData.ChatCount; i++) { infoStruct.FriendOpData.ChatList[i - 1] = infoStruct.FriendOpData.ChatList[i]; } infoStruct.FriendOpData.ChatList[infoStruct.FriendOpData.ChatCount - 1] = 0; infoStruct.FriendOpData.ChatCount--; infoStruct.SetNeedSave(true); } public static unsafe void DoDeleteAllChat(ref FriendCacheInfoStruct infoStruct) { TraceLog.Trace("FriendOp.DoDeleteAllChat uid {0} chat count {1}" , infoStruct.Self.Uid, infoStruct.FriendOpData.ChatCount); for (int i = 0; i < infoStruct.FriendOpData.ChatCount; i++) { DoFreeChatListByIndex(infoStruct.FriendOpData.ChatList[i]); } infoStruct.FriendOpData.ChatCount = 0; infoStruct.SetNeedSave(true); } public static unsafe void DoDeleteStranger(ref FriendCacheInfoStruct selfInfo, long targetUid) { int arrayIndex = -1; for (int i = 0; i < selfInfo.FriendOpData.StrangerCount; i++) { ref DBFriendStrangerOneStruct strangerOneStruct = ref FriendInfoCache.m_cacheStructFriendStranger.GetByIndex(selfInfo.FriendOpData.StrangerList[i]); ref var oneFriend = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(strangerOneStruct.DBFriendOneID); if (! oneFriend.IsNull() && oneFriend.oneFriend.Uid == targetUid) { arrayIndex = i; DoFreeStrangerByIndex(strangerOneStruct.GetObjectID()); break; } } if (arrayIndex >= 0) { selfInfo.FriendOpData.StrangerList[arrayIndex] = selfInfo.FriendOpData.StrangerList[selfInfo.FriendOpData.StrangerCount - 1]; selfInfo.FriendOpData.StrangerList[selfInfo.FriendOpData.StrangerCount - 1] = 0; selfInfo.FriendOpData.StrangerCount--; selfInfo.SetNeedSave(true); } } public static bool GMDeleteFriendCache = false; /// /// 以后实现逻辑,可以暂时不管(逻辑上问题,这个之后估计要废弃,缓存数据将跟随PlayerInfoFriend创建而创建,PlayerInfoFriend销毁而销毁) /// /// /// public static bool CanDeleteFriendCacheInfo(long uid) { ref FriendCacheInfoStruct info = ref GetFriendInfoByUid(uid); //数据需要保存 if (info.GetNeedSave()) { return false; } // 停服时存盘后直接删除, 不用等超时 if (FriendServerUtils.GetApp().IsStopping) { return true; } //在线玩家不删除 PlayerInfoFriend player = FriendServerUtils.GetPlayerTableOp().GetPlayerInfo(uid); if (player != null && player.IsOnline) { return false; } // GM测试 //if (GMDeleteFriendCache) //{ // return true; //} //配置的超时时间 if (FriendServerUtils.GetTimeSecond() - info.LastAccessTime < FriendConfig.GetFriendCacheTimeout()) { return false; } return true; } /// /// 清除一个玩家的数据,注意现在好像没有删除PlayerInfoFriend player的逻辑 /// /// public static unsafe void DeleteFriendCacheInfo(long uid) { ref FriendCacheInfoStruct info = ref GetFriendInfoByUid(uid); if (! info.IsNull()) { DeleteFriendCacheInfo(ref info); } } private static bool logEx = false; public static unsafe void DeleteFriendCacheInfo(ref FriendCacheInfoStruct info) { // 外网好像在这里core了, 没有log, catch看看是不是core这里了 try { for (int i = 0; i < info.FriendList.iCount; i++) { FriendInfoCache.m_cacheStructFriendOne.Free(info.FriendList.DBFriendOneID[i]); } info.FriendList.iCount = 0; for (int i = 0; i < info.FriendOpData.OpCount; i++) { DoFreeFriendOPByIndex(info.FriendOpData.OpList[i]); } info.FriendOpData.OpCount = 0; for (int i = 0; i < info.FriendOpData.ChatCount; i++) { FriendInfoCache.m_cacheStructFriendChat.Free(info.FriendOpData.ChatList[i]); } info.FriendOpData.ChatCount = 0; for (int i = 0; i < info.FriendOpData.StrangerCount; i++) { DoFreeStrangerByIndex(info.FriendOpData.StrangerList[i]); } info.FriendOpData.StrangerCount = 0; FriendInfoCache.m_cacheStructFriendInfo.Free(info.GetObjectID()); FriendInfoCache.m_selfIDMap.Remove(info.Self.Uid); } catch (Exception e) { if (! logEx) { TraceLog.Exception(e); logEx = true; } } TraceLog.Trace("FriendCacheOp.DeleteFriendCacheInfo FriendInfoCache used {0} free {1}" , FriendInfoCache.m_cacheStructFriendInfo.GetUsedCount() , FriendInfoCache.m_cacheStructFriendInfo.GetFreeCount()); TraceLog.Trace("FriendCacheOp.DeleteFriendCacheInfo FriendOp used {0} free {1}" , FriendInfoCache.m_cacheStructFriendOneOp.GetUsedCount() , FriendInfoCache.m_cacheStructFriendOneOp.GetFreeCount()); TraceLog.Trace("FriendCacheOp.DeleteFriendCacheInfo FriendChat used {0} free {1}" , FriendInfoCache.m_cacheStructFriendChat.GetUsedCount() , FriendInfoCache.m_cacheStructFriendChat.GetFreeCount()); TraceLog.Trace("FriendCacheOp.DeleteFriendCacheInfo FriendOne used {0} free {1}" , FriendInfoCache.m_cacheStructFriendOne.GetUsedCount() , FriendInfoCache.m_cacheStructFriendOne.GetFreeCount()); //TraceLog.Trace("FriendCacheOp.DeleteFriendCacheInfo FriendStranger used {0} free {1}" // , FriendInfoCache.m_cacheStructFriendStranger.GetUsedCount() // , FriendInfoCache.m_cacheStructFriendStranger.GetFreeCount()); } #endregion #region 一些辅助操作函数 public static unsafe ref DBFriendOneOpStruct GetOpStructByTypeAndTargeid(long selfUid, long Uid, int type) { ref FriendCacheInfoStruct info = ref GetFriendInfoByUid(selfUid); for (int i = 0; i < info.FriendOpData.OpCount; i++) { ref DBFriendOneOpStruct op = ref GetFriendOpByIndex(ref info, i); if (op.IsNull()) { continue; } ref DBFriendOneStruct oneFriend = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(op.DBFriendOneID); if (oneFriend.IsNull()) { continue; } if (op.OpType == type && oneFriend.oneFriend.Uid == Uid) { return ref op; } } return ref FriendInfoCache.m_cacheStructFriendOneOp.GetByIndex(0); } public static unsafe long GetFriendOpIndex(ref FriendCacheInfoStruct infoStruct, long Uid, int opType) { ref var aa = ref GetOpStructByTypeAndTargeid(infoStruct.Self.Uid, Uid, opType); return aa.GetObjectID(); } public static unsafe void UpdateSelfInfoTOFriendOne(ref DBFriendSelf self, ref DBFriendOneStruct one) { FriendUtils.CopySelfToFriendOne(ref self, ref one.oneFriend); } /// /// -1: error /// 0: 不是好友,也不在stranger里面 /// 1:是好友 /// 2:stranger里面已经有了 /// public static unsafe int ISAlreadyBeenFriend(long self, long stranger) { ref FriendCacheInfoStruct selfinfo = ref GetFriendInfoByUid(self); if (selfinfo.IsNull()) { return -1; } return IsAlreadyBeenFriend(ref selfinfo, stranger); } /// /// 0: 不是好友,也不在stranger里面 /// 1:是好友 /// 2:stranger里面已经有了 /// public static unsafe int IsAlreadyBeenFriend(ref FriendCacheInfoStruct selfinfo, long stranger) { //是不是已经是好友了 for (int i = 0; i < selfinfo.FriendList.iCount; i++) { ref var oneStruct = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(selfinfo.FriendList.DBFriendOneID[i]); if (! oneStruct.IsNull() && oneStruct.oneFriend.Uid == stranger) { return 1; } } //陌生人列表里面有没有 for (int i = 0; i < selfinfo.FriendOpData.StrangerCount; i++) { //获取陌生人 ref DBFriendStrangerOneStruct strangerOneStruct = ref FriendInfoCache.m_cacheStructFriendStranger.GetByIndex(selfinfo.FriendOpData.StrangerList[i]); //获取陌生人指向的DBFriendOneStruct ref var oneFriend = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(strangerOneStruct.DBFriendOneID); if (! oneFriend.IsNull() && oneFriend.oneFriend.Uid == stranger) { return 2; } } return 0; } /// /// 是不是已经存在该好友的op操作 /// public static unsafe bool AlreadyExistOp(ref FriendCacheInfoStruct info, long opUid, DBFriendOpType opType) { for (int i = 0; i < info.FriendOpData.OpCount; i++) { ref DBFriendOneOpStruct opStruct = ref FriendInfoCache.m_cacheStructFriendOneOp.GetByIndex(info.FriendOpData.OpList[i]); ref var oneFriend = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(opStruct.DBFriendOneID); if (oneFriend.IsNull()) { continue; } if ((oneFriend.oneFriend.Uid == opUid) && (opStruct.OpType == (int)opType)) { return true; } } return false; } #endregion #region 好友进行操作时变化缓存结构数据 /// /// 添加好友到我的Friendlist /// public static unsafe int DoAddFriendOneToMySelf(ref FriendCacheInfoStruct selfInfo, long friendUid, bool isSelfSubmit) { for (int i = 0; i < selfInfo.FriendList.iCount; i++) { ref var oneStruct = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(selfInfo.FriendList.DBFriendOneID[i]); // 已经是好友了 if (! oneStruct.IsNull() && oneStruct.oneFriend.Uid == friendUid) { return 0; } } // 好友列表已满 if (FriendSvc.IsFriendListFull(ref selfInfo)) { return isSelfSubmit ? (int) CSErrCode.SelfFriendListIsFull : (int) CSErrCode.TargetFriendListIsFull; } long posIndex = AddFriendOneToCache(friendUid); // 新增FriendOne失败, 预先分配的空间不够了 if (posIndex <= 0) { return -1; } // 赠送礼物后删除好友, 重新添加后设置赠送时间 if (selfInfo.Self.TodaySendGiftUid.Contains(friendUid)) { ref DBFriendOneStruct friendOne = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(posIndex); friendOne.oneFriend.LastSendGiftTime = (int)FriendServerUtils.GetTimeSecond(); } // 加入好友列表 selfInfo.FriendList.DBFriendOneID[selfInfo.FriendList.iCount] = posIndex; selfInfo.FriendList.iCount++; // 检查陌生人列表, 如果有则删除 DoDeleteStranger(ref selfInfo, friendUid); // 检查Op列表, 如果有邀请则删除 DoDeleteFriendOp(ref selfInfo, friendUid, (int)DBFriendOpType.Invite); selfInfo.SetNeedSave(true); return 0; } public static unsafe void AddOrUpdateRemoteFriendOne(ref DBFriendOne friend, ref RepeatedDBFriendHeroOne_8 heroList) { if (FriendInfoCache.m_remoteFriendIDMap.TryGetValue(friend.Uid, out long index)) { ref RemoteFriendOneStruct remoteFriend = ref FriendInfoCache.m_remoteFriendOneCache.GetByIndex(index); if (remoteFriend.IsNull()) { FriendInfoCache.m_remoteFriendOneCache.Free(index); FriendInfoCache.m_remoteFriendIDMap.Remove(friend.Uid); } else { // 更新数据 FriendUtils.CopyDBFriendToRemoteFriend(ref friend, ref heroList, ref remoteFriend); return; } } if (FriendInfoCache.m_remoteFriendOneCache.m_freeQueue.Count == 0) { TraceLog.Error("FriendOp.AddOrUpdateRemoteFriendOne FriendInfoCache.m_remoteFriendOneCache not enough space"); return; } FriendInfoCache.m_remoteFriendIDMap.Add(friend.Uid, FriendInfoCache.m_remoteFriendOneCache.m_freeQueue.Peek()); ref RemoteFriendOneStruct friendData = ref FriendInfoCache.m_remoteFriendOneCache.Malloc(); FriendUtils.CopyDBFriendToRemoteFriend(ref friend, ref heroList, ref friendData); TraceLog.Debug("FriendOp.AddOrUpdateRemoteFriendOne add uid {0} succ", friend.Uid); } public static unsafe void AddOrUpdateRemoteFriendOne(ref DBFriendSelf self) { if (FriendInfoCache.m_remoteFriendIDMap.TryGetValue(self.Uid, out long index)) { ref RemoteFriendOneStruct remoteFriend = ref FriendInfoCache.m_remoteFriendOneCache.GetByIndex(index); if (remoteFriend.IsNull()) { FriendInfoCache.m_remoteFriendOneCache.Free(index); FriendInfoCache.m_remoteFriendIDMap.Remove(self.Uid); } else { FriendUtils.CopySelfToRemoteFriend(ref self, ref remoteFriend); return; } } if (FriendInfoCache.m_remoteFriendOneCache.m_freeQueue.Count == 0) { TraceLog.Error("FriendOp.AddOrUpdateRemoteFriendOne FriendInfoCache.m_remoteFriendOneCache not enough space"); return; } FriendInfoCache.m_remoteFriendIDMap.Add(self.Uid, FriendInfoCache.m_remoteFriendOneCache.m_freeQueue.Peek()); ref RemoteFriendOneStruct friendData = ref FriendInfoCache.m_remoteFriendOneCache.Malloc(); FriendUtils.CopySelfToRemoteFriend(ref self, ref friendData); TraceLog.Debug("FriendOp.AddOrUpdateRemoteFriendOne add uid {0} succ", self.Uid); } public static unsafe ref RemoteFriendOneStruct GetRemoteFriendOneByUid(long uid) { FriendInfoCache.m_remoteFriendIDMap.TryGetValue(uid, out long posIndex); return ref FriendInfoCache.m_remoteFriendOneCache.GetByIndex(posIndex); } public static void DeleteRemoteFriendOne(long uid) { if (FriendInfoCache.m_remoteFriendIDMap.TryGetValue(uid, out long index)) { TraceLog.Debug("FriendOp.DeleteRemoteFriendOne uid {0}", uid); FriendInfoCache.m_remoteFriendOneCache.Free(index); FriendInfoCache.m_remoteFriendIDMap.Remove(uid); } } public static unsafe int DoAddBlackList(ref FriendCacheInfoStruct selfInfo, long targetUid) { ref var blacklist = ref selfInfo.Self.BlackList; if(blacklist.Count >= FriendConfig.GetMaxBlacklistCount()) { return (int)CSErrCode.MaxLimitSys; } if(blacklist.Contains(targetUid)) { return (int)CSErrCode.InSelfBlacklist; } blacklist.Add(targetUid); return 0; } public static unsafe int DoDelBlackList(ref FriendCacheInfoStruct selfInfo, long targetUid) { ref var blacklist = ref selfInfo.Self.BlackList; for(int i = 0; i < blacklist.Count; i++) { if(blacklist[i] == targetUid) { blacklist.RemoveAt(i); return 0; } } return (int)CSErrCode.NotBlacklist; } public static unsafe bool IsFriend(ref FriendCacheInfoStruct selfInfo, long friendUid) { for (int i = 0; i < selfInfo.FriendList.iCount; i++) { ref var oneStruct = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(selfInfo.FriendList.DBFriendOneID[i]); // 已经是好友了 if (!oneStruct.IsNull() && oneStruct.oneFriend.Uid == friendUid) { return true; } } return false; } /// /// 添加聊天信息到我的聊天记录中 /// /// /// public static unsafe long DoAddNewFriendChatToMySelf(long SelfUid, ref CSChatRes chatRes) { FriendInfoCache.m_selfIDMap.TryGetValue(SelfUid, out long index); ref FriendCacheInfoStruct selfStruct = ref FriendInfoCache.m_cacheStructFriendInfo.GetByIndex(index); if (selfStruct.IsNull()) { return 0; } //删除第一条聊天信息 while (selfStruct.FriendOpData.ChatCount >= FriendConfig.GetChatListCountMax()) { FriendInfoCache.m_cacheStructFriendChat.Free(selfStruct.FriendOpData.ChatList[0]); for (int i = 1; i < selfStruct.FriendOpData.ChatCount; i++) { selfStruct.FriendOpData.ChatList[i - 1] = selfStruct.FriendOpData.ChatList[i]; } selfStruct.FriendOpData.ChatCount--; } selfStruct.FriendOpData.LastChatSeq++; long posIndex = AddFriendChatOneToCache(ref chatRes, selfStruct.FriendOpData.LastChatSeq); if (posIndex > 0) { selfStruct.FriendOpData.ChatList[selfStruct.FriendOpData.ChatCount] = posIndex; selfStruct.FriendOpData.ChatCount++; } TraceLog.Trace("DoAddNewFriendChatToMySelf uid {0} curr chatSeq {1} count {2}", selfStruct.Self.Uid , selfStruct.FriendOpData.LastChatSeq, selfStruct.FriendOpData.ChatCount); selfStruct.SetNeedSave(true); return posIndex; } /// /// 好友操作,比如某人邀请我加入 /// /// /// /// public static unsafe ref DBFriendOneOpStruct DoAddNewFriendOpToMySelf(long selfUid, long opUid, int opType) { ref DBFriendOneOpStruct empty = ref FriendInfoCache.m_cacheStructFriendOneOp.GetByIndex(0); if (!FriendInfoCache.m_selfIDMap.TryGetValue(selfUid, out long index)) { return ref empty; } ref FriendCacheInfoStruct infoStruct = ref FriendInfoCache.m_cacheStructFriendInfo.GetByIndex(index); if (AlreadyExistOp(ref infoStruct, opUid, (DBFriendOpType)opType)) { return ref empty; } if (FriendSvc.IsFriendOpListFull(ref infoStruct)) { return ref empty; } //判断操作者信息 if (!FriendInfoCache.m_selfIDMap.TryGetValue(opUid, out long opIndex)) { return ref empty; } ref FriendCacheInfoStruct opInfoStruct = ref FriendInfoCache.m_cacheStructFriendInfo.GetByIndex(opIndex); if (opInfoStruct.IsNull()) { return ref empty; } //生成一个op DBFriendOneOp newOp = new DBFriendOneOp(); newOp.OpType = opType; newOp.OpSeq = ++infoStruct.FriendOpData.LastOpSeq; FriendUtils.CopySelfToFriendOne(ref opInfoStruct.Self, ref newOp.Friend); //注意这个里面的一环套了一环DBFriendOne long opPosIndex = AddFriendOneOpToCache(ref newOp); if (opPosIndex > 0) { infoStruct.FriendOpData.OpList[infoStruct.FriendOpData.OpCount] = opPosIndex; infoStruct.FriendOpData.OpCount++; infoStruct.SetNeedSave(true); return ref FriendInfoCache.m_cacheStructFriendOneOp.GetByIndex(opPosIndex); } return ref empty; } /// /// 将好友操作加入到我的FriendOp列表, 比如某人申请加我为好友 /// public static unsafe int DoAddNewFriendOpToMySelf(ref FriendCacheInfoStruct selfInfo, long opUid, int opType) { if (AlreadyExistOp(ref selfInfo, opUid, (DBFriendOpType)opType)) { return 0; } if (FriendSvc.IsFriendOpListFull(ref selfInfo)) { return (int)CSErrCode.FriendOpListIsFull; } //注意这个里面的一环套了一环DBFriendOne long opPosIndex = AddFriendOneOpToCache(opUid, opType, ++selfInfo.FriendOpData.LastOpSeq); if (opPosIndex > 0) { selfInfo.FriendOpData.OpList[selfInfo.FriendOpData.OpCount] = opPosIndex; selfInfo.FriendOpData.OpCount++; selfInfo.SetNeedSave(true); return 0; } return -1; } /// /// 通过缓存中的info解析出self, friendList, opData /// /// /// /// /// public static unsafe bool TransFriendCacheInfoStruct(ref FriendCacheInfoStruct friendInfo, ref DBFriendSelf self, ref DBFriendList friendList, ref DBFriendOpData opData) { if (friendInfo.IsNull()) { TraceLog.Error("FriendOp.TransFriendCacheInfoStruct friendCacheInfo is null"); return false; } //拷贝DBFriendSelf 信息 self.CopyFrom(ref friendInfo.Self); //拷贝DBFriendList 信息 for (int i = 0; i < friendInfo.FriendList.iCount; i++) { ref DBFriendOneStruct one = ref GetFriendOneByIndex(ref friendInfo, i); if (! one.IsNull()) { friendList.AllFriends.Add(ref one.oneFriend); } } //拷贝DBFriendOpData 操作信息 opData.LastOpSeq = friendInfo.FriendOpData.LastOpSeq; for (int i = 0; i < friendInfo.FriendOpData.OpCount; i++) { ref DBFriendOneOpStruct one = ref GetFriendOpByIndex(ref friendInfo, i); if (! one.IsNull()) { DBFriendOneOp oneOp = new DBFriendOneOp(); oneOp.OpType = one.OpType; oneOp.OpSeq = one.OpSeq; ref DBFriendOneStruct oneFriend = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(one.DBFriendOneID); oneOp.Friend.CopyFrom(ref oneFriend.oneFriend); opData.OpList.Add(ref oneOp); } } //拷贝DBFriendOpData 聊天信息 opData.LastChatSeq = friendInfo.FriendOpData.LastChatSeq; for (int i = 0; i < friendInfo.FriendOpData.ChatCount; i++) { ref DBFriendChatOneStruct one = ref GetFriendChatByIndex(ref friendInfo, i); if (! one.IsNull()) { opData.ChatList.Add(ref one.oneFriendChat); } } //拷贝DBFriendOpData 陌生人信息 for (int i = 0; i < friendInfo.FriendOpData.StrangerCount; i++) { ref DBFriendStrangerOneStruct one = ref GetFriendStrangerByIndex(ref friendInfo, i); if (! one.IsNull()) { DBFriendStrangerOne oneStranger = new DBFriendStrangerOne(); oneStranger.PlayTogetherCount = one.PlayTogetherCount; oneStranger.LastUpdateTime = one.LastUpdateTime; ref DBFriendOneStruct oneFriend = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(one.DBFriendOneID); oneStranger.Data.CopyFrom(ref oneFriend.oneFriend); opData.StrangerList.Add(ref oneStranger); } } friendInfo.LastAccessTime = FriendServerUtils.GetTimeSecond(); return true; } // 从FriendInfoCache中获取FriendOpData public static unsafe void GetOpDataFromFriendCacheInfo(long selfUid, ref DBFriendOpData opData, bool needChat) { if (FriendInfoCache.m_selfIDMap.TryGetValue(selfUid, out long posIndex) == false) { TraceLog.Error("FriendOp.GetOpDataFromFriendCacheInfo not find selfUid {0} in FriendInfoCache.m_selfIDMap", selfUid); return ; } ref FriendCacheInfoStruct myInfo = ref FriendInfoCache.m_cacheStructFriendInfo.GetByIndex(posIndex); if (myInfo.IsNull()) { TraceLog.Error("FriendOp.GetOpDataFromFriendCacheInfo not find FriendCacheInfoStruct uid {0},index {1}", selfUid, posIndex); return ; } //拷贝DBFriendOpData 操作信息 opData.LastOpSeq = myInfo.FriendOpData.LastOpSeq; for (int i = 0; i < myInfo.FriendOpData.OpCount; i++) { ref DBFriendOneOpStruct one = ref GetFriendOpByIndex(ref myInfo, i); if (! one.IsNull()) { DBFriendOneOp oneOp = new DBFriendOneOp(); oneOp.OpType = one.OpType; oneOp.OpSeq = one.OpSeq; ref DBFriendOneStruct oneFriend = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(one.DBFriendOneID); oneOp.Friend.CopyFrom(ref oneFriend.oneFriend); opData.OpList.Add(ref oneOp); } } // chat要走chatsvr单独下发, 拉取好友数据时不下发 if (needChat) { //拷贝DBFriendOpData 聊天信息 opData.LastChatSeq = myInfo.FriendOpData.LastChatSeq; for (int i = 0; i < myInfo.FriendOpData.ChatCount; i++) { ref DBFriendChatOneStruct one = ref GetFriendChatByIndex(ref myInfo, i); if (! one.IsNull()) { opData.ChatList.Add(ref one.oneFriendChat); } } } //拷贝DBFriendOpData 陌生人信息 for (int i = 0; i < myInfo.FriendOpData.StrangerCount; i++) { ref DBFriendStrangerOneStruct one = ref GetFriendStrangerByIndex(ref myInfo, i); if (! one.IsNull()) { DBFriendStrangerOne oneStranger = new DBFriendStrangerOne(); oneStranger.PlayTogetherCount = one.PlayTogetherCount; oneStranger.LastUpdateTime = one.LastUpdateTime; ref DBFriendOneStruct oneFriend = ref FriendInfoCache.m_cacheStructFriendOne.GetByIndex(one.DBFriendOneID); oneStranger.Data.CopyFrom(ref oneFriend.oneFriend); opData.StrangerList.Add(ref oneStranger); } } myInfo.LastAccessTime = FriendServerUtils.GetTimeSecond(); } public static unsafe void FillBlacklistInfo(ref FriendCacheInfoStruct info, ref SSQueryBlacklistRes res) { ref var blacklist = ref info.Self.BlackList; for (int i = 0; i < blacklist.Count; i++) { if (FriendUtils.IsPlayerBelongThisServer(blacklist[i])) { ref FriendCacheInfoStruct friendInfo = ref FriendOp.GetFriendInfoByUid(blacklist[i]); if (!friendInfo.IsNull()) { DBFriendOne friendOne = new DBFriendOne(); FriendUtils.CopySelfToFriendOne(ref friendInfo.Self, ref friendOne); res.Blacklist.Add(ref friendOne); } } else { ref RemoteFriendOneStruct friendInfo = ref FriendOp.GetRemoteFriendOneByUid(blacklist[i]); if (!friendInfo.IsNull()) { DBFriendOne friendOne = new DBFriendOne(); FriendUtils.CopyRemoteFriendToDBFriend(ref friendInfo, ref friendOne); res.Blacklist.Add(ref friendOne); } } } } /// /// 增加一个玩家的FriendcacheInfoStruct 到内存中 /// /// /// /// public static unsafe ref FriendCacheInfoStruct AddFriendCacheInfoStruct(ref DBFriendSelf self, ref DBFriendList friendList, ref DBFriendOpData opData) { //玩家正好自己登陆,或者有人调用好友信息 if (FriendInfoCache.m_selfIDMap.TryGetValue(self.Uid, out long index)) { ref FriendCacheInfoStruct info = ref FriendInfoCache.m_cacheStructFriendInfo.GetByIndex(index); if (!info.IsNull()) { return ref info; } // index存在但是数据不存在? FriendInfoCache.m_cacheStructFriendInfo.Free(index); FriendInfoCache.m_selfIDMap.Remove(self.Uid); } if (FriendInfoCache.m_cacheStructFriendInfo.m_freeQueue.Count == 0) { TraceLog.Error("FriendOp.AddFriendCacheInfoStruct FriendInfoCache.m_cacheStructFriendInfo not enough space"); return ref FriendInfoCache.m_cacheStructFriendInfo.GetByIndex(0); } ref FriendCacheInfoStruct infoStruct = ref FriendInfoCache.m_cacheStructFriendInfo.Malloc(); // 拷贝DBFriendSelf 信息 infoStruct.Self.CopyFrom(ref self); // 设置realm信息 if (infoStruct.Self.RealmId == 0 || infoStruct.Self.RealmName.Length == 0) { var player = FriendServerUtils.GetPlayerTableOp().GetPlayerInfo(self.Uid); if (player != null) { if (infoStruct.Self.RealmId == 0) { infoStruct.Self.RealmId = player.realmId; } if (infoStruct.Self.RealmName.Length == 0) { var realm = FriendServerUtils.GetRealmBriefInfo(player.realmId); if (realm != null) { infoStruct.Self.RealmName.SetString(realm.realmName); } } } } // 防止运营期间修改参数, 正常情况不会出现 if (friendList.AllFriends.Count > FriendConfig.GetFriendListCountMax()) { TraceLog.Error("FriendOp.AddFriendCacheInfoStruct uid {0} friendNum {1} > friendMax {2}" , self.Uid, friendList.AllFriends.Count, FriendConfig.GetFriendListCountMax()); } //拷贝DBFriendList 信息 infoStruct.FriendList.iCount = 0; for (int i = 0; i < friendList.AllFriends.Count; i++) { long posIndex = AddFriendOneToCache(ref friendList.AllFriends[i]); if (posIndex == 0) { TraceLog.Error("FriendOp.AddFriendCacheInfoStruct uid {0} add friendOne {1} failed" , self.Uid, friendList.AllFriends[i].Uid); DeleteFriendCacheInfo(ref infoStruct); return ref FriendInfoCache.m_cacheStructFriendInfo.GetByIndex(0); } infoStruct.FriendList.DBFriendOneID[i] = posIndex; infoStruct.FriendList.iCount++; } // 防止运营期间修改参数, 正常情况不会出现 if (opData.OpList.Count > FriendConfig.GetFriendOpListCountMax()) { TraceLog.Error("FriendOp.AddFriendCacheInfoStruct uid {0} friendOpNum {1} > friendOpMax {2}" , self.Uid, opData.OpList.Count, FriendConfig.GetFriendOpListCountMax()); } //拷贝DBFriendOpData 操作信息 infoStruct.FriendOpData.LastOpSeq = opData.LastOpSeq; infoStruct.FriendOpData.OpCount = 0; for (int i = 0; i < opData.OpList.Count; i++) { long posIndex = AddFriendOneOpToCache(ref opData.OpList[i]); if (posIndex == 0) { TraceLog.Error("FriendOp.AddFriendCacheInfoStruct uid {0} add friendOp failed", self.Uid); DeleteFriendCacheInfo(ref infoStruct); return ref FriendInfoCache.m_cacheStructFriendInfo.GetByIndex(0); } infoStruct.FriendOpData.OpList[i] = posIndex; infoStruct.FriendOpData.OpCount++; } // 防止运营期间修改参数, 正常情况不会出现 if (opData.ChatList.Count > FriendConfig.GetChatListCountMax()) { TraceLog.Error("FriendOp.AddFriendCacheInfoStruct uid {0} chatNum {1} > chatMax {2}" , self.Uid, opData.ChatList.Count, FriendConfig.GetChatListCountMax()); } //拷贝DBFriendOpData 聊天信息 infoStruct.FriendOpData.LastChatSeq = opData.LastChatSeq; infoStruct.FriendOpData.ChatCount = 0; for (int i = 0; i < opData.ChatList.Count; i++) { long posIndex = AddFriendChatOneToCache(ref opData.ChatList[i]); if (posIndex == 0) { TraceLog.Error("FriendOp.AddFriendCacheInfoStruct uid {0} add chat failed", self.Uid); DeleteFriendCacheInfo(ref infoStruct); return ref FriendInfoCache.m_cacheStructFriendInfo.GetByIndex(0); } infoStruct.FriendOpData.ChatList[i] = posIndex; infoStruct.FriendOpData.ChatCount++; } // 防止运营期间修改参数, 正常情况不会出现 if (opData.StrangerList.Count > FriendConfig.GetFriendStrangerListCountMax()) { TraceLog.Error("FriendOp.AddFriendCacheInfoStruct uid {0} strangerNum {1} > strangerMax {2}" , self.Uid, opData.StrangerList.Count, FriendConfig.GetFriendStrangerListCountMax()); } //拷贝DBFriendOpData 陌生人信息 infoStruct.FriendOpData.StrangerCount = 0; for (int i = 0; i < opData.StrangerList.Count; i++) { long posIndex = AddFriendStrangerOneToCache(ref opData.StrangerList[i]); if (posIndex == 0) { TraceLog.Error("FriendOp.AddFriendCacheInfoStruct uid {0} add stranger failed", self.Uid); DeleteFriendCacheInfo(ref infoStruct); return ref FriendInfoCache.m_cacheStructFriendInfo.GetByIndex(0); } infoStruct.FriendOpData.StrangerList[i] = posIndex; infoStruct.FriendOpData.StrangerCount++; } infoStruct.LastAccessTime = FriendServerUtils.GetTimeSecond(); // 最后加入哈希表 FriendInfoCache.m_selfIDMap.Add(self.Uid, infoStruct.GetObjectID()); TraceLog.Trace("FriendOp.AddFriendCacheInfoStruct uid {0} succ", infoStruct.Self.Uid); AlertUsedPercent(); return ref infoStruct; } private static void AlertUsedPercent() { int usedRate = FriendInfoCache.m_cacheStructFriendInfo.GetUsedPercent(); if (usedRate > 80) { string str = $"warning: friendInfo used {usedRate}% > 80%"; FriendServerUtils.GetApp().Alerter.AlertMsg(str, 3600); } usedRate = FriendInfoCache.m_cacheStructFriendOne.GetUsedPercent(); if (usedRate > 80) { string str = $"warning: friendOne used {usedRate}% > 80%"; FriendServerUtils.GetApp().Alerter.AlertMsg(str, 3600); } usedRate = FriendInfoCache.m_cacheStructFriendOneOp.GetUsedPercent(); if (usedRate > 80) { string str = $"warning: friendOp used {usedRate}% > 80%"; FriendServerUtils.GetApp().Alerter.AlertMsg(str, 3600); } usedRate = FriendInfoCache.m_cacheStructFriendChat.GetUsedPercent(); if (usedRate > 80) { string str = $"warning: friendChat used {usedRate}% > 80%"; FriendServerUtils.GetApp().Alerter.AlertMsg(str, 3600); } usedRate = FriendInfoCache.m_remoteFriendOneCache.GetUsedPercent(); if (usedRate > 80) { string str = $"warning: remote friendOne used {usedRate}% > 80%"; FriendServerUtils.GetApp().Alerter.AlertMsg(str, 3600); } } #endregion #region 从数据库拉取时添加 // 该接口只是个简单封装, 调用前必须保证friendUid相关数据在FriendInfoCache.DBFriendSelf或RemoteFriendOne.DBFriendOne中 private static long AddFriendOneToCache(long friendUid) { if (FriendUtils.IsPlayerBelongThisServer(friendUid)) { ref FriendCacheInfoStruct friendCache = ref GetFriendInfoByUid(friendUid); if (! friendCache.IsNull()) { return AddFriendOneToCache(ref friendCache.Self); } TraceLog.Error("AddFriendOneToCache error, local uid {0} no FriendCacheInfoStruct", friendUid); } else { ref RemoteFriendOneStruct remoteFriend = ref GetRemoteFriendOneByUid(friendUid); if (!remoteFriend.IsNull()) { return AddFriendOneToCache(ref remoteFriend); } TraceLog.Error("AddFriendOneToCache error, remote uid {0} no RemoteFriendOneStruct", friendUid); } return 0; } /// /// 添加一个新的DBFriendOne到缓存中,返回添加后的 索引位置 /// 所有添加到缓存中的操作请通过这个接口 /// /// /// private static long AddFriendOneToCache(ref DBFriendOne oneFriend) { //不够时操作,一旦到这里问题就大了,前面之后一定要添加目前已经使用多少缓存的警告,如果超过要及时停服维护扩充 if (FriendInfoCache.m_cacheStructFriendOne.m_freeQueue.Count == 0) { TraceLog.Error("AddFriendOneToCache error, m_freeQueue.Count not enough"); return 0; } long cacheIndex = FriendInfoCache.m_cacheStructFriendOne.m_freeQueue.Peek(); ref DBFriendOneStruct one = ref FriendInfoCache.m_cacheStructFriendOne.Malloc(); one.oneFriend.CopyFrom(ref oneFriend); return cacheIndex; } private static long AddFriendOneToCache(ref DBFriendSelf self) { //不够时操作,一旦到这里问题就大了,前面之后一定要添加目前已经使用多少缓存的警告,如果超过要及时停服维护扩充 if (FriendInfoCache.m_cacheStructFriendOne.m_freeQueue.Count == 0) { TraceLog.Error("AddFriendOneToCache error, m_freeQueue.Count not enough"); return 0; } long cacheIndex = FriendInfoCache.m_cacheStructFriendOne.m_freeQueue.Peek(); ref DBFriendOneStruct one = ref FriendInfoCache.m_cacheStructFriendOne.Malloc(); FriendUtils.CopySelfToFriendOne(ref self, ref one.oneFriend); return cacheIndex; } private static long AddFriendOneToCache(ref RemoteFriendOneStruct remoteFriend) { //不够时操作,一旦到这里问题就大了,前面之后一定要添加目前已经使用多少缓存的警告,如果超过要及时停服维护扩充 if (FriendInfoCache.m_cacheStructFriendOne.m_freeQueue.Count == 0) { TraceLog.Error("AddFriendOneToCache error, m_freeQueue.Count not enough"); return 0; } long cacheIndex = FriendInfoCache.m_cacheStructFriendOne.m_freeQueue.Peek(); ref DBFriendOneStruct one = ref FriendInfoCache.m_cacheStructFriendOne.Malloc(); FriendUtils.CopyRemoteFriendToDBFriend(ref remoteFriend, ref one.oneFriend); return cacheIndex; } /// /// 添加一条聊天信息到缓存中 /// /// /// private static long AddFriendChatOneToCache(ref DBFriendChatOne chatone) { //不够时操作,一旦到这里问题就大了,前面之后一定要添加目前已经使用多少缓存的警告,如果超过要及时停服维护扩充 if (FriendInfoCache.m_cacheStructFriendChat.m_freeQueue.Count == 0) { TraceLog.Error("AddFriendChatOneToCache error FriendInfoCache.m_cacheStructFriendChat.m_freeQueue.Count not enough"); return 0; } ref DBFriendChatOneStruct chatOneStruct = ref FriendInfoCache.m_cacheStructFriendChat.Malloc(); chatOneStruct.oneFriendChat.CopyFrom(ref chatone); return chatOneStruct.GetObjectID(); } private static long AddFriendChatOneToCache(ref CSChatRes chatRes, int seq) { //不够时操作,一旦到这里问题就大了,前面之后一定要添加目前已经使用多少缓存的警告,如果超过要及时停服维护扩充 if (FriendInfoCache.m_cacheStructFriendChat.m_freeQueue.Count == 0) { TraceLog.Error("AddFriendChatOneToCache error FriendInfoCache.m_cacheStructFriendChat.m_freeQueue.Count not enough"); return 0; } ref DBFriendChatOneStruct chatOneStruct = ref FriendInfoCache.m_cacheStructFriendChat.Malloc(); chatOneStruct.oneFriendChat.SenderUid = chatRes.Uid; chatOneStruct.oneFriendChat.SenderNick = chatRes.Nick; chatOneStruct.oneFriendChat.SenderIcon = chatRes.Icon; chatOneStruct.oneFriendChat.SenderGender = chatRes.Gender; chatOneStruct.oneFriendChat.Message = chatRes.Message; chatOneStruct.oneFriendChat.SendTime = chatRes.SendTime; chatOneStruct.oneFriendChat.ChatSeq = seq; chatOneStruct.oneFriendChat.TargetUid = chatRes.TargetUid; chatOneStruct.oneFriendChat.IconFrameId = chatRes.IconFrameId; return chatOneStruct.GetObjectID(); } /// /// 添加一条操作到缓存中(注意不是添加到玩家中,返回的位置索引再关联到玩家) /// /// /// private static long AddFriendOneOpToCache(ref DBFriendOneOp oneOp) { //不够时操作,一旦到这里问题就大了,前面之后一定要添加目前已经使用多少缓存的警告,如果超过要及时停服维护扩充 if (FriendInfoCache.m_cacheStructFriendOneOp.m_freeQueue.Count == 0) { TraceLog.Error("AddFriendOneOpToCache error FriendInfoCache.m_cacheStructFriendOneOp.m_freeQueue.Count not enough"); return 0; } //1:先判断能不能添加一个好友信息到缓存 long oneFriendindex = AddFriendOneToCache(ref oneOp.Friend); if (oneFriendindex == 0) { return 0; } long cacheIndex = FriendInfoCache.m_cacheStructFriendOneOp.m_freeQueue.Peek(); ref DBFriendOneOpStruct oneOpStruct = ref FriendInfoCache.m_cacheStructFriendOneOp.Malloc(); oneOpStruct.OpSeq = oneOp.OpSeq; oneOpStruct.OpType = oneOp.OpType; oneOpStruct.DBFriendOneID = oneFriendindex; oneOpStruct.FriendUid = oneOp.Friend.Uid; return cacheIndex; } // 调用前必须保证submit的信息在FriendCache或者RemoteFriendOne中 private static long AddFriendOneOpToCache(long submitUid, int opType, int opSeq) { //不够时操作,一旦到这里问题就大了,前面之后一定要添加目前已经使用多少缓存的警告,如果超过要及时停服维护扩充 if (FriendInfoCache.m_cacheStructFriendOneOp.m_freeQueue.Count == 0) { TraceLog.Error("AddFriendOneOpToCache error FriendInfoCache.m_cacheStructFriendOneOp not enough space"); return 0; } // 先判断能不能添加一个好友信息到缓存 long oneFriendindex = AddFriendOneToCache(submitUid); if (oneFriendindex <= 0) { return 0; } long cacheIndex = FriendInfoCache.m_cacheStructFriendOneOp.m_freeQueue.Peek(); ref DBFriendOneOpStruct oneOpStruct = ref FriendInfoCache.m_cacheStructFriendOneOp.Malloc(); oneOpStruct.OpType = opType; oneOpStruct.OpSeq = opSeq; oneOpStruct.DBFriendOneID = oneFriendindex; oneOpStruct.FriendUid = submitUid; return cacheIndex; } /// /// 添加一个陌生人到缓存中(注意不是添加到玩家中,返回的位置索引再关联到玩家) /// /// /// private static long AddFriendStrangerOneToCache(ref DBFriendStrangerOne strangerOne) { //不够时操作,一旦到这里问题就大了,前面之后一定要添加目前已经使用多少缓存的警告,如果超过要及时停服维护扩充 if (FriendInfoCache.m_cacheStructFriendStranger.m_freeQueue.Count == 0) { TraceLog.Error("AddFriendStrangerOneToCache error FriendInfoCache.m_cacheStructFriendStranger.m_freeQueue.Count not enough"); return 0; } //1:先判断能不能添加一个好友信息到缓存 long oneFriendindex = AddFriendOneToCache(ref strangerOne.Data); if (oneFriendindex == 0) { return 0; } long cacheIndex = FriendInfoCache.m_cacheStructFriendStranger.m_freeQueue.Peek(); ref DBFriendStrangerOneStruct oneStruct = ref FriendInfoCache.m_cacheStructFriendStranger.Malloc(); oneStruct.PlayTogetherCount = strangerOne.PlayTogetherCount; oneStruct.LastUpdateTime = strangerOne.LastUpdateTime; oneStruct.DBFriendOneID = oneFriendindex; return cacheIndex; } #endregion #region 释放缓存 public static void DoFreeFriendOPByIndex(long index) { DBFriendOneOpStruct oneOpStruct = FriendInfoCache.m_cacheStructFriendOneOp.GetByIndex(index); if (oneOpStruct.IsNull()) { return; } DoFreeFriendOneByIndex(oneOpStruct.DBFriendOneID); FriendInfoCache.m_cacheStructFriendOneOp.Free(index); } private static void DoFreeStrangerByIndex(long index) { DBFriendStrangerOneStruct oneStruct = FriendInfoCache.m_cacheStructFriendStranger.GetByIndex(index); if (oneStruct.IsNull()) { return; } DoFreeFriendOneByIndex(oneStruct.DBFriendOneID); FriendInfoCache.m_cacheStructFriendStranger.Free(index); } private static void DoFreeChatListByIndex(long index) { DBFriendChatOneStruct chatOneStruct = FriendInfoCache.m_cacheStructFriendChat.GetByIndex(index); if (chatOneStruct.IsNull()) { return; } FriendInfoCache.m_cacheStructFriendChat.Free(index); } private static void DoFreeFriendOneByIndex(long index) { DBFriendOneStruct one = FriendInfoCache.m_cacheStructFriendOne.GetByIndex(index); if (one.IsNull()) { return; } FriendInfoCache.m_cacheStructFriendOne.Free(index); } #endregion } }