Getting closer to seeing output on second scoreboard screen, hopefully

This commit is contained in:
LumaLivy 2021-11-07 21:50:50 -05:00
commit 41be6285dd

View file

@ -27,6 +27,9 @@ namespace LBPUnion.ProjectLighthouse.Controllers
[HttpPost("scoreboard/user/{id:int}")] [HttpPost("scoreboard/user/{id:int}")]
public async Task<IActionResult> SubmitScore(int id) public async Task<IActionResult> SubmitScore(int id)
{ {
User? user = await this.database.UserFromRequest(this.Request);
if (user == null) return this.StatusCode(403, "");
this.Request.Body.Position = 0; this.Request.Body.Position = 0;
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync(); string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
@ -51,22 +54,28 @@ namespace LBPUnion.ProjectLighthouse.Controllers
} }
await this.database.SaveChangesAsync(); await this.database.SaveChangesAsync();
return await TopScores(score.SlotId, score.Type); string myRanking = await GetScores(score.SlotId, score.Type, user);
return this.Ok(myRanking);
} }
[HttpGet("friendscores/user/{slotId:int}/{type:int}")] [HttpGet("friendscores/user/{slotId:int}/{type:int}")]
public async Task<IActionResult> FriendScores(int slotId, int type) public async Task<IActionResult> FriendScores(int slotId, int type)
=> await TopScores(slotId, type); //=> await TopScores(slotId, type);
=> this.Ok("<scores />");
[HttpGet("topscores/user/{slotId:int}/{type:int}")] [HttpGet("topscores/user/{slotId:int}/{type:int}")]
[SuppressMessage("ReSharper", "PossibleMultipleEnumeration")] [SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
public async Task<IActionResult> TopScores(int slotId, int type, [FromQuery] int pageStart = -1, [FromQuery] int pageSize = 5) public async Task<IActionResult> TopScores(int slotId, int type, [FromQuery] int pageStart = -1, [FromQuery] int pageSize = 5) {
{
// Get username // Get username
User? user = await this.database.UserFromRequest(this.Request); User? user = await this.database.UserFromRequest(this.Request);
if (user == null) return this.StatusCode(403, ""); if (user == null) return this.StatusCode(403, "");
return this.Ok(await GetScores(slotId, type, user, pageStart, pageSize));
}
public async Task<string> GetScores(int slotId, int type, User user, int pageStart = -1, int pageSize = 5)
{
// This is hella ugly but it technically assigns the proper rank to a score // This is hella ugly but it technically assigns the proper rank to a score
// var needed for Anonymous type returned from SELECT // var needed for Anonymous type returned from SELECT
var rankedScores = this.database.Scores.Where(s => s.SlotId == slotId && s.Type == type) var rankedScores = this.database.Scores.Where(s => s.SlotId == slotId && s.Type == type)
@ -89,7 +98,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers
// Paginated viewing: if not requesting pageStart, get results around user // Paginated viewing: if not requesting pageStart, get results around user
var pagedScores = rankedScores var pagedScores = rankedScores
.Skip(pageStart != -1 ? pageStart - 1 : myScore.Rank - 3) .Skip(pageStart != -1 || myScore == null ? pageStart - 1 : myScore.Rank - 3)
.Take(Math.Min(pageSize, 30)); .Take(Math.Min(pageSize, 30));
string serializedScores = pagedScores.Aggregate string serializedScores = pagedScores.Aggregate
@ -128,7 +137,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers
); );
} }
return this.Ok(res); return res;
} }
} }
} }