Add infinite time

This commit is contained in:
Dragon Fire
2021-01-29 16:44:32 -05:00
parent 03edaee368
commit c92cba2301
+15 -11
View File
@@ -39,10 +39,10 @@ module.exports = class ChessCommand extends Command {
}, },
{ {
key: 'time', key: 'time',
prompt: 'How long should the chess timers be set for (in minutes)?', prompt: 'How long should the chess timers be set for (in minutes)? Use 0 for infinite.',
type: 'integer', type: 'integer',
max: 60, max: 120,
min: 5 min: 0
} }
] ]
}); });
@@ -67,18 +67,21 @@ module.exports = class ChessCommand extends Command {
} }
const resumeGame = await this.client.redis.get(`chess-${msg.author.id}`); const resumeGame = await this.client.redis.get(`chess-${msg.author.id}`);
let game; let game;
let whiteTime = time * 60000; let whiteTime = time === 0 ? Infinity : time * 60000;
let blackTime = time * 60000; let blackTime = time === 0 ? Infinity : time * 60000;
let whitePlayer = msg.author; let whitePlayer = msg.author;
let blackPlayer = opponent; let blackPlayer = opponent;
if (resumeGame) { if (resumeGame) {
await msg.reply('You have a saved game, do you want to resume it?'); await msg.reply(stripIndents`
You have a saved game, do you want to resume it?
**This will delete your saved game.**
`);
const verification = await verify(msg.channel, msg.author); const verification = await verify(msg.channel, msg.author);
if (verification) { if (verification) {
const data = JSON.parse(resumeGame); const data = JSON.parse(resumeGame);
game = new jsChess.Game(data.fen); game = new jsChess.Game(data.fen);
whiteTime = data.whiteTime; whiteTime = data.whiteTime === -1 ? Infinity : data.whiteTime;
blackTime = data.blackTime; blackTime = data.blackTime === -1 ? Infinity : data.blackTime;
whitePlayer = data.color === 'white' ? msg.author : opponent; whitePlayer = data.color === 'white' ? msg.author : opponent;
blackPlayer = data.color === 'black' ? msg.author : opponent; blackPlayer = data.color === 'black' ? msg.author : opponent;
await this.client.redis.del(`chess-${msg.author.id}`); await this.client.redis.del(`chess-${msg.author.id}`);
@@ -102,12 +105,13 @@ module.exports = class ChessCommand extends Command {
if (gameState.turn === 'black') blackTime -= timeTaken - 5000; if (gameState.turn === 'black') blackTime -= timeTaken - 5000;
if (gameState.turn === 'white') whiteTime -= timeTaken - 5000; if (gameState.turn === 'white') whiteTime -= timeTaken - 5000;
} else { } else {
const displayTime = userTime === Infinity ? 'Infinite' : moment.duration(userTime).format();
await msg.say(stripIndents` await msg.say(stripIndents`
${user}, what move do you want to make (ex. A1A2)? Type \`end\` to forfeit. ${user}, what move do you want to make (ex. A1A2)? Type \`end\` to forfeit.
You can save your game by typing \`save\`. You can save your game by typing \`save\`.
_You are ${gameState.check ? '**in check!**' : 'not in check.'}_ _You are ${gameState.check ? '**in check!**' : 'not in check.'}_
**Time Remaining: ${moment.duration(userTime).format()}** (Max 10min per turn) **Time Remaining: ${displayTime}** (Max 10min per turn)
`, { files: [{ attachment: this.displayBoard(gameState, prevPieces), name: 'chess.png' }] }); `, { files: [{ attachment: this.displayBoard(gameState, prevPieces), name: 'chess.png' }] });
prevPieces = Object.assign({}, game.exportJson().pieces); prevPieces = Object.assign({}, game.exportJson().pieces);
const moves = game.moves(); const moves = game.moves();
@@ -290,8 +294,8 @@ module.exports = class ChessCommand extends Command {
exportGame(game, blackTime, whiteTime, playerColor) { exportGame(game, blackTime, whiteTime, playerColor) {
return JSON.stringify({ return JSON.stringify({
fen: game.exportFEN(), fen: game.exportFEN(),
blackTime, blackTime: blackTime === Infinity ? -1 : blackTime,
whiteTime, whiteTime: whiteTime === Infinity ? -1 : whiteTime,
color: playerColor color: playerColor
}); });
} }