132 lines
3.8 KiB
C#
132 lines
3.8 KiB
C#
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)
|
|
{
|
|
self.allCdkList.Clear();
|
|
self.gotCdkList.Clear();
|
|
var cdkConfigs = DataTableHelper.GetAll<CdkConfig>().ToArray();
|
|
|
|
|
|
int? version =cdkConfigs.Length>0? cdkConfigs[0].Version:null;
|
|
using CDKComponent cdkInDatabase = await UnitHelper.Query<CDKComponent>(self.DomainZone(), 0);
|
|
if (cdkInDatabase != null)
|
|
{
|
|
self.allCdkList = cdkInDatabase.allCdkList;
|
|
self.gotCdkList = cdkInDatabase.gotCdkList;
|
|
|
|
if (version != null && version.Value != cdkInDatabase.version)
|
|
{
|
|
foreach (CdkConfig cdkConfig in cdkConfigs)
|
|
{
|
|
if (cdkInDatabase.gotCdkList.Contains(cdkConfig.Cdk))
|
|
{
|
|
Log.Error($"config's cdk is already in set where id is {cdkConfig.Id}");
|
|
return;
|
|
}
|
|
|
|
var cdkRewards = new List<CDKReward>();
|
|
foreach (CdkConfig.Reward reward in cdkConfig.RewardArr)
|
|
{
|
|
cdkRewards.Add(new CDKReward(reward));
|
|
}
|
|
bool add = self.allCdkList.TryAdd(cdkConfig.Cdk, cdkRewards.ToArray());
|
|
if (!add)
|
|
{
|
|
Log.Error($"config's cdk is already in set where id is {cdkConfig.Id}");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
foreach (CdkConfig cdkConfig in cdkConfigs)
|
|
{
|
|
var cdkRewards = new List<CDKReward>();
|
|
foreach (CdkConfig.Reward reward in cdkConfig.RewardArr)
|
|
{
|
|
cdkRewards.Add(new CDKReward(reward));
|
|
}
|
|
bool add = self.allCdkList.TryAdd(cdkConfig.Cdk, cdkRewards.ToArray());
|
|
if (!add)
|
|
{
|
|
Log.Error($"config's cdk is already in set where id is {cdkConfig.Id}");
|
|
return;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
self.version = version ?? 0;
|
|
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.allCdkList.TryGetValue(cdk, out var rewards))
|
|
{
|
|
return "cdk 无效";
|
|
}
|
|
try
|
|
{
|
|
foreach (CDKReward reward in rewards)
|
|
{
|
|
await MailHelper.AddItem(unit.Id, reward.ItemId, reward.Count, true, getSource: "CDK获取", "CDK", "CDK获取的物品。", "CDK");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e);
|
|
return "cdk 获取物品错误";
|
|
}
|
|
|
|
self.allCdkList.Remove(cdk);
|
|
self.gotCdkList.Add(cdk);
|
|
self.Save();
|
|
return "获取物品成功";
|
|
}
|
|
} |