Skip to content

Commit

Permalink
Merge pull request #56 from moreal/avatar-ap
Browse files Browse the repository at this point in the history
Response avatar's actionPoint too
  • Loading branch information
moreal authored May 14, 2024
2 parents 256bf5e + abb0fa2 commit 9883016
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Mimir.Worker/Mimir.Worker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Lib9c" Version="1.8.0-dev.c6a17ceb8856f4c7c6bdc7a7288be58479d9d006" />
<PackageReference Include="Libplanet" Version="4.1.0-dev.20242745721" />
<PackageReference Include="Lib9c" Version="1.8.0-dev.c5644aff871034a92042833edca03b85c2a24a1c" />
<PackageReference Include="Libplanet" Version="4.4.1" />
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.25.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
5 changes: 5 additions & 0 deletions Mimir.Worker/Scrapper/StateGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public async Task<AvatarState> GetAvatarState(Address avatarAddress)
throw new ArgumentException($"Unsupported state type for address: {avatarAddress}");
}

if (await GetStateWithLegacyAccount(avatarAddress, Addresses.ActionPoint) is Integer actionPoint)
{
avatarState.actionPoint = actionPoint;
}

return avatarState;
}

Expand Down
6 changes: 3 additions & 3 deletions Mimir/Arena/ArenaBulkSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public ArenaLog SimulateBattle(
}
}
}

var arenaLog = simulator.Simulate(
new ArenaPlayerDigest(myAvatar.AvatarState, myAvatar.ItemSlotState.Equipments, myAvatar.ItemSlotState.Costumes, myAvatar.RuneStates),
new ArenaPlayerDigest(enemyAvatar.AvatarState, enemyAvatar.ItemSlotState.Equipments, enemyAvatar.ItemSlotState.Costumes, enemyAvatar.RuneStates),
new ArenaPlayerDigest(myAvatar.AvatarState, myAvatar.ItemSlotState.Equipments, myAvatar.ItemSlotState.Costumes, myAvatar.RuneStates, myAvatar.RuneSlotState),
new ArenaPlayerDigest(enemyAvatar.AvatarState, enemyAvatar.ItemSlotState.Equipments, enemyAvatar.ItemSlotState.Costumes, enemyAvatar.RuneStates, enemyAvatar.RuneSlotState),
simulatorSheets,
modifiers[myAvatar.AvatarState.address],
modifiers[enemyAvatar.AvatarState.address],
Expand Down
6 changes: 4 additions & 2 deletions Mimir/Arena/AvatarStatesForArena.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ public record AvatarStatesForArena
{
public AvatarState AvatarState { get; private set; }
public ItemSlotState ItemSlotState { get; private set; }
public List<RuneState> RuneStates { get; private set; }
public AllRuneState RuneStates { get; private set; }
public RuneSlotState RuneSlotState { get; private set; }

public AvatarStatesForArena(AvatarState avatarState, ItemSlotState itemSlotState, List<RuneState> runeStates)
public AvatarStatesForArena(AvatarState avatarState, ItemSlotState itemSlotState, AllRuneState runeStates, RuneSlotState runeSlotState)
{
AvatarState = avatarState;
ItemSlotState = itemSlotState;
RuneStates = runeStates;
RuneSlotState = runeSlotState;
}
}
2 changes: 1 addition & 1 deletion Mimir/Controllers/AgentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ IStateService stateService
}

return new AvatarsResponse(
avatars.Select(e => new Avatar(e.address.ToString(), e.name, e.level)).ToList()
avatars.Select(e => new Avatar(e.address.ToString(), e.name, e.level, e.actionPoint)).ToList()
);
}
}
13 changes: 10 additions & 3 deletions Mimir/Controllers/ArenaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using Mimir.Repositories;
using Mimir.Services;
using Mimir.Util;
using Nekoyume.Model.EnumType;
using Nekoyume.TableData;
using Nekoyume.TableData.Rune;

namespace Mimir.Controllers;

Expand Down Expand Up @@ -69,17 +71,20 @@ IStateService stateService
var myAvatarState = await stateGetter.GetAvatarStateAsync(myAvatarAddress);
var myAvatarItemSlotState = await stateGetter.GetItemSlotStateAsync(myAvatarAddress);
var myAvatarRuneStates = await stateGetter.GetRuneStatesAsync(myAvatarAddress);
var myAvatarRuneSlotState = await stateGetter.GetRuneSlotStateAsync(myAvatarAddress, BattleType.Arena);
var enemyAvatarState = await stateGetter.GetAvatarStateAsync(enemyAvatarAddress);
var enemyAvatarItemSlotState = await stateGetter.GetItemSlotStateAsync(enemyAvatarAddress);
var enemyAvatarRuneStates = await stateGetter.GetRuneStatesAsync(enemyAvatarAddress);
var enemyAvatarRuneSlotState = await stateGetter.GetRuneSlotStateAsync(enemyAvatarAddress, BattleType.Arena);

var bulkSimulator = new ArenaBulkSimulator();
var result = await bulkSimulator.BulkSimulate(
new AvatarStatesForArena(myAvatarState, myAvatarItemSlotState, myAvatarRuneStates),
new AvatarStatesForArena(myAvatarState, myAvatarItemSlotState, myAvatarRuneStates, myAvatarRuneSlotState),
new AvatarStatesForArena(
enemyAvatarState,
enemyAvatarItemSlotState,
enemyAvatarRuneStates
enemyAvatarRuneStates,
enemyAvatarRuneSlotState
),
new ArenaSimulatorSheets(
await stateGetter.GetSheetAsync<MaterialItemSheet>(),
Expand All @@ -93,7 +98,9 @@ await stateGetter.GetSheetAsync<CharacterLevelSheet>(),
await stateGetter.GetSheetAsync<EquipmentItemSetEffectSheet>(),
await stateGetter.GetSheetAsync<CostumeStatSheet>(),
await stateGetter.GetSheetAsync<WeeklyArenaRewardSheet>(),
await stateGetter.GetSheetAsync<RuneOptionSheet>()
await stateGetter.GetSheetAsync<RuneOptionSheet>(),
await stateGetter.GetSheetAsync<RuneListSheet>(),
await stateGetter.GetSheetAsync<RuneLevelBonusSheet>()
),
await stateGetter.GetCollectionStatesAsync([myAvatarAddress, enemyAvatarAddress]),
await stateGetter.GetSheetAsync<CollectionSheet>(),
Expand Down
4 changes: 2 additions & 2 deletions Mimir/Mimir.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.25.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Lib9c" Version="1.8.0-dev.c6a17ceb8856f4c7c6bdc7a7288be58479d9d006" />
<PackageReference Include="Libplanet" Version="4.1.0-dev.20242745721" />
<PackageReference Include="Lib9c" Version="1.8.0-dev.c5644aff871034a92042833edca03b85c2a24a1c" />
<PackageReference Include="Libplanet" Version="4.4.1" />
<PackageReference Include="StrawberryShake" Version="13.8.1" />
<PackageReference Include="StrawberryShake.Transport.Http" Version="13.8.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.3.1" />
Expand Down
3 changes: 2 additions & 1 deletion Mimir/Models/Agent/Avatar.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace Mimir.Models.Agent;

public class Avatar(string avatarAddress, string avatarName, int level)
public class Avatar(string avatarAddress, string avatarName, int level, int actionPoint)
{
public string AvatarAddress { get; set; } = avatarAddress;
public string AvatarName { get; set; } = avatarName;
public int Level { get; set; } = level;
public int ActionPoint { get; private set; } = actionPoint;
}
3 changes: 2 additions & 1 deletion Mimir/Repositories/ArenaRankingRespository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ private async Task<ArenaRanking> BuildArenaRankingFromDocument(BsonDocument docu
var avatar = new Avatar(
document["Avatar"]["Avatar"]["address"].AsString,
document["Avatar"]["Avatar"]["name"].AsString,
document["Avatar"]["Avatar"]["level"].AsInt32
document["Avatar"]["Avatar"]["level"].AsInt32,
document["Avatar"]["Avatar"]["actionPoint"].AsInt32
);
arenaRanking.Avatar = avatar;

Expand Down
12 changes: 11 additions & 1 deletion Mimir/Repositories/CpRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using Mimir.Models.Agent;
using Mimir.Services;
using Mimir.Util;
using Nekoyume;
using Nekoyume.Helper;
using Nekoyume.TableData.Rune;

namespace Mimir.Repositories;

Expand Down Expand Up @@ -61,6 +64,12 @@ public CpRepository(IStateService stateService)
}
}
}

var runeListSheet = await _stateGetter.GetSheetAsync<RuneListSheet>();
var runeStates = await _stateGetter.GetRuneStatesAsync(avatarAddress);
var runeLevelBonusSheet = await _stateGetter.GetSheetAsync<RuneLevelBonusSheet>();
var runeLevelBonus =
RuneHelper.CalculateRuneLevelBonus(runeStates, runeListSheet, runeLevelBonusSheet);

return CPHelper.TotalCP(
equipments,
Expand All @@ -69,7 +78,8 @@ public CpRepository(IStateService stateService)
avatar.Level,
characterRow,
costumeStatSheet,
modifiers[avatarAddress]
modifiers[avatarAddress],
runeLevelBonus
);
}
catch (NullReferenceException)
Expand Down
60 changes: 41 additions & 19 deletions Mimir/Util/StateGetter.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using Libplanet.Crypto;
using Bencodex.Types;
using Libplanet.Action.State;
using Nekoyume.TableData;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Model.EnumType;
using Nekoyume.Model.Item;
using Nekoyume.Model.State;
using Mimir.Services;
using Nekoyume.Model.Arena;
using Nekoyume.TableData.Rune;

namespace Mimir.Util;

Expand Down Expand Up @@ -93,7 +96,12 @@ await stateService.GetState(address, accountAddress) ??
avatarState.inventory = inventory;
}
}


if (await GetStateAsync(avatarAddress, Addresses.ActionPoint) is Integer actionPoint)
{
avatarState.actionPoint = actionPoint;
}

return avatarState;
}

Expand All @@ -119,29 +127,43 @@ await stateService.GetState(address, accountAddress) ??
_ => null,
};
}

public async Task<List<RuneState>> GetRuneStatesAsync(Address avatarAddress)
public async Task<AllRuneState> GetRuneStatesAsync(Address avatarAddress)
{
var state = await stateService.GetState(
RuneSlotState.DeriveAddress(avatarAddress, BattleType.Arena));
var runeSlotState = state switch
AllRuneState allRuneState;
if (await GetStateAsync(avatarAddress, Addresses.RuneState) is List list)
{
List list => new RuneSlotState(list),
null => new RuneSlotState(BattleType.Arena),
_ => null,
};
if (runeSlotState is null)
allRuneState = new AllRuneState(list);
}
else
{
// Get legacy rune states
var runeListSheet = await GetSheetAsync<RuneListSheet>();
allRuneState = new AllRuneState();
foreach (var rune in runeListSheet.Values)
{
var runeAddress = RuneState.DeriveAddress(avatarAddress, rune.Id);
if (await GetStateAsync(runeAddress, ReservedAddresses.LegacyAccount) is List rawState)
{
var runeState = new RuneState(rawState);
allRuneState.AddRuneState(runeState);
}
}
}

return allRuneState;
}

public async Task<RuneSlotState> GetRuneSlotStateAsync(Address avatarAddress, BattleType battleType)
{
var runeSlotStateAddress = RuneSlotState.DeriveAddress(avatarAddress, battleType);
var serialized = await GetStateAsync(runeSlotStateAddress, ReservedAddresses.LegacyAccount);
if (serialized is List list)
{
return [];
return new RuneSlotState(list);
}

var runeAddresses = runeSlotState.GetEquippedRuneSlotInfos()
.Select(info => RuneState.DeriveAddress(avatarAddress, info.RuneId))
.ToArray();
var runeValues = await stateService.GetStates(runeAddresses);
return runeValues.OfType<List>()
.Select(e => new RuneState(e))
.ToList();
return new RuneSlotState(battleType);
}

public async Task<Dictionary<Address, CollectionState>> GetCollectionStatesAsync(List<Address> addresses)
Expand Down

0 comments on commit 9883016

Please sign in to comment.