@@ -74,32 +74,37 @@ await runInLoop(async function rate() {
7474 const ratingDiff2 = kFactor * ( win2 - e2 ) ;
7575 // Start transaction
7676 const trx = await db . transaction ( ) ;
77- // Rate each player
78- console . log ( "match %s, radiant_win: %s" , row . match_id , row . radiant_win ) ;
79- for ( let p of gcMatch . players ) {
80- const oldRating = ratingMap . get ( p . account_id ! ) ?? DEFAULT_RATING ;
81- const delta = isRadiant ( p ) ? ratingDiff1 : ratingDiff2 ;
82- // apply delta to each player (all players on a team will have the same rating change)
83- const newRating = oldRating + delta ;
84- console . log (
85- "account_id: %s, oldRating: %s, newRating: %s, delta: %s" ,
86- p . account_id ,
87- oldRating ,
88- newRating ,
89- delta ,
90- ) ;
91- // Write ratings back to players
77+ try {
78+ // Rate each player
79+ console . log ( "match %s, radiant_win: %s" , row . match_id , row . radiant_win ) ;
80+ for ( let p of gcMatch . players ) {
81+ const oldRating = ratingMap . get ( p . account_id ! ) ?? DEFAULT_RATING ;
82+ const delta = isRadiant ( p ) ? ratingDiff1 : ratingDiff2 ;
83+ // apply delta to each player (all players on a team will have the same rating change)
84+ const newRating = oldRating + delta ;
85+ console . log (
86+ "account_id: %s, oldRating: %s, newRating: %s, delta: %s" ,
87+ p . account_id ,
88+ oldRating ,
89+ newRating ,
90+ delta ,
91+ ) ;
92+ // Write ratings back to players
93+ await trx . raw (
94+ `INSERT INTO ${ tableName } (account_id, computed_mmr, delta, match_id) VALUES(?, ?, ?, ?) ON CONFLICT(account_id) DO UPDATE SET computed_mmr = EXCLUDED.computed_mmr, delta = EXCLUDED.delta, match_id = EXCLUDED.match_id` ,
95+ [ p . account_id , newRating , delta , row . match_id ] ,
96+ ) ;
97+ }
98+ // Delete row
9299 await trx . raw (
93- `INSERT INTO ${ tableName } (account_id, computed_mmr, delta, match_id) VALUES(?, ?, ?, ?) ON CONFLICT(account_id) DO UPDATE SET computed_mmr = EXCLUDED.computed_mmr, delta = EXCLUDED.delta, match_id = EXCLUDED.match_id` ,
94- [ p . account_id , newRating , delta , row . match_id ] ,
100+ "DELETE FROM rating_queue WHERE match_seq_num = ?" ,
101+ row . match_seq_num ,
95102 ) ;
103+ // Commit transaction
104+ await trx . commit ( ) ;
105+ redisCount ( "rater" ) ;
106+ } catch ( e ) {
107+ await trx . rollback ( ) ;
108+ throw e ;
96109 }
97- // Delete row
98- await trx . raw (
99- "DELETE FROM rating_queue WHERE match_seq_num = ?" ,
100- row . match_seq_num ,
101- ) ;
102- // Commit transaction
103- await trx . commit ( ) ;
104- redisCount ( "rater" ) ;
105110} , 0 ) ;
0 commit comments