chore: add variant scoring fields, fix tie handling, and fix test files for normalizedName

This commit is contained in:
2026-03-30 22:48:31 -07:00
parent fb04a6c3f1
commit 5d1755f082
9 changed files with 174 additions and 35 deletions
+1
View File
@@ -42,6 +42,7 @@ export const auth = betterAuth({
const newPlayer = await prisma.player.create({
data: {
name: uniqueName,
normalizedName: uniqueName.toLowerCase(),
},
});
+59 -13
View File
@@ -51,16 +51,20 @@ export function calculateTeamElo(rating1: number, rating2: number): number {
* @param currentRating Current player rating
* @param currentGamesPlayed Current games played count
* @param currentWins Current wins count
* @param currentLosses Current losses count
* @param eloChange Elo rating change
* @param won Whether the player won
* @param isTie Whether the match was a tie
* @returns Updated statistics
*/
export function updatePlayerStats(
currentRating: number,
currentGamesPlayed: number,
currentWins: number,
currentLosses: number,
eloChange: number,
won: boolean
won: boolean,
isTie: boolean = false
): {
newRating: number;
newGamesPlayed: number;
@@ -70,8 +74,17 @@ export function updatePlayerStats(
} {
const newRating = currentRating + Math.round(eloChange);
const newGamesPlayed = currentGamesPlayed + 1;
const newWins = currentWins + (won ? 1 : 0);
const newLosses = newGamesPlayed - newWins;
let newWins = currentWins;
let newLosses = currentLosses;
if (isTie) {
// For ties, don't increment wins or losses
} else if (won) {
newWins += 1;
} else {
newLosses += 1;
}
const winRate = newGamesPlayed > 0 ? newWins / newGamesPlayed : 0;
return {
@@ -245,40 +258,66 @@ export async function recalculateAllElo(prisma: PrismaClient) {
// Update player stats
const team1Won = team1Score > team2Score;
const team2Won = team2Score > team1Score;
const isTie = team1Score === team2Score;
// Update Player 1 (team 1, player 1)
const stats1 = getPlayerStats(team1P1.id);
stats1.rating += p1Change;
stats1.gamesPlayed += 1;
stats1.wins += team1Won ? 1 : 0;
stats1.losses += team1Won ? 0 : 1;
if (isTie) {
// For ties, don't increment wins or losses (gamesPlayed is already incremented)
} else if (team1Won) {
stats1.wins += 1;
} else {
stats1.losses += 1;
}
// Update Player 2 (team 1, player 2)
const stats2 = getPlayerStats(team1P2.id);
stats2.rating += p2Change;
stats2.gamesPlayed += 1;
stats2.wins += team1Won ? 1 : 0;
stats2.losses += team1Won ? 0 : 1;
if (isTie) {
// For ties, don't increment wins or losses (gamesPlayed is already incremented)
} else if (team1Won) {
stats2.wins += 1;
} else {
stats2.losses += 1;
}
// Update Player 3 (team 2, player 1)
const stats3 = getPlayerStats(team2P1.id);
stats3.rating += p3Change;
stats3.gamesPlayed += 1;
stats3.wins += team2Won ? 1 : 0;
stats3.losses += team2Won ? 0 : 1;
if (isTie) {
// For ties, don't increment wins or losses (gamesPlayed is already incremented)
} else if (team2Won) {
stats3.wins += 1;
} else {
stats3.losses += 1;
}
// Update Player 4 (team 2, player 2)
const stats4 = getPlayerStats(team2P2.id);
stats4.rating += p4Change;
stats4.gamesPlayed += 1;
stats4.wins += team2Won ? 1 : 0;
stats4.losses += team2Won ? 0 : 1;
if (isTie) {
// For ties, don't increment wins or losses (gamesPlayed is already incremented)
} else if (team2Won) {
stats4.wins += 1;
} else {
stats4.losses += 1;
}
// Update partnership stats for team 1
const partnership1 = getPartnershipStats(team1P1.id, team1P2.id);
partnership1.gamesPlayed += 1;
partnership1.wins += team1Won ? 1 : 0;
partnership1.losses += team1Won ? 0 : 1;
if (isTie) {
// For ties, don't increment wins or losses (gamesPlayed is already incremented)
} else if (team1Won) {
partnership1.wins += 1;
} else {
partnership1.losses += 1;
}
partnership1.totalEloChange += p1Change + p2Change;
if (playedAt && (!partnership1.lastPlayed || playedAt > partnership1.lastPlayed)) {
partnership1.lastPlayed = playedAt;
@@ -287,6 +326,13 @@ export async function recalculateAllElo(prisma: PrismaClient) {
// Update partnership stats for team 2
const partnership2 = getPartnershipStats(team2P1.id, team2P2.id);
partnership2.gamesPlayed += 1;
if (isTie) {
// For ties, don't increment wins or losses (gamesPlayed is already incremented)
} else if (team2Won) {
partnership2.wins += 1;
} else {
partnership2.losses += 1;
}
partnership2.wins += team2Won ? 1 : 0;
partnership2.losses += team2Won ? 0 : 1;
partnership2.totalEloChange += p3Change + p4Change;