CTT/Server/Hotfix/Game/System/UI/FamilyBossComponentSystem.cs

311 lines
12 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using Cal.DataTable;
using ET.EventType;
using System;
using System.Collections.Generic;
namespace ET
{
2022-05-29 23:02:28 +08:00
public class FamilyBossComponentAwakeSystem: AwakeSystem<FamilyBossComponent>
2021-04-08 20:09:59 +08:00
{
public override void Awake(FamilyBossComponent self)
{
FamilyBossComponent.instance = self;
}
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
public static class FamilyBossComponentSystem
{
2022-05-29 23:02:28 +08:00
public class FamilyUpdatePerMinuteEvent: AEvent<EventType.UpdatePer1HourOfDay>
2021-04-08 20:09:59 +08:00
{
public override async ETTask Run(UpdatePer1HourOfDay args)
{
2021-04-11 19:50:39 +08:00
int time = DateTime.UtcNow.Hour;
2022-05-29 23:02:28 +08:00
Log.Info($"{DateTime.UtcNow}");
2021-04-08 20:09:59 +08:00
if (time != 14) return;
2021-04-11 19:50:39 +08:00
List<FamilyBoss> list = await DBComponent.Instance.QueryJson<FamilyBoss>("{}");
2021-04-08 20:09:59 +08:00
if (list == null || list.Count == 0)
{
return;
}
2022-05-29 23:02:28 +08:00
2021-04-11 19:50:39 +08:00
IEnumerable<FamilyBossConfig> bossList = DataTableHelper.GetAll<FamilyBossConfig>();
foreach (FamilyBoss familyBoss in list)
2021-04-08 20:09:59 +08:00
{
try
{
2021-04-11 19:50:39 +08:00
foreach (FamilyBossConfig config in bossList)
2021-04-08 20:09:59 +08:00
{
2022-05-29 23:02:28 +08:00
await FamilyBossComponent.instance.SettleReword(familyBoss, config);
2021-04-08 20:09:59 +08:00
}
}
catch (Exception e)
{
2022-05-29 23:02:28 +08:00
Log.Error(e);
2021-04-08 20:09:59 +08:00
}
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
await ETTask.CompletedTask;
}
}
2022-05-29 23:02:28 +08:00
public class FamilyUpdatePerDayEvent: AEvent<EventType.UpdatePer1DayOfMonth>
2021-04-08 20:09:59 +08:00
{
public override async ETTask Run(UpdatePer1DayOfMonth args)
{
2021-04-11 19:50:39 +08:00
List<FamilyBoss> list = await DBComponent.Instance.QueryJson<FamilyBoss>("{}");
2021-04-08 20:09:59 +08:00
if (list == null || list.Count == 0)
{
return;
}
2022-05-29 23:02:28 +08:00
2021-04-11 19:50:39 +08:00
IEnumerable<FamilyBossConfig> bossList = DataTableHelper.GetAll<FamilyBossConfig>();
foreach (FamilyBoss familyBoss in list)
2021-04-08 20:09:59 +08:00
{
try
{
foreach (FamilyBossConfig boss in bossList)
{
try
{
2021-04-24 17:39:11 +08:00
MonsterBase monsterBase = MonsterBaseCategory.Instance.Get(boss.MonsterId);
2022-05-29 23:02:28 +08:00
familyBoss.SetBossHp((int) boss.Id, monsterBase.Hp);
2021-04-08 20:09:59 +08:00
}
catch (Exception e)
{
2022-05-29 23:02:28 +08:00
Log.Error(e);
2021-04-08 20:09:59 +08:00
}
}
2022-05-29 23:02:28 +08:00
2021-04-11 19:50:39 +08:00
foreach (KeyValuePair<int, LinkedList<BossDamageMap>> item in familyBoss.rankDic)
2021-04-08 20:09:59 +08:00
{
try
{
item.Value.Clear();
}
catch (Exception e)
{
2022-05-29 23:02:28 +08:00
Log.Error(e);
2021-04-08 20:09:59 +08:00
}
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
await DBComponent.Instance.Save(familyBoss);
}
catch (Exception e)
{
2022-05-29 23:02:28 +08:00
Log.Error(e);
2021-04-08 20:09:59 +08:00
}
}
2022-05-29 23:02:28 +08:00
2021-04-11 19:50:39 +08:00
Dictionary<string, FamilyBoss> dic = FamilyBossComponent.instance.familyBossDic;
Dictionary<string, FamilyBoss>.ValueCollection _list = dic.Values;
2021-04-08 20:09:59 +08:00
dic.Clear();
2021-04-11 19:50:39 +08:00
foreach (FamilyBoss item in _list)
2021-04-08 20:09:59 +08:00
{
item.Dispose();
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
Log.Info($"更改boss血量");
await ETTask.CompletedTask;
}
}
public static async ETTask<FamilyBoss> Query(this FamilyBossComponent self, string name)
{
if (string.IsNullOrWhiteSpace(name)) return null;
2021-04-11 19:50:39 +08:00
if (!self.familyBossDic.TryGetValue(name, out FamilyBoss friend))
2021-04-08 20:09:59 +08:00
{
2021-04-11 19:50:39 +08:00
List<FamilyBoss> list = await DBComponent.Instance.Query<FamilyBoss>(t => t.name == name);
2021-04-08 20:09:59 +08:00
if (list == null || list.Count == 0)
{
return null;
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
self.familyBossDic[name] = friend = list[0];
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
return friend;
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
public static async ETTask Save(this FamilyBossComponent self, FamilyBoss family)
{
await DBComponent.Instance.Save(family);
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
public static async ETTask Create(this FamilyBossComponent self, string name)
{
FamilyBoss familyBoss = EntityFactory.CreateWithParent<FamilyBoss>(self);
familyBoss.lockedMaxId = 1;
familyBoss.name = name;
if (familyBoss.rankDic.Count == 0)
{
2021-04-11 19:50:39 +08:00
IEnumerable<FamilyBossConfig> arr = DataTableHelper.GetAll<FamilyBossConfig>();
2021-04-08 20:09:59 +08:00
foreach (FamilyBossConfig item in arr)
{
2022-05-29 23:02:28 +08:00
familyBoss.rankDic.Add((int) item.Id, new LinkedList<BossDamageMap>());
2021-04-24 17:39:11 +08:00
MonsterBase monsterBase = MonsterBaseCategory.Instance.Get(item.MonsterId);
2022-05-29 23:02:28 +08:00
familyBoss.SetBossHp((int) item.Id, monsterBase.Hp);
2021-04-08 20:09:59 +08:00
}
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
self.familyBossDic.Add(name, familyBoss);
await self.Save(familyBoss);
}
2022-05-29 23:02:28 +08:00
public static async ETTask<string> GetFamilyBossInfo(this FamilyBossComponent self, Unit unit,
System.Collections.Generic.List<BossInfo> bossInfoList)
2021-04-08 20:09:59 +08:00
{
User user = UserComponent.Instance.Get(unit.Id);
FamilyBoss familyBoss = await self.Query(user.Family);
if (familyBoss == null)
{
return "您还没有加入家族!";
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
Family family = await FamilyComponent.Instance.Query(familyBoss.name);
if (family == null)
{
return "您还没有加入家族!";
}
2022-05-29 23:02:28 +08:00
2021-04-11 19:50:39 +08:00
foreach (KeyValuePair<int, int> kv in familyBoss.bossHpDic)
2021-04-08 20:09:59 +08:00
{
FamilyBossConfig familyBossConfig = DataTableHelper.Get<FamilyBossConfig>(kv.Key);
2021-04-24 17:39:11 +08:00
MonsterBase monsterBase = MonsterBaseCategory.Instance.Get(familyBossConfig.MonsterId);
2022-05-29 23:02:28 +08:00
bossInfoList.Add(
new BossInfo { Index = kv.Key, Hp = kv.Value, MaxHp = monsterBase.Hp, HasReward = family.HasReward(kv.Key, unit.Id) });
2021-04-08 20:09:59 +08:00
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
return null;
}
2022-05-29 23:02:28 +08:00
public static async ETTask<string> GetRankList(this FamilyBossComponent self, Unit unit, int bossId,
System.Collections.Generic.List<BossDamageMap> rankList)
2021-04-08 20:09:59 +08:00
{
User user = UserComponent.Instance.Get(unit.Id);
FamilyBoss familyBoss = await self.Query(user.Family);
if (familyBoss == null)
{
return "您还没有加入家族!";
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
rankList.AddRange(familyBoss.GetRankList(bossId));
return null;
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
public static async ETTask<string> SettleReword(this FamilyBossComponent self, FamilyBoss familyBoss, FamilyBossConfig config)
{
if (familyBoss == null)
{
Log.Error($"familyBoss == null");
return "系统错误!";
}
2022-05-29 23:02:28 +08:00
Log.Info($"开始结算 {familyBoss.name}");
int bossId = (int) config.Id;
bool isKilled = familyBoss.GetBossHp(bossId) <= 0;
Family family = await FamilyComponent.Instance.Query(familyBoss.name);
if (family==null)
{
Log.Error($"family == null where {familyBoss.name}");
return "系统错误!";
}
family.ClearReward(bossId);
2021-04-11 19:50:39 +08:00
LinkedList<BossDamageMap> rank = familyBoss.GetRankList(bossId);
2021-04-24 17:39:11 +08:00
MonsterBase monsterBase = MonsterBaseCategory.Instance.Get(config.MonsterId);
int maxHp = monsterBase.Hp;
2021-04-08 20:09:59 +08:00
LinkedList<(long, float)> contributeList = new LinkedList<(long, float)>();
2021-04-11 19:50:39 +08:00
foreach (BossDamageMap damageInfo in rank)
2021-04-08 20:09:59 +08:00
{
2022-05-29 23:02:28 +08:00
float totalPercent = damageInfo.TotalDamage * 1f / maxHp;
2022-06-03 21:23:02 +08:00
if(totalPercent<0.01f)
continue;
2022-05-29 23:02:28 +08:00
float percent = totalPercent / damageInfo.DamageList.Count;
2021-04-11 19:50:39 +08:00
foreach (BossDamagePerMemberMap memberMap in damageInfo.DamageList)
2021-04-08 20:09:59 +08:00
{
ModifyList(contributeList, memberMap.Id, percent);
2022-05-29 23:02:28 +08:00
int personalContribute = MathHelper.RoundToInt(config.PersonalContribute * totalPercent);
family.AddReward(bossId, memberMap.Id, BagHelper.FamilyCoinId, personalContribute);
2021-04-08 20:09:59 +08:00
}
}
2021-04-11 19:50:39 +08:00
foreach ((long id, float percent) in contributeList)
2021-04-08 20:09:59 +08:00
{
if (!family.Caintains(id))
continue;
int contribute = MathHelper.RoundToInt(config.Contribute * percent);
family.AddContribution(id, contribute);
PlayerData playerData;
int parentSetId = config.Dropasubset;
2021-04-11 19:50:39 +08:00
Unit unit = MapUnitComponent.Instance.Get(id);
2021-04-08 20:09:59 +08:00
if (unit == null)
{
playerData = await DBComponent.Instance.Query<PlayerData>(id);
parentSetId = playerData.UpdateDrop(config.Dropasubset);
2022-05-29 23:02:28 +08:00
UnitHelper.SaveComponenet(self.DomainZone(), playerData);
2021-04-08 20:09:59 +08:00
}
else
{
playerData = unit.GetComponent<PlayerData>();
parentSetId = playerData.UpdateDrop(config.Dropasubset);
2022-05-29 23:02:28 +08:00
UnitHelper.SaveComponenet(self.DomainZone(), playerData);
2021-04-08 20:09:59 +08:00
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
Parentset parentSet = ParentsetCategory.Instance.Get(parentSetId);
2021-04-11 19:50:39 +08:00
foreach (Parentset.Subset subSet in parentSet.SubsetArr)
2021-04-08 20:09:59 +08:00
{
SonSet sonSet = SonSetCategory.Instance.Get(subSet._Id);
(int itemId, int itemCount) = GetItemFormSet(sonSet.DropArr, percent, isKilled);
if (itemId == 0) continue;
family.AddReward(bossId, id, itemId, itemCount);
Log.Info($"【{UserComponent.Instance.Get(id)?.NickName}({id})】 掉落了【{BagHelper.GetName(itemId)}】X {itemCount}");
}
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
FamilyComponent.Instance.Save(family).Coroutine();
Log.Info($"是否击杀{bossId}阶boss {isKilled}");
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
return null;
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
private static (int, int) GetItemFormSet(SonSet.Drop[] arr, float damagePercent, bool isKilled)
{
2021-04-11 19:50:39 +08:00
using ListComponent<int> listComponent = ListComponent<int>.Create();
2021-04-08 20:09:59 +08:00
foreach (SonSet.Drop drop in arr)
{
if (drop._Id == 0)
{
2022-05-29 23:02:28 +08:00
float weight = isKilled? 0.5f * drop.Weight : drop.Weight;
2021-04-08 20:09:59 +08:00
weight *= (2 - damagePercent * damagePercent);
2022-05-29 23:02:28 +08:00
listComponent.List.Add(MathHelper.RoundToInt(weight));
2021-04-08 20:09:59 +08:00
}
else
listComponent.List.Add(drop.Weight);
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
int index = MathHelper.GetProbabilityIndexByWeight(listComponent.List);
2021-04-11 19:50:39 +08:00
SonSet.Drop item = arr[index];
2021-04-08 20:09:59 +08:00
return (item._Id, RandomHelper.RandomNumber(item.MinCount, item.MaxCount + 1));
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
private static void ModifyList(LinkedList<(long, float)> list, long id, float addValue)
{
2021-04-11 19:50:39 +08:00
float oldScord = 0f;
for (LinkedListNode<(long, float)>? item = list.First; item != null; item = item.Next)
2021-04-08 20:09:59 +08:00
{
if (item.Value.Item1 == id)
{
oldScord = item.Value.Item2;
list.Remove(item);
break;
}
}
2022-05-29 23:02:28 +08:00
2021-04-08 20:09:59 +08:00
oldScord += addValue;
LinkedListHelper.InsertToLinkedListAndSort(list, (id, oldScord), (t1, t2) => t1.Item2 > t2.Item2);
}
}
2022-05-29 23:02:28 +08:00
}