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.

301 lines
6.5 KiB

  1. const serverUtils = require('./serverUtils.js');
  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. var room = function(capacityP, pass, owner)
  10. {
  11. //max capacity of room -- default is 4 for now
  12. this.capacity = capacityP;
  13. //name of the room
  14. this.roomName = owner.name;
  15. this.addUser(owner);
  16. //list of words used in the game
  17. //7 for now will change later to be room specific
  18. this.words = utils.getRandomWords(7);
  19. this.currentWord = this.words.pop();
  20. //list players -- so we can push requests to them
  21. this.users = [];
  22. //increments when rounds pass
  23. this.currentRound = 0;
  24. // the password of the room -- null if no password
  25. this.password = null;
  26. /**
  27. 1 = Waiting for users
  28. 2 = Word shown, Waiting for response from users
  29. 3 = Showing Result
  30. 4 = Game Over, Display Final Results
  31. */
  32. this.state = 1;
  33. /**
  34. * adds a user to a room
  35. * @param p
  36. * return 0 if they could join
  37. */
  38. this.addUser = function(player)
  39. {
  40. //check if room is not full
  41. this.users.push(player);
  42. player.room = this;
  43. if(this.users.length == this.capacity)
  44. {
  45. this.state = 2;
  46. }
  47. this.sendRoomUpdate();
  48. }
  49. this.sendRoomUpdate = function()
  50. {
  51. var message = this.generateRoomUpdate();
  52. this.users.forEach(function(u)
  53. {
  54. u.socket.emit('roomUpdate', message);
  55. });
  56. }
  57. /**
  58. * Removes a specific user from the room and adjusts the size of the array
  59. * if the array is empty, the room closes
  60. * @param p
  61. */
  62. this.removeUser = function(p)
  63. {
  64. var temp = new Array();
  65. for(var i = 0; i < temp.length; i++)
  66. {
  67. if(p.name === this.users[i].name)
  68. {
  69. }
  70. else
  71. {
  72. temp.push(this.users[i]);
  73. }
  74. }
  75. this.users = temp;
  76. //if room is empty remove the room from rooms list
  77. if(this.users.length == 0)
  78. {
  79. rooms[this.roomName] = null;
  80. }
  81. }
  82. /**
  83. * creates json to send in the 'roomUpdate' socket event
  84. *
  85. * {users: gameState: roundWinner: currentWord: }
  86. */
  87. this.generateRoomUpdate = function()
  88. {
  89. var result = new Object();
  90. result.users = [];
  91. this.users.forEach(function(u)
  92. {
  93. result.users.push(u.genJASON());
  94. });
  95. result.gameState = this.state;
  96. result.roundWinner = "meh";
  97. result.currentWord = this.currentWord;
  98. return result;
  99. }
  100. /**
  101. * Whether or not a user can join this room -- checks for number of people are
  102. * already in the room and the password
  103. * @param p
  104. * @returns {boolean}
  105. */
  106. this.canJoin = function(p)
  107. {
  108. if(this.password == null)
  109. {
  110. return (this.users.length < this.capacity);
  111. }
  112. else
  113. {
  114. return (this.users.length < this.capacity) && (p === this.password);
  115. }
  116. }
  117. //updates room variables
  118. this.update = function()
  119. {
  120. }
  121. }
  122. //list of all the rooms
  123. var rooms = {};
  124. var player = function(s)
  125. {
  126. //name of the user
  127. this.name = null;
  128. //players socket
  129. this.socket = s;
  130. //score of the player
  131. this.score = 0;
  132. //reference to the room -- might not need this
  133. this.room = null;
  134. //the word the user selected for current round
  135. this.sumbission = null;
  136. /**
  137. * generate the json object used in 'roomUpdate' socket io event
  138. *
  139. * return {name: score: word:}
  140. */
  141. this.genJASON = function()
  142. {
  143. var result = new Object();
  144. result.name = this.name;
  145. result.score = this.score;
  146. result.word = this.sumbission;
  147. }
  148. /**
  149. * data -- literally a string
  150. * @param data
  151. */
  152. this.selectWord = function(data)
  153. {
  154. this.sumbission = data;
  155. this.room.update();
  156. }
  157. }
  158. //list of all players --accessed using names like a dic
  159. var players = {};
  160. var app = require('express')();
  161. var http = require('http').Server(app);
  162. var io = require('socket.io')(http);
  163. const port = 3000;
  164. //Whenever someone connects this gets executed
  165. io.on('connection', function(socket)
  166. {
  167. var player = new player(socket);
  168. console.log('A user connected');
  169. /**
  170. *Register user nickname/handle (register) Client => Server
  171. */
  172. socket.on('register', function(data) {
  173. console.log("Register event called");
  174. console.log(data);
  175. console.log(" ");
  176. //checks for user name in use
  177. if(serverUtils.userAvailable(data, players))
  178. {
  179. player.name = data;
  180. players[data] = player;
  181. socket.emit('sendRooms', serverUtils.generateSendRoomsJSON(rooms));
  182. }
  183. else
  184. {
  185. socket.emit('registerFailed', 'User name taken');
  186. }
  187. });
  188. /**
  189. *Create Room (createRoom) Client => Server
  190. * data {password: , capacity: }
  191. */
  192. socket.on('createRoom', function(data) {
  193. console.log("create room event called");
  194. console.log(data);
  195. console.log(" ");
  196. rooms[player.name] = new room(data.capacity, data.password, player);
  197. });
  198. /**
  199. * Room Selection (joinRoom) Client => Server
  200. * data {roomName: , password: }
  201. */
  202. socket.on('joinRoom', function(data) {
  203. console.log("join room event called");
  204. console.log(data);
  205. console.log(" ");
  206. if(rooms[data.roomName].canJoin(data.password))
  207. {
  208. rooms[data.roomName].addUser(player);
  209. }
  210. else
  211. {
  212. socket.emit('registerFailed', 'Failed connecting to room');
  213. }
  214. });
  215. /**
  216. * data -- literally a string
  217. */
  218. socket.on('submitWord', function(data) {
  219. console.log("submitWord called");
  220. console.log(data);
  221. console.log(" ");
  222. player.selectWord(data);
  223. });
  224. //Whenever someone disconnects
  225. socket.on('disconnect', function () {
  226. console.log('A user disconnected');
  227. if(rooms[player.name] != null)
  228. {
  229. rooms[player.name] = null;
  230. }
  231. //leave the room
  232. if(player.room != null)
  233. {
  234. player.room.removeUser(player);
  235. }
  236. players[player.name] = null;
  237. });
  238. });
  239. http.listen(port, function() {
  240. console.log('listening on *:3000');
  241. });