using System.Linq; using Sog; namespace Battle { public class BattleTableOp : BaseReloadableService { // 每个mainland最多保存最近5个scene public static int MAX_SCENE_NUM_PER_MAINLAND = 5; // 每个scene最多保存3个battleInfo public static int MAX_BATTLE_NUM_PER_SCENE = 3; public BattleTable Table; public override int GetServiceType() { return BattleServiceType.BattleTableOp; } public override void Dispose() { Table = null; } public BattleTableOp(BattleTable table) { Table = table; } public int GetBattleCount() { return Table.m_battleTable.Count; } public BattleInfoCache GetBattleCache(long uid) { Table.m_battleTable.TryGetValue(uid, out BattleInfoCache cache); return cache; } public ProtoCSClass.BattleInfoAll GetBattleInfo(long uid, ulong battleId,bool autoChallenge) { if (autoChallenge) { //if (Table.m_autoChallenge_battleTable.TryGetValue(uid, out ProtoCSClass.BattleInfoAll battleInfoAll)) //{ // if (battleInfoAll.BattleId == battleId) // { // return battleInfoAll; // } //} return null; } if (Table.m_battleTable.TryGetValue(uid, out BattleInfoCache cache)) { //foreach (MainlandBattleInfo scene in cache.mainlandBattle) //{ // foreach (ProtoCSClass.BattleInfoAll battleInfoAll in scene.battleList) // { // if (battleInfoAll.BattleId == battleId) // { // return battleInfoAll; // } // } //} } return null; } private void DeleteEarliestMainlandBattle(BattleInfoCache cache) { int idx = -1; long min = 0; for (int i = 0; i < cache.mainlandBattle.Count; i++) { if (min == 0 || cache.mainlandBattle[i].lastReqTime < min) { min = cache.mainlandBattle[i].lastReqTime; idx = i; } } if (idx != -1) { for (int i = 0; i < cache.mainlandBattle[idx].battleList.Count; i++) { TraceLog.Trace("BattleTableOp.DeleteEarliestMainlandBattle uid {0} battleId {1}" , cache.uid, cache.mainlandBattle[idx].battleList[i].BattleId); } cache.mainlandBattle.RemoveAt(idx); } } public void AddBattleInfo(long uid, ProtoCSClass.BattleInfoAll battleInfo, long nowMs,bool autoChallenge) { if (autoChallenge) { //if (Table.m_autoChallenge_battleTable.ContainsKey(uid)) //{ // Table.m_autoChallenge_battleTable[uid] = battleInfo; //} //else //{ // Table.m_autoChallenge_battleTable.Add(uid, battleInfo); //} return; } if (Table.m_battleTable.TryGetValue(uid, out BattleInfoCache cache)) { // 新的副本战斗 if (cache.mainlandId != battleInfo.MainlandId) { TraceLog.Trace("BattleTableOp.AddBattleInfo uid {0} new mainland {1} clear cache" , uid, battleInfo.MainlandId); cache.mainlandId = battleInfo.MainlandId; cache.mainlandBattle.Clear(); } //var scene = cache.mainlandBattle.FirstOrDefault(mb => mb.mainlandScene == battleInfo.MainlandScene); // mainland scene没有缓存 //if (scene == null) //{ // // scene应该不会超 // if (cache.mainlandBattle.Count >= MAX_SCENE_NUM_PER_MAINLAND) // { // DeleteEarliestMainlandBattle(cache); // } // TraceLog.Trace("BattleTableOp.AddBattleInfo uid {0} battleId {1} mainland {2} scene {3}" // , uid, battleInfo.BattleId, battleInfo.MainlandId, battleInfo.MainlandScene); // scene = new MainlandBattleInfo{lastReqTime = nowMs, mainlandScene = battleInfo.MainlandScene}; // scene.battleList.Add(battleInfo); // cache.mainlandBattle.Add(scene); //} //else //{ // // 同一个mainland scene 3秒内开始的战斗都加入缓存, 避免网络延迟导致客户端多次发送MainlandEnterReq // if (nowMs <= scene.lastReqTime + 3000) // { // if (scene.battleList.Count >= MAX_BATTLE_NUM_PER_SCENE) // { // TraceLog.Trace("BattleTableOp.AddBattleInfo list is full, uid {0} remove battleId {1} " // , uid, scene.battleList[0].BattleId); // scene.battleList.RemoveAt(0); // } // } // else // { // for (int i = 0; i < scene.battleList.Count; i++) // { // TraceLog.Trace("BattleTableOp.AddBattleInfo uid {0} clear battleId {1}" // , uid, scene.battleList[i].BattleId); // } // scene.battleList.Clear(); // } // TraceLog.Trace("BattleTableOp.AddBattleInfo uid {0} battleId {1} mainland {2} scene {3}" // , uid, battleInfo.BattleId, battleInfo.MainlandId, battleInfo.MainlandScene); // scene.battleList.Add(battleInfo); // scene.lastReqTime = nowMs; //} } else { //TraceLog.Trace("BattleTableOp.AddBattleInfo uid {0} battleId {1} mainland {2} scene {3}" // , uid, battleInfo.BattleId, battleInfo.MainlandId, battleInfo.MainlandScene); //var mldBattle = new MainlandBattleInfo {lastReqTime = nowMs, mainlandScene = battleInfo.MainlandScene}; //mldBattle.battleList.Add(battleInfo); //cache = new BattleInfoCache {uid = uid, mainlandId = battleInfo.MainlandId}; //cache.mainlandBattle.Add(mldBattle); Table.m_battleTable.Add(uid, cache); } } public bool RemoveBattleInfo(long uid, bool autoChallenge) { TraceLog.Trace("BattleTableOp.RemoveBattleInfo uid {0} autoChallenge {1}", uid, autoChallenge); if (autoChallenge) { return Table.m_autoChallenge_battleTable.Remove(uid); } else { return Table.m_battleTable.Remove(uid); } } } }