Skip to content

Commit

Permalink
Merge pull request #2396 from planetarium/improve/refactor-set-stats
Browse files Browse the repository at this point in the history
Refactor configure character stats
  • Loading branch information
ipdae authored Feb 16, 2024
2 parents 1649b20 + 2150195 commit 5b9758a
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 56 deletions.
1 change: 1 addition & 0 deletions .Lib9c.Tests/Model/Skill/BuffFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void GetBuffs_Arena(bool setExtraValueBuffBeforeGetBuffs)
digest,
arenaSheets,
simulator.HpModifier,
new List<StatModifier>(),
setExtraValueBuffBeforeGetBuffs: setExtraValueBuffBeforeGetBuffs);
var buffs = BuffFactory.GetBuffs(
challenger.Stats,
Expand Down
20 changes: 2 additions & 18 deletions Lib9c/Arena/ArenaSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,33 +121,17 @@ private static SimplePriorityQueue<ArenaCharacter, decimal> SpawnPlayers(
challengerDigest,
simulatorSheets,
simulator.HpModifier,
challengerCollectionModifiers,
setExtraValueBuffBeforeGetBuffs: setExtraValueBuffBeforeGetBuffs);
if (challengerDigest.Runes != null)
{
challenger.SetRune(
challengerDigest.Runes,
simulatorSheets.RuneOptionSheet,
simulatorSheets.SkillSheet);
}

challenger.Stats.SetCollections(challengerCollectionModifiers);

var enemy = new ArenaCharacter(
simulator,
enemyDigest,
simulatorSheets,
simulator.HpModifier,
enemyCollectionModifiers,
isEnemy: true,
setExtraValueBuffBeforeGetBuffs: setExtraValueBuffBeforeGetBuffs);
if (enemyDigest.Runes != null)
{
enemy.SetRune(
enemyDigest.Runes,
simulatorSheets.RuneOptionSheet,
simulatorSheets.SkillSheet);
}

enemy.Stats.SetCollections(enemyCollectionModifiers);

challenger.Spawn(enemy);
enemy.Spawn(challenger);
Expand Down
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
8 changes: 1 addition & 7 deletions Lib9c/Battle/RaidSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ public RaidSimulator(
CostumeStatSheet costumeStatSheet,
List<StatModifier> collectionModifiers) : base(random, avatarState, foods, simulatorSheets)
{
Player.SetCostumeStat(costumeStatSheet);
if (runeStates != null)
{
Player.SetRune(runeStates, simulatorSheets.RuneOptionSheet, simulatorSheets.SkillSheet);
}

Player.Stats.SetCollections(collectionModifiers);
Player.ConfigureStats(costumeStatSheet, runeStates, simulatorSheets.RuneOptionSheet, simulatorSheets.SkillSheet, collectionModifiers);

BossId = bossId;
_waves = new List<RaidBoss>();
Expand Down
8 changes: 1 addition & 7 deletions Lib9c/Battle/StageSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ public StageSimulator(IRandom random,
simulatorSheets,
logEvent)
{
Player.SetCostumeStat(costumeStatSheet);
if (runeStates != null)
{
Player.SetRune(runeStates, simulatorSheets.RuneOptionSheet, simulatorSheets.SkillSheet);
}

Player.Stats.SetCollections(collectionModifiers);
Player.ConfigureStats(costumeStatSheet, runeStates, simulatorSheets.RuneOptionSheet, simulatorSheets.SkillSheet, collectionModifiers);

_waves = new List<Wave>();
_waveRewards = waveRewards;
Expand Down
10 changes: 10 additions & 0 deletions Lib9c/Model/Character/ArenaCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public ArenaCharacter(
ArenaPlayerDigest digest,
ArenaSimulatorSheets sheets,
long hpModifier,
List<StatModifier> collectionModifiers,
bool isEnemy = false,
bool setExtraValueBuffBeforeGetBuffs = false)
{
Expand Down Expand Up @@ -186,6 +187,15 @@ public ArenaCharacter(
_skills = GetSkills(digest.Equipments, sheets.SkillSheet);
_attackCountMax = AttackCountHelper.GetCountMax(digest.Level);
ResetCurrentHP();
if (digest.Runes != null)
{
SetRune(
digest.Runes,
sheets.RuneOptionSheet,
sheets.SkillSheet);
}

Stats.SetCollections(collectionModifiers);
}

private ArenaCharacter(ArenaCharacter value)
Expand Down
11 changes: 11 additions & 0 deletions Lib9c/Model/Character/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,17 @@ public void SetRune(
}
}

public void ConfigureStats(CostumeStatSheet costumeStatSheet, List<RuneState> runeStates, RuneOptionSheet runeOptionSheet, SkillSheet skillSheet, List<StatModifier> collectionModifiers)
{
SetCostumeStat(costumeStatSheet);
if (runeStates != null)
{
SetRune(runeStates, runeOptionSheet, skillSheet);
}

Stats.SetCollections(collectionModifiers);
}

[Obsolete("Use SetRune")]
public void SetRuneV1(
List<RuneState> runes,
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 5b9758a

Please sign in to comment.