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.

222 lines
4.7 KiB

  1. /**
  2. * 1-27-18
  3. *
  4. * Main server file which handles users, rooms -- everythang
  5. */
  6. //eh?
  7. const serverUtils = require('./serverUtils.js');
  8. //used for the getting the word array
  9. const utils = require("./utils.js");
  10. //const sqlStuff = require("./sql.js");
  11. var Room = require("./room");
  12. var Player = require("./player");
  13. //import {Room} from 'room.js';
  14. //import {Player} from 'player.js';
  15. //list of all players --accessed using names like a dic
  16. var players = {};
  17. //list of all the rooms
  18. var rooms = {};
  19. var app = require('express')();
  20. var http = require('http').Server(app);
  21. var io = require('socket.io')(http);
  22. const PORT = 3000;
  23. /**
  24. * Generates json sent to user on 'sendRooms'
  25. *
  26. * return [{name: passwordBool: capacity: occupants: }]
  27. */
  28. var generateSendRoomsJSON = function()
  29. {
  30. var obj = new Object();
  31. obj.rooms = [];
  32. //rooms.forEach(function(r)
  33. Object.keys(rooms).forEach(function(key)
  34. {
  35. console.log("**************");
  36. console.log(key);
  37. if(rooms[key] != null)
  38. {
  39. var roomObj = new Object();
  40. roomObj.name = key;
  41. if(rooms[key].password == null)
  42. {
  43. roomObj.passwordBool = false;
  44. }
  45. else
  46. {
  47. roomObj.passwordBool = true;
  48. }
  49. roomObj.capacity = rooms[key].capacity;
  50. roomObj.occupants = rooms[key].users.length;
  51. obj.rooms.push(roomObj);
  52. }
  53. else
  54. {
  55. console.log("would not tough it with a 10ft pole");
  56. }
  57. });
  58. return obj;
  59. }
  60. app.get('/', function(req, res)
  61. {
  62. console.log("err");
  63. res.sendfile('index.html');
  64. });
  65. //Whenever someone connects this gets executed
  66. io.on('connection', function(socket)
  67. {
  68. var p = new Player(socket);
  69. console.log('A user connected');
  70. /**
  71. *Register user nickname/handle (register) Client => Server
  72. */
  73. socket.on('register', function(data)
  74. {
  75. console.log(data + " registered");
  76. //checks for user name in use
  77. //if(serverUtils.userAvailable(data, players))
  78. if(!(data in players))
  79. {
  80. p.name = data;
  81. players[data] = p;
  82. socket.emit('sendRooms', generateSendRoomsJSON());
  83. console.log("send rooms called");
  84. //console.log(generateSendRoomsJSON());
  85. }
  86. else
  87. {
  88. socket.emit('registerFailed', 'User name taken');
  89. console.log("registration failed sent");
  90. }
  91. //console.log(player);
  92. });
  93. /**
  94. *Create Room (createRoom) Client => Server
  95. * data {password: , capacity: }
  96. */
  97. socket.on('createRoom', function(data)
  98. {
  99. console.log(data + "create room");
  100. // console.log(data);
  101. // console.log(" ");
  102. rooms[p.name] = new Room(data.capacity, data.password, p);
  103. //sends updated room list to all users not in a room
  104. var dd = generateSendRoomsJSON();
  105. Object.keys(players).forEach(function(key)
  106. {
  107. if(players[key] != null)
  108. {
  109. if(players[key].room == null)
  110. {
  111. players[key].socket.emit('sendRooms', dd);
  112. }
  113. }
  114. else
  115. {
  116. console.log("player was null Bad!");
  117. }
  118. });
  119. });
  120. /**
  121. * Room Selection (joinRoom) Client => Server
  122. * data {roomName: , password: }
  123. */
  124. socket.on('joinRoom', function(data)
  125. {
  126. console.log(p.name + " joined room " + data.roomName);
  127. if(rooms[data.roomName] != null && rooms[data.roomName].canJoin(data.password))
  128. {
  129. p.room = rooms[data.roomName];
  130. rooms[data.roomName].addUser(p);
  131. console.log("user joined room");
  132. }
  133. else
  134. {
  135. socket.emit('joinFailed', 'Failed connecting to room');
  136. }
  137. //console.log(rooms);
  138. });
  139. /**
  140. * data -- literally a string
  141. */
  142. socket.on('submitWord', function(data)
  143. {
  144. console.log("submitWord called");
  145. p.selectWord(data).then(function(score)
  146. {
  147. p.roundScore = score;
  148. p.score += score;
  149. p.submission = data;
  150. p.room.update();
  151. })
  152. });
  153. //Whenever someone disconnects
  154. socket.on('disconnect', function ()
  155. {
  156. console.log('A user disconnected');
  157. if(rooms[p.name] != null)
  158. {
  159. rooms[p.name] = null;
  160. }
  161. //leave the room
  162. if(p.room != null)
  163. {
  164. p.room.removeUser(p);
  165. }
  166. //players[p.name] = null;
  167. delete players[p.name];
  168. });
  169. });
  170. http.listen(PORT, function()
  171. {
  172. console.log('listening on *:3000');
  173. });