Skip to content

Commit

Permalink
Merge pull request #2419 from planetarium/bugfix/get-collection-states
Browse files Browse the repository at this point in the history
Refactoring CollectionModule
  • Loading branch information
ipdae authored Feb 26, 2024
2 parents 8084d81 + acc1165 commit fe39c9d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 40 deletions.
19 changes: 4 additions & 15 deletions .Lib9c.Tests/Module/CollectionModuleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public void CollectionStates()
var address2 = new PrivateKey().Address;
var addresses = new[] { address, address2 };
var result = states.GetCollectionStates(addresses);
Assert.Equal(addresses.Length, result.Count);
Assert.All(result, Assert.Null);
Assert.Empty(result);

var state = new CollectionState
{
Expand All @@ -54,19 +53,9 @@ public void CollectionStates()
};
states = states.SetCollectionState(address, state);
result = states.GetCollectionStates(addresses);
for (int i = 0; i < addresses.Length; i++)
{
switch (i)
{
case 0:
Assert.NotNull(result[i]);
Assert.Equal(state.Ids, result[i].Ids);
break;
case 1:
Assert.Null(result[i]);
break;
}
}
Assert.Contains(address, result.Keys);
Assert.Equal(state.Ids, result[address].Ids);
Assert.DoesNotContain(address2, result.Keys);
}
}
}
24 changes: 10 additions & 14 deletions Lib9c/Action/BattleArena.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public override IWorld Execute(IActionContext context)

var collectionStates =
states.GetCollectionStates(new[]{ myAvatarAddress, enemyAvatarAddress });
var collectionExist = collectionStates.Any(r => r is not null);
var collectionExist = collectionStates.Count > 0;
var sheetTypes = new List<Type>
{
typeof(ArenaSheet),
Expand Down Expand Up @@ -383,23 +383,19 @@ public override IWorld Execute(IActionContext context)
var defeatCount = 0;
var rewards = new List<ItemBase>();
var random = context.GetRandom();
var modifiers = new List<List<StatModifier>>
var modifiers = new Dictionary<Address, List<StatModifier>>
{
new(),
new(),
[myAvatarAddress] = new(),
[enemyAvatarAddress] = new(),
};
if (collectionExist)
{
var collectionSheet = sheets.GetSheet<CollectionSheet>();
for (int i = 0; i < collectionStates.Count; i++)
#pragma warning disable LAA1002
foreach (var (address, state) in collectionStates)
#pragma warning restore LAA1002
{
var state = collectionStates[i];
if (state is null)
{
continue;
}

var modifier = modifiers[i];
var modifier = modifiers[address];
foreach (var collectionId in state.Ids)
{
modifier.AddRange(collectionSheet[collectionId].StatModifiers);
Expand All @@ -413,8 +409,8 @@ public override IWorld Execute(IActionContext context)
myArenaPlayerDigest,
enemyArenaPlayerDigest,
arenaSheets,
modifiers[0],
modifiers[1],
modifiers[myAvatarAddress],
modifiers[enemyAvatarAddress],
true);
if (log.Result.Equals(ArenaLog.ArenaResult.Win))
{
Expand Down
26 changes: 15 additions & 11 deletions Lib9c/Module/CollectionModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class CollectionModule
/// <exception cref="InvalidCastException">Thrown when the serialized Collection state is not in the correct format.</exception>
public static CollectionState GetCollectionState(this IWorldState worldState, Address address)
{
var serializedCollection = worldState.GetResolvedState(address, Addresses.Collection);
var serializedCollection = worldState.GetAccountState(Addresses.Collection).GetState(address);
if (serializedCollection is null)
{
var msg = $"No Collection state ({address.ToHex()})";
Expand Down Expand Up @@ -92,21 +92,25 @@ public static bool TryGetCollectionState(this IWorldState worldState, Address ad
/// </summary>
/// <param name="worldState">The world state used to retrieve the collection states.</param>
/// <param name="addresses">The list of addresses to retrieve the collection states for.</param>
/// <returns>A list of CollectionState objects representing the collection states for the given addresses,
/// or null for addresses that do not have a collection state.</returns>
public static List<CollectionState> GetCollectionStates(this IWorldState worldState,
/// <returns>A dictionary of Address and CollectionState pairs representing the collection states
/// for the given addresses,
/// or an empty dictionary for addresses that do not have a collection state.</returns>
public static Dictionary<Address, CollectionState> GetCollectionStates(
this IWorldState worldState,
IReadOnlyList<Address> addresses)
{
var result = new List<CollectionState>();
foreach (var serialized in worldState.GetAccountState(Addresses.Collection).GetStates(addresses))
var result = new Dictionary<Address, CollectionState>();
IReadOnlyList<IValue> values =
worldState
.GetAccountState(Addresses.Collection)
.GetStates(addresses);
for (int i = 0; i < addresses.Count; i++)
{
var serialized = values[i];
var address = addresses[i];
if (serialized is List bencoded)
{
result.Add(new CollectionState(bencoded));
}
else
{
result.Add(null);
result.TryAdd(address, new CollectionState(bencoded));
}
}

Expand Down

0 comments on commit fe39c9d

Please sign in to comment.