zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Server/Hotfix/Game/System/Cache/PlayerInfoComponentSystem.cs

111 lines
3.6 KiB
C#
Raw Normal View History

2021-06-29 11:28:15 +08:00
using System;
using System.Linq;
2022-11-26 23:20:12 +08:00
using ET.EventType;
2021-06-29 11:28:15 +08:00
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);
}
}
2022-11-26 23:20:12 +08:00
class ClearPerDay: AEvent<UpdatePer1DayOfMonth>
{
#region Overrides of AEvent<UpdatePer1DayOfMonth>
public override async ETTask Run(UpdatePer1DayOfMonth args)
{
PlayerInfoComponent playerInfoComponent = args.domain.GetComponent<PlayerInfoComponent>();
await playerInfoComponent.ClearAll();
}
#endregion
}
2021-06-29 11:28:15 +08:00
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();
}
}
2022-11-26 23:20:12 +08:00
public static async ETTask SaveAsync(this PlayerInfoComponent self)
{
Log.Info($"存档 「{self.unitInfoDic.Count}」");
foreach (var kv in self.unitInfoDic)
{
await DBComponent.Instance.Save(Game.IdGenerater.GenerateId(),kv.Value.Values.ToList());
}
}
public static async ETTask ClearAll(this PlayerInfoComponent self)
2022-05-29 23:02:28 +08:00
{
Log.Info($"存档 「{self.unitInfoDic.Count}」");
using var listcomponent = ListComponent<ETTask>.Create();
foreach (var kv in self.unitInfoDic)
{
2022-11-26 23:20:12 +08:00
listcomponent.List.Add(DBComponent.Instance.Save(Game.IdGenerater.GenerateId(),kv.Value.Values.ToList()));
2022-05-29 23:02:28 +08:00
}
self.unitInfoDic.Clear();
2022-11-26 23:20:12 +08:00
await ETTaskHelper.WaitAll(listcomponent.List);
2022-05-29 23:02:28 +08:00
}
2021-06-29 11:28:15 +08:00
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);
2021-09-04 14:55:51 +08:00
Entity entity = null;
2021-06-29 11:28:15 +08:00
if (dic == null)
{
2021-09-04 14:55:51 +08:00
entity = await GetEntity(id, type);
if (entity == null)
2021-06-29 11:28:15 +08:00
{
Log.Error($"e == null id = {id} type = {type}");
return null;
}
2021-09-04 14:55:51 +08:00
self.unitInfoDic.Add(type, id, entity);
2021-06-29 11:28:15 +08:00
dic = self.unitInfoDic[type];
}
2021-09-04 14:55:51 +08:00
dic.TryGetValue(id, out entity);
if (entity == null)
2021-06-29 11:28:15 +08:00
{
2021-09-04 14:55:51 +08:00
entity = await GetEntity(id, type);
if (entity == null)
2021-06-29 11:28:15 +08:00
{
2021-09-04 14:55:51 +08:00
Log.Error($"entity == null id = {id} type = {type}");
2021-06-29 11:28:15 +08:00
return null;
}
2021-09-04 14:55:51 +08:00
dic[id] = entity;
2021-06-29 11:28:15 +08:00
}
2021-09-04 14:55:51 +08:00
return entity;
2021-06-29 11:28:15 +08:00
}
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;
}
}
}