Created
September 23, 2021 16:40
-
-
Save Pyrolistical/5bb54291c543b6b99fa7d50324221e38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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