Save all high score users

This commit is contained in:
Dragon Fire
2021-01-17 12:08:45 -05:00
parent 983544c1cd
commit a1a6021e00
7 changed files with 66 additions and 41 deletions
+9 -3
View File
@@ -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);
+11 -5
View File
@@ -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);
+2 -12
View File
@@ -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);
+9 -3
View File
@@ -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);
+9 -3
View File
@@ -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})
`);
}
};
+11 -15
View File
@@ -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}
+15
View File
@@ -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, '');