diff --git a/ReplayBrowser/Helpers/ReplayHelper.cs b/ReplayBrowser/Helpers/ReplayHelper.cs index de57cf8..ba2cc05 100644 --- a/ReplayBrowser/Helpers/ReplayHelper.cs +++ b/ReplayBrowser/Helpers/ReplayHelper.cs @@ -37,7 +37,7 @@ public async Task> GetMostRecentReplays(AuthenticationState state) var caller = AccountHelper.GetAccountGuid(state); replays = FilterReplays(replays, caller); - var account = _accountService.GetAccount(state); + var account = await _accountService.GetAccount(state); await _accountService.AddHistory(account, new HistoryEntry() { @@ -51,7 +51,7 @@ public async Task> GetMostRecentReplays(AuthenticationState state) public async Task GetPlayerProfile(Guid playerGuid, AuthenticationState authenticationState) { - var accountCaller = _accountService.GetAccount(authenticationState); + var accountCaller = await _accountService.GetAccount(authenticationState); var accountRequested = _accountService.GetAccountSettings(playerGuid); @@ -244,7 +244,7 @@ public async Task GetTotalReplayCount() if (replay == null) return null; - var caller = _accountService.GetAccount(authstate); + var caller = await _accountService.GetAccount(authstate); replay = FilterReplay(replay, caller); return replay; } @@ -281,7 +281,7 @@ private Replay FilterReplay(Replay replay, Account? caller = null) public async Task SearchReplays(SearchMode searchMode, string query, int page, AuthenticationState authenticationState) { - var callerAccount = _accountService.GetAccount(authenticationState); + var callerAccount = await _accountService.GetAccount(authenticationState); switch (searchMode) { diff --git a/ReplayBrowser/Pages/Account/Logs.razor b/ReplayBrowser/Pages/Account/Logs.razor index 825c159..d9797f0 100644 --- a/ReplayBrowser/Pages/Account/Logs.razor +++ b/ReplayBrowser/Pages/Account/Logs.razor @@ -128,7 +128,7 @@ else } var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); - var account = AccountService.GetAccount(authState); + var account = await AccountService.GetAccount(authState); if (account == null || !account.IsAdmin) { Is1984 = true; diff --git a/ReplayBrowser/Pages/Account/Manage.razor b/ReplayBrowser/Pages/Account/Manage.razor index 36c5c6c..d2950e7 100644 --- a/ReplayBrowser/Pages/Account/Manage.razor +++ b/ReplayBrowser/Pages/Account/Manage.razor @@ -15,7 +15,7 @@ @if(ErrorMessage != null) { } @@ -68,7 +68,13 @@ else protected override async Task OnInitializedAsync() { var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); - account = AccountService.GetAccount(authState); + account = await AccountService.GetAccount(authState); + + if (account == null) + { + ErrorMessage = "Account not found, please go to the home page and then try again. How did you get here?"; + return; + } var uri = new Uri(NavigationManager.Uri); var query = uri.Query; diff --git a/ReplayBrowser/Services/AccountService.cs b/ReplayBrowser/Services/AccountService.cs index c8a9504..4903f35 100644 --- a/ReplayBrowser/Services/AccountService.cs +++ b/ReplayBrowser/Services/AccountService.cs @@ -16,13 +16,15 @@ public class AccountService : IHostedService, IDisposable { private readonly IMemoryCache _cache; private readonly IServiceScopeFactory _scopeFactory; + private readonly Ss14ApiHelper _apiHelper; private bool _settingsGenerated = false; - public AccountService(IMemoryCache cache, IServiceScopeFactory scopeFactory) + public AccountService(IMemoryCache cache, IServiceScopeFactory scopeFactory, Ss14ApiHelper apiHelper) { _cache = cache; _scopeFactory = scopeFactory; + _apiHelper = apiHelper; } public Task StartAsync(CancellationToken cancellationToken) @@ -77,15 +79,36 @@ public void Dispose() _cache.Dispose(); } - public Account? GetAccount(AuthenticationState authstate) + public async Task? GetAccount(AuthenticationState authstate) { var guid = AccountHelper.GetAccountGuid(authstate); using var scope = _scopeFactory.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); - return context.Accounts + var account = context.Accounts .Include(a => a.Settings) .Include(a => a.History) .FirstOrDefault(a => a.Guid == guid); + + if (account == null) + { + if (guid == null) + { + return null; + } + + // If the account doesn't exist, we need to create it. + account = new Account() + { + Guid = (Guid)guid, + Username = (await _apiHelper.FetchPlayerDataFromGuid((Guid)guid)).Username, + Settings = new AccountSettings() + }; + + context.Accounts.Add(account); + await context.SaveChangesAsync(); + } + + return account; } public async Task UpdateAccount(Account? account) diff --git a/ReplayBrowser/Services/LeaderboardService.cs b/ReplayBrowser/Services/LeaderboardService.cs index d7e03b4..9a7e582 100644 --- a/ReplayBrowser/Services/LeaderboardService.cs +++ b/ReplayBrowser/Services/LeaderboardService.cs @@ -144,10 +144,10 @@ public async Task GetLeaderboard(RangeOption rangeOption, strin { var context = _scopeFactory.CreateScope().ServiceProvider.GetRequiredService(); - var accountCaller = _accountService.GetAccount(authenticationState); - + Account? accountCaller = null; if (logAction) { + accountCaller = await _accountService.GetAccount(authenticationState); await _accountService.AddHistory(accountCaller, new HistoryEntry() { Action = Enum.GetName(typeof(Action), Action.LeaderboardViewed) ?? "Unknown", @@ -156,7 +156,7 @@ public async Task GetLeaderboard(RangeOption rangeOption, strin }); } - if (username != null) + if (username != null && accountCaller != null) { var accountRequested = await context.Accounts .Include(a => a.Settings)