Test saving user scores

This commit is contained in:
Dragon Fire
2021-01-17 11:45:57 -05:00
parent 7a22683df2
commit f05e0b1aa1
2 changed files with 34 additions and 3 deletions
+16 -2
View File
@@ -100,8 +100,22 @@ module.exports = class MinesweeperCommand extends Command {
const newScore = Date.now() - startTime; const newScore = Date.now() - startTime;
const highScoreGet = await this.client.redis.get(`minesweeper-${size}`); const highScoreGet = await this.client.redis.get(`minesweeper-${size}`);
const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null;
if (win && (!highScore || highScore > newScore)) { const highScoreUser = await this.client.redis.get(`minesweeper-${size}-user`);
const scoreBeat = win && (!highScore || highScore > newScore);
let user;
if (user) {
try {
const fetched = await this.client.users.fetch(highScoreUser);
user = fetched.tag;
} catch {
user = 'Unknown';
}
} else {
user = 'no one';
}
if (scoreBeat) {
await this.client.redis.set(`minesweeper-${size}`, newScore); await this.client.redis.set(`minesweeper-${size}`, newScore);
await this.client.redis.set(`minesweeper-${size}-user`, msg.author.id);
} }
this.client.games.delete(msg.channel.id); this.client.games.delete(msg.channel.id);
if (win === null) return msg.say('Game ended due to inactivity.'); if (win === null) return msg.say('Game ended due to inactivity.');
@@ -109,7 +123,7 @@ module.exports = class MinesweeperCommand extends Command {
const displayTime = moment.duration(highScore).format('mm:ss'); const displayTime = moment.duration(highScore).format('mm:ss');
return msg.say(stripIndents` return msg.say(stripIndents`
${win ? `Nice job! You win! (Took ${newDisplayTime})` : 'Sorry... You lose.'} ${win ? `Nice job! You win! (Took ${newDisplayTime})` : 'Sorry... You lose.'}
${win && (!highScore || highScore > newScore) ? `**New High Score!** Old:` : `High Score:`} ${displayTime} ${scoreBeat ? `**New High Score!** Old:` : `High Score:`} ${displayTime} (Held by ${user})
${this.displayBoard(game.board)} ${this.displayBoard(game.board)}
`); `);
+18 -1
View File
@@ -22,13 +22,30 @@ module.exports = class HighScoresCommand extends Command {
const anagramsGet = await this.client.redis.get('anagramica'); const anagramsGet = await this.client.redis.get('anagramica');
const anagrams = anagramsGet ? Number.parseInt(anagramsGet, 10) : null; const anagrams = anagramsGet ? Number.parseInt(anagramsGet, 10) : null;
const minesweeperScores = {}; const minesweeperScores = {};
const minesweeperUsers = {};
for (const size of minesweeperSizes) { for (const size of minesweeperSizes) {
const minesweeperGet = await this.client.redis.get(`minesweeper-${size}`); const minesweeperGet = await this.client.redis.get(`minesweeper-${size}`);
const minesweeper = minesweeperGet ? Number.parseInt(minesweeperGet, 10) : null; 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'); minesweeperScores[size] = moment.duration(minesweeper).format('mm:ss');
let user;
if (user) {
try {
const fetched = await this.client.users.fetch(minesweeperUser);
user = fetched.tag;
} catch {
user = 'Unknown';
}
} else {
user = 'no one';
}
minesweeperUsers[size] = user;
} }
const reactionTimeGet = await this.client.redis.get('reaction-time'); const reactionTimeGet = await this.client.redis.get('reaction-time');
const reactionTime = reactionTimeGet ? Number.parseInt(reactionTimeGet, 10) : null; const reactionTime = reactionTimeGet ? Number.parseInt(reactionTimeGet, 10) : null;
const minesweeperDisplay = Object.entries(minesweeperScores)
.map(([size, score]) => `\`${size}x${size}\`: ${score} (Held by ${minesweeperUsers[size]})`)
.join('\n');
return msg.say(stripIndents` return msg.say(stripIndents`
__**Single-Score Games:**__ __**Single-Score Games:**__
\`typing-race\`/\`typing-test\`: ${typingRace / 1000}s \`typing-race\`/\`typing-test\`: ${typingRace / 1000}s
@@ -36,7 +53,7 @@ module.exports = class HighScoresCommand extends Command {
\`reaction-time\`: ${reactionTime / 1000}s \`reaction-time\`: ${reactionTime / 1000}s
__**Minesweeper:**__ __**Minesweeper:**__
${Object.entries(minesweeperScores).map(([size, score]) => `\`${size}x${size}\`: ${score}`).join('\n')} ${minesweeperDisplay}
`); `);
} }
}; };