UNPKG

polish-gamecord

Version:

Discord Gamecord is a powerful npm package with a collection of minigames for your discord bot

233 lines (196 loc) 10.9 kB
const { MessageEmbed, MessageButton, MessageActionRow, MessageAttachment } = require('discord.js'); const { disableButtons, formatMessage, move, oppDirection } = require('../utils/utils'); const chars = '0123456789abcdefghijklmnopqrstuvwxyz'; const events = require('events'); module.exports = class TwoZeroFourEight extends events { constructor(options = {}) { if (!options.isSlashGame) options.isSlashGame = false; if (!options.message) throw new TypeError('BRAK_WIADOMOŚCI: Nie podano opcji wiadomości.'); if (typeof options.message !== 'object') throw new TypeError('NIEPRAWIDŁOWA_WIADOMOŚĆ: Opcja wiadomości musi być obiektem.'); if (typeof options.isSlashGame !== 'boolean') throw new TypeError('NIEPRAWIDŁOWY_TYP_KOMENDY: Opcja isSlashGame musi być typem boolean.'); if (!options.embed) options.embed = {}; if (!options.embed.title) options.embed.title = '2048'; if (!options.embed.currentScore) options.embed.currentScore = 'Aktualny wynik'; if (!options.embed.color) options.embed.color = '#5865F2'; if (!options.emojis) options.emojis = {}; if (!options.emojis.up) options.emojis.up = '⬆️'; if (!options.emojis.down) options.emojis.down = '⬇️'; if (!options.emojis.left) options.emojis.left = '⬅️'; if (!options.emojis.right) options.emojis.right = '➡️'; if (!options.timeoutTime) options.timeoutTime = 60000; if (!options.buttonStyle) options.buttonStyle = 'PRIMARY'; if (typeof options.embed !== 'object') throw new TypeError('NIEPRAWIDŁOWY_EMBED: Opcja embed musi być obiektem.'); if (typeof options.embed.title !== 'string') throw new TypeError('NIEPRAWIDŁOWY_EMBED: Tytuł embed musi być ciągiem znaków.'); if (typeof options.embed.currentScore !== 'string') throw new TypeError('NIEPRAWIDŁOWY_EMBED: Aktualny wynik musi być ciągiem znaków.'); if (typeof options.embed.color !== 'string') throw new TypeError('NIEPRAWIDŁOWY_EMBED: Kolor embed musi być ciągiem znaków.'); if (typeof options.emojis !== 'object') throw new TypeError('NIEPRAWIDŁOWE_EMOJI: Opcja emojis musi być obiektem.'); if (typeof options.emojis.up !== 'string') throw new TypeError('NIEPRAWIDŁOWE_EMOJI: Emoji do góry musi być ciągiem znaków.'); if (typeof options.emojis.down !== 'string') throw new TypeError('NIEPRAWIDŁOWE_EMOJI: Emoji w dół musi być ciągiem znaków.'); if (typeof options.emojis.left !== 'string') throw new TypeError('NIEPRAWIDŁOWE_EMOJI: Emoji w lewo musi być ciągiem znaków.'); if (typeof options.emojis.right !== 'string') throw new TypeError('NIEPRAWIDŁOWE_EMOJI: Emoji w prawo musi być ciągiem znaków.'); if (typeof options.timeoutTime !== 'number') throw new TypeError('NIEPRAWIDŁOWY_CZAS: Opcja czasu oczekiwania musi być liczbą.'); if (typeof options.buttonStyle !== 'string') throw new TypeError('NIEPRAWIDŁOWY_STYLE_PRZYCISKU: Styl przycisku musi być ciągiem znaków.'); if (options.playerOnlyMessage !== false) { if (!options.playerOnlyMessage) options.playerOnlyMessage = 'Tylko {player} może używać tych przycisków.'; if (typeof options.playerOnlyMessage !== 'string') throw new TypeError('NIEPRAWIDŁOWA_WIADOMOŚĆ: Opcja playerOnlyMessage musi być ciągiem znaków.'); } super(); this.options = options; this.message = options.message; this.gameBoard = []; this.mergedPos = []; this.length = 4; this.score = 0; for (let y = 0; y < this.length; y++) { for (let x = 0; x < this.length; x++) { this.gameBoard[y * this.length + x] = 0; } } } async sendMessage(content) { if (this.options.isSlashGame) return await this.message.editReply(content).catch(e => {}); else return await this.message.channel.send(content).catch(e => {}); } async getBoardImage() { const url = 'https://api.gamecord.xyz/2048?board=' + this.gameBoard.map(c => chars[c]).join(''); return await new MessageAttachment(url, 'gameboard.png'); } async startGame() { if (this.options.isSlashGame || !this.message.author) { if (!this.message.deferred) await this.message.deferReply().catch(e => {}); this.message.author = this.message.user; this.options.isSlashGame = true; } this.placeRandomTile(); this.placeRandomTile(); const embed = new MessageEmbed() .setTitle(this.options.embed.title) .setColor(this.options.embed.color) .setImage('attachment://gameboard.png') .addFields({ name: this.options.embed.currentScore, value: this.score.toString() }) .setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) }); const up = new MessageButton().setEmoji(this.options.emojis.up).setStyle(this.options.buttonStyle).setCustomId('2048_up'); const down = new MessageButton().setEmoji(this.options.emojis.down).setStyle(this.options.buttonStyle).setCustomId('2048_down'); const left = new MessageButton().setEmoji(this.options.emojis.left).setStyle(this.options.buttonStyle).setCustomId('2048_left'); const right = new MessageButton().setEmoji(this.options.emojis.right).setStyle(this.options.buttonStyle).setCustomId('2048_right'); const row = new MessageActionRow().addComponents(up, down, left, right); const msg = await this.sendMessage({ embeds: [embed], components: [row], files: [await this.getBoardImage()] }); return this.handleButtons(msg); } placeRandomTile() { let tilePos = { x: 0, y: 0 }; do { tilePos = { x: parseInt(Math.random() * this.length), y: parseInt(Math.random() * this.length) }; } while (this.gameBoard[tilePos.y * this.length + tilePos.x] != 0) this.gameBoard[tilePos.y * this.length + tilePos.x] = (Math.random() > 0.8 ? 2 : 1); } async handleButtons(msg) { const collector = msg.createMessageComponentCollector({ idle: this.options.timeoutTime }); collector.on('collect', async btn => { await btn.deferUpdate().catch(e => {}); if (btn.user.id !== this.message.author.id) { if (this.options.playerOnlyMessage) btn.followUp({ content: formatMessage(this.options, 'playerOnlyMessage'), ephemeral: true }); return; } let moved = false; this.mergedPos = []; const direction = btn.customId.split('_')[1]; if (direction === 'up' || direction === 'down') moved = this.shiftVertical(direction); if (direction === 'left' || direction === 'right') moved = this.shiftHorizontal(direction); if (moved) this.placeRandomTile(); if (this.isGameOver()) return collector.stop(); const embed = new MessageEmbed() .setTitle(this.options.embed.title) .setColor(this.options.embed.color) .setImage('attachment://gameboard.png') .addFields({ name: this.options.embed.currentScore, value: this.score.toString() }) .setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) }); return msg.edit({ embeds: [embed], files: [await this.getBoardImage()], attachments: [] }); }) collector.on('end', (_, reason) => { if (reason === 'idle' || reason === 'user') { return this.gameOver(msg, this.gameBoard.includes('b')); } }) } async gameOver(msg, result) { const TwoZeroFourEightGame = { player: this.message.author, score: this.score }; this.emit('gameOver', { result: (result ? 'wygrał' : 'przegrał'), ...TwoZeroFourEightGame }); const embed = new MessageEmbed() .setTitle(this.options.embed.title) .setColor(this.options.embed.color) .setImage('attachment://gameboard.png') .addFields({ name: 'Całkowity wynik', value: this.score.toString() }) .setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) }); return msg.edit({ embeds: [embed], components: disableButtons(msg.components), files: [await this.getBoardImage()], attachments: [] }); } isGameOver() { let boardFull = true; let numMoves = 0; for (let y = 0; y < this.length; y++) { for (let x = 0; x < this.length; x++) { if (this.gameBoard[y * this.length + x] === 0) boardFull = false; const posNum = this.gameBoard[y * this.length + x]; ['down', 'left', 'right', 'up'].forEach(dir => { const newPos = move({x, y}, dir); if (this.isInsideBlock(newPos) && (this.gameBoard[newPos.y * this.length + newPos.x] === 0 || this.gameBoard[newPos.y * this.length + newPos.x] === posNum)) numMoves++; }) } } return (boardFull && numMoves === 0); } shiftVertical(dir) { let moved = false; for (let x = 0; x < this.length; x++) { if (dir === 'up') { for (let y = 1; y < this.length; y++) moved = this.shift({ x, y }, 'up') || moved; } else { for (let y = this.length - 2; y >= 0; y--) moved = this.shift({ x, y }, 'down') || moved; } } return moved; } shiftHorizontal(dir) { let moved = false; for (let y = 0; y < this.length; y++) { if (dir === 'left') { for (let x = 1; x < this.length; x++) moved = this.shift({ x, y }, 'left') || moved; } else { for (let x = this.length - 2; x >= 0; x--) moved = this.shift({ x, y }, 'right') || moved; } } return moved; } isInsideBlock(pos) { return pos.x >= 0 && pos.y >= 0 && pos.x < this.length && pos.y < this.length; } shift(pos, dir) { let moved = false; const movingTile = this.gameBoard[pos.y * this.length + pos.x]; if (movingTile === 0) return false; let set = false; let moveTo = pos; while (!set) { moveTo = move(moveTo, dir); const moveToTile = this.gameBoard[moveTo.y * this.length + moveTo.x]; if (!this.isInsideBlock(moveTo) || (moveToTile !== 0 && moveToTile !== movingTile) || !!this.mergedPos.find(p => p.x === moveTo.x && p.y === moveTo.y)) { const moveBack = move(moveTo, oppDirection(dir)); if (!(moveBack.x === pos.x && moveBack.y === pos.y)) { this.gameBoard[pos.y * this.length + pos.x] = 0; this.gameBoard[moveBack.y * this.length + moveBack.x] = movingTile; moved = true; } set = true; } else if (moveToTile === movingTile) { moved = true; this.gameBoard[moveTo.y * this.length + moveTo.x] += 1; this.score += Math.floor(Math.pow(this.gameBoard[moveTo.y * this.length + moveTo.x], 2)); this.gameBoard[pos.y * this.length + pos.x] = 0; this.mergedPos.push(moveTo); set = true; } } return moved; } }