From 4925224b0faaaa154c41d27b29a06668d6f1fea7 Mon Sep 17 00:00:00 2001 From: Jack Date: Tue, 7 Jul 2020 20:30:39 +0100 Subject: [PATCH] fix for a bug where the role would get overwritten --- functions/index.js | 31 +++++++++++++++++++++---------- public/js/script.js | 1 - 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/functions/index.js b/functions/index.js index 262f2aef0..f6a5f9b03 100644 --- a/functions/index.js +++ b/functions/index.js @@ -367,16 +367,27 @@ exports.testCompleted = functions.https.onCall((request, response) => { request.obj )}` ); - if ( - obj.mode === "time" && - String(obj.mode2) === "60" && - obj.discordId !== null && - obj.discordId !== undefined - ) { - console.log( - `sending command to the bot to update the role for user ${request.uid} with wpm ${obj.wpm}` - ); - updateDiscordRole(obj.discordId, obj.wpm); + if (obj.mode === "time" && String(obj.mode2) === "60") { + db.collection("users") + .doc(request.uid) + .get() + .then((ret) => { + let userdata = ret.data(); + + let besttime60 = 0; + try { + dbSnapshot.personalBests.time[60].forEach((result) => { + if (result.wpm > besttime60) besttime60 = result.wpm; + }); + } catch (e) {} + + if (obj.wpm > besttime60) { + console.log( + `sending command to the bot to update the role for user ${request.uid} with wpm ${obj.wpm}` + ); + updateDiscordRole(userdata.discordId, obj.wpm); + } + }); } return 2; } else { diff --git a/public/js/script.js b/public/js/script.js index e46df5798..d614d187f 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -1008,7 +1008,6 @@ function showResult(difficultyFailed = false) { blindMode: config.blindMode, theme: config.theme, tags: activeTags, - discordId: dbSnapshot !== null ? dbSnapshot.discordId : null, }; if ( config.difficulty == "normal" ||