mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-25 22:32:50 +02:00
Fix
This commit is contained in:
@@ -4,6 +4,7 @@ const { createCanvas, loadImage } = require('canvas');
|
|||||||
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');
|
||||||
|
const { drawImageWithTint } = require('../../util/Canvas');
|
||||||
const { FAILURE_EMOJI_ID } = process.env;
|
const { FAILURE_EMOJI_ID } = process.env;
|
||||||
const turnRegex = /^([A-H][1-8]) ?([A-H][1-8])$/;
|
const turnRegex = /^([A-H][1-8]) ?([A-H][1-8])$/;
|
||||||
const pieces = ['pawn', 'rook', 'knight', 'king', 'queen', 'bishop'];
|
const pieces = ['pawn', 'rook', 'knight', 'king', 'queen', 'bishop'];
|
||||||
@@ -59,6 +60,7 @@ module.exports = class ChessCommand extends Command {
|
|||||||
}
|
}
|
||||||
const game = new jsChess.Game();
|
const game = new jsChess.Game();
|
||||||
let lastTurnTimeout = false;
|
let lastTurnTimeout = false;
|
||||||
|
let prevGameState = null;
|
||||||
while (!game.exportJson().checkMate) {
|
while (!game.exportJson().checkMate) {
|
||||||
const user = game.exportJson().turn === 'black' ? opponent : msg.author;
|
const user = game.exportJson().turn === 'black' ? opponent : msg.author;
|
||||||
const gameState = game.exportJson();
|
const gameState = game.exportJson();
|
||||||
@@ -68,7 +70,7 @@ module.exports = class ChessCommand extends Command {
|
|||||||
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.'}_
|
||||||
`, { files: [{ attachment: this.displayBoard(gameState), name: 'chess.png' }] });
|
`, { files: [{ attachment: this.displayBoard(gameState, prevGameState), name: 'chess.png' }] });
|
||||||
const moves = game.moves();
|
const moves = game.moves();
|
||||||
const pickFilter = res => {
|
const pickFilter = res => {
|
||||||
if (res.author.id !== user.id) return false;
|
if (res.author.id !== user.id) return false;
|
||||||
@@ -102,6 +104,7 @@ module.exports = class ChessCommand extends Command {
|
|||||||
if (turn.first().content.toLowerCase() === 'end') break;
|
if (turn.first().content.toLowerCase() === 'end') break;
|
||||||
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]);
|
||||||
|
prevGameState = gameState;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.client.games.delete(msg.channel.id);
|
this.client.games.delete(msg.channel.id);
|
||||||
@@ -117,7 +120,7 @@ module.exports = class ChessCommand extends Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
displayBoard(gameState) {
|
displayBoard(gameState, prevGameState) {
|
||||||
const canvas = createCanvas(this.images.board.width, this.images.board.height);
|
const canvas = createCanvas(this.images.board.width, this.images.board.height);
|
||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
ctx.drawImage(this.images.board, 0, 0);
|
ctx.drawImage(this.images.board, 0, 0);
|
||||||
@@ -127,9 +130,20 @@ module.exports = class ChessCommand extends Command {
|
|||||||
let col = 0;
|
let col = 0;
|
||||||
for (let i = 0; i < 64; i++) {
|
for (let i = 0; i < 64; i++) {
|
||||||
const piece = gameState.pieces[`${cols[col]}${row}`];
|
const piece = gameState.pieces[`${cols[col]}${row}`];
|
||||||
|
const prevGamePiece = prevGameState.pieces[`${cols[col]}${row}`];
|
||||||
if (piece) {
|
if (piece) {
|
||||||
const parsed = this.pickImage(piece);
|
const parsed = this.pickImage(piece);
|
||||||
ctx.drawImage(this.images[parsed.color][parsed.name], w, h, 52, 52);
|
if (prevGameState && prevGamePiece !== piece && !prevGamePiece) {
|
||||||
|
drawImageWithTint(ctx, 'green', this.images[parsed.color][parsed.name], w, h, 52, 52);
|
||||||
|
} else {
|
||||||
|
ctx.drawImage(this.images[parsed.color][parsed.name], w, h, 52, 52);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (prevGameState && prevGamePiece !== piece && !piece) {
|
||||||
|
ctx.fillStyle = 'green';
|
||||||
|
ctx.globalAlpha = 0.5;
|
||||||
|
ctx.fillRect(w, h, 52, 52);
|
||||||
|
ctx.globalAlpha = 1;
|
||||||
}
|
}
|
||||||
w += 52 + 2;
|
w += 52 + 2;
|
||||||
col += 1;
|
col += 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user