From 8719be1c238d430373bb9231cc926fd18cf2c8f8 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 7 Feb 2021 14:21:38 -0500 Subject: [PATCH] Fix Chess Set Up --- README.md | 3 ++- commands/games-mp/chess-set-up.js | 34 ++++++++++++++++++++++++++++++ commands/games-mp/chess.js | 35 ++++++++++++++++++++----------- package.json | 2 +- 4 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 commands/games-mp/chess-set-up.js diff --git a/README.md b/README.md index d8df7625..f739adde 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/commands/games-mp/chess-set-up.js b/commands/games-mp/chess-set-up.js new file mode 100644 index 00000000..c7cfa0f3 --- /dev/null +++ b/commands/games-mp/chess-set-up.js @@ -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}.`); + } +}; diff --git a/commands/games-mp/chess.js b/commands/games-mp/chess.js index 0bc8b8cc..c0b4e553 100644 --- a/commands/games-mp/chess.js +++ b/commands/games-mp/chess.js @@ -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() diff --git a/package.json b/package.json index 3268cca9..010f2da4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "128.4.7", + "version": "128.5.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {