From 5f862519f41ce82d7499bd4ea7482b6411d84bac Mon Sep 17 00:00:00 2001 From: Jay Malhotra <5047192+SapiensAnatis@users.noreply.github.com> Date: Thu, 29 Aug 2024 19:07:01 +0100 Subject: [PATCH] Add new entity types to save editor (#1057) Add wyrmite, diamantium, and hustle hammers to the website save editor --- .../Features/Present/PresentTest.cs | 34 +++++++++++- .../Reward/Handlers/GenericRewardHandler.cs | 8 --- .../Reward/Handlers/HustleHammerHandler.cs | 54 +++++++++++++++++++ .../Reward/Handlers/WyrmiteHandler.cs | 54 +++++++++++++++++++ .../Web/Savefile/EditorWidgetsService.cs | 10 +++- .../Web/Savefile/SavefileEditService.cs | 8 +-- .../src/lib/translations/en/save-editor.json | 18 +++++++ .../save-editor/present/presentTypes.ts | 5 +- global.json | 2 +- 9 files changed, 176 insertions(+), 17 deletions(-) create mode 100644 DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/HustleHammerHandler.cs create mode 100644 DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/WyrmiteHandler.cs diff --git a/DragaliaAPI/DragaliaAPI.Integration.Test/Features/Present/PresentTest.cs b/DragaliaAPI/DragaliaAPI.Integration.Test/Features/Present/PresentTest.cs index 8b33982de..de37cf3f8 100644 --- a/DragaliaAPI/DragaliaAPI.Integration.Test/Features/Present/PresentTest.cs +++ b/DragaliaAPI/DragaliaAPI.Integration.Test/Features/Present/PresentTest.cs @@ -204,7 +204,13 @@ public async Task Receive_ReceiveAllPresents_ClaimsAll() { ViewerId = ViewerId, EntityType = EntityTypes.Wyrmite, - EntityQuantity = 100, + EntityQuantity = 50, + }, + new() + { + ViewerId = ViewerId, + EntityType = EntityTypes.Wyrmite, + EntityQuantity = 50, }, new() { @@ -241,7 +247,13 @@ public async Task Receive_ReceiveAllPresents_ClaimsAll() { ViewerId = ViewerId, EntityType = EntityTypes.HustleHammer, - EntityQuantity = 100, + EntityQuantity = 50, + }, + new() + { + ViewerId = ViewerId, + EntityType = EntityTypes.HustleHammer, + EntityQuantity = 50, }, new() { @@ -256,6 +268,20 @@ public async Task Receive_ReceiveAllPresents_ClaimsAll() EntityId = (int)Emblems.SupremeBogfish, EntityQuantity = 1, }, + new() + { + ViewerId = ViewerId, + EntityType = EntityTypes.FreeDiamantium, + EntityId = 0, + EntityQuantity = 50, + }, + new() + { + ViewerId = ViewerId, + EntityType = EntityTypes.FreeDiamantium, + EntityId = 0, + EntityQuantity = 50, + }, }; await this.AddRangeToDatabase(presents); @@ -304,6 +330,10 @@ await this.Client.PostMsgpack( .Data.UpdateDataList.PresentNotice.Should() .BeEquivalentTo(new PresentNotice() { PresentCount = 0, PresentLimitCount = 0 }); + response + .Data.UpdateDataList.DiamondData.Should() + .BeEquivalentTo(new DiamondData() { FreeDiamond = 100, PaidDiamond = 0 }); + // Not sure if entity_result is correct so won't test that } diff --git a/DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/GenericRewardHandler.cs b/DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/GenericRewardHandler.cs index 540ca8f1e..63bfc220f 100644 --- a/DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/GenericRewardHandler.cs +++ b/DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/GenericRewardHandler.cs @@ -29,10 +29,8 @@ IUserService userService ImmutableArray.Create( EntityTypes.Item, EntityTypes.Dew, - EntityTypes.HustleHammer, EntityTypes.Rupies, EntityTypes.SkipTicket, - EntityTypes.Wyrmite, EntityTypes.Material, EntityTypes.Mana, EntityTypes.FortPlant, @@ -60,18 +58,12 @@ public async Task Grant(Entity entity) case EntityTypes.Dew: await userDataRepository.UpdateDewpoint(entity.Quantity); break; - case EntityTypes.HustleHammer: - (await userDataRepository.UserData.SingleAsync()).BuildTimePoint += entity.Quantity; - break; case EntityTypes.Rupies: await userDataRepository.UpdateCoin(entity.Quantity); break; case EntityTypes.SkipTicket: await userService.AddQuestSkipPoint(entity.Quantity); break; - case EntityTypes.Wyrmite: - await userDataRepository.GiveWyrmite(entity.Quantity); - break; case EntityTypes.Material: ( await inventoryRepository.GetMaterial((Materials)entity.Id) diff --git a/DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/HustleHammerHandler.cs b/DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/HustleHammerHandler.cs new file mode 100644 index 000000000..f20d4d681 --- /dev/null +++ b/DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/HustleHammerHandler.cs @@ -0,0 +1,54 @@ +using DragaliaAPI.Database; +using DragaliaAPI.Database.Entities; +using DragaliaAPI.Shared.Definitions.Enums; +using Microsoft.EntityFrameworkCore; + +namespace DragaliaAPI.Features.Reward.Handlers; + +public class HustleHammerHandler(ApiContext apiContext) : IRewardHandler, IBatchRewardHandler +{ + // "Why would you ever need more than this...?" + private const int MaxHammers = 999_999; + + public IReadOnlyList SupportedTypes { get; } = [EntityTypes.HustleHammer]; + + public async Task> GrantRange( + IDictionary entities + ) + where TKey : struct + { + DbPlayerUserData userData = await apiContext.PlayerUserData.FirstAsync(); + Dictionary results = new(entities.Count); + + foreach ((TKey key, Entity entity) in entities) + { + GrantReturn result = TryIncrementBuildTimePoint(userData, entity.Quantity) + ? GrantReturn.Added() + : GrantReturn.Limit(); + + results[key] = result; + } + + return results; + } + + public async Task Grant(Entity entity) + { + DbPlayerUserData userData = await apiContext.PlayerUserData.FirstAsync(); + + return TryIncrementBuildTimePoint(userData, entity.Quantity) + ? GrantReturn.Added() + : GrantReturn.Limit(); + } + + private static bool TryIncrementBuildTimePoint(DbPlayerUserData userData, int quantity) + { + if (userData.BuildTimePoint + quantity > MaxHammers) + { + return false; + } + + userData.BuildTimePoint += quantity; + return true; + } +} diff --git a/DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/WyrmiteHandler.cs b/DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/WyrmiteHandler.cs new file mode 100644 index 000000000..5b972af63 --- /dev/null +++ b/DragaliaAPI/DragaliaAPI/Features/Reward/Handlers/WyrmiteHandler.cs @@ -0,0 +1,54 @@ +using DragaliaAPI.Database; +using DragaliaAPI.Database.Entities; +using DragaliaAPI.Shared.Definitions.Enums; +using Microsoft.EntityFrameworkCore; + +namespace DragaliaAPI.Features.Reward.Handlers; + +public class WyrmiteHandler(ApiContext apiContext) : IRewardHandler, IBatchRewardHandler +{ + // Assumed maximum before UI bugs. True max is int.MaxValue. + private const int MaxWyrmite = 999_999_999; + + public IReadOnlyList SupportedTypes => [EntityTypes.Wyrmite]; + + public async Task> GrantRange( + IDictionary entities + ) + where TKey : struct + { + DbPlayerUserData userData = await apiContext.PlayerUserData.FirstAsync(); + Dictionary results = new(entities.Count); + + foreach ((TKey key, Entity entity) in entities) + { + GrantReturn result = TryIncrementCrystal(userData, entity.Quantity) + ? GrantReturn.Added() + : GrantReturn.Limit(); + + results[key] = result; + } + + return results; + } + + public async Task Grant(Entity entity) + { + DbPlayerUserData userData = await apiContext.PlayerUserData.FirstAsync(); + + return TryIncrementCrystal(userData, entity.Quantity) + ? GrantReturn.Added() + : GrantReturn.Limit(); + } + + private static bool TryIncrementCrystal(DbPlayerUserData userData, int quantity) + { + if (userData.Crystal + quantity > MaxWyrmite) + { + return false; + } + + userData.Crystal += quantity; + return true; + } +} diff --git a/DragaliaAPI/DragaliaAPI/Features/Web/Savefile/EditorWidgetsService.cs b/DragaliaAPI/DragaliaAPI/Features/Web/Savefile/EditorWidgetsService.cs index d2b14cfe5..c8d7de1c5 100644 --- a/DragaliaAPI/DragaliaAPI/Features/Web/Savefile/EditorWidgetsService.cs +++ b/DragaliaAPI/DragaliaAPI/Features/Web/Savefile/EditorWidgetsService.cs @@ -18,6 +18,9 @@ public static PresentWidgetData GetPresentWidgetData() new() { Type = EntityTypes.DmodePoint, HasQuantity = true }, new() { Type = EntityTypes.SkipTicket, HasQuantity = true }, new() { Type = EntityTypes.DragonGift, HasQuantity = true }, + new() { Type = EntityTypes.FreeDiamantium, HasQuantity = true }, + new() { Type = EntityTypes.Wyrmite, HasQuantity = true }, + new() { Type = EntityTypes.HustleHammer, HasQuantity = true }, ]; Dictionary> availableItems = @@ -28,8 +31,11 @@ public static PresentWidgetData GetPresentWidgetData() [EntityTypes.Dragon] = GetDragonList(), [EntityTypes.Material] = GetMaterialList(), [EntityTypes.DmodePoint] = GetDmodePointList(), - [EntityTypes.SkipTicket] = GetSkipTicketList(), + [EntityTypes.SkipTicket] = GetId0List(), [EntityTypes.DragonGift] = GetDragonGiftList(), + [EntityTypes.FreeDiamantium] = GetId0List(), + [EntityTypes.Wyrmite] = GetId0List(), + [EntityTypes.HustleHammer] = GetId0List(), }; return new() { Types = typeInfo, AvailableItems = availableItems }; @@ -61,7 +67,7 @@ private static List GetDmodePointList() => new EntityTypeItem { Id = (int)DmodePoint.Point2 }, ]; - private static List GetSkipTicketList() => [new EntityTypeItem { Id = 0 }]; + private static List GetId0List() => [new EntityTypeItem { Id = 0 }]; private static List GetDragonGiftList() => Enum.GetValues().Select(x => new EntityTypeItem() { Id = (int)x }).ToList(); diff --git a/DragaliaAPI/DragaliaAPI/Features/Web/Savefile/SavefileEditService.cs b/DragaliaAPI/DragaliaAPI/Features/Web/Savefile/SavefileEditService.cs index 849c4a989..20457b3b4 100644 --- a/DragaliaAPI/DragaliaAPI/Features/Web/Savefile/SavefileEditService.cs +++ b/DragaliaAPI/DragaliaAPI/Features/Web/Savefile/SavefileEditService.cs @@ -58,8 +58,11 @@ private bool ValidatePresents(List presents) EntityTypes.Item => ValidateItemPresent(present), EntityTypes.Material => ValidateMaterialPresent(present), EntityTypes.DmodePoint => ValidateDmodePointPresent(present), - EntityTypes.SkipTicket => ValidateSkipTicketPresent(present), + EntityTypes.SkipTicket => ValidateId0Present(present), EntityTypes.DragonGift => ValidateDragonGiftPresent(present), + EntityTypes.FreeDiamantium => ValidateId0Present(present), + EntityTypes.Wyrmite => ValidateId0Present(present), + EntityTypes.HustleHammer => ValidateId0Present(present), _ => false, }; @@ -88,8 +91,7 @@ private static bool ValidateMaterialPresent(PresentFormSubmission present) => private static bool ValidateDmodePointPresent(PresentFormSubmission present) => (DmodePoint)present.Item is DmodePoint.Point1 or DmodePoint.Point2; - private static bool ValidateSkipTicketPresent(PresentFormSubmission present) => - present.Item == 0; + private static bool ValidateId0Present(PresentFormSubmission present) => present.Item == 0; private static bool ValidateDragonGiftPresent(PresentFormSubmission present) => Enum.IsDefined((DragonGifts)present.Item); diff --git a/Website/src/lib/translations/en/save-editor.json b/Website/src/lib/translations/en/save-editor.json index b35c4093e..214a8c01c 100644 --- a/Website/src/lib/translations/en/save-editor.json +++ b/Website/src/lib/translations/en/save-editor.json @@ -926,6 +926,24 @@ "30003": "Valentine's Card", "40001": "Pup Grub" } + }, + "FreeDiamantium": { + "label": "Diamantium", + "item": { + "0": "Diamantium" + } + }, + "Wyrmite": { + "label": "Wyrmite", + "item": { + "0": "Wyrmite" + } + }, + "HustleHammer": { + "label": "Hustle Hammer", + "item": { + "0": "Hustle Hammer" + } } } } diff --git a/Website/src/routes/(main)/account/save-editor/present/presentTypes.ts b/Website/src/routes/(main)/account/save-editor/present/presentTypes.ts index 453837aa2..efa8a331a 100644 --- a/Website/src/routes/(main)/account/save-editor/present/presentTypes.ts +++ b/Website/src/routes/(main)/account/save-editor/present/presentTypes.ts @@ -7,7 +7,10 @@ export const EntityType = z.enum([ 'Material', 'DmodePoint', 'SkipTicket', - 'DragonGift' + 'DragonGift', + 'FreeDiamantium', + 'Wyrmite', + 'HustleHammer' ]); export type EntityType = z.infer; diff --git a/global.json b/global.json index 4ef052235..41336fb9c 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "8.0.301", + "version": "8.0.401", "rollForward": "latestFeature" } } \ No newline at end of file