From c57c235f73e39e482b8f7c0810e8cda409e94b74 Mon Sep 17 00:00:00 2001 From: moreal Date: Tue, 24 Sep 2024 22:16:04 +0900 Subject: [PATCH 1/2] Store `SimpleAvatar` in `ArenaDocument` --- Mimir.MongoDB/Bson/ArenaDocument.cs | 4 +- .../Bson/Simplified/SimplifiedAvatarState.cs | 76 +++++++++++++++++++ .../ActionHandler/BattleArenaHandler.cs | 32 ++++---- .../ActionHandler/JoinArenaHandler.cs | 19 ++--- .../ArenaCollectionUpdater.cs | 4 +- Mimir.Worker/Initializer/ArenaInitializer.cs | 3 + 6 files changed, 105 insertions(+), 33 deletions(-) diff --git a/Mimir.MongoDB/Bson/ArenaDocument.cs b/Mimir.MongoDB/Bson/ArenaDocument.cs index afcc6787..b2fd14fa 100644 --- a/Mimir.MongoDB/Bson/ArenaDocument.cs +++ b/Mimir.MongoDB/Bson/ArenaDocument.cs @@ -1,4 +1,5 @@ using Lib9c.Models.Arena; +using Lib9c.Models.States; using Libplanet.Crypto; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; @@ -13,7 +14,8 @@ public record ArenaDocument( int ChampionshipId, int Round, ArenaInformation ArenaInformation, - ArenaScore ArenaScore) + ArenaScore ArenaScore, + SimplifiedAvatarState SimpleAvatar) : MimirBsonDocument(Address) { [BsonIgnore, JsonIgnore] diff --git a/Mimir.MongoDB/Bson/Simplified/SimplifiedAvatarState.cs b/Mimir.MongoDB/Bson/Simplified/SimplifiedAvatarState.cs index 956f1124..701e0f19 100644 --- a/Mimir.MongoDB/Bson/Simplified/SimplifiedAvatarState.cs +++ b/Mimir.MongoDB/Bson/Simplified/SimplifiedAvatarState.cs @@ -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
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 + ); + } } diff --git a/Mimir.Worker/ActionHandler/BattleArenaHandler.cs b/Mimir.Worker/ActionHandler/BattleArenaHandler.cs index feca27ee..20014bad 100644 --- a/Mimir.Worker/ActionHandler/BattleArenaHandler.cs +++ b/Mimir.Worker/ActionHandler/BattleArenaHandler.cs @@ -1,4 +1,5 @@ using Lib9c.Abstractions; +using Lib9c.Models.States; using Libplanet.Action; using Libplanet.Crypto; using Mimir.Worker.CollectionUpdaters; @@ -49,7 +50,6 @@ await ItemSlotCollectionUpdater.UpdateAsync( ); await ProcessArena(blockIndex, battleArena, session, stoppingToken); - await ProcessAvatar(battleArena, session, stoppingToken); return true; } @@ -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, @@ -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, @@ -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); - } } diff --git a/Mimir.Worker/ActionHandler/JoinArenaHandler.cs b/Mimir.Worker/ActionHandler/JoinArenaHandler.cs index 5fd2242a..bcfd265e 100644 --- a/Mimir.Worker/ActionHandler/JoinArenaHandler.cs +++ b/Mimir.Worker/ActionHandler/JoinArenaHandler.cs @@ -1,4 +1,5 @@ using Lib9c.Abstractions; +using Lib9c.Models.States; using Libplanet.Action; using Libplanet.Crypto; using Mimir.Worker.CollectionUpdaters; @@ -45,7 +46,6 @@ await ItemSlotCollectionUpdater.UpdateAsync( ); await ProcessArena(joinArena, session, stoppingToken); - await ProcessAvatar(joinArena, session, stoppingToken); return true; } @@ -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, @@ -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); - } } diff --git a/Mimir.Worker/CollectionUpdaters/ArenaCollectionUpdater.cs b/Mimir.Worker/CollectionUpdaters/ArenaCollectionUpdater.cs index 4f6385f0..59f1c04f 100644 --- a/Mimir.Worker/CollectionUpdaters/ArenaCollectionUpdater.cs +++ b/Mimir.Worker/CollectionUpdaters/ArenaCollectionUpdater.cs @@ -1,4 +1,5 @@ using Lib9c.Models.Arena; +using Lib9c.Models.States; using Libplanet.Crypto; using Mimir.MongoDB; using Mimir.MongoDB.Bson; @@ -11,6 +12,7 @@ public static class ArenaCollectionUpdater { public static async Task UpsertAsync( MongoDbService dbService, + SimplifiedAvatarState simpleAvatar, ArenaScore arenaScore, ArenaInformation arenaInfo, Address avatarAddress, @@ -22,7 +24,7 @@ public static async Task UpsertAsync( { await dbService.UpsertStateDataManyAsync( CollectionNames.GetCollectionName(), - [new ArenaDocument(avatarAddress, championshipId, round, arenaInfo, arenaScore),], + [new ArenaDocument(avatarAddress, championshipId, round, arenaInfo, arenaScore, simpleAvatar),], session, stoppingToken ); diff --git a/Mimir.Worker/Initializer/ArenaInitializer.cs b/Mimir.Worker/Initializer/ArenaInitializer.cs index 5aee5cb2..0cc2d6fc 100644 --- a/Mimir.Worker/Initializer/ArenaInitializer.cs +++ b/Mimir.Worker/Initializer/ArenaInitializer.cs @@ -1,4 +1,5 @@ using Mimir.MongoDB.Bson; +using Lib9c.Models.States; using Mimir.Worker.CollectionUpdaters; using Mimir.Worker.Services; using MongoDB.Bson; @@ -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, From 6ee9e2ffdb5feaed35b08b9ed6bb04fb5ca63777 Mon Sep 17 00:00:00 2001 From: moreal Date: Tue, 24 Sep 2024 22:28:04 +0900 Subject: [PATCH 2/2] Use `SimpleAvatar` in `ArenaDocument` --- Mimir/Repositories/ArenaRepository.cs | 36 --------------------------- 1 file changed, 36 deletions(-) diff --git a/Mimir/Repositories/ArenaRepository.cs b/Mimir/Repositories/ArenaRepository.cs index c161920a..2be811a4 100644 --- a/Mimir/Repositories/ArenaRepository.cs +++ b/Mimir/Repositories/ArenaRepository.cs @@ -54,42 +54,6 @@ private async Task> 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)), };