Skip to content

Instantly share code, notes, and snippets.

@Pyrolistical
Created September 23, 2021 16:40
Show Gist options
  • Save Pyrolistical/5bb54291c543b6b99fa7d50324221e38 to your computer and use it in GitHub Desktop.
Save Pyrolistical/5bb54291c543b6b99fa7d50324221e38 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) => {
const results: ApexLegendsGameResult[] = body;
await db.collection('apexLegendsGames').updateOne({
_id: gameID
}, {
$set: {
state: 'completed',
results
}
});
const {tournamentID} = await db.collection('apexLegendsGames').findOne({
_id: gameID
});
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);
await db.collection('teams').bulkWrite(teamIDs.map((teamID) => ({
updateOne: {
filter: {
_id: teamID
},
update: {
$set: {
'stats.totalKills': totalKillsByTeamID[teamID],
'stats.averageKills': totalKillsByTeamID[teamID] / results.length
}
}
}
})));
return res.sendStatus(204);
});
application.get('/tournament/:tournamentID/team-stats', ({params: {tournamentID}, query: {sort, page}}, res) => {
const teamsPerPage = 10;
const pageIndex = page * teamsPerPage;
const page = db.collection('teams').find({
tournamentID
})
.sort({
[`stats.${sort}`]: 1
})
.skip(pageIndex)
.limit(teamsPerPage)
.toArray()
.map(({name, stats: {totalKills, averageKills}}) => ({
teamName: name,
totalKills,
averageKills
}));
return res.send(page);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment