CTT/Server/Hotfix/Game/System/Other/CDKComponentSystem.cs

117 lines
3.0 KiB
C#
Raw Normal View History

2023-09-07 00:06:37 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using Cal.DataTable;
namespace ET;
static class CDKComponentSystem
{
class CDKComponentAwakeSystem: AwakeSystem<CDKComponent>
{
public override void Awake(CDKComponent self)
{
self.Awake();
}
}
class CDKComponentLoadSystem: LoadSystem<CDKComponent>
{
public override void Load(CDKComponent self)
{
self.Load();
}
}
private static void Awake(this CDKComponent self)
{
self.Id = 0;
self.Load();
}
private static void Load(this CDKComponent self)
{
self.LoadAsync().Coroutine();
}
private static async ETVoid LoadAsync(this CDKComponent self)
{
using CDKComponent cdkInDatabase = await UnitHelper.Query<CDKComponent>(self.DomainZone(), 0);
Dictionary<string, long> toAddDictionary = null;
HashSet<string> gotList = null;
if (cdkInDatabase != null)
{
toAddDictionary = cdkInDatabase.nameList;
gotList = cdkInDatabase.gotList;
}
var cdkConfigs = DataTableHelper.GetAll<CdkConfig>().ToArray();
self.nameList.Clear();
if (toAddDictionary != null)
{
foreach (var keyValuePair in toAddDictionary)
{
self.nameList.Add(keyValuePair.Key, keyValuePair.Value);
}
}
foreach (CdkConfig cdkConfig in cdkConfigs)
{
if ( gotList!=null && gotList.Contains(cdkConfig.Cdk))
{
continue;
}
bool add = self.nameList.TryAdd(cdkConfig.Cdk, cdkConfig.Id);
if (!add)
{
Log.Error($"config's cdk is already in set where id is {cdkConfig.Id}");
self.nameList.Clear();
return;
}
}
self.Save();
}
private static void Save(this CDKComponent self)
{
UnitHelper.SaveComponenet(self.DomainZone(), self);
}
public static async ETTask<string> GetCDKReward(this CDKComponent self, Unit unit, string cdk)
{
if (string.IsNullOrWhiteSpace(cdk))
{
return "cdk 为空";
}
if (!self.nameList.TryGetValue(cdk, out var id))
{
return "cdk 无效";
}
CdkConfig cdkConfig = DataTableHelper.Get<CdkConfig>(id);
if (cdkConfig == null)
{
return "cdk 系统错误";
}
try
{
foreach (CdkConfig.Reward reward in cdkConfig.RewardArr)
{
await MailHelper.AddItem(unit.Id, reward._Id, reward.Count, true, getSource: "CDK获取", "GM邮件", "CDK获取的物品。", "CDK");
}
}
catch (Exception e)
{
Log.Error(e);
return "cdk 获取物品错误";
}
self.nameList.Remove(cdk);
self.gotList.Add(cdk);
self.Save();
return "获取物品成功";
}
}