Skip to content

Commit

Permalink
Refactor CharacterStats set stats
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Feb 16, 2024
1 parent a5e7333 commit 2150195
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
9 changes: 1 addition & 8 deletions Lib9c/Battle/CPHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ public static int TotalCP(
{
// Prepare CharacterStats for calculate collection Stats
var characterStats = new CharacterStats(row, level);
characterStats.SetEquipments(equipments, new EquipmentItemSetEffectSheet());
characterStats.SetCostumeStat(costumes, costumeStatSheet);
foreach (var runeOption in runeOptions)
{
characterStats.AddRuneStat(runeOption);
}

characterStats.SetCollections(collectionStatModifiers);
characterStats.ConfigureStats(equipments, costumes, runeOptions, costumeStatSheet, collectionStatModifiers);
foreach (var (statType, value) in characterStats.CollectionStats.GetStats())
{
collectionCp += GetStatCP(statType, value);
Expand Down
65 changes: 49 additions & 16 deletions Lib9c/Model/Stat/CharacterStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public CharacterStats(
{
_initialStatModifiers.AddRange(initialStatModifiers);
}

SetStats(level);
}

Expand Down Expand Up @@ -183,7 +184,8 @@ public CharacterStats SetEquipments(

// set effects.
var setEffectRows = sheet.GetSetEffectRows(equipments);
foreach (var statModifier in setEffectRows.SelectMany(row => row.StatModifiers.Values))
foreach (var statModifier in setEffectRows.SelectMany(row =>
row.StatModifiers.Values))
{
_equipmentStatModifiers.Add(statModifier);
}
Expand Down Expand Up @@ -263,7 +265,8 @@ public CharacterStats SetRunes(IEnumerable<StatModifier> value, bool updateImmed
/// <param name="value"></param>
/// <param name="updateImmediate"></param>
/// <returns></returns>
public CharacterStats SetBuffs(IEnumerable<Buff.StatBuff> value, bool updateImmediate = true)
public CharacterStats SetBuffs(IEnumerable<Buff.StatBuff> value,
bool updateImmediate = true)
{
_buffStatModifiers.Clear();
if (!(value is null))
Expand Down Expand Up @@ -307,7 +310,8 @@ public CharacterStats SetCollections(IEnumerable<StatModifier> statModifiers,
{
var statType = group.Key;
var sum = group.Sum(g => g.Value);
_collectionStatModifiers.Add(new StatModifier(statType, StatModifier.OperationType.Percentage, sum));
_collectionStatModifiers.Add(new StatModifier(statType,
StatModifier.OperationType.Percentage, sum));
}

if (updateImmediate)
Expand Down Expand Up @@ -361,9 +365,10 @@ private void SetCostumes(IEnumerable<Costume> costumes, CostumeStatSheet costume
var stat = costumeStatSheet.OrderedList
.Where(r => r.CostumeId == costume.Id)
.Select(row => new StatModifier(row.StatType, StatModifier.OperationType.Add,
(int)row.Stat));
(int) row.Stat));
statModifiers.AddRange(stat);
}

SetCostume(statModifiers);
}

Expand Down Expand Up @@ -455,23 +460,28 @@ private void UpdateBuffStats()
{
var statType = group.Key;
var sum = group.Sum(g => g.Value);
buffModifiers.Add(new StatModifier(statType, StatModifier.OperationType.Percentage, sum));
buffModifiers.Add(new StatModifier(statType, StatModifier.OperationType.Percentage,
sum));
}

_buffStats.Set(buffModifiers, _baseStats, _equipmentStats, _consumableStats, _runeStats, _costumeStats, _collectionStats);
_buffStats.Set(buffModifiers, _baseStats, _equipmentStats, _consumableStats, _runeStats,
_costumeStats, _collectionStats);
UpdateTotalStats();
}

private void UpdateCostumeStats()
{
_costumeStats.Set(_costumeStatModifiers, _baseStats, _equipmentStats, _consumableStats, _runeStats);
_costumeStats.Set(_costumeStatModifiers, _baseStats, _equipmentStats, _consumableStats,
_runeStats);
UpdateCollectionStats();
}

private void UpdateCollectionStats()
{
_collectionStats.Set(_collectionStatModifiers, _baseStats, _equipmentStats, _consumableStats, _runeStats, _costumeStats);
Set(StatWithoutBuffs, _baseStats, _equipmentStats, _consumableStats, _runeStats, _costumeStats, _collectionStats);
_collectionStats.Set(_collectionStatModifiers, _baseStats, _equipmentStats,
_consumableStats, _runeStats, _costumeStats);
Set(StatWithoutBuffs, _baseStats, _equipmentStats, _consumableStats, _runeStats,
_costumeStats, _collectionStats);
foreach (var stat in StatWithoutBuffs.GetDecimalStats(false))
{
if (!LegacyDecimalStatTypes.Contains(stat.StatType))
Expand All @@ -491,7 +501,8 @@ private void UpdateCollectionStats()

private void UpdateTotalStats()
{
Set(_statMap, _baseStats, _equipmentStats, _consumableStats, _runeStats, _costumeStats, _collectionStats, _buffStats);
Set(_statMap, _baseStats, _equipmentStats, _consumableStats, _runeStats, _costumeStats,
_collectionStats, _buffStats);

foreach (var stat in _statMap.GetDecimalStats(false))
{
Expand Down Expand Up @@ -519,12 +530,14 @@ public override object Clone()
return new CharacterStats(this);
}

public IEnumerable<(StatType statType, long baseValue)> GetBaseStats(bool ignoreZero = false)
public IEnumerable<(StatType statType, long baseValue)> GetBaseStats(
bool ignoreZero = false)
{
return _baseStats.GetStats(ignoreZero);
}

public IEnumerable<(StatType statType, long additionalValue)> GetAdditionalStats(bool ignoreZero = false)
public IEnumerable<(StatType statType, long additionalValue)> GetAdditionalStats(
bool ignoreZero = false)
{
var baseStats = _baseStats.GetStats();
foreach (var (statType, stat) in baseStats)
Expand All @@ -537,8 +550,9 @@ public override object Clone()
}
}

public IEnumerable<(StatType statType, long baseValue, long additionalValue)> GetBaseAndAdditionalStats(
bool ignoreZero = false)
public IEnumerable<(StatType statType, long baseValue, long additionalValue)>
GetBaseAndAdditionalStats(
bool ignoreZero = false)
{
var additionalStats = GetAdditionalStats();
foreach (var (statType, additionalStat) in additionalStats)
Expand All @@ -552,17 +566,20 @@ public override object Clone()
}
}

public void SetCostumeStat(IReadOnlyCollection<Costume> costumes, CostumeStatSheet costumeStatSheet)
public void SetCostumeStat(IReadOnlyCollection<Costume> costumes,
CostumeStatSheet costumeStatSheet)
{
var statModifiers = new List<StatModifier>();
foreach (var itemId in costumes.Select(costume => costume.Id))
{
statModifiers.AddRange(
costumeStatSheet.OrderedList
.Where(r => r.CostumeId == itemId)
.Select(row => new StatModifier(row.StatType, StatModifier.OperationType.Add, (int) row.Stat))
.Select(row => new StatModifier(row.StatType,
StatModifier.OperationType.Add, (int) row.Stat))
);
}

SetCostume(statModifiers);
}

Expand All @@ -577,5 +594,21 @@ public void AddRuneStat(RuneOptionSheet.Row.RuneOptionInfo optionInfo)
x.stat.BaseValueAsLong)));
AddRune(statModifiers);
}

public void ConfigureStats(
IReadOnlyCollection<Equipment> equipments,
IReadOnlyCollection<Costume> costumes,
IReadOnlyCollection<RuneOptionSheet.Row.RuneOptionInfo> runeOptions,
CostumeStatSheet costumeStatSheet, List<StatModifier> collectionStatModifiers)
{
SetEquipments(equipments, new EquipmentItemSetEffectSheet());
SetCostumeStat(costumes, costumeStatSheet);
foreach (var runeOption in runeOptions)
{
AddRuneStat(runeOption);
}

SetCollections(collectionStatModifiers);
}
}
}

0 comments on commit 2150195

Please sign in to comment.