|
4 | 4 | getWinningTeamFromMatch, |
5 | 5 | updatePlayerMmrAll, |
6 | 6 | } from '../queryDB' |
7 | | -import type { teamResults } from 'psqlDB' |
| 7 | +import type { Matches, Settings, teamResults } from 'psqlDB' |
8 | 8 | import { setUserQueueRole } from 'utils/queueHelpers' |
9 | 9 | import { clamp } from 'lodash-es' |
10 | 10 |
|
@@ -131,49 +131,55 @@ export async function calculatePredictedMMR( |
131 | 131 |
|
132 | 132 | export async function calculateNewMMR( |
133 | 133 | queueId: number, |
134 | | - matchId: number, |
| 134 | + matchData: Matches, |
| 135 | + queueSettings: Settings, |
135 | 136 | teamResults: teamResults, |
| 137 | + winningTeamId: number, |
136 | 138 | ): Promise<teamResults> { |
137 | | - const matchData = await getMatchData(matchId) |
138 | | - const settings = await getQueueSettings(matchData.queue_id) |
139 | | - const winningTeamId = await getWinningTeamFromMatch(matchId) |
140 | 139 |
|
141 | 140 | try { |
142 | 141 | const { teamStats, ratingChange, loserCount } = |
143 | 142 | await calculateTeamStatsAndChanges( |
144 | 143 | teamResults, |
145 | | - winningTeamId ?? 0, |
146 | | - settings.default_elo, |
| 144 | + winningTeamId, |
| 145 | + queueSettings.default_elo, |
147 | 146 | ) |
148 | 147 |
|
149 | 148 | // Apply changes to all teams and players |
| 149 | + const updatePromises: Promise<void>[] = [] |
| 150 | + |
150 | 151 | for (const ts of teamStats) { |
151 | 152 | const isWinner = ts.isWinner |
152 | 153 | const mmrChange = isWinner ? ratingChange : -ratingChange / loserCount |
153 | 154 |
|
154 | 155 | for (const player of ts.team.players) { |
155 | | - const oldMMR = player.elo ?? settings.default_elo |
| 156 | + const oldMMR = player.elo ?? queueSettings.default_elo |
156 | 157 | const oldVolatility = player.volatility ?? 0 |
157 | 158 |
|
158 | 159 | const newMMR = parseFloat((oldMMR + mmrChange).toFixed(1)) |
159 | 160 | const newVolatility = Math.min(oldVolatility + 1, 10) |
160 | 161 |
|
161 | | - // Update database |
162 | | - await updatePlayerMmrAll(queueId, player.user_id, newMMR, newVolatility) |
163 | | - |
164 | | - // Update teamResults object |
| 162 | + // Update teamResults object immediately |
165 | 163 | player.elo = clamp(newMMR, 0, 9999) |
166 | 164 | player.elo_change = parseFloat(mmrChange.toFixed(1)) |
167 | 165 | player.volatility = newVolatility |
168 | 166 |
|
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 | + ) |
171 | 174 | } |
172 | 175 |
|
173 | 176 | // Set team score |
174 | 177 | ts.team.score = isWinner ? 1 : 0 |
175 | 178 | } |
176 | 179 |
|
| 180 | + // Run all database updates in parallel |
| 181 | + await Promise.all(updatePromises) |
| 182 | + |
177 | 183 | return teamResults |
178 | 184 | } catch (err) { |
179 | 185 | console.error('Error calculating new MMR:', err) |
|
0 commit comments