CTT/Server/Hotfix/Game/Helper/DropHelper.cs

102 lines
4.6 KiB
C#
Executable File

using System;
using System.Collections.Generic;
using Cal.DataTable;
namespace ET
{
public static class DropHelper
{
public static void Drop(Unit unit, PlayerData playerData, BattleType battleType, int parentId,
UnOrderMultiMap<long, (int, int)> rewordMap, float dropProperbility, string getSource, bool isLock =false ,List<RewardItem> list=null)
{
try
{
if (parentId == 0) return ;
Parentset parentSet = ParentsetCategory.Instance.Get(parentId);
foreach (Parentset.Subset subSet in parentSet.SubsetArr)
{
int sonSetId = playerData.UpdateDrop(subSet._Id);
SonSet sonSet = SonSetCategory.Instance.Get(sonSetId);
(int itemId, int itemCount) = GetItemFormSet(sonSet.DropArr, dropProperbility, false,out bool sonSetIsLock);
if (itemId == 0) continue;
if (!isLock)
{
isLock = sonSetIsLock;
}
BagHelper.AddItem(unit, itemId, itemCount, isLock, getSource: getSource);
TaskHelper.UpdateTask(unit, itemId).Coroutine();
list?.Add(new RewardItem() { Id = itemId, Count = itemCount, ItemType = BagHelper.GetItemType(itemId) });
rewordMap?.Add(unit.Id, (itemId, itemCount));
try
{
switch (itemId)
{
case BagHelper.CoinId:
{
string sources = battleType switch
{
BattleType.MainStory => StatisticsTypes.CoinSources_MainStory,
BattleType.TrialCopy => StatisticsTypes.CoinSources_TrialCopy,
BattleType.Boss => StatisticsTypes.CoinSources_Boss,
BattleType.IdleBattle => StatisticsTypes.CoinSources_IdleBattle,
_ => null,
};
if (sources != null)
StatisticsHelper.AddInfo(unit.Id, StatisticComponent.StatisticType.Coin, sources, (long) itemCount);
break;
}
case BagHelper.GemId:
{
string sources = battleType switch
{
BattleType.MainStory => StatisticsTypes.GemSources_MainStory,
BattleType.TrialCopy => StatisticsTypes.GemSources_TrialCopy,
BattleType.Boss => StatisticsTypes.GemSources_Boss,
_ => null,
};
if (sources != null)
StatisticsHelper.AddInfo(unit.Id, StatisticComponent.StatisticType.Gem, sources, (long) itemCount);
break;
}
}
}
catch (Exception)
{
// ignored
}
if (AppConfig.inst.isTest)
Log.Info($"【{UserComponent.Instance.Get(unit.Id)?.NickName}({unit.Id})】 掉落了【{BagHelper.GetName(itemId)}】X {itemCount}");
}
}
catch (Exception e)
{
Log.Error(e);
}
}
private static (int, int) GetItemFormSet(IReadOnlyList<SonSet.Drop> arr, float damagePercent, bool isKilled,out bool isLock)
{
using ListComponent<int> listComponent = ListComponent<int>.Create();
List<int> weightList = listComponent.List;
foreach (SonSet.Drop drop in arr)
{
if (drop._Id == 0)
{
float weight = isKilled? 0.5f * drop.Weight : drop.Weight;
weight /= (damagePercent * damagePercent);
weightList.Add(MathHelper.RoundToInt(weight));
}
else
weightList.Add(drop.Weight);
}
int index = MathHelper.GetProbabilityIndexByWeight(weightList);
SonSet.Drop item = arr[index];
isLock = item.IsLock;
return (item._Id, RandomHelper.RandomNumber(item.MinCount, item.MaxCount + 1));
}
}
}