Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store SimpleAvatar in ArenaDocument #339

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Mimir.MongoDB/Bson/ArenaDocument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lib9c.Models.Arena;
using Lib9c.Models.States;
using Libplanet.Crypto;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
Expand All @@ -13,7 +14,8 @@ public record ArenaDocument(
int ChampionshipId,
int Round,
ArenaInformation ArenaInformation,
ArenaScore ArenaScore)
ArenaScore ArenaScore,
SimplifiedAvatarState SimpleAvatar)
: MimirBsonDocument(Address)
{
[BsonIgnore, JsonIgnore]
Expand Down
76 changes: 76 additions & 0 deletions Mimir.MongoDB/Bson/Simplified/SimplifiedAvatarState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,80 @@ public SimplifiedAvatarState(IValue bencoded)
bencoded.Kind
);
}

public SimplifiedAvatarState(
Address address,
int version,
string name,
int characterId,
int level,
long exp,
long updatedAt,
Address agentAddress,
MailBox mailBox,
long blockIndex,
long dailyRewardReceivedIndex,
int actionPoint,
CollectionMap stageMap,
CollectionMap monsterMap,
CollectionMap itemMap,
CollectionMap eventMap,
int hair,
int lens,
int ear,
int tail,
List<Address> combinationSlotAddresses,
Address rankingMapAddress)
: base(new List(address.Bencoded))
{
Version = version;
Name = name;
CharacterId = characterId;
Level = level;
Exp = exp;
UpdatedAt = updatedAt;
AgentAddress = agentAddress;
MailBox = mailBox;
BlockIndex = blockIndex;
DailyRewardReceivedIndex = dailyRewardReceivedIndex;
ActionPoint = actionPoint;
StageMap = stageMap;
MonsterMap = monsterMap;
ItemMap = itemMap;
EventMap = eventMap;
Hair = hair;
Lens = lens;
Ear = ear;
Tail = tail;
CombinationSlotAddresses = combinationSlotAddresses;
RankingMapAddress = rankingMapAddress;
}

public static SimplifiedAvatarState FromAvatarState(AvatarState avatarState)
{
return new SimplifiedAvatarState(
avatarState.Address,
avatarState.Version,
avatarState.Name,
avatarState.CharacterId,
avatarState.Level,
avatarState.Exp,
avatarState.UpdatedAt,
avatarState.AgentAddress,
avatarState.MailBox,
avatarState.BlockIndex,
avatarState.DailyRewardReceivedIndex,
avatarState.ActionPoint,
avatarState.StageMap,
avatarState.MonsterMap,
avatarState.ItemMap,
avatarState.EventMap,
avatarState.Hair,
avatarState.Lens,
avatarState.Ear,
avatarState.Tail,
avatarState.CombinationSlotAddresses,
avatarState.RankingMapAddress
);
}
}
32 changes: 13 additions & 19 deletions Mimir.Worker/ActionHandler/BattleArenaHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lib9c.Abstractions;
using Lib9c.Models.States;
using Libplanet.Action;
using Libplanet.Crypto;
using Mimir.Worker.CollectionUpdaters;
Expand Down Expand Up @@ -49,7 +50,6 @@ await ItemSlotCollectionUpdater.UpdateAsync(
);

await ProcessArena(blockIndex, battleArena, session, stoppingToken);
await ProcessAvatar(battleArena, session, stoppingToken);

return true;
}
Expand All @@ -73,8 +73,14 @@ private async Task ProcessArena(
battleArena.Round,
stoppingToken
);
var myAvatarState = await StateGetter.GetAvatarState(
battleArena.MyAvatarAddress,
stoppingToken
);
var mySimpleAvatarState = SimplifiedAvatarState.FromAvatarState(myAvatarState);
await ArenaCollectionUpdater.UpsertAsync(
Store,
mySimpleAvatarState,
myArenaScore,
myArenaInfo,
battleArena.MyAvatarAddress,
Expand All @@ -96,8 +102,14 @@ await ArenaCollectionUpdater.UpsertAsync(
battleArena.Round,
stoppingToken
);
var enemyAvatarState = await StateGetter.GetAvatarState(
battleArena.EnemyAvatarAddress,
stoppingToken
);
var enemySimpleAvatarState = SimplifiedAvatarState.FromAvatarState(enemyAvatarState);
await ArenaCollectionUpdater.UpsertAsync(
Store,
enemySimpleAvatarState,
enemyArenaScore,
enemyArenaInfo,
battleArena.EnemyAvatarAddress,
Expand All @@ -107,22 +119,4 @@ await ArenaCollectionUpdater.UpsertAsync(
stoppingToken
);
}

private async Task ProcessAvatar(
IBattleArenaV1 battleArena,
IClientSessionHandle? session = null,
CancellationToken stoppingToken = default
)
{
var myAvatarState = await StateGetter.GetAvatarState(
battleArena.MyAvatarAddress,
stoppingToken
);
var enemyAvatarState = await StateGetter.GetAvatarState(
battleArena.EnemyAvatarAddress,
stoppingToken
);
await AvatarCollectionUpdater.UpsertAsync(Store, myAvatarState, session, stoppingToken);
await AvatarCollectionUpdater.UpsertAsync(Store, enemyAvatarState, session, stoppingToken);
}
}
19 changes: 7 additions & 12 deletions Mimir.Worker/ActionHandler/JoinArenaHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lib9c.Abstractions;
using Lib9c.Models.States;
using Libplanet.Action;
using Libplanet.Crypto;
using Mimir.Worker.CollectionUpdaters;
Expand Down Expand Up @@ -45,7 +46,6 @@ await ItemSlotCollectionUpdater.UpdateAsync(
);

await ProcessArena(joinArena, session, stoppingToken);
await ProcessAvatar(joinArena, session, stoppingToken);

return true;
}
Expand All @@ -68,8 +68,14 @@ private async Task ProcessArena(
joinArena.Round,
stoppingToken
);
var avatarState = await StateGetter.GetAvatarState(
joinArena.AvatarAddress,
stoppingToken
);
var simpleAvatarState = SimplifiedAvatarState.FromAvatarState(avatarState);
await ArenaCollectionUpdater.UpsertAsync(
Store,
simpleAvatarState,
arenaScore,
arenaInfo,
joinArena.AvatarAddress,
Expand All @@ -79,15 +85,4 @@ await ArenaCollectionUpdater.UpsertAsync(
stoppingToken
);
}

private async Task ProcessAvatar(
IJoinArenaV1 joinArena,
IClientSessionHandle? session = null,
CancellationToken stoppingToken = default
)
{
var avatarState = await StateGetter.GetAvatarState(joinArena.AvatarAddress, stoppingToken);

await AvatarCollectionUpdater.UpsertAsync(Store, avatarState, session, stoppingToken);
}
}
4 changes: 3 additions & 1 deletion Mimir.Worker/CollectionUpdaters/ArenaCollectionUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lib9c.Models.Arena;
using Lib9c.Models.States;
using Libplanet.Crypto;
using Mimir.MongoDB;
using Mimir.MongoDB.Bson;
Expand All @@ -11,6 +12,7 @@ public static class ArenaCollectionUpdater
{
public static async Task UpsertAsync(
MongoDbService dbService,
SimplifiedAvatarState simpleAvatar,
ArenaScore arenaScore,
ArenaInformation arenaInfo,
Address avatarAddress,
Expand All @@ -22,7 +24,7 @@ public static async Task UpsertAsync(
{
await dbService.UpsertStateDataManyAsync(
CollectionNames.GetCollectionName<ArenaDocument>(),
[new ArenaDocument(avatarAddress, championshipId, round, arenaInfo, arenaScore),],
[new ArenaDocument(avatarAddress, championshipId, round, arenaInfo, arenaScore, simpleAvatar),],
session,
stoppingToken
);
Expand Down
3 changes: 3 additions & 0 deletions Mimir.Worker/Initializer/ArenaInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mimir.MongoDB.Bson;
using Lib9c.Models.States;
using Mimir.Worker.CollectionUpdaters;
using Mimir.Worker.Services;
using MongoDB.Bson;
Expand Down Expand Up @@ -45,10 +46,12 @@ public override async Task RunAsync(CancellationToken stoppingToken)
);

var avatarState = await stateGetter.GetAvatarState(avatarAddress, stoppingToken);
var simpleAvatarState = SimplifiedAvatarState.FromAvatarState(avatarState);

_logger.Information("Init arena, address: {AvatarAddress}", avatarAddress);
await ArenaCollectionUpdater.UpsertAsync(
_store,
simpleAvatarState,
arenaScore,
arenaInfo,
avatarAddress,
Expand Down
36 changes: 0 additions & 36 deletions Mimir/Repositories/ArenaRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,42 +54,6 @@ private async Task<List<ArenaRankingDocument>> GetOrCreateLeaderboardAsync(
}
)
),
new(
"$lookup",
new BsonDocument
{
{ "from", "avatar" },
{ "localField", "Address" },
{ "foreignField", "Address" },
{ "as", "SimpleAvatar" }
}
),
new(
"$unwind",
new BsonDocument
{
{ "path", "$SimpleAvatar" },
{ "preserveNullAndEmptyArrays", true }
}
),
new(
"$project",
new BsonDocument
{
{ "SimpleAvatar.Object.EventMap", 0 },
{ "SimpleAvatar.Object.ItemMap", 0 },
{ "SimpleAvatar.Object.MailBox", 0 },
{ "SimpleAvatar.Object.MonsterMap", 0 },
{ "SimpleAvatar.Object.StageMap", 0 },
}
),
new(
"$addFields",
new BsonDocument
{
{ "SimpleAvatar", "$SimpleAvatar.Object" },
}
),
new("$sort", new BsonDocument("ArenaScore.Score", -1)),
};

Expand Down
Loading