Skip to content

Commit

Permalink
Merge pull request #159 from Prodeko/main
Browse files Browse the repository at this point in the history
Fix bug in deleting latest game
  • Loading branch information
nlinnanen authored Jan 18, 2024
2 parents 9ecfeb8 + d5a78fc commit 7c34a38
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions server/db/games/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ const removeLatestGame = async (): Promise<GameModel> => {
order: [['createdAt', 'DESC']],
})

const winnerLatest = await GameModel.findOne({
where: {
[Op.and]: [{ winnerId: latest?.winnerId }, { seasonId: { [Op.ne]: null } }],
},
order: [['createdAt', 'DESC']],
})

const loserLatest = await GameModel.findOne({
where: {
[Op.and]: [{ loserId: latest?.loserId }, { seasonId: { [Op.ne]: null } }],
},
order: [['createdAt', 'DESC']],
})

if (!latest) throw Error('No games in database')

// Delete the game and remove update player players' elos
Expand All @@ -187,10 +201,12 @@ const removeLatestGame = async (): Promise<GameModel> => {
updatePlayerById(latest.winnerId, {
elo: latest.winnerEloBefore,
seasonElo: latest.winnerSeasonEloBefore,
latestSeasonId: winnerLatest?.seasonId ?? null,
}),
updatePlayerById(latest.loserId, {
elo: latest.loserEloBefore,
seasonElo: latest.loserSeasonEloBefore,
latestSeasonId: loserLatest?.seasonId ?? null,
}),
])

Expand Down

0 comments on commit 7c34a38

Please sign in to comment.