mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-21 05:54:33 +02:00
Add proper chess timer system
This commit is contained in:
+19
-15
@@ -1,6 +1,7 @@
|
|||||||
const Command = require('../../structures/Command');
|
const Command = require('../../structures/Command');
|
||||||
const jsChess = require('js-chess-engine');
|
const jsChess = require('js-chess-engine');
|
||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
|
const moment = require('moment');
|
||||||
const { stripIndents } = require('common-tags');
|
const { stripIndents } = require('common-tags');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { verify, reactIfAble } = require('../../util/Util');
|
const { verify, reactIfAble } = require('../../util/Util');
|
||||||
@@ -59,18 +60,26 @@ module.exports = class ChessCommand extends Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const game = new jsChess.Game();
|
const game = new jsChess.Game();
|
||||||
let lastTurnTimeout = false;
|
|
||||||
let prevPieces = null;
|
let prevPieces = null;
|
||||||
|
let whiteTime = 900000;
|
||||||
|
let blackTime = 900000;
|
||||||
while (!game.exportJson().checkMate) {
|
while (!game.exportJson().checkMate) {
|
||||||
const user = game.exportJson().turn === 'black' ? opponent : msg.author;
|
|
||||||
const gameState = game.exportJson();
|
const gameState = game.exportJson();
|
||||||
|
const user = gameState.turn === 'black' ? opponent : msg.author;
|
||||||
|
const time = gameState.turn === 'black' ? blackTime : whiteTime;
|
||||||
if (user.bot) {
|
if (user.bot) {
|
||||||
prevPieces = Object.assign({}, game.exportJson().pieces);
|
prevPieces = Object.assign({}, game.exportJson().pieces);
|
||||||
|
const now = new Date();
|
||||||
game.aiMove(3);
|
game.aiMove(3);
|
||||||
|
const timeTaken = new Date() - now;
|
||||||
|
if (gameState.turn === 'black') blackTime -= timeTaken - 5000;
|
||||||
|
if (gameState.turn === 'white') whiteTime -= timeTaken - 5000;
|
||||||
} else {
|
} else {
|
||||||
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 are ${gameState.check ? '**in check!**' : 'not in check.'}_
|
_You are ${gameState.check ? '**in check!**' : 'not in check.'}_
|
||||||
|
|
||||||
|
**Time Remaining: ${moment.duration(time).format()}**
|
||||||
`, { 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();
|
||||||
@@ -86,31 +95,26 @@ module.exports = class ChessCommand extends Command {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
const now = new Date();
|
||||||
const turn = await msg.channel.awaitMessages(pickFilter, {
|
const turn = await msg.channel.awaitMessages(pickFilter, {
|
||||||
max: 1,
|
max: 1,
|
||||||
time: 120000
|
time
|
||||||
});
|
});
|
||||||
if (!turn.size) {
|
if (!turn.size) {
|
||||||
if (lastTurnTimeout) {
|
this.client.games.delete(msg.channel.id);
|
||||||
break;
|
return msg.say(`${user.id === msg.author.id ? opponent : msg.author} wins from timeout!`);
|
||||||
} else {
|
|
||||||
const available = Object.keys(moves);
|
|
||||||
const piece = available[Math.floor(Math.random() * available.length)];
|
|
||||||
const move = moves[piece][Math.floor(Math.random() * moves[piece].length)];
|
|
||||||
await msg.say(`Sorry, time is up! Playing random move (${piece}->${move}).`);
|
|
||||||
game.move(piece, move);
|
|
||||||
lastTurnTimeout = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (turn.first().content.toLowerCase() === 'end') break;
|
if (turn.first().content.toLowerCase() === 'end') break;
|
||||||
|
const timeTaken = new Date() - now;
|
||||||
|
if (gameState.turn === 'black') blackTime -= timeTaken - 5000;
|
||||||
|
if (gameState.turn === 'white') whiteTime -= timeTaken - 5000;
|
||||||
const choice = turn.first().content.toUpperCase().match(turnRegex);
|
const choice = turn.first().content.toUpperCase().match(turnRegex);
|
||||||
game.move(choice[1], choice[2]);
|
game.move(choice[1], choice[2]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.client.games.delete(msg.channel.id);
|
this.client.games.delete(msg.channel.id);
|
||||||
const gameState = game.exportJson();
|
const gameState = game.exportJson();
|
||||||
if (!gameState.checkMate) return msg.say('Game ended due to inactivity or forfeit.');
|
if (!gameState.checkMate) return msg.say('Game ended due to forfeit.');
|
||||||
const winner = gameState.turn === 'black' ? msg.author : opponent;
|
const winner = gameState.turn === 'black' ? msg.author : opponent;
|
||||||
return msg.say(`Checkmate! Congrats, ${winner}!`, {
|
return msg.say(`Checkmate! Congrats, ${winner}!`, {
|
||||||
files: [{ attachment: this.displayBoard(gameState), name: 'chess.png' }]
|
files: [{ attachment: this.displayBoard(gameState), name: 'chess.png' }]
|
||||||
|
|||||||
Reference in New Issue
Block a user