From 87d83051fa3acb752bc13046e87c68193381eb9e Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 13 Dec 2020 09:51:16 -0500 Subject: [PATCH] High Scores in a few games --- commands/games-mp/typing-race.js | 9 ++++++++- commands/games-sp/anagramica.js | 22 +++++++++++++++++++--- commands/games-sp/reaction-time.js | 10 +++++++++- commands/games-sp/typing-test.js | 9 ++++++++- package.json | 2 +- 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/commands/games-mp/typing-race.js b/commands/games-mp/typing-race.js index 11b0a426..0ea85729 100644 --- a/commands/games-mp/typing-race.js +++ b/commands/games-mp/typing-race.js @@ -45,9 +45,16 @@ module.exports = class TypingRaceCommand extends Command { max: 1, time: 30000 }); + 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); this.client.games.delete(msg.channel.id); if (!winner.size) return msg.say('Oh... No one won.'); - return msg.say(`The winner is ${winner.first().author}! (Took ${(Date.now() - now) / 1000} seconds)`); + 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} + `); } catch (err) { this.client.games.delete(msg.channel.id); throw err; diff --git a/commands/games-sp/anagramica.js b/commands/games-sp/anagramica.js index 7cc693e8..bef413f0 100644 --- a/commands/games-sp/anagramica.js +++ b/commands/games-sp/anagramica.js @@ -66,10 +66,26 @@ module.exports = class AnagramicaCommand extends Command { const msgs = await msg.channel.awaitMessages(filter, { time: time * 1000 }); + 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); this.client.games.delete(msg.channel.id); - if (!msgs.size) return msg.reply('Couldn\'t even think of one? Ouch.'); - if (points < 1) return msg.reply(`Ouch, your final score was **${points}**. Try harder next time!`); - return msg.reply(`Nice job! Your final score was **${points}**!`); + if (!msgs.size) { + return msg.reply(stripIndents` + Couldn't even think of one? Ouch. + ${!highScore || highScore < points ? `**New High Score!** Old:` : `High Score:`} ${highScore} + `); + } + 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} + `); + } + return msg.reply(stripIndents` + Nice job! Your final score was **${points}**! + ${!highScore || highScore < points ? `**New High Score!** Old:` : `High Score:`} ${highScore} + `); } catch (err) { this.client.games.delete(msg.channel.id); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); diff --git a/commands/games-sp/reaction-time.js b/commands/games-sp/reaction-time.js index 73420b86..d2cd7e72 100644 --- a/commands/games-sp/reaction-time.js +++ b/commands/games-sp/reaction-time.js @@ -1,4 +1,5 @@ const Command = require('../../structures/Command'); +const { stripIndents } = require('common-tags'); const { delay, randomRange } = require('../../util/Util'); const words = ['fire', 'draw', 'shoot', 'bang', 'pull', 'boom']; @@ -28,9 +29,16 @@ module.exports = class ReactionTimeCommand extends Command { max: 1, time: 30000 }); + 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); this.client.games.delete(msg.channel.id); if (!msgs.size) return msg.say('Failed to answer within 30 seconds.'); - return msg.say(`Nice one! (Took ${(Date.now() - now) / 1000} seconds)`); + return msg.say(stripIndents` + Nice one! (Took ${newScore / 1000} seconds) + ${!highScore || highScore < newScore ? `**New High Score!** Old:` : `High Score:`} ${highScore / 1000} + `); } catch (err) { this.client.games.delete(msg.channel.id); throw err; diff --git a/commands/games-sp/typing-test.js b/commands/games-sp/typing-test.js index f4d4e7b1..cdcadaa9 100644 --- a/commands/games-sp/typing-test.js +++ b/commands/games-sp/typing-test.js @@ -44,8 +44,15 @@ module.exports = class TypingTestCommand extends Command { max: 1, time }); + 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); 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(`Nice job! 10/10! You deserve some cake! (Took ${(Date.now() - now) / 1000} seconds)`); + 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} + `); } }; diff --git a/package.json b/package.json index bb337aed..4c357fce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "123.5.5", + "version": "123.6.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {