-
-
Notifications
You must be signed in to change notification settings - Fork 41
Fix Arsene dragon stories not unlocked on receipt #1504
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0813be1
Fix Arsene dragon stories not unlocked on receipt
github-actions[bot] 3dc9b10
Address review comments: simplify Debug.Assert, scope V28Update to Ar…
github-actions[bot] d6fb50d
Address review comments: simplify V28Update query and remove obsolete…
github-actions[bot] 472e7e5
Apply csharpier formatting
github-actions[bot] 7e44997
More tests
SapiensAnatis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
DragaliaAPI/DragaliaAPI.Integration.Test/Features/SavefileUpdate/V28UpdateTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| using DragaliaAPI.Database.Entities; | ||
| using DragaliaAPI.Shared.MasterAsset; | ||
| using DragaliaAPI.Shared.MasterAsset.Models.Story; | ||
|
|
||
| namespace DragaliaAPI.Integration.Test.Features.SavefileUpdate; | ||
|
|
||
| public class V28UpdateTest : SavefileUpdateTestFixture | ||
| { | ||
| protected override int StartingVersion => 27; | ||
|
|
||
| public V28UpdateTest(CustomWebApplicationFactory factory, ITestOutputHelper outputHelper) | ||
| : base(factory, outputHelper) { } | ||
|
|
||
| [Fact] | ||
| public async Task V28Update_UnlocksBothStoriesForDragonAtLevel30() | ||
| { | ||
| this.ApiContext.PlayerStoryState.Count(x => x.StoryType == StoryTypes.Dragon) | ||
| .Should() | ||
| .Be(0); | ||
|
|
||
| await this.AddRangeToDatabase([ | ||
| new DbPlayerDragonReliability() { DragonId = DragonId.Arsene, Level = 30 }, | ||
| ]); | ||
|
|
||
| StoryData arseneStories = MasterAsset.DragonStories[(int)DragonId.Arsene]; | ||
|
|
||
| this.ApiContext.ChangeTracker.Clear(); | ||
|
|
||
| await this.LoadIndex(); | ||
|
|
||
| List<DbPlayerStoryState> dragonStories = ApiContext | ||
| .PlayerStoryState.Where(x => x.StoryType == StoryTypes.Dragon) | ||
| .ToList(); | ||
|
|
||
| dragonStories | ||
| .Should() | ||
| .Contain(x => x.StoryId == arseneStories.StoryIds[0]) | ||
| .Which.Should() | ||
| .BeEquivalentTo( | ||
| new DbPlayerStoryState() | ||
| { | ||
| ViewerId = this.ViewerId, | ||
| StoryId = arseneStories.StoryIds[0], | ||
| StoryType = StoryTypes.Dragon, | ||
| State = StoryState.Unlocked, | ||
| } | ||
| ); | ||
|
|
||
| dragonStories | ||
| .Should() | ||
| .Contain(x => x.StoryId == arseneStories.StoryIds[1]) | ||
| .Which.Should() | ||
| .BeEquivalentTo( | ||
| new DbPlayerStoryState() | ||
| { | ||
| ViewerId = this.ViewerId, | ||
| StoryId = arseneStories.StoryIds[1], | ||
| StoryType = StoryTypes.Dragon, | ||
| State = StoryState.Unlocked, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task V28Update_DoesNotDuplicateExistingStories() | ||
| { | ||
| await this.AddRangeToDatabase([ | ||
| new DbPlayerDragonReliability() { DragonId = DragonId.Arsene, Level = 30 }, | ||
| ]); | ||
|
|
||
| StoryData arseneStories = MasterAsset.DragonStories[(int)DragonId.Arsene]; | ||
|
|
||
| await this.AddRangeToDatabase([ | ||
| new DbPlayerStoryState() | ||
| { | ||
| StoryId = arseneStories.StoryIds[0], | ||
| StoryType = StoryTypes.Dragon, | ||
| }, | ||
| new DbPlayerStoryState() | ||
| { | ||
| StoryId = arseneStories.StoryIds[1], | ||
| StoryType = StoryTypes.Dragon, | ||
| }, | ||
| ]); | ||
|
|
||
| this.ApiContext.ChangeTracker.Clear(); | ||
|
|
||
| await this.LoadIndex(); | ||
|
|
||
| ApiContext | ||
| .PlayerStoryState.Where(x => | ||
| x.StoryType == StoryTypes.Dragon | ||
| && ( | ||
| x.StoryId == arseneStories.StoryIds[0] || x.StoryId == arseneStories.StoryIds[1] | ||
| ) | ||
| ) | ||
| .Should() | ||
| .HaveCount(2); | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
DragaliaAPI/DragaliaAPI/Features/Login/SavefileUpdate/V28Update.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| using DragaliaAPI.Database; | ||
| using DragaliaAPI.Database.Entities; | ||
| using DragaliaAPI.Shared.Definitions.Enums; | ||
| using DragaliaAPI.Shared.MasterAsset; | ||
| using DragaliaAPI.Shared.MasterAsset.Models.Story; | ||
| using DragaliaAPI.Shared.PlayerDetails; | ||
| using LinqToDB; | ||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| namespace DragaliaAPI.Features.Login.SavefileUpdate; | ||
|
|
||
| /// <summary> | ||
| /// Fixes missing dragon stories for Arsene, whose default reliability level of 30 meant that | ||
| /// stories were not unlocked on receipt (unlike level-up, which is handled by <see cref="V25Update"/>). | ||
| /// </summary> | ||
| public partial class V28Update( | ||
| ApiContext apiContext, | ||
| IPlayerIdentityService playerIdentityService, | ||
| ILogger<V28Update> logger | ||
| ) : ISavefileUpdate | ||
| { | ||
| public int SavefileVersion => 28; | ||
|
|
||
| public async Task Apply() | ||
| { | ||
| DbPlayerDragonReliability? arsene = | ||
| await apiContext.PlayerDragonReliability.FirstOrDefaultAsync(x => | ||
| x.DragonId == DragonId.Arsene | ||
| ); | ||
|
|
||
| if ( | ||
| arsene is null | ||
| || !MasterAsset.DragonStories.TryGetValue((int)DragonId.Arsene, out StoryData? data) | ||
| ) | ||
| { | ||
| Log.AddedNewStories(logger, 0); | ||
| return; | ||
| } | ||
|
|
||
| // Arsene's default reliability level is 30, so any existing reliability entry implies | ||
| // both stories should be unlocked -- there is no need to check the level. | ||
| List<DbPlayerStoryState> intendedStoryStates = data | ||
| .StoryIds.Select(storyId => new DbPlayerStoryState() | ||
| { | ||
| ViewerId = playerIdentityService.ViewerId, | ||
| StoryId = storyId, | ||
| State = StoryState.Unlocked, | ||
| StoryType = StoryTypes.Dragon, | ||
| }) | ||
| .ToList(); | ||
|
|
||
| int rowsAffected = await apiContext | ||
| .PlayerStoryState.Merge() | ||
| .Using(intendedStoryStates) | ||
| .OnTargetKey() | ||
| .InsertWhenNotMatched() | ||
| .MergeAsync(); | ||
|
|
||
| Log.AddedNewStories(logger, rowsAffected); | ||
| } | ||
|
|
||
| private static partial class Log | ||
| { | ||
| [LoggerMessage(LogLevel.Information, "V28Update added {Count} missing dragon stories")] | ||
| public static partial void AddedNewStories(ILogger logger, int count); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| using System.Diagnostics; | ||
| using DragaliaAPI.Database; | ||
| using DragaliaAPI.Database.Entities; | ||
| using DragaliaAPI.Shared.Definitions.Enums; | ||
| using DragaliaAPI.Shared.MasterAsset; | ||
| using DragaliaAPI.Shared.MasterAsset.Models.Story; | ||
| using DragaliaAPI.Shared.PlayerDetails; | ||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
|
|
@@ -47,7 +51,9 @@ public async Task<GrantReturn> Grant(Entity entity) | |
| && !await apiContext.PlayerDragonReliability.AnyAsync(x => x.DragonId == dragon) | ||
| ) | ||
| { | ||
| apiContext.PlayerDragonReliability.Add(new(playerIdentityService.ViewerId, dragon)); | ||
| DbPlayerDragonReliability reliability = new(playerIdentityService.ViewerId, dragon); | ||
| apiContext.PlayerDragonReliability.Add(reliability); | ||
| this.AddDefaultLevelStories(dragon, reliability.Level); | ||
| } | ||
|
|
||
| return GrantReturn.Added(); | ||
|
|
@@ -99,8 +105,10 @@ IDictionary<TKey, Entity> entities | |
| && !apiContext.PlayerDragonReliability.Local.Any(x => x.DragonId == dragon) | ||
| ) | ||
| { | ||
| apiContext.PlayerDragonReliability.Add(new(playerIdentityService.ViewerId, dragon)); | ||
| DbPlayerDragonReliability reliability = new(playerIdentityService.ViewerId, dragon); | ||
| apiContext.PlayerDragonReliability.Add(reliability); | ||
| ownedReliabilities.Add(dragon); | ||
| this.AddDefaultLevelStories(dragon, reliability.Level); | ||
| } | ||
|
|
||
| resultDict.Add(key, GrantReturn.Added()); | ||
|
|
@@ -110,6 +118,36 @@ IDictionary<TKey, Entity> entities | |
| return resultDict; | ||
| } | ||
|
|
||
| private void AddDefaultLevelStories(DragonId dragon, int reliabilityLevel) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test for this in the present tests |
||
| { | ||
| if ( | ||
| reliabilityLevel < 5 | ||
| || !MasterAsset.DragonStories.TryGetValue((int)dragon, out StoryData? storyData) | ||
| ) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| Debug.Assert( | ||
| storyData.StoryIds.Length == 2, | ||
| "Expected all dragons to have exactly two stories" | ||
| ); | ||
|
|
||
| int storiesToUnlock = reliabilityLevel >= 15 ? 2 : 1; | ||
|
|
||
| for (int i = 0; i < storiesToUnlock; i++) | ||
| { | ||
| apiContext.PlayerStoryState.Add( | ||
| new DbPlayerStoryState() | ||
| { | ||
| ViewerId = playerIdentityService.ViewerId, | ||
| StoryType = StoryTypes.Dragon, | ||
| StoryId = storyData.StoryIds[i], | ||
| } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private static partial class Log | ||
| { | ||
| [LoggerMessage( | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.