Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1.  
  2. module.exports = (client, message, new_message, player1_id, player2_id, turn_id, symbol, symbols, grid_message) => {
  3.  
  4. var score = [
  5. [null, null, null],
  6. [null, null, null],
  7. [null, null, null]
  8. ];
  9. var will_end_game = false;
  10.  
  11. client.on('messageReactionAdd', (reaction, user) => {
  12. if (reaction.message.id == new_message.id && turn_id == user.id && !will_end_game) {
  13.  
  14. let emoji;
  15. // Convert emoji identifier to :emoji: format
  16. switch(reaction.emoji.identifier) {
  17. case '1%E2%83%A3':
  18. emoji = ':one:';
  19. if (score[0][0] == null) score[0][0] = symbol;
  20. break;
  21. case '2%E2%83%A3':
  22. emoji = ':two:';
  23. if (score[0][1] == null) score[0][1] = symbol;
  24. break;
  25. case '3%E2%83%A3':
  26. emoji = ':three:';
  27. if (score[0][2] == null) score[0][2] = symbol;
  28. break;
  29. case '4%E2%83%A3':
  30. emoji = ':four:';
  31. if (score[1][0] == null) score[1][0] = symbol;
  32. break;
  33. case '5%E2%83%A3':
  34. emoji = ':five:';
  35. if (score[1][1] == null) score[1][1] = symbol;
  36. break;
  37. case '6%E2%83%A3':
  38. emoji = ':six:';
  39. if (score[1][2] == null) score[1][2] = symbol;
  40. break;
  41. case '7%E2%83%A3':
  42. emoji = ':seven:';
  43. if (score[2][0] == null) score[2][0] = symbol;
  44. break;
  45. case '8%E2%83%A3':
  46. emoji = ':eight:';
  47. if (score[2][1] == null) score[2][1] = symbol;
  48. break;
  49. case '9%E2%83%A3':
  50. emoji = ':nine:';
  51. if (score[2][2] == null) score[2][2] = symbol;
  52. break;
  53. default:
  54. break;
  55. }
  56.  
  57. // Replace number tile with O or X (checks if it exists first)
  58. if (grid_message.content.indexOf(emoji) == -1) {
  59. return;
  60. }
  61. grid_message.edit(grid_message.content.replace(emoji, symbol))
  62. .then((new_mes) => {
  63. grid_message = new_mes;
  64. console.log("Successful # tile to symbol switch");
  65. })
  66. .catch(console.error);
  67.  
  68. // Check if the game has concluded
  69. if (didPlayerWin(symbols[0], player1_id) || didPlayerWin(symbols[1], player2_id) || didItTie()) {
  70. will_end_game = true;
  71. return;
  72. }
  73.  
  74. // Replace player with the next and symbol with the next
  75. let temp_message = new_message.content.replace(`<@${turn_id}>`, `<@${toggle_player(turn_id, player1_id, player2_id)}>`);
  76. temp_message = temp_message.replace(symbol, toggle_symbol(symbol));
  77. new_message.edit(temp_message)
  78. .then(console.log("Successful turn switch"))
  79. .catch(console.error);
  80.  
  81. // Toggle symbols between O and X and players 1 and 2
  82. symbol = toggle_symbol(symbol);
  83. turn_id = toggle_player(turn_id, player1_id, player2_id);
  84. }
  85. })
  86.  
  87.  
  88. // Function for toggling players
  89. function toggle_player(turn_id, player1_id, player2_id) {
  90. let player_switched;
  91. if (turn_id == player1_id) {
  92. player_switched = player2_id;
  93. }
  94. else {
  95. player_switched = player1_id;
  96. }
  97. return player_switched;
  98. }
  99.  
  100. // Function for toggling symbols
  101. function toggle_symbol(symbol) {
  102. return symbols[Math.abs(symbols.findIndex((sym) => {
  103. return sym == symbol;
  104. }) - 1)];
  105. }
  106.  
  107. // Function for checking if a player won
  108. function didPlayerWin(sym, player) {
  109. for (let i = 0; i < score.length; i++) {
  110. // Horizontal checks
  111. if (score[i][0] == sym &&
  112. score[i][1] == sym &&
  113. score[i][2] == sym) {
  114. new_message.edit(`<@${player}> Congratulations I won! :white_check_mark: `)
  115. .then(console.log('Successful win'))
  116. .catch(console.error);
  117. return true;
  118. }
  119. // Vertical checks
  120. else if (score[0][i] == sym &&
  121. score[1][i] == sym &&
  122. score[2][i] == sym) {
  123. new_message.edit(`<@${player}> Congratulations I won! :white_check_mark: `)
  124. .then(console.log('Successful win'))
  125. .catch(console.error);
  126. return true;
  127. }
  128. }
  129. // Diagonal checks
  130. if (score[0][0] == sym &&
  131. score[1][1] == sym &&
  132. score[2][2] == sym) {
  133. new_message.edit(`<@${player}> Congratulations I won! :white_check_mark: `)
  134. .then(console.log('Successful win'))
  135. .catch(console.error);
  136. return true;
  137. }
  138. else if (score[0][2] == sym &&
  139. score[1][1] == sym &&
  140. score[2][0] == sym) {
  141. new_message.edit(`<@${player}> Congratulations I won! :white_check_mark: `)
  142. .then(console.log('Successful win'))
  143. .catch(console.error);
  144. return true;
  145. }
  146.  
  147. return false;
  148. }
  149.  
  150. // Function for checking if it's a tie
  151. function didItTie() {
  152. let null_counter = 0;
  153. for (let i = 0; i < score.length; i++) {
  154. for (let j = 0; j < score.length; j++) {
  155. if (score[i][j] == null) {
  156. null_counter++;
  157. }
  158. }
  159. }
  160. if (null_counter == 0) {
  161. new_message.edit('**It\'s A tie!**')
  162. .then(console.log('Successful tie'))
  163. .catch(console.error);
  164. return true;
  165. }
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement