Browse Source

Fix post-game rank announcements not getting displayed

Clément Wolf 6 tháng trước cách đây
mục cha
commit
3e7fb9787a
2 tập tin đã thay đổi với 19 bổ sung3 xóa
  1. 17 3
      elo.js
  2. 2 0
      elo_cache.js

+ 17 - 3
elo.js

@@ -2,7 +2,7 @@ import bancho from './bancho.js';
 import db from './database.js';
 import Config from './util/config.js';
 import {get_user_by_id} from './user.js';
-import {gen_url} from './util/helpers.js';
+import {capture_sentry_exception, gen_url} from './util/helpers.js';
 import {get_division_from_elo, get_rankup_progress, update_the_one} from './elo_cache.js';
 
 
@@ -170,10 +170,14 @@ async function save_game_and_update_rating(lobby, game) {
     'The One',
   ];
 
+  // Ensure users exist
+  for (const score of game.scores) {
+    score.player = await get_user_by_id(score.user_id);
+  }
+
   const rank_changes = [];
   for (const score of game.scores) {
-    const player = await get_user_by_id(score.user_id);
-    await update_the_one(score.user_id, score.rating.elo, game.mode_int);
+    const player = score.player;
     if (player.ratings[game.mode_int].s3_scores < Config.games_needed_for_rank) continue;
 
     const old_rank_text = player.ratings[game.mode_int].division;
@@ -186,6 +190,16 @@ async function save_game_and_update_rating(lobby, game) {
     }
   }
 
+  // Update "The One"
+  for (const score of game.scores) {
+    try {
+      await update_the_one(score.user_id, score.rating.elo, game.mode_int);
+    } catch (err) {
+      console.error('Error while updating the one', err);
+      capture_sentry_exception(err);
+    }
+  }
+
   if (rank_changes.length > 0) {
     // Max 8 rank updates per message - or else it starts getting truncated
     const MAX_UPDATES_PER_MSG = 6;

+ 2 - 0
elo_cache.js

@@ -27,6 +27,8 @@ async function update_the_one(user_id, elo, mode) {
   // Check that "The One" is not deranking after a loss
   if (user_id == the_ones[mode].user_id) {
     const the_one = db.prepare('SELECT user_id, elo FROM rating WHERE s3_scores >= ? AND mode = ? ORDER BY elo DESC LIMIT 1').get(Config.games_needed_for_rank, mode);
+    if (!the_one) return;
+
     division_floors[mode][divisions.length - 1] = the_one.elo;
     the_ones[mode] = {
       user_id: the_one.user_id,