Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. const settings = require("../bot/settings.json");
  2. const Discord = require("discord.js");
  3. const fs = require("fs");
  4. const ms = require("ms");
  5. const emojiCharacters = require("../emojiCharacters");
  6.  
  7. module.exports.run = async (bot, message, args) => {
  8. var board = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]];
  9.  
  10. var player1 = message.author.id;
  11. var winner;
  12.  
  13. if (
  14. bot.game //&& user.id !== message.author.id
  15. ) {
  16. message.reply("There is already a game on, try again later");
  17. return;
  18. } else {
  19. //bot.game = true;
  20.  
  21. function win(board) {
  22. let winner;
  23.  
  24. for (let i = 0; i < board.length; i++) {
  25. if (board[i][1] === board[i][0] && board[i][1] === board[i][2]) {
  26. winner = board[i][1];
  27. //message.reply("The winner is(linha) " + winner);
  28. }
  29. if (board[1][i] === board[0][i] && board[1][i] === board[2][i]) {
  30. winner = board[1][i];
  31. //message.reply("The winner is(coluna) " + winner);
  32. }
  33. }
  34. if (board[0][0] === board[1][1] && board[1][1] === board[2][2]) {
  35. winner = board[1][1];
  36. //message.reply("The winner is(diagonal descer) " + winner);
  37. }
  38. if (board[2][0] === board[1][1] && board[1][1] === board[0][2]) {
  39. winner = board[1][1];
  40. //message.reply("The winner is(diagonal subir) " + winner);
  41. }
  42.  
  43. if (winner) bot.game = false;
  44. return winner;
  45. }
  46.  
  47. let game = await message.channel.send("Click on the emote to join");
  48. await game.react("🚪");
  49.  
  50. const filterEmoji = (reaction, user) => {
  51. return ["🚪"].includes(reaction.emoji.name);
  52. };
  53.  
  54. const join = await game
  55. .awaitReactions(filterEmoji, {
  56. // funcçao que permite verificar qual emote foi escolhido
  57. max: 2,
  58. time: 60000,
  59. errors: ["time"]
  60. })
  61. .catch(collected => {
  62. console.log(`After a minute, only ${collected.size} out of 1 reacted.`);
  63.  
  64. game.edit(`TIME OUT ⏰\nNobody joined the game`);
  65. bot.game = false;
  66. });
  67.  
  68. const reaction = join.last();
  69. //message.channel.send("The user join the game: " + reaction.users.last());
  70. const player2 = reaction.users.last().id;
  71.  
  72. //const data = await promiseFunction().catch(err => console.log(err))
  73.  
  74. //Inicio do jogo
  75.  
  76. if (player1 && player2) {
  77. await message.channel.send(
  78. "O player 1 = <@" + player1 + "> e o player 2 é = <@" + player2 + ">"
  79. );
  80. }
  81.  
  82. await game.edit(
  83. `This is the board: \n${emojiCharacters[board[0][0]]} | ${
  84. emojiCharacters[board[0][1]]
  85. } | ${emojiCharacters[board[0][2]]}\n${emojiCharacters[board[1][0]]} | ${
  86. emojiCharacters[board[1][1]]
  87. } | ${emojiCharacters[board[1][2]]}\n${emojiCharacters[board[2][0]]} | ${
  88. emojiCharacters[board[2][1]]
  89. } | ${emojiCharacters[board[2][2]]}`
  90. );
  91.  
  92. while (1 === 1 /*bot.game === true*/) {
  93. const filterMessage = m =>
  94. m.author.id === player1 &&
  95. m.content.length === 1 &&
  96. [
  97. "a",
  98. "b",
  99. "c",
  100. "d",
  101. "e",
  102. "f",
  103. "g",
  104. "h",
  105. "i",
  106. "A",
  107. "B",
  108. "C",
  109. "D",
  110. "E",
  111. "F",
  112. "G",
  113. "H",
  114. "I"
  115. ].includes(m.content);
  116.  
  117. let pick = await message.channel
  118. .awaitMessages(filterMessage, {
  119. max: 1,
  120. time: 60000,
  121. errors: ["time"]
  122. })
  123. .catch(collected => message.channel.send("Time out"));
  124.  
  125. let move = pick.first().content.toLowerCase();
  126. console.log("move= " + move);
  127.  
  128. for (let i = 0; i < board.length; i++) {
  129. for (let j = 0; j < board[i].length; j++) {
  130. if (board[i][j] === move) {
  131. console.log(`O lugar [${i}] [${j}] esta ocupado`);
  132. board[i][j] = "x";
  133. }
  134. }
  135. }
  136.  
  137. await game.edit(
  138. `**Player 2 turn** \n${emojiCharacters[board[0][0]]} | ${
  139. emojiCharacters[board[0][1]]
  140. } | ${emojiCharacters[board[0][2]]}\n${
  141. emojiCharacters[board[1][0]]
  142. } | ${emojiCharacters[board[1][1]]} | ${
  143. emojiCharacters[board[1][2]]
  144. }\n${emojiCharacters[board[2][0]]} | ${
  145. emojiCharacters[board[2][1]]
  146. } | ${emojiCharacters[board[2][2]]}`
  147. );
  148.  
  149. winner = win(board);
  150. if (winner) {
  151. message.channel.send(`The winer is <@${player1}>`);
  152. return;
  153. }
  154.  
  155. const filterMessageP2 = m =>
  156. m.author.id === player2 &&
  157. m.content.length === 1 &&
  158. [
  159. "a",
  160. "b",
  161. "c",
  162. "d",
  163. "e",
  164. "f",
  165. "g",
  166. "h",
  167. "i",
  168. "A",
  169. "B",
  170. "C",
  171. "D",
  172. "E",
  173. "F",
  174. "G",
  175. "H",
  176. "I"
  177. ].includes(m.content);
  178.  
  179. let pick2 = await message.channel
  180. .awaitMessages(filterMessageP2, {
  181. max: 1,
  182. time: 60000,
  183. errors: ["time"]
  184. })
  185. .catch(collected => message.channel.send("Time out"));
  186. let move2 = pick2.first().content.toLowerCase();
  187. console.log("move= " + move);
  188.  
  189. for (let i = 0; i < board.length; i++) {
  190. for (let j = 0; j < board[i].length; j++) {
  191. if (board[i][j] === move2) {
  192. console.log(`O lugar [${i}] [${j}] esta ocupado`);
  193. board[i][j] = "o";
  194. }
  195. }
  196. }
  197.  
  198. await game.edit(
  199. `**Player 1 turn** \n${emojiCharacters[board[0][0]]} | ${
  200. emojiCharacters[board[0][1]]
  201. } | ${emojiCharacters[board[0][2]]}\n${
  202. emojiCharacters[board[1][0]]
  203. } | ${emojiCharacters[board[1][1]]} | ${
  204. emojiCharacters[board[1][2]]
  205. }\n${emojiCharacters[board[2][0]]} | ${
  206. emojiCharacters[board[2][1]]
  207. } | ${emojiCharacters[board[2][2]]}`
  208. );
  209.  
  210. winner = win(board);
  211. if (winner) {
  212. message.channel.send(`The winer is <@${player2}>`);
  213. return;
  214. }
  215. }
  216.  
  217. // https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=createMessageCollector
  218. // https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=awaitMessages
  219.  
  220. bot.game = false;
  221. }
  222. };
  223.  
  224. module.exports.help = {
  225. name: "Game2",
  226. command: "game2",
  227. aliases: ["tictactoe"],
  228. helpInfo: ["Tic tac toe game, make a row to win"]
  229. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement