| 102 | usage: '.tictactoe [room name]', |
| 103 | groupOnly: true, |
| 104 | async handler(sock, message, args, context) { |
| 105 | const chatId = context.chatId || message.key.remoteJid; |
| 106 | const senderId = context.senderId || message.key.participant || message.key.remoteJid; |
| 107 | const text = args.join(' ').trim(); |
| 108 | try { |
| 109 | if (Object.values(games).find((room) => room.id.startsWith('tictactoe') && |
| 110 | [room.game.playerX, room.game.playerO].includes(senderId))) { |
| 111 | await sock.sendMessage(chatId, { |
| 112 | text: '*You are already in a game*\n\nType *surrender* to quit the current game first.' |
| 113 | }, { quoted: message }); |
| 114 | return; |
| 115 | } |
| 116 | let room = Object.values(games).find((room) => room.state === 'WAITING' && |
| 117 | (text ? room.name === text : true)); |
| 118 | if (room) { |
| 119 | room.o = chatId; |
| 120 | room.game.playerO = senderId; |
| 121 | room.state = 'PLAYING'; |
| 122 | const arr = room.game.render().map((v) => ({ |
| 123 | 'X': '❎', |
| 124 | 'O': '⭕', |
| 125 | '1': '1️⃣', |
| 126 | '2': '2️⃣', |
| 127 | '3': '3️⃣', |
| 128 | '4': '4️⃣', |
| 129 | '5': '5️⃣', |
| 130 | '6': '6️⃣', |
| 131 | '7': '7️⃣', |
| 132 | '8': '8️⃣', |
| 133 | '9': '9️⃣', |
| 134 | }[v] || v)); |
| 135 | const str = ` |
| 136 | 🎮 *TicTacToe Game Started!* |
| 137 | |
| 138 | Waiting for @${room.game.currentTurn.split('@')[0]} to play... |
| 139 | |
| 140 | ${arr.slice(0, 3).join('')} |
| 141 | ${arr.slice(3, 6).join('')} |
| 142 | ${arr.slice(6).join('')} |
| 143 | |
| 144 | ▢ *Room ID:* ${room.id} |
| 145 | ▢ *Rules:* |
| 146 | • Make 3 rows of symbols vertically, horizontally or diagonally to win |
| 147 | • Type a number (1-9) to place your symbol |
| 148 | • Type *surrender* to give up |
| 149 | `; |
| 150 | await sock.sendMessage(chatId, { |
| 151 | text: str, |
| 152 | mentions: [room.game.currentTurn, room.game.playerX, room.game.playerO] |
| 153 | }, { quoted: message }); |
| 154 | } |
| 155 | else { |
| 156 | room = { |
| 157 | id: `tictactoe-${ +new Date}`, |
| 158 | x: chatId, |
| 159 | o: '', |
| 160 | game: new TicTacToe(senderId, 'o'), |
| 161 | state: 'WAITING' |