mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-09-22 17:29:10 +00:00
Add support for score ranks
This commit is contained in:
parent
0938140e95
commit
85f1afaf35
1 changed files with 23 additions and 18 deletions
|
@ -58,33 +58,38 @@ namespace LBPUnion.ProjectLighthouse.Controllers
|
||||||
// Get username
|
// Get username
|
||||||
User user = await this.database.UserFromRequest(this.Request);
|
User user = await this.database.UserFromRequest(this.Request);
|
||||||
|
|
||||||
// Find your score, even if you aren't in the top list
|
// This is hella ugly but it technically assigns the proper rank to a score
|
||||||
List<Score> myScore = this.database.Scores
|
// var needed for Anonymous type returned from SELECT
|
||||||
.Where(s => s.SlotId == slotId && s.Type == type && s.PlayerIdCollection.Contains(user.Username)).ToList();
|
var rankedScores = this.database.Scores
|
||||||
|
.Where(s => s.SlotId == slotId && s.Type == type)
|
||||||
|
.OrderByDescending(s => s.Points)
|
||||||
|
.ToList()
|
||||||
|
.Select((Score s, int rank) => new { Score = s, Rank = rank + 1 });
|
||||||
|
|
||||||
//Split this out from pagination, so we can count totalNumScores below
|
// Find your score, since even if you aren't in the top list your score is pinned
|
||||||
IQueryable<Score> allScoresOfType = this.database.Scores
|
var myScore = rankedScores
|
||||||
.Where(s => s.SlotId == slotId && s.Type == type);
|
.Where(rs => rs.Score.PlayerIdCollection.Contains(user.Username))
|
||||||
|
.OrderByDescending(rs => rs.Score.Points)
|
||||||
|
.FirstOrDefault();
|
||||||
|
|
||||||
// Paginated viewing
|
// Paginated viewing
|
||||||
IQueryable<Score> pagedScores = allScoresOfType
|
var pagedScores = rankedScores
|
||||||
.OrderByDescending(s => s.Points)
|
|
||||||
.Skip(pageStart - 1)
|
.Skip(pageStart - 1)
|
||||||
.Take(Math.Min(pageSize, 30));
|
.Take(Math.Min(pageSize, 30));
|
||||||
|
|
||||||
//Calculate rank and serialize top scores
|
string serializedScores = Enumerable.Aggregate(pagedScores, string.Empty, (current, rs) => {
|
||||||
int rank = 1;
|
rs.Score.Rank = rs.Rank;
|
||||||
string serializedScores = Enumerable.Aggregate(pagedScores, string.Empty, (current, score) => {
|
return current + rs.Score.Serialize();
|
||||||
score.Rank = (pageStart - 1) * pageSize + rank++;
|
|
||||||
return current + score.Serialize();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
string res = LbpSerializer.TaggedStringElement("scores", serializedScores, new Dictionary<string, object>() {
|
string res = LbpSerializer.TaggedStringElement("scores", serializedScores, new Dictionary<string, object>() {
|
||||||
{"yourScore", myScore.OrderByDescending(score => score.Points).FirstOrDefault()},
|
{"yourScore", myScore.Score.Points},
|
||||||
{"yourRank", 0 }, // afaik, this is unused
|
{"yourRank", myScore.Rank }, //This is the numerator of your position globally in the side menu.
|
||||||
{"totalNumScores", allScoresOfType.Count() } // This is shown as "Ranked 1/x" in the side menu if you have the global highscore
|
{"totalNumScores", rankedScores.Count() } // This is the denominator of your position globally in the side menu.
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Console.WriteLine(res);
|
||||||
|
|
||||||
return this.Ok(res);
|
return this.Ok(res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue