77 lines
2.3 KiB
C#
77 lines
2.3 KiB
C#
|
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)
|
||
|
{
|
||
|
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)
|
||
|
{
|
||
|
var cursor = await DBComponent.Instance.GetCollection(type).FindAsync(d => d.Id == id);
|
||
|
Entity e = await cursor.FirstOrDefaultAsync();
|
||
|
|
||
|
return e;
|
||
|
}
|
||
|
}
|
||
|
}
|