Skip to content

Commit bef491e

Browse files
committed
Potential adjustments to speed up queues
1 parent 07b9426 commit bef491e

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/utils/algorithms/calculateMMR.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
getWinningTeamFromMatch,
55
updatePlayerMmrAll,
66
} from '../queryDB'
7-
import type { teamResults } from 'psqlDB'
7+
import type { Matches, Settings, teamResults } from 'psqlDB'
88
import { setUserQueueRole } from 'utils/queueHelpers'
99
import { clamp } from 'lodash-es'
1010

@@ -131,49 +131,55 @@ export async function calculatePredictedMMR(
131131

132132
export async function calculateNewMMR(
133133
queueId: number,
134-
matchId: number,
134+
matchData: Matches,
135+
queueSettings: Settings,
135136
teamResults: teamResults,
137+
winningTeamId: number,
136138
): Promise<teamResults> {
137-
const matchData = await getMatchData(matchId)
138-
const settings = await getQueueSettings(matchData.queue_id)
139-
const winningTeamId = await getWinningTeamFromMatch(matchId)
140139

141140
try {
142141
const { teamStats, ratingChange, loserCount } =
143142
await calculateTeamStatsAndChanges(
144143
teamResults,
145-
winningTeamId ?? 0,
146-
settings.default_elo,
144+
winningTeamId,
145+
queueSettings.default_elo,
147146
)
148147

149148
// Apply changes to all teams and players
149+
const updatePromises: Promise<void>[] = []
150+
150151
for (const ts of teamStats) {
151152
const isWinner = ts.isWinner
152153
const mmrChange = isWinner ? ratingChange : -ratingChange / loserCount
153154

154155
for (const player of ts.team.players) {
155-
const oldMMR = player.elo ?? settings.default_elo
156+
const oldMMR = player.elo ?? queueSettings.default_elo
156157
const oldVolatility = player.volatility ?? 0
157158

158159
const newMMR = parseFloat((oldMMR + mmrChange).toFixed(1))
159160
const newVolatility = Math.min(oldVolatility + 1, 10)
160161

161-
// Update database
162-
await updatePlayerMmrAll(queueId, player.user_id, newMMR, newVolatility)
163-
164-
// Update teamResults object
162+
// Update teamResults object immediately
165163
player.elo = clamp(newMMR, 0, 9999)
166164
player.elo_change = parseFloat(mmrChange.toFixed(1))
167165
player.volatility = newVolatility
168166

169-
// Set user queue role
170-
await setUserQueueRole(queueId, player.user_id)
167+
// Collect database update promises to run in parallel
168+
updatePromises.push(
169+
updatePlayerMmrAll(queueId, player.user_id, newMMR, newVolatility)
170+
)
171+
updatePromises.push(
172+
setUserQueueRole(queueId, player.user_id)
173+
)
171174
}
172175

173176
// Set team score
174177
ts.team.score = isWinner ? 1 : 0
175178
}
176179

180+
// Run all database updates in parallel
181+
await Promise.all(updatePromises)
182+
177183
return teamResults
178184
} catch (err) {
179185
console.error('Error calculating new MMR:', err)

src/utils/matchHelpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ export async function endMatch(
611611

612612
const queueId = await getQueueIdFromMatch(matchId)
613613
console.log(`Queue ID for match ${matchId}: ${queueId}`)
614-
const queueSettings = await getQueueSettings(queueId, ['queue_name', 'color'])
614+
const queueSettings = await getQueueSettings(queueId)
615615
console.log(`Queue settings for match ${matchId}:`, queueSettings)
616616
const matchData = await getMatchData(matchId)
617617
console.log(`Match data for match ${matchId}:`, matchData)
@@ -626,7 +626,9 @@ export async function endMatch(
626626
})),
627627
}
628628

629-
teamResults = await calculateNewMMR(queueId, matchId, teamResultsData)
629+
console.log(`match ${matchId} team results made`)
630+
631+
teamResults = await calculateNewMMR(queueId, matchData, queueSettings, teamResultsData, winningTeamId)
630632

631633
console.log(`match ${matchId} results: ${teamResults.teams}`)
632634

0 commit comments

Comments
 (0)