mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 15:07:42 +02:00
Fix Chess Set Up
This commit is contained in:
@@ -262,7 +262,7 @@ in the appropriate channel's topic to use it.
|
||||
|
||||
## Commands
|
||||
|
||||
Total: 593
|
||||
Total: 594
|
||||
|
||||
### Utility:
|
||||
|
||||
@@ -618,6 +618,7 @@ Total: 593
|
||||
* **bingo:** Play bingo with up to 99 other users.
|
||||
* **car-race:** Race a car against another user or the AI.
|
||||
* **chess-delete:** Deletes your saved Chess game.
|
||||
* **chess-set-up:** Sets up and saves a custom Chess game.
|
||||
* **chess:** Play a game of Chess with another user or the AI.
|
||||
* **connect-four:** Play a game of Connect Four with another user or the AI.
|
||||
* **cram:** Play a game of Cram with another user.
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
const Command = require('../../structures/Command');
|
||||
|
||||
module.exports = class ChessSetUpCommand extends Command {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: 'chess-set-up',
|
||||
aliases: ['set-up-chess', 'chess-create', 'create-chess'],
|
||||
group: 'games-mp',
|
||||
memberName: 'chess-set-up',
|
||||
description: 'Sets up and saves a custom Chess game.',
|
||||
args: [
|
||||
{
|
||||
key: 'fen',
|
||||
prompt: 'What FEN would you like to use for the game?',
|
||||
type: 'string'
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
async run(msg, { fen }) {
|
||||
const data = await this.client.redis.exists(`chess-${msg.author.id}`);
|
||||
if (data) return msg.reply('You already have a saved Chess game.');
|
||||
await this.client.redis.set(`chess-${msg.author.id}`, JSON.stringify({
|
||||
fen,
|
||||
whiteTime: -1,
|
||||
blackTime: -1,
|
||||
color: 'white',
|
||||
fiftyRuleMove: 0
|
||||
}));
|
||||
const usage = this.client.registry.commands.get('chess').usage();
|
||||
return msg.say(`Your custom game has been saved. You can use it using ${usage}.`);
|
||||
}
|
||||
};
|
||||
+23
-12
@@ -7,7 +7,7 @@ const path = require('path');
|
||||
const { verify, reactIfAble } = require('../../util/Util');
|
||||
const { centerImagePart } = require('../../util/Canvas');
|
||||
const { FAILURE_EMOJI_ID } = process.env;
|
||||
const turnRegex = /^((?:[A-H][1-8])|(?:[PKRQBNX]))?([A-H])?(?: |, ?|-?>?)?([A-H][1-8])(=[QRNB])?$/;
|
||||
const turnRegex = /^((?:[A-H][1-8])|(?:[PKRQBNX]))?([A-H])?(?: |, ?|-?>?)?([A-H][1-8])(?:=([QRNB]))?$/;
|
||||
const pieces = ['pawn', 'rook', 'knight', 'king', 'queen', 'bishop'];
|
||||
const cols = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
|
||||
|
||||
@@ -79,14 +79,25 @@ module.exports = class ChessCommand extends Command {
|
||||
`);
|
||||
const verification = await verify(msg.channel, msg.author);
|
||||
if (verification) {
|
||||
const data = JSON.parse(resumeGame);
|
||||
game = new jsChess.Game(data.fen);
|
||||
whiteTime = data.whiteTime === -1 ? Infinity : data.whiteTime;
|
||||
blackTime = data.blackTime === -1 ? Infinity : data.blackTime;
|
||||
whitePlayer = data.color === 'white' ? msg.author : opponent;
|
||||
blackPlayer = data.color === 'black' ? msg.author : opponent;
|
||||
fiftyRuleMove = data.fiftyRuleMove;
|
||||
await this.client.redis.del(`chess-${msg.author.id}`);
|
||||
try {
|
||||
const data = JSON.parse(resumeGame);
|
||||
game = new jsChess.Game(data.fen);
|
||||
whiteTime = data.whiteTime === -1 ? Infinity : data.whiteTime;
|
||||
blackTime = data.blackTime === -1 ? Infinity : data.blackTime;
|
||||
whitePlayer = data.color === 'white' ? msg.author : opponent;
|
||||
blackPlayer = data.color === 'black' ? msg.author : opponent;
|
||||
fiftyRuleMove = data.fiftyRuleMove;
|
||||
await this.client.redis.del(`chess-${msg.author.id}`);
|
||||
} catch {
|
||||
await msg.reply('An error occurred reading your saved game. Deleting it and aborting...');
|
||||
game = new jsChess.Game();
|
||||
whiteTime = time * 60000;
|
||||
blackTime = time * 60000;
|
||||
whitePlayer = msg.author;
|
||||
blackPlayer = opponent;
|
||||
fiftyRuleMove = 0;
|
||||
await this.client.redis.del(`chess-${msg.author.id}`);
|
||||
}
|
||||
} else {
|
||||
game = new jsChess.Game();
|
||||
}
|
||||
@@ -175,14 +186,14 @@ module.exports = class ChessCommand extends Command {
|
||||
if (gameState.turn === 'black') blackTime -= timeTaken - 5000;
|
||||
if (gameState.turn === 'white') whiteTime -= timeTaken - 5000;
|
||||
const choice = this.parseSAN(gameState, moves, turn.first().content.toUpperCase().match(turnRegex));
|
||||
if (gameState.pieces[choice[0]].toUpperCase() === 'P') {
|
||||
const pawnMoved = gameState.pieces[choice[0]].toUpperCase() === 'P';
|
||||
if (pawnMoved) {
|
||||
fiftyRuleMove = 0;
|
||||
} else {
|
||||
fiftyRuleMove++;
|
||||
}
|
||||
game.move(choice[0], choice[1]);
|
||||
const finalRow = gameState.turn === 'black' ? '8' : '1';
|
||||
if (gameState.pieces[choice[1]].toUpperCase() === 'P' && choice[1].endsWith(finalRow)) {
|
||||
if (pawnMoved && choice[1].endsWith(gameState.turn === 'black' ? '8' : '1')) {
|
||||
game.board.configuration.pieces[choice[1]] = gameState.turn = 'black'
|
||||
? choice[2]
|
||||
: choice[2].toLowerCase()
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xiao",
|
||||
"version": "128.4.7",
|
||||
"version": "128.5.0",
|
||||
"description": "Your personal server companion.",
|
||||
"main": "Xiao.js",
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user