Skip to content

Instantly share code, notes, and snippets.

@Pyrolistical
Last active September 23, 2021 16:39
Show Gist options
  • Save Pyrolistical/fe9c046b9bdb87a850fff2062cc55841 to your computer and use it in GitHub Desktop.
Save Pyrolistical/fe9c046b9bdb87a850fff2062cc55841 to your computer and use it in GitHub Desktop.
type ApexLegendsGameResult = {
teamID: string,
placement: number,
kills: number
};
application.post('/apex-legends/:gameID/report-score', async ({params: {gameID}, body}, res) => {
await db.collection('apexLegendsGames').updateOne({
_id: gameID
}, {
$set: {
state: 'completed',
results: body as ApexLegendsGameResult[]
}
});
return res.sendStatus(204);
});
application.get('/tournament/:tournamentID/team-stats', ({params: {tournamentID}, query: {sort, page}}, res) => {
const games = await db.collection('apexLegendsGames').find({
state: 'completed',
tournamentID
})
.toArray();
let totalKillsByTeamID = 0;
for (const {results} of games) {
for (const {teamID, kills} of results) {
totalKillsByTeamID[teamID] = totalKillsByTeamID[teamID] || {};
totalKillsByTeamID[teamID] += kills;
}
}
const teamIDs = Object.keys(totalKillsByTeamID);
const teams = await db.collection('teams').find({
_id: {
$in: teamIDs
}
})
.toArray();
const teamByID = _.keyBy(teams, '_id');
let teamsTable = teamIDs.map((teamID) => ({
teamName: teamByID[teamID].name,
totalKills: totalKillsByTeamID[teamID],
averageKills: totalKillsByTeamID[teamID] / games.length
}));
teamsTable = _.sortBy(teamsTable, [sort]);
const teamsPerPage = 10;
const pageIndex = page * teamsPerPage;
const page = teamsTable.slice(pageIndex, pageIndex + itemsPerPage);
return res.send(page);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment