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.

203 lines
4.1 KiB

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