not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
6.7 KiB

  1. //used for the getting the word array
  2. const utils = require("./utils.js");
  3. /**
  4. * Object used for storing rooms
  5. * @param capacityP -- the number of people that can be in room
  6. * @param pass -- the room password -- null if none
  7. * @param owner -- the person who is creating the room
  8. */
  9. class Room
  10. {
  11. constructor(capacityP, pass, owner)
  12. {
  13. //max capacity of room -- default is 4 for now
  14. this.capacity = capacityP;
  15. //name of the room
  16. this.roomName = owner.name;
  17. //list of words used in the game
  18. //7 for now will change later to be room specific
  19. this.words = utils.getRandomWords(7);
  20. this.currentWord = this.words.pop();
  21. //list players -- so we can push requests to them
  22. this.users = [];
  23. //increments when rounds pass
  24. this.currentRound = 0;
  25. // the password of the room -- null if no password
  26. this.password = pass;
  27. /**
  28. 1 = Waiting for users
  29. 2 = Word shown, Waiting for response from users
  30. 3 = Showing Result
  31. 4 = Game Over, Display Final Results
  32. */
  33. this.state = 1;
  34. this.addUser(owner);
  35. }
  36. /**
  37. * creates json to send in the 'roomUpdate' socket event
  38. *
  39. * {users: gameState: roundWinner: currentWord: }
  40. */
  41. generateRoomUpdate()
  42. {
  43. var result = new Object();
  44. result.users = [];
  45. this.users.forEach(function(u)
  46. {
  47. result.users.push(u.genJASON());
  48. });
  49. //sort the users based on score
  50. var countOuter = 0;
  51. var countInner = 0;
  52. var countSwap = 0;
  53. // var swapped;
  54. // do
  55. // {
  56. // countOuter++;
  57. // swapped = false;
  58. // for(var i = 0; i < result.users.length; i++)
  59. // {
  60. // countInner++;
  61. // if(result.users[i].score && result.users[i + 1].score &&
  62. // result.users[i].score > result.users[i + 1].score)
  63. // {
  64. // countSwap++;
  65. // var temp = result.users[i];
  66. // result.users[i] = result.users[j];
  67. // result.users[j] = temp;
  68. // swapped = true;
  69. // }
  70. // }
  71. // } while(swapped);
  72. result.gameState = this.state;
  73. //sets round winner
  74. var rWinner = -1;
  75. for(var i = 0; i < this.users.length; i++)
  76. {
  77. if(rWinner < this.users[i].roundScore)
  78. {
  79. result.roundWinner = this.users[i].name;
  80. rWinner = this.users[i].roundScore;
  81. }
  82. }
  83. result.currentWord = this.currentWord;
  84. return result;
  85. }
  86. /**
  87. * grabs roomUpdate json and beams it to every user in the channel
  88. */
  89. sendRoomUpdate()
  90. {
  91. var message = this.generateRoomUpdate();
  92. this.users.forEach(function(u)
  93. {
  94. //console.log("room update called");
  95. u.socket.emit('roomUpdate', message);
  96. //console.log(message);
  97. });
  98. }
  99. /**
  100. * adds a user to a room
  101. * @param p
  102. * return 0 if they could join
  103. */
  104. addUser(player)
  105. {
  106. //console.log("user added");
  107. //check if room is not full
  108. this.users.push(player);
  109. player.room = this;
  110. if(this.users.length == this.capacity)
  111. {
  112. this.state = 2;
  113. }
  114. console.log("user added to room " + player.name);
  115. //console.log(this.users);
  116. this.update();
  117. }
  118. /**
  119. * Removes a specific user from the room and adjusts the size of the array
  120. * if the array is empty, the room closes
  121. * @param p
  122. */
  123. removeUser(p)
  124. {
  125. console.log("remove users fnc called");
  126. var temp = new Array();
  127. for(var i = 0; i < temp.length; i++)
  128. {
  129. if(p.name === this.users[i].name)
  130. {
  131. }
  132. else
  133. {
  134. temp.push(this.users[i]);
  135. }
  136. }
  137. this.users = temp;
  138. //if room is empty remove the room from rooms list
  139. if(this.users.length == 0)
  140. {
  141. console.log("room scrubbed");
  142. delete rooms[this.roomName];
  143. }
  144. this.update();
  145. }
  146. /**
  147. * Whether or not a user can join this room -- checks for number of people are
  148. * already in the room and the password
  149. * @param p
  150. * @returns {boolean}
  151. */
  152. canJoin(p)
  153. {
  154. if(this.password == null)
  155. {
  156. return (this.users.length < this.capacity);
  157. }
  158. else
  159. {
  160. return (this.users.length < this.capacity) && (p === this.password);
  161. }
  162. }
  163. /**
  164. * starts new round for the room -- called once all the players have submitted
  165. */
  166. newRound()
  167. {
  168. console.log("new round started");
  169. if(this.words.length == 0)
  170. {
  171. this.state == 4;
  172. }
  173. else
  174. {
  175. this.currentRound++;
  176. this.users.forEach(function(u)
  177. {
  178. u.submission = '';
  179. });
  180. this.currentWord = this.words.pop();
  181. this.state = 2;
  182. }
  183. this.sendRoomUpdate();
  184. }
  185. //updates room variables
  186. update()
  187. {
  188. switch(this.state)
  189. {
  190. case 1: //waiting for users to join
  191. {
  192. if(this.users.length == this.capacity)
  193. {
  194. this.newRound();
  195. }
  196. break;
  197. }
  198. case 2: // waiting for responses
  199. {
  200. var flag = true;
  201. var test = "";
  202. this.users.forEach(function(u)
  203. {
  204. test+=u.submission;
  205. if(u.submission === '')
  206. {
  207. flag = false;
  208. }
  209. });
  210. console.log("big stuff " + test);
  211. if(flag)
  212. {
  213. this.state = 3;
  214. this.newRound();
  215. // setTimeout(function() {
  216. //
  217. // }, 4000);
  218. }
  219. break;
  220. }
  221. case 3: // showing results -- time out fnc
  222. {
  223. console.log("error &&&&&&&&&&&&&&&&&&");
  224. break;
  225. }
  226. case 4: //game over display final result
  227. {
  228. //sqlStuff.dumpRoom(this);
  229. break;
  230. }
  231. default:
  232. {
  233. console.log("You don goof up")
  234. }
  235. }
  236. console.log(this.state + " state");
  237. this.sendRoomUpdate();
  238. }
  239. }
  240. module.exports = Room;