Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/utils/algorithms/calculateMMR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export async function calculatePredictedMMR(
}
}


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes my favorite kind of change

export async function calculateNewMMR(
queueId: number,
matchId: number,
Expand Down
14 changes: 7 additions & 7 deletions src/utils/matchHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,18 +644,18 @@ export async function endMatch(
teamResults = await calculateNewMMR(queueId, matchId, teamResultsData)

// Save elo_change and winstreak to database
// IMPORTANT: Always update elo_change to prevent race conditions
const updatePromises = teamResults.teams.flatMap((team) =>
team.players.map(async (player) => {
// Update win streak
await updatePlayerWinStreak(player.user_id, queueId, team.score == 1)

// Update elo change if it exists
if (player.elo_change !== undefined && player.elo_change !== null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Im being honest I don't see why exactly this would've caused issues to begin with. Maybe add some logging first to try and trace back the issue? ,flatMap is usually a bit confusing to work with, I have a suspicion something might be off because of it. Maybe rewrite it to use simpler methods instead?

I also don't see how this change would fix the issue tbh

await pool.query(
`UPDATE match_users SET elo_change = $1 WHERE match_id = $2 AND user_id = $3`,
[player.elo_change, matchId, player.user_id],
)
}
// Always update elo_change - use 0 as fallback if calculation failed
const eloChange = player.elo_change ?? 0
await pool.query(
`UPDATE match_users SET elo_change = $1 WHERE match_id = $2 AND user_id = $3`,
[eloChange, matchId, player.user_id],
)
}),
)

Expand Down
26 changes: 26 additions & 0 deletions src/utils/queueHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ export async function createMatch(
)

// Insert match_users (queue_join_time is already NULL from matchUpGames)
// First, insert all users to create the match_users records
for (const userId of userIds) {
await pool.query(
`INSERT INTO match_users (user_id, match_id, team)
Expand All @@ -543,6 +544,31 @@ export async function createMatch(
} catch (err) {}
}

// Calculate and store initial ELO changes for all players
// This prevents the race condition where elo_change is 0
try {
const { getTeamsInMatch } = await import('./matchHelpers')
const { calculatePredictedMMR } = await import('./algorithms/calculateMMR')

const teamData = await getTeamsInMatch(matchId)
if (teamData.teams.length > 0) {
// Calculate ELO changes assuming team 1 wins (as a baseline)
const winningTeamId = teamData.teams[0]?.id ?? 1
const eloChanges = await calculatePredictedMMR(queueId, teamData, winningTeamId)

// Store the calculated ELO changes in match_users table
for (const [userId, eloChange] of eloChanges.entries()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a transaction

await pool.query(
`UPDATE match_users SET elo_change = $1 WHERE match_id = $2 AND user_id = $3`,
[eloChange, matchId, userId],
)
}
}
} catch (err) {
console.error('Error calculating initial ELO changes:', err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe log additional details for ease of debugging later?

// Continue even if ELO calculation fails - it will be recalculated at match end
}

await updateQueueMessage()

// Wait 2 seconds for channel to fully propagate in Discord's API
Expand Down