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.

286 lines
7.6 KiB

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