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.

236 lines
5.0 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. var room = function(capacityP, pass, owner)
  8. {
  9. //max capacity of room -- default is 4 for now
  10. this.capacity = capacityP;
  11. //name of the room
  12. this.roomName = owner.name;
  13. this.addUser(owner);
  14. //list of words used in the game
  15. this.words = [];
  16. //list players -- so we can push requests to them
  17. this.users = [];
  18. //increments when rounds pass
  19. this.currentRound = 0;
  20. // the password of the room -- null if no password
  21. this.password = null;
  22. this.state = 1;
  23. /**
  24. * adds a user to a room
  25. * @param p
  26. * return 0 if they could join
  27. */
  28. this.addUser = function(player)
  29. {
  30. //check if room is not full
  31. this.users.push(player);
  32. player.room = this;
  33. }
  34. /**
  35. * Removes a specific user from the room and adjusts the size of the array
  36. * if the array is empty, the room closes
  37. * @param p
  38. */
  39. this.removeUser = function(p)
  40. {
  41. var temp = new Array();
  42. for(var i = 0; i < temp.length; i++)
  43. {
  44. if(p.name === this.users[i].name)
  45. {
  46. }
  47. else
  48. {
  49. temp.push(this.users[i]);
  50. }
  51. }
  52. this.users = temp;
  53. //if room is empty remove the room from rooms list
  54. if(this.users.length == 0)
  55. {
  56. rooms[this.roomName] = null;
  57. }
  58. }
  59. /**
  60. * creates json to send in the 'roomUpdate' socket event
  61. */
  62. this.generateRoomUpdate()
  63. {
  64. }
  65. /**
  66. * Whether or not a user can join this room -- checks for number of people are
  67. * already in the room and the password
  68. * @param p
  69. * @returns {boolean}
  70. */
  71. this.canJoin = function(p)
  72. {
  73. if(this.password == null)
  74. {
  75. return (this.users.length < this.capacity);
  76. }
  77. else
  78. {
  79. return (this.users.length < this.capacity) && (p === this.password);
  80. }
  81. }
  82. }
  83. //list of all the rooms
  84. var rooms = {};
  85. var player = function(s)
  86. {
  87. //name of the user
  88. this.name = null;
  89. //players socket
  90. this.socket = s;
  91. //score of the player
  92. this.score = 0;
  93. //reference to the room -- might not need this
  94. this.room = null;
  95. this.sumbission = null;
  96. /**
  97. * generate the json object used in 'roomUpdate' socket io event
  98. */
  99. this.genJASON = function()
  100. {
  101. }
  102. }
  103. //list of all players --accessed using names like a dic
  104. var players = {};
  105. var app = require('express')();
  106. var http = require('http').Server(app);
  107. var io = require('socket.io')(http);
  108. const port = 3000;
  109. const serverUtils = require('./serverUtils.js');
  110. //Whenever someone connects this gets executed
  111. io.on('connection', function(socket)
  112. {
  113. var player = new player(socket);
  114. console.log('A user connected');
  115. /**
  116. *Register user nickname/handle (register) Client => Server
  117. */
  118. socket.on('register', function(data) {
  119. console.log("Register event called");
  120. console.log(data);
  121. console.log(" ");
  122. //checks for user name in use
  123. if(serverUtils.userAvailable(data, players))
  124. {
  125. player.name = data;
  126. players[data] = player;
  127. socket.emit('sendRooms', serverUtils.generateSendRoomsJSON(rooms));
  128. }
  129. else
  130. {
  131. socket.emit('registerFailed', 'User name taken');
  132. }
  133. });
  134. /**
  135. *Create Room (createRoom) Client => Server
  136. * data {password: , capacity: }
  137. */
  138. socket.on('createRoom', function(data) {
  139. console.log("create room event called");
  140. console.log(data);
  141. console.log(" ");
  142. rooms[player.name] = new room(data.capacity, data.password, player);
  143. });
  144. /**
  145. * Room Selection (joinRoom) Client => Server
  146. * data {roomName: , password: }
  147. */
  148. socket.on('joinRoom', function(data) {
  149. console.log("join room event called");
  150. console.log(data);
  151. console.log(" ");
  152. if(rooms[data.roomName].canJoin(data.password))
  153. {
  154. rooms[data.roomName].addUser(player);
  155. }
  156. else
  157. {
  158. socket.emit('registerFailed', 'Failed connecting to room');
  159. }
  160. });
  161. /**
  162. * data -- literally a string
  163. */
  164. socket.on('submitWord', function(data) {
  165. console.log("submitWord called");
  166. console.log(data);
  167. console.log(" ");
  168. });
  169. //Whenever someone disconnects
  170. socket.on('disconnect', function () {
  171. console.log('A user disconnected');
  172. if(rooms[player.name] != null)
  173. {
  174. rooms[player.name] = null;
  175. }
  176. //leave the room
  177. if(player.room != null)
  178. {
  179. player.room.removeUser(player);
  180. }
  181. players[player.name] = null;
  182. });
  183. });
  184. http.listen(port, function() {
  185. console.log('listening on *:3000');
  186. });