Skip to content

Commit

Permalink
Hopefully optimize the DB and split the YAML schema out (#27)
Browse files Browse the repository at this point in the history
* Hopefully optimize the DB and split the YAML schema out

* Rewrite leaderboard to use better queries, more migrations

* Tidy up GetAccountHistory

* Fix up the ingestion, hopefully...

* Fix index out of range, with excessive lists

* Try to fix data again

* Actually assign a username to a player
  • Loading branch information
SaphireLattice authored Aug 21, 2024
1 parent 4c0ce89 commit 95add8e
Show file tree
Hide file tree
Showing 30 changed files with 3,408 additions and 610 deletions.
179 changes: 90 additions & 89 deletions ReplayBrowser/Controllers/AccountController.cs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions ReplayBrowser/Controllers/DataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public DataController(ReplayDbContext context)
public async Task<List<string>> GetUsernameCompletion(
[FromQuery] string username)
{
var completions = await _context.Players
.Where(p => p.PlayerOocName.ToLower().StartsWith(username.ToLower()))
.Select(p => p.PlayerOocName)
var completions = await _context.ReplayParticipants
.Where(p => p.Username.ToLower().StartsWith(username.ToLower()))
.Select(p => p.Username)
.Distinct() // Remove duplicates
.Take(10)
.ToListAsync();
Expand Down
55 changes: 28 additions & 27 deletions ReplayBrowser/Controllers/ReplayController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ReplayBrowser.Data;
using ReplayBrowser.Data.Models;
using ReplayBrowser.Helpers;
using ReplayBrowser.Services;

Expand All @@ -16,14 +17,14 @@ public class ReplayController : Controller
private readonly ReplayDbContext _dbContext;
private readonly AccountService _accountService;
private readonly ReplayHelper _replayHelper;

public ReplayController(ReplayDbContext dbContext, AccountService accountService, ReplayHelper replayHelper)
{
_dbContext = dbContext;
_accountService = accountService;
_replayHelper = replayHelper;
}

[HttpGet("{replayId}")]
[AllowAnonymous]
public async Task<IActionResult> GetReplay(int replayId)
Expand All @@ -34,10 +35,10 @@ public async Task<IActionResult> GetReplay(int replayId)
{
return NotFound();
}

return Ok(replay);
}

/// <summary>
/// Deletes all stored profiles for a server group.
/// </summary>
Expand All @@ -49,27 +50,27 @@ public async Task<IActionResult> DeleteProfile(string serverId)
{
return Unauthorized();
}

var guidRequestor = AccountHelper.GetAccountGuid(User);

var requestor = await _dbContext.Accounts
.Include(a => a.Settings)
.Include(a => a.History)
.FirstOrDefaultAsync(a => a.Guid == guidRequestor);

if (requestor == null)
{
return NotFound("Account is null. This should not happen.");
}
if (!requestor.IsAdmin)

if (!requestor.IsAdmin)
return Unauthorized("You are not an admin.");

var players = await _dbContext.Replays
.Where(r => r.ServerId == serverId)
.Include(r => r.RoundEndPlayers)
.Where(r => r.RoundEndPlayers != null)
.SelectMany(r => r.RoundEndPlayers)
.Include(r => r.RoundParticipants)
.Where(r => r.RoundParticipants != null)
.SelectMany(r => r.RoundParticipants)

Check warning on line 73 in ReplayBrowser/Controllers/ReplayController.cs

View workflow job for this annotation

GitHub Actions / deploy

Possible null reference return.
.Select(p => p.PlayerGuid)
.Distinct()
.ToListAsync();
Expand All @@ -80,26 +81,26 @@ await _dbContext.Database.ExecuteSqlRawAsync($"""
DELETE FROM "CharacterData"
WHERE "CollectedPlayerDataPlayerGuid" = '{player}';
""");

await _dbContext.Database.ExecuteSqlRawAsync($"""
DELETE FROM "JobCountData"
WHERE "CollectedPlayerDataPlayerGuid" = '{player}';
""");
}

await _dbContext.PlayerProfiles
.Where(p => players.Contains(p.PlayerGuid))
.ExecuteDeleteAsync();

return Ok();
}

[HttpGet("profile/{profileGuid:guid}")]
public async Task<IActionResult> GetPlayerData(Guid profileGuid)
{
// ok very jank, we construct a AuthenticationState object from the current user
var authState = new AuthenticationState(HttpContext.User);

try
{
return Ok(await _replayHelper.GetPlayerProfile(profileGuid, authState));
Expand All @@ -109,7 +110,7 @@ public async Task<IActionResult> GetPlayerData(Guid profileGuid)
return Unauthorized(e.Message);
}
}

/// <summary>
/// Marks a profile "watched" for the current user.
/// </summary>
Expand All @@ -122,25 +123,25 @@ public async Task<IActionResult> WatchProfile(Guid profileGuid)
.Include(a => a.Settings)
.Include(a => a.History)
.FirstOrDefaultAsync(a => a.Guid == guid);

if (account == null)
{
return Unauthorized();
}

var isWatched = account.SavedProfiles.Contains(profileGuid);

if (!account.SavedProfiles.Remove(profileGuid))
{
account.SavedProfiles.Add(profileGuid);
}

await _dbContext.SaveChangesAsync();

return Ok(!isWatched);
}


/// <summary>
/// Marks a replay as a favorite for the current user.
/// </summary>
Expand All @@ -153,25 +154,25 @@ public async Task<IActionResult> FavoriteReplay(int replayId)
.Include(a => a.Settings)
.Include(a => a.History)
.FirstOrDefaultAsync(a => a.Guid == guid);

if (account == null)
{
return Unauthorized();
}

var replay = await _dbContext.Replays.FindAsync(replayId);
if (replay == null)
{
return NotFound();
}

var isFavorited = account.FavoriteReplays.Contains(replayId);

if (!account.FavoriteReplays.Remove(replayId))
{
account.FavoriteReplays.Add(replayId);
}

await _dbContext.SaveChangesAsync();

return Ok(!isFavorited);
Expand Down
Loading

0 comments on commit 95add8e

Please sign in to comment.