using System; using System.Linq; using MongoDB.Bson; using MongoDB.Driver; namespace ET { class PlayerInfoComponentStartSystem:StartSystem { public override void Start(PlayerInfoComponent self) { self.AddComponent().Init(10*60*1000,self.Save); } } public static class PlayerInfoComponentSystem { public static void Save(this PlayerInfoComponent self) { Log.Info($"存档 「{self.unitInfoDic.Count}」"); foreach (var kv in self.unitInfoDic) { DBComponent.Instance.Save(Game.IdGenerater.GenerateId(),kv.Value.Values.ToList()).Coroutine(); } } public static void AddInfo(this PlayerInfoComponent self, long id, string type, Entity str) { self.unitInfoDic.TryGetDic(type, out var dic); if (dic == null) { self.unitInfoDic.Add(type, id, str); return; } dic[id] = str; } public static async ETTask GetInfo(this PlayerInfoComponent self, long id, string type) { Log.Info($"{id} 查询 {type}"); self.unitInfoDic.TryGetDic(type, out var dic); Entity e = null; if (dic == null) { e = await GetEntity(id, type); if (e == null) { Log.Error($"e == null id = {id} type = {type}"); return null; } self.unitInfoDic.Add(type, id, e); dic = self.unitInfoDic[type]; } dic.TryGetValue(id, out e); if (e == null) { e = await GetEntity(id, type); if (e == null) { Log.Error($"e == null id = {id} type = {type}"); return null; } dic[id] = e; } return e; } private static async ETTask GetEntity(long id, string type) { Log.Info($"{id} 从数据库查询 {type}"); var cursor = await DBComponent.Instance.GetCollection(type).FindAsync(d => d.Id == id); Entity e = await cursor.FirstOrDefaultAsync(); return e; } } }