From a1a6021e00f517137f6b59340e56fa53c4bb93cc Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 17 Jan 2021 12:08:45 -0500 Subject: [PATCH] Save all high score users --- commands/games-mp/typing-race.js | 12 +++++++++--- commands/games-sp/anagramica.js | 16 +++++++++++----- commands/games-sp/minesweeper.js | 14 ++------------ commands/games-sp/reaction-time.js | 12 +++++++++--- commands/games-sp/typing-test.js | 12 +++++++++--- commands/util-public/high-scores.js | 26 +++++++++++--------------- util/Util.js | 15 +++++++++++++++ 7 files changed, 66 insertions(+), 41 deletions(-) diff --git a/commands/games-mp/typing-race.js b/commands/games-mp/typing-race.js index 975189f2..208ea33d 100644 --- a/commands/games-mp/typing-race.js +++ b/commands/games-mp/typing-race.js @@ -1,6 +1,6 @@ const Command = require('../../structures/Command'); const { stripIndents } = require('common-tags'); -const { verify } = require('../../util/Util'); +const { verify, fetchHSUserDisplay } = require('../../util/Util'); const sentences = require('../../assets/json/typing-test'); module.exports = class TypingRaceCommand extends Command { @@ -48,12 +48,18 @@ module.exports = class TypingRaceCommand extends Command { const newScore = Date.now() - now; const highScoreGet = await this.client.redis.get('typing-test'); const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; - if (!highScore || highScore > newScore) await this.client.redis.set('typing-test', newScore); + const highScoreUser = await this.client.redis.get('typing-test-user'); + const scoreBeat = !highScore || highScore > newScore; + const user = await fetchHSUserDisplay(this.client, highScoreUser); + if (scoreBeat) { + await this.client.redis.set('typing-test', newScore); + await this.client.redis.set('typing-test-user', winner.first().author.id); + } this.client.games.delete(msg.channel.id); if (!winner.size) return msg.say('Oh... No one won.'); return msg.say(stripIndents` The winner is ${winner.first().author}! (Took ${newScore / 1000} seconds) - ${!highScore || highScore > newScore ? `**New High Score!** Old:` : `High Score:`} ${highScore / 1000} + ${scoreBeat ? `**New High Score!** Old:` : `High Score:`} ${highScore / 1000} (Held by ${user}) `); } catch (err) { this.client.games.delete(msg.channel.id); diff --git a/commands/games-sp/anagramica.js b/commands/games-sp/anagramica.js index bef413f0..cb7a06b3 100644 --- a/commands/games-sp/anagramica.js +++ b/commands/games-sp/anagramica.js @@ -1,7 +1,7 @@ const Command = require('../../structures/Command'); const { stripIndents } = require('common-tags'); const request = require('node-superfetch'); -const { reactIfAble } = require('../../util/Util'); +const { reactIfAble, fetchHSUserDisplay } = require('../../util/Util'); const scores = require('../../assets/json/anagramica'); const pool = 'abcdefghijklmnopqrstuvwxyz'.split(''); const { SUCCESS_EMOJI_ID, FAILURE_EMOJI_ID } = process.env; @@ -68,23 +68,29 @@ module.exports = class AnagramicaCommand extends Command { }); const highScoreGet = await this.client.redis.get('anagramica'); const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; - if (!highScore || highScore < points) await this.client.redis.set('anagramica', points); + const highScoreUser = await this.client.redis.get('anagramica-user'); + const scoreBeat = !highScore || highScore < points; + const user = await fetchHSUserDisplay(this.client, highScoreUser); + if (scoreBeat) { + await this.client.redis.set('anagramica', points); + await this.client.redis.set('anagramica-user', msg.author.id); + } this.client.games.delete(msg.channel.id); if (!msgs.size) { return msg.reply(stripIndents` Couldn't even think of one? Ouch. - ${!highScore || highScore < points ? `**New High Score!** Old:` : `High Score:`} ${highScore} + ${scoreBeat ? `**New High Score!** Old:` : `High Score:`} ${highScore} (Held by ${user}) `); } if (points < 1) { return msg.reply(stripIndents` Ouch, your final score was **${points}**. Try harder next time! - ${!highScore || highScore < points ? `**New High Score!** Old:` : `High Score:`} ${highScore} + ${scoreBeat ? `**New High Score!** Old:` : `High Score:`} ${highScore} (Held by ${user}) `); } return msg.reply(stripIndents` Nice job! Your final score was **${points}**! - ${!highScore || highScore < points ? `**New High Score!** Old:` : `High Score:`} ${highScore} + ${scoreBeat ? `**New High Score!** Old:` : `High Score:`} ${highScore} (Held by ${user}) `); } catch (err) { this.client.games.delete(msg.channel.id); diff --git a/commands/games-sp/minesweeper.js b/commands/games-sp/minesweeper.js index b361f3e6..5dd9e8ed 100644 --- a/commands/games-sp/minesweeper.js +++ b/commands/games-sp/minesweeper.js @@ -3,7 +3,7 @@ const BombSweeper = require('bombsweeper.js'); const moment = require('moment'); require('moment-duration-format'); const { stripIndents } = require('common-tags'); -const { removeFromArray, verify } = require('../../util/Util'); +const { removeFromArray, verify, fetchHSUserDisplay } = require('../../util/Util'); const nums = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟']; const turnRegex = /^(flag )?(\d+), ?(\d+)/i; @@ -102,17 +102,7 @@ module.exports = class MinesweeperCommand extends Command { const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; const highScoreUser = await this.client.redis.get(`minesweeper-${size}-user`); const scoreBeat = win && (!highScore || highScore > newScore); - let user; - if (highScoreUser) { - try { - const fetched = await this.client.users.fetch(highScoreUser); - user = fetched.tag; - } catch { - user = 'Unknown'; - } - } else { - user = 'no one'; - } + const user = await fetchHSUserDisplay(this.client, highScoreUser); if (scoreBeat) { await this.client.redis.set(`minesweeper-${size}`, newScore); await this.client.redis.set(`minesweeper-${size}-user`, msg.author.id); diff --git a/commands/games-sp/reaction-time.js b/commands/games-sp/reaction-time.js index c3db211f..677ff1c6 100644 --- a/commands/games-sp/reaction-time.js +++ b/commands/games-sp/reaction-time.js @@ -1,6 +1,6 @@ const Command = require('../../structures/Command'); const { stripIndents } = require('common-tags'); -const { delay, randomRange } = require('../../util/Util'); +const { delay, randomRange, fetchHSUserDisplay } = require('../../util/Util'); const words = ['fire', 'draw', 'shoot', 'bang', 'pull', 'boom']; module.exports = class ReactionTimeCommand extends Command { @@ -32,12 +32,18 @@ module.exports = class ReactionTimeCommand extends Command { const newScore = Date.now() - now; const highScoreGet = await this.client.redis.get('reaction-time'); const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; - if (!highScore || highScore > newScore) await this.client.redis.set('reaction-time', newScore); + const highScoreUser = await this.client.redis.get('reaction-time-user'); + const scoreBeat = !highScore || highScore > newScore; + const user = await fetchHSUserDisplay(this.client, highScoreUser); + if (scoreBeat) { + await this.client.redis.set('reaction-time', newScore); + await this.client.redis.set('reaction-time-user', msg.author.id); + } this.client.games.delete(msg.channel.id); if (!msgs.size) return msg.say('Failed to answer within 30 seconds.'); return msg.say(stripIndents` Nice one! (Took ${newScore / 1000} seconds) - ${!highScore || highScore > newScore ? `**New High Score!** Old:` : `High Score:`} ${highScore / 1000} + ${scoreBeat ? `**New High Score!** Old:` : `High Score:`} ${highScore / 1000} (Held by ${user}) `); } catch (err) { this.client.games.delete(msg.channel.id); diff --git a/commands/games-sp/typing-test.js b/commands/games-sp/typing-test.js index f4144c6f..577e7a57 100644 --- a/commands/games-sp/typing-test.js +++ b/commands/games-sp/typing-test.js @@ -1,6 +1,6 @@ const Command = require('../../structures/Command'); const { stripIndents } = require('common-tags'); -const { list } = require('../../util/Util'); +const { list, fetchHSUserDisplay } = require('../../util/Util'); const sentences = require('../../assets/json/typing-test'); const difficulties = ['baby', 'easy', 'medium', 'hard', 'extreme', 'impossible']; const times = { @@ -47,12 +47,18 @@ module.exports = class TypingTestCommand extends Command { const newScore = Date.now() - now; const highScoreGet = await this.client.redis.get('typing-test'); const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; - if (!highScore || highScore > newScore) await this.client.redis.set('typing-test', newScore); + const highScoreUser = await this.client.redis.get('typing-test-user'); + const scoreBeat = !highScore || highScore > newScore; + const user = await fetchHSUserDisplay(this.client, highScoreUser); + if (scoreBeat) { + await this.client.redis.set('typing-test', newScore); + await this.client.redis.set('typing-test-user', msg.author.id); + } if (!msgs.size) return msg.reply('Sorry! You lose!'); if (msgs.first().content !== sentence) return msg.reply('Sorry! You made a typo, so you lose!'); return msg.reply(stripIndents` Nice job! 10/10! You deserve some cake! (Took ${newScore / 1000} seconds) - ${!highScore || highScore > newScore ? `**New High Score!** Old:` : `High Score:`} ${highScore / 1000} + ${scoreBeat ? `**New High Score!** Old:` : `High Score:`} ${highScore / 1000} (Held by ${user}) `); } }; diff --git a/commands/util-public/high-scores.js b/commands/util-public/high-scores.js index 9267b0fe..3f5257f1 100644 --- a/commands/util-public/high-scores.js +++ b/commands/util-public/high-scores.js @@ -2,6 +2,7 @@ const Command = require('../../structures/Command'); const { stripIndents } = require('common-tags'); const moment = require('moment'); require('moment-duration-format'); +const { fetchHSUserDisplay } = require('../../util/Util'); const minesweeperSizes = [3, 4, 5, 6, 7, 8, 9, 10]; module.exports = class HighScoresCommand extends Command { @@ -19,8 +20,12 @@ module.exports = class HighScoresCommand extends Command { async run(msg) { const typingRaceGet = await this.client.redis.get('typing-test'); const typingRace = typingRaceGet ? Number.parseInt(typingRaceGet, 10) : null; + const typingRaceUser = await this.client.redis.get('typing-test-user'); + const typingRaceUserDisplay = await fetchHSUserDisplay(this.client, typingRaceUser); const anagramsGet = await this.client.redis.get('anagramica'); const anagrams = anagramsGet ? Number.parseInt(anagramsGet, 10) : null; + const anagramsUser = await this.client.redis.get('anagramica-user'); + const anagramsUserDisplay = await fetchHSUserDisplay(this.client, anagramsUser); const minesweeperScores = {}; const minesweeperUsers = {}; for (const size of minesweeperSizes) { @@ -28,29 +33,20 @@ module.exports = class HighScoresCommand extends Command { const minesweeper = minesweeperGet ? Number.parseInt(minesweeperGet, 10) : null; const minesweeperUser = await this.client.redis.get(`minesweeper-${size}-user`); minesweeperScores[size] = moment.duration(minesweeper).format('mm:ss'); - let user; - if (minesweeperUser) { - try { - const fetched = await this.client.users.fetch(minesweeperUser); - user = fetched.tag; - } catch { - user = 'Unknown'; - } - } else { - user = 'no one'; - } - minesweeperUsers[size] = user; + minesweeperUsers[size] = await fetchHSUserDisplay(this.client, minesweeperUser); } const reactionTimeGet = await this.client.redis.get('reaction-time'); const reactionTime = reactionTimeGet ? Number.parseInt(reactionTimeGet, 10) : null; + const reactionTimeUser = await this.client.redis.get('reaction-time'); + const reactionTimeUserDisplay = await fetchHSUserDisplay(this.client, reactionTimeUser); const minesweeperDisplay = Object.entries(minesweeperScores) .map(([size, score]) => `\`${size}x${size}\`: ${score} (Held by ${minesweeperUsers[size]})`) .join('\n'); return msg.say(stripIndents` __**Single-Score Games:**__ - \`typing-race\`/\`typing-test\`: ${typingRace / 1000}s - \`anagramica\`: ${anagrams} - \`reaction-time\`: ${reactionTime / 1000}s + \`typing-race\`/\`typing-test\`: ${typingRace / 1000}s (Held by ${typingRaceUserDisplay}) + \`anagramica\`: ${anagrams} (Held by ${anagramsUserDisplay}) + \`reaction-time\`: ${reactionTime / 1000}s (Held by ${reactionTimeUserDisplay}) __**Minesweeper:**__ ${minesweeperDisplay} diff --git a/util/Util.js b/util/Util.js index 95071e41..68b142c5 100644 --- a/util/Util.js +++ b/util/Util.js @@ -271,6 +271,21 @@ module.exports = class Util { return verify.map(player => player.author.id); } + static async fetchHSUserDisplay(client, userID) { + let user; + if (userID) { + try { + const fetched = await client.users.fetch(userID); + user = fetched.tag; + } catch { + user = 'Unknown'; + } + } else { + user = 'no one'; + } + return user; + } + static cleanAnilistHTML(html, removeLineBreaks = true) { let clean = html; if (removeLineBreaks) clean = clean.replace(/\r|\n|\f/g, '');