2021-06-29 11:28:15 +08:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using MongoDB.Bson;
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
|
|
|
namespace ET
|
|
|
|
{
|
|
|
|
class PlayerInfoComponentStartSystem:StartSystem<PlayerInfoComponent>
|
|
|
|
{
|
|
|
|
public override void Start(PlayerInfoComponent self)
|
|
|
|
{
|
|
|
|
self.AddComponent<FixedTimeSaveComponent>().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<Entity> GetInfo(this PlayerInfoComponent self, long id, string type)
|
|
|
|
{
|
2021-08-06 15:04:29 +08:00
|
|
|
Log.Info($"{id} 查询 {type}");
|
2021-06-29 11:28:15 +08:00
|
|
|
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<Entity> GetEntity(long id, string type)
|
|
|
|
{
|
2021-08-06 15:04:29 +08:00
|
|
|
Log.Info($"{id} 从数据库查询 {type}");
|
2021-06-29 11:28:15 +08:00
|
|
|
var cursor = await DBComponent.Instance.GetCollection(type).FindAsync(d => d.Id == id);
|
|
|
|
Entity e = await cursor.FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|