Skip to content

Commit

Permalink
Use GetStates in rune, item slot
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Sep 10, 2024
1 parent 41f1d3c commit 7c34c64
Showing 1 changed file with 55 additions and 13 deletions.
68 changes: 55 additions & 13 deletions ArenaService/ArenaService/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,11 @@ public void OnPreloadEnd()
return participants;
}

/// <summary>
/// <summary>
/// Retrieves the avatar addresses and scores with ranks for a given list of avatar addresses, current round data, and world state.
/// </summary>
/// <param name="avatarAddrList">The list of avatar addresses.</param>
/// <param name="currentRoundData">The current round data.</param>
/// <param name="worldState">The world state.</param>
/// <returns>The list of avatar addresses, scores, and ranks.</returns>
public async Task<List<ArenaScoreAndRank>> AvatarAddrAndScoresWithRank(Block block, List<Address> avatarAddrList, ArenaSheet.RoundData currentRoundData)
{
Expand Down Expand Up @@ -343,22 +342,15 @@ public async Task<List<ArenaParticipant>> GetArenaParticipants(Block block, List
collectionSheetExist = false;
}

var itemSlotStates = await GetItemSlotStates(block, avatarAddrList);
var runeSlotStates = await GetRuneSlotStates(block, avatarAddrList);
var tasks = avatarAddrAndScoresWithRank.Select(async tuple =>
{
var avatarAddr = tuple.AvatarAddr;
var runeStates = await GetRuneState(block, avatarAddr, runeListSheet);
var avatar = await GetAvatarState(block, avatarAddr);
var itemSlotState =
await GetLegacyState(block, ItemSlotState.DeriveAddress(avatarAddr, BattleType.Arena)) is
List itemSlotList
? new ItemSlotState(itemSlotList)
: new ItemSlotState(BattleType.Arena);

var runeSlotState =
await GetLegacyState(block, RuneSlotState.DeriveAddress(avatarAddr, BattleType.Arena)) is
List runeSlotList
? new RuneSlotState(runeSlotList)
: new RuneSlotState(BattleType.Arena);
var itemSlotState = itemSlotStates[avatarAddr];
var runeSlotState = runeSlotStates[avatarAddr];

var equippedRuneStates = new List<RuneState>();
foreach (var runeId in runeSlotState.GetRuneSlot().Select(slot => slot.RuneId))
Expand Down Expand Up @@ -539,6 +531,56 @@ public async Task<AvatarState> GetAvatarState(Block block, Address avatarAddress
throw new Exception();
}

/// <summary>
/// Retrieves the item slot states for the given avatar addresses from the world state.
/// </summary>
/// <param name="block">The world state used to retrieve the collection states.</param>
/// <param name="avatarAddresses">The list of addresses to retrieve the item slot states for.</param>
/// <returns>A dictionary of Address and <see cref="ItemSlotState"/> pairs representing the item slot states
/// for the given addresses.</returns>
public async Task<Dictionary<Address, ItemSlotState>> GetItemSlotStates(Block block, IReadOnlyList<Address> avatarAddresses)
{
var result = new Dictionary<Address, ItemSlotState>();
var slotAddresses = avatarAddresses.Select(a => ItemSlotState.DeriveAddress(a, BattleType.Arena)).ToList();
var values = await GetStates(block, ReservedAddresses.LegacyAccount, slotAddresses);
foreach (var address in avatarAddresses)
{
var slotAddress = ItemSlotState.DeriveAddress(address, BattleType.Arena);
var serialized = values[slotAddress];
var itemSlotState = serialized is List bencoded
? new ItemSlotState(bencoded)
: new ItemSlotState(BattleType.Arena);
result.TryAdd(address, itemSlotState);
}

return result;
}

/// <summary>
/// Retrieves the rune slot states for the given avatar addresses from the world state.
/// </summary>
/// <param name="block">The world state used to retrieve the collection states.</param>
/// <param name="avatarAddresses">The list of avatar addresses to retrieve the rune slot states for.</param>
/// <returns>A dictionary of Address and <see cref="RuneSlotState"/> pairs representing the rune slot states
/// for the given addresses.</returns>
public async Task<Dictionary<Address, RuneSlotState>> GetRuneSlotStates(Block block, IReadOnlyList<Address> avatarAddresses)
{
var result = new Dictionary<Address, RuneSlotState>();
var slotAddresses = avatarAddresses.Select(a => RuneSlotState.DeriveAddress(a, BattleType.Arena)).ToList();
var values = await GetStates(block, ReservedAddresses.LegacyAccount, slotAddresses);
foreach (var address in avatarAddresses)
{
var slotAddress = RuneSlotState.DeriveAddress(address, BattleType.Arena);
var serialized = values[slotAddress];
var runeSlotState = serialized is List bencoded
? new RuneSlotState(bencoded)
: new RuneSlotState(BattleType.Arena);
result.TryAdd(address, runeSlotState);
}

return result;
}

public static List<RuneOptionSheet.Row.RuneOptionInfo> GetRuneOptions(
List<RuneState> runeStates,
RuneOptionSheet sheet)
Expand Down

0 comments on commit 7c34c64

Please sign in to comment.