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
+25 -17
View File
@@ -112,14 +112,14 @@ export async function POST(request: Request) {
// Update player stats (only if it's a casual match - tournament matches update via their own flow)
if (isCasual) {
await updatePlayerStats(parseInt(team1P1Id), team1Won, player1EloChange);
await updatePlayerStats(parseInt(team1P2Id), team1Won, player2EloChange);
await updatePlayerStats(parseInt(team2P1Id), team2Won, player3EloChange);
await updatePlayerStats(parseInt(team2P2Id), team2Won, player4EloChange);
await updatePlayerStats(parseInt(team1P1Id), team1Won, player1EloChange, isTie);
await updatePlayerStats(parseInt(team1P2Id), team1Won, player2EloChange, isTie);
await updatePlayerStats(parseInt(team2P1Id), team2Won, player3EloChange, isTie);
await updatePlayerStats(parseInt(team2P2Id), team2Won, player4EloChange, isTie);
// Update partnership stats for casual matches
await updatePartnershipStats(parseInt(team1P1Id), parseInt(team1P2Id), team1Won, player1EloChange);
await updatePartnershipStats(parseInt(team2P1Id), parseInt(team2P2Id), team2Won, player3EloChange);
await updatePartnershipStats(parseInt(team1P1Id), parseInt(team1P2Id), team1Won, player1EloChange, isTie);
await updatePartnershipStats(parseInt(team2P1Id), parseInt(team2P2Id), team2Won, player3EloChange, isTie);
}
return NextResponse.json({
@@ -136,7 +136,7 @@ export async function POST(request: Request) {
}
}
async function updatePlayerStats(playerId: number, won: boolean, eloChange: number) {
async function updatePlayerStats(playerId: number, won: boolean, eloChange: number, isTie: boolean = false) {
const player = await prisma.player.findUnique({
where: { id: playerId },
});
@@ -150,13 +150,13 @@ async function updatePlayerStats(playerId: number, won: boolean, eloChange: numb
data: {
currentElo: player.currentElo + Math.round(eloChange),
gamesPlayed: player.gamesPlayed + 1,
wins: won ? player.wins + 1 : player.wins,
losses: won ? player.losses : player.losses + 1,
wins: isTie ? player.wins : (won ? player.wins + 1 : player.wins),
losses: isTie ? player.losses : (won ? player.losses : player.losses + 1),
},
});
}
async function updatePartnershipStats(player1Id: number, player2Id: number, won: boolean, eloChange: number) {
async function updatePartnershipStats(player1Id: number, player2Id: number, won: boolean, eloChange: number, isTie: boolean = false) {
const [smallId, largeId] = player1Id < player2Id ? [player1Id, player2Id] : [player2Id, player1Id];
const existing = await prisma.partnershipStat.findFirst({
@@ -166,16 +166,24 @@ async function updatePartnershipStats(player1Id: number, player2Id: number, won:
},
});
if (existing) {
await prisma.partnershipStat.update({
where: { id: existing.id },
data: {
const updateData = isTie
? {
gamesPlayed: { increment: 1 },
totalEloChange: { increment: eloChange },
lastPlayed: new Date(),
}
: {
gamesPlayed: { increment: 1 },
wins: won ? { increment: 1 } : undefined,
losses: won ? undefined : { increment: 1 },
totalEloChange: { increment: eloChange },
lastPlayed: new Date(),
},
};
if (existing) {
await prisma.partnershipStat.update({
where: { id: existing.id },
data: updateData,
});
} else {
await prisma.partnershipStat.create({
@@ -183,8 +191,8 @@ async function updatePartnershipStats(player1Id: number, player2Id: number, won:
player1Id: smallId,
player2Id: largeId,
gamesPlayed: 1,
wins: won ? 1 : 0,
losses: won ? 0 : 1,
wins: isTie ? 0 : (won ? 1 : 0),
losses: isTie ? 0 : (won ? 0 : 1),
totalEloChange: eloChange,
lastPlayed: new Date(),
},