Skip to content

Commit

Permalink
Update handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Atralupus committed Dec 27, 2024
1 parent c2df3f2 commit 98cef80
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"AvatarAddress":"0000000000000000000000000000000000000000","Object":{"BattleType":"Adventure","Costumes":[],"Equipments":["0468bfd9-7c76-4379-b1bc-10a3da90c584","0c4c772f-70d9-4a0d-b7e6-4cf25ad7f8f9","13164d27-1948-44a9-9bc7-b23a00aacc5f","2d9d4118-0e3b-445e-aa16-8203e50d2523","4da15d1b-503c-4e08-8476-6f405192258c","98c3abdb-17bc-49a0-8f9c-a61f48662dc7","a45c62d6-e612-4a0e-957a-9ecb7ae6f04f"]},"Metadata":{"SchemaVersion":2,"StoredBlockIndex":0}}
{"ItemSlotAddress":"0000000000000000000000000000000000000000","Object":{"BattleType":"Adventure","Costumes":[],"Equipments":["0468bfd9-7c76-4379-b1bc-10a3da90c584","0c4c772f-70d9-4a0d-b7e6-4cf25ad7f8f9","13164d27-1948-44a9-9bc7-b23a00aacc5f","2d9d4118-0e3b-445e-aa16-8203e50d2523","4da15d1b-503c-4e08-8476-6f405192258c","98c3abdb-17bc-49a0-8f9c-a61f48662dc7","a45c62d6-e612-4a0e-957a-9ecb7ae6f04f"]},"Metadata":{"SchemaVersion":2,"StoredBlockIndex":0}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Object":{"BattleType":"Adventure","Slots":[{"Index":0,"RuneSlotType":"Default","RuneType":"Stat","IsLock":false,"RuneId":10012},{"Index":1,"RuneSlotType":"Ncg","RuneType":"Stat","IsLock":true},{"Index":2,"RuneSlotType":"Stake","RuneType":"Stat","IsLock":true},{"Index":3,"RuneSlotType":"Default","RuneType":"Skill","IsLock":false},{"Index":4,"RuneSlotType":"Ncg","RuneType":"Skill","IsLock":true},{"Index":5,"RuneSlotType":"Stake","RuneType":"Skill","IsLock":true},{"Index":6,"RuneSlotType":"Crystal","RuneType":"Stat","IsLock":true},{"Index":7,"RuneSlotType":"Crystal","RuneType":"Skill","IsLock":true}]},"Metadata":{"SchemaVersion":1,"StoredBlockIndex":0}}
{"RuneSlotAddress":"0000000000000000000000000000000000000000","Object":{"BattleType":"Adventure","Slots":[{"Index":0,"RuneSlotType":"Default","RuneType":"Stat","IsLock":false,"RuneId":10012},{"Index":1,"RuneSlotType":"Ncg","RuneType":"Stat","IsLock":true},{"Index":2,"RuneSlotType":"Stake","RuneType":"Stat","IsLock":true},{"Index":3,"RuneSlotType":"Default","RuneType":"Skill","IsLock":false},{"Index":4,"RuneSlotType":"Ncg","RuneType":"Skill","IsLock":true},{"Index":5,"RuneSlotType":"Stake","RuneType":"Skill","IsLock":true},{"Index":6,"RuneSlotType":"Crystal","RuneType":"Stat","IsLock":true},{"Index":7,"RuneSlotType":"Crystal","RuneType":"Skill","IsLock":true}]},"Metadata":{"SchemaVersion":1,"StoredBlockIndex":0}}
1 change: 1 addition & 0 deletions Mimir.MongoDB.Tests/Bson/RuneSlotDocumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class RuneSlotDocumentTest
public Task JsonSnapshot()
{
var docs = new RuneSlotDocument(
default,
default,
default,
new RuneSlotState(TestDataHelpers.LoadState("RuneSlotState.bin"))
Expand Down
6 changes: 3 additions & 3 deletions Mimir.MongoDB/Bson/ItemSlotDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Mimir.MongoDB.Bson;
[BsonIgnoreExtraElements]
public record ItemSlotDocument(
[property: BsonIgnore, JsonIgnore] long StoredBlockIndex,
[property: BsonIgnore, JsonIgnore] Address Address,
Address AvatarAddress,
[property: BsonIgnore, JsonIgnore] Address AvatarAddress,
Address ItemSlotAddress,
ItemSlotState Object
) : MimirBsonDocument(Address.ToHex(), new DocumentMetadata(2, StoredBlockIndex));
) : MimirBsonDocument(AvatarAddress.ToHex(), new DocumentMetadata(2, StoredBlockIndex));
5 changes: 3 additions & 2 deletions Mimir.MongoDB/Bson/RuneSlotDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Mimir.MongoDB.Bson;
[BsonIgnoreExtraElements]
public record RuneSlotDocument(
[property: BsonIgnore, JsonIgnore] long StoredBlockIndex,
[property: BsonIgnore, JsonIgnore] Address Address,
[property: BsonIgnore, JsonIgnore] Address AvatarAddress,
Address RuneSlotAddress,
RuneSlotState Object
) : MimirBsonDocument(Address.ToHex(), new DocumentMetadata(1, StoredBlockIndex));
) : MimirBsonDocument(AvatarAddress.ToHex(), new DocumentMetadata(1, StoredBlockIndex));
2 changes: 1 addition & 1 deletion Mimir.MongoDB/Repositories/RuneSlotRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ BattleType battleType
);
var collectionName = CollectionNames.GetCollectionName<RuneSlotDocument>();
var collection = dbService.GetCollection<RuneSlotDocument>(collectionName);
var filter = Builders<RuneSlotDocument>.Filter.Eq("Address", runeSlotAddress.ToHex());
var filter = Builders<RuneSlotDocument>.Filter.Eq("_id", runeSlotAddress.ToHex());
var document = await collection.Find(filter).FirstOrDefaultAsync();
if (document is null)
{
Expand Down
2 changes: 1 addition & 1 deletion Mimir.Tests/QueryTests/RuneSlotTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task GraphQL_Query_RuneSlot_Returns_CorrectValue()
Nekoyume.Model.EnumType.BattleType.Adventure
)
)
.ReturnsAsync(new RuneSlotDocument(1, address, state));
.ReturnsAsync(new RuneSlotDocument(1, address, address, state));
var serviceProvider = TestServices.Builder.With(mockRepo.Object).Build();
var query = $$"""
query {
Expand Down
46 changes: 34 additions & 12 deletions Mimir.Worker/ActionHandler/RuneSlotStateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ protected override async Task<IEnumerable<WriteModel<BsonDocument>>> HandleActio
)
{
return (
await TryProcessRuneSlotStateAsync(actionPlainValue, actionType, stoppingToken)
await TryProcessRuneSlotStateAsync(
blockIndex,
actionPlainValue,
actionType,
stoppingToken
)
).Concat(
await TryProcessUnlockRuneSlotAsync(
blockIndex,
actionPlainValue,
actionType,
session,
Expand All @@ -55,6 +61,7 @@ await TryProcessUnlockRuneSlotAsync(
}

private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStateAsync(
long blockIndex,
IValue actionPlainValue,
string actionType,
CancellationToken stoppingToken = default
Expand All @@ -64,48 +71,49 @@ private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStat
{
var action = new BattleArena();
action.LoadPlainValue(actionPlainValue);
return await TryProcessRuneSlotStateAsync(action, stoppingToken);
return await TryProcessRuneSlotStateAsync(blockIndex, action, stoppingToken);
}

if (Regex.IsMatch(actionType, "^event_dungeon_battle[0-9]*$"))
{
var action = new EventDungeonBattle();
action.LoadPlainValue(actionPlainValue);
return await TryProcessRuneSlotStateAsync(action, stoppingToken);
return await TryProcessRuneSlotStateAsync(blockIndex, action, stoppingToken);
}

if (Regex.IsMatch(actionType, "^hack_and_slash[0-9]*$"))
{
var action = new HackAndSlash();
action.LoadPlainValue(actionPlainValue);
return await TryProcessRuneSlotStateAsync(action, stoppingToken);
return await TryProcessRuneSlotStateAsync(blockIndex, action, stoppingToken);
}

if (Regex.IsMatch(actionType, "^hack_and_slash_sweep[0-9]*$"))
{
var action = new HackAndSlashSweep();
action.LoadPlainValue(actionPlainValue);
return await TryProcessRuneSlotStateAsync(action, stoppingToken);
return await TryProcessRuneSlotStateAsync(blockIndex, action, stoppingToken);
}

if (Regex.IsMatch(actionType, "^join_arena[0-9]*$"))
{
var action = new JoinArena();
action.LoadPlainValue(actionPlainValue);
return await TryProcessRuneSlotStateAsync(action, stoppingToken);
return await TryProcessRuneSlotStateAsync(blockIndex, action, stoppingToken);
}

if (Regex.IsMatch(actionType, "^raid[0-9]*$"))
{
var action = new Raid();
action.LoadPlainValue(actionPlainValue);
return await TryProcessRuneSlotStateAsync(action, stoppingToken);
return await TryProcessRuneSlotStateAsync(blockIndex, action, stoppingToken);
}

return [];
}

private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStateAsync(
long blockIndex,
BattleArena action,
CancellationToken stoppingToken = default
)
Expand All @@ -117,6 +125,7 @@ private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStat
}

return await RuneSlotCollectionUpdater.UpdateAsync(
blockIndex,
StateService,
BattleType.Arena,
action.myAvatarAddress,
Expand All @@ -125,6 +134,7 @@ private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStat
}

private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStateAsync(
long blockIndex,
EventDungeonBattle action,
CancellationToken stoppingToken = default
)
Expand All @@ -136,14 +146,16 @@ private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStat
}

return await RuneSlotCollectionUpdater.UpdateAsync(
blockIndex,
StateService,
BattleType.Arena,
BattleType.Adventure,
action.AvatarAddress,
stoppingToken
);
}

private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStateAsync(
long blockIndex,
HackAndSlash action,
CancellationToken stoppingToken = default
)
Expand All @@ -155,14 +167,16 @@ private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStat
}

return await RuneSlotCollectionUpdater.UpdateAsync(
blockIndex,
StateService,
BattleType.Arena,
BattleType.Adventure,
action.AvatarAddress,
stoppingToken
);
}

private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStateAsync(
long blockIndex,
HackAndSlashSweep action,
CancellationToken stoppingToken = default
)
Expand All @@ -174,14 +188,16 @@ private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStat
}

return await RuneSlotCollectionUpdater.UpdateAsync(
blockIndex,
StateService,
BattleType.Arena,
BattleType.Adventure,
action.avatarAddress,
stoppingToken
);
}

private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStateAsync(
long blockIndex,
JoinArena action,
CancellationToken stoppingToken = default
)
Expand All @@ -193,6 +209,7 @@ private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStat
}

return await RuneSlotCollectionUpdater.UpdateAsync(
blockIndex,
StateService,
BattleType.Arena,
action.avatarAddress,
Expand All @@ -201,6 +218,7 @@ private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStat
}

private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStateAsync(
long blockIndex,
Raid action,
CancellationToken stoppingToken = default
)
Expand All @@ -212,14 +230,16 @@ private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessRuneSlotStat
}

return await RuneSlotCollectionUpdater.UpdateAsync(
blockIndex,
StateService,
BattleType.Arena,
BattleType.Raid,
action.AvatarAddress,
stoppingToken
);
}

private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessUnlockRuneSlotAsync(
long blockIndex,
IValue actionPlainValue,
string actionType,
IClientSessionHandle? session = null,
Expand All @@ -230,13 +250,14 @@ private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessUnlockRuneSl
{
var action = new UnlockRuneSlot();
action.LoadPlainValue(actionPlainValue);
return await TryProcessUnlockRuneSlotAsync(action, session, stoppingToken);
return await TryProcessUnlockRuneSlotAsync(blockIndex, action, session, stoppingToken);
}

return [];
}

private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessUnlockRuneSlotAsync(
long blockIndex,
UnlockRuneSlot action,
IClientSessionHandle? session = null,
CancellationToken stoppingToken = default
Expand All @@ -247,6 +268,7 @@ private async Task<IEnumerable<WriteModel<BsonDocument>>> TryProcessUnlockRuneSl
{
ops.AddRange(
await RuneSlotCollectionUpdater.UpdateAsync(
blockIndex,
StateService,
battleType,
action.AvatarAddress,
Expand Down
8 changes: 7 additions & 1 deletion Mimir.Worker/CollectionUpdaters/RuneSlotCollectionUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Mimir.Worker.CollectionUpdaters;
public static class RuneSlotCollectionUpdater
{
public static async Task<IEnumerable<WriteModel<BsonDocument>>> UpdateAsync(
long blockIndex,
IStateService stateService,
BattleType battleType,
Address avatarAddress,
Expand All @@ -29,7 +30,12 @@ public static async Task<IEnumerable<WriteModel<BsonDocument>>> UpdateAsync(
}

var runeSlotState = new RuneSlotState(serialized);
var runeSlotDocument = new RuneSlotDocument(runeSlotAddress, runeSlotState);
var runeSlotDocument = new RuneSlotDocument(
blockIndex,
avatarAddress,
runeSlotAddress,
runeSlotState
);
return [runeSlotDocument.ToUpdateOneModel()];
}

Expand Down

0 comments on commit 98cef80

Please sign in to comment.