Skip to content

Commit

Permalink
Merge pull request #3085 from eugene-doobu/feature/remove-grade-key
Browse files Browse the repository at this point in the history
remove grade key in synthesize
  • Loading branch information
eugene-doobu authored Dec 13, 2024
2 parents 8e39ec6 + 74342ce commit af4013a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 40 deletions.
8 changes: 8 additions & 0 deletions .Lib9c.Tests/Action/SynthesizeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ public void ExecuteMultiple(Grade grade, ItemSubType itemSubType)
if (result.IsSuccess)
{
Assert.Equal((int)grade + 1, result.ItemBase.Grade);

var weightSheet = TableSheets.SynthesizeWeightSheet;
var weightRow = weightSheet.Values.FirstOrDefault(r => r.ItemId == result.ItemBase.Id);

if (weightRow != null)
{
Assert.True(weightRow.Weight != 0);
}
}
else
{
Expand Down
14 changes: 7 additions & 7 deletions Lib9c/Helper/SynthesizeSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ private static ItemBase GetRandomCostume(Grade grade, bool isSuccess, ItemSubTyp
throw new InvalidOperationException($"No available items to synthesize for grade {grade} and subtype {itemSubType}");
}

var randomValue = GetRandomValueForItem(grade, synthesizeResultPool, weightSheet, random, out var itemWeights);
var randomValue = GetRandomValueForItem(synthesizeResultPool, weightSheet, random, out var itemWeights);
var cumulativeWeight = 0;
foreach (var (itemId, weight) in itemWeights)
{
Expand Down Expand Up @@ -440,7 +440,7 @@ private static ItemBase GetRandomEquipment(
throw new InvalidOperationException($"No available items to synthesize for grade {grade} and subtype {itemSubType}");
}

var randomValue = GetRandomValueForItem(grade, synthesizeResultPool, weightSheet, random, out var itemWeights);
var randomValue = GetRandomValueForItem(synthesizeResultPool, weightSheet, random, out var itemWeights);
var cumulativeWeight = 0;
foreach (var (itemId, weight) in itemWeights)
{
Expand Down Expand Up @@ -506,14 +506,14 @@ private static ItemBase GetRandomEquipment(
throw new InvalidOperationException("Failed to select a synthesized item.");
}

private static int GetRandomValueForItem(Grade grade, HashSet<int> synthesizeResultPool, SynthesizeWeightSheet synthesizeWeightSheet,
private static int GetRandomValueForItem(HashSet<int> synthesizeResultPool, SynthesizeWeightSheet synthesizeWeightSheet,
IRandom random, out List<(int ItemId, int Weight)> itemWeights)
{
var totalWeight = 0;
itemWeights = new List<(int ItemId, int Weight)>();
foreach (var itemId in synthesizeResultPool)
{
var weight = GetWeight(grade, itemId, synthesizeWeightSheet);
var weight = GetWeight(itemId, synthesizeWeightSheet);
itemWeights.Add((itemId, weight));
totalWeight += weight;
}
Expand Down Expand Up @@ -631,11 +631,11 @@ public static Grade GetUpgradeGrade(Grade grade ,ItemSubType subType, EquipmentI
/// <param name="itemId">item id of material item</param>
/// <param name="sheet">SynthesizeWeightSheet to use</param>
/// <returns>weight of the item that can be obtained by synthesizing the item</returns>
public static int GetWeight(Grade grade, int itemId, SynthesizeWeightSheet sheet)
public static int GetWeight(int itemId, SynthesizeWeightSheet sheet)
{
var defaultWeight = SynthesizeWeightSheet.DefaultWeight;
var gradeRow = sheet.Values.FirstOrDefault(r => r.GradeId == (int)grade);
return gradeRow == null ? defaultWeight : gradeRow.WeightDict.GetValueOrDefault(itemId, defaultWeight);
var gradeRow = sheet.Values.FirstOrDefault(r => r.Key == itemId);
return gradeRow?.Weight ?? defaultWeight;
}

// TODO: move to ItemExtensions
Expand Down
13 changes: 7 additions & 6 deletions Lib9c/TableCSV/Item/SynthesizeWeightSheet.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
grade_id,item_id,weight
item_id,weight
_default value for weight is 10000.
3,40100001,3000
3,40100015,1000
3,40100016,1000
3,40100019,1000
5,49900027,40000
40100001,3000
40100015,1000
40100016,1000
40100017,0
40100019,1000
49900027,40000
32 changes: 5 additions & 27 deletions Lib9c/TableData/Item/SynthesizeWeightSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,20 @@ public class SynthesizeWeightSheet : Sheet<int, SynthesizeWeightSheet.Row>
[Serializable]
public class Row : SheetRow<int>
{
public override int Key => GradeId;
public override int Key => ItemId;

public int GradeId { get; private set; }

public Dictionary<int, int> WeightDict { get; private set; }
public int ItemId { get; private set; }
public int Weight { get; private set; }

public override void Set(IReadOnlyList<string> fields)
{
GradeId = ParseInt(fields[0]);

WeightDict = new Dictionary<int, int>();
var itemId = ParseInt(fields[1]);
var weight = ParseInt(fields[2], DefaultWeight);
WeightDict.Add(itemId, weight);
ItemId = ParseInt(fields[0]);
Weight = TryParseInt(fields[1], out var weight) ? weight : DefaultWeight;
}
}

public SynthesizeWeightSheet() : base(nameof(SynthesizeWeightSheet))
{
}

protected override void AddRow(int key, Row value)
{
if (!TryGetValue(key, out var row))
{
Add(key, value);

return;
}

if (!value.WeightDict.Any())
{
return;
}

row.WeightDict.TryAdd(value.WeightDict.First().Key, value.WeightDict.First().Value);
}
}
}

0 comments on commit af4013a

Please sign in to comment.