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.

431 lines
9.6 KiB

  1. const serverUtils = require('./serverUtils.js');
  2. const utils = require("./utils.js");
  3. const trendingAPI = require("./trendsAPI.js");
  4. /**
  5. * Object used for storing rooms
  6. * @param capacityP -- the number of people that can be in room
  7. * @param pass -- the room password -- null if none
  8. * @param owner -- the person who is creating the room
  9. */
  10. var room = function(capacityP, pass, owner)
  11. {
  12. //max capacity of room -- default is 4 for now
  13. this.capacity = capacityP;
  14. //name of the room
  15. this.roomName = owner.name;
  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. * creates json to send in the 'roomUpdate' socket event
  35. *
  36. * {users: gameState: roundWinner: currentWord: }
  37. */
  38. this.generateRoomUpdate = function()
  39. {
  40. var result = new Object();
  41. result.users = [];
  42. this.users.forEach(function(u)
  43. {
  44. result.users.push(u.genJASON());
  45. });
  46. result.gameState = this.state;
  47. result.roundWinner = "meh";
  48. result.currentWord = this.currentWord;
  49. return result;
  50. }
  51. this.sendRoomUpdate = function()
  52. {
  53. var message = this.generateRoomUpdate();
  54. this.users.forEach(function(u)
  55. {
  56. u.socket.emit('roomUpdate', message);
  57. console.log(message);
  58. });
  59. }
  60. /**
  61. * adds a user to a room
  62. * @param p
  63. * return 0 if they could join
  64. */
  65. this.addUser = function(player)
  66. {
  67. console.log("user added");
  68. //check if room is not full
  69. this.users.push(player);
  70. player.room = this;
  71. if(this.users.length == this.capacity)
  72. {
  73. this.state = 2;
  74. }
  75. console.log("rooms users");
  76. console.log(this.users);
  77. this.sendRoomUpdate();
  78. }
  79. this.addUser(owner);
  80. /**
  81. * Removes a specific user from the room and adjusts the size of the array
  82. * if the array is empty, the room closes
  83. * @param p
  84. */
  85. this.removeUser = function(p)
  86. {
  87. console.log("remove users fnc called");
  88. var temp = new Array();
  89. for(var i = 0; i < temp.length; i++)
  90. {
  91. if(p.name === this.users[i].name)
  92. {
  93. }
  94. else
  95. {
  96. temp.push(this.users[i]);
  97. }
  98. }
  99. this.users = temp;
  100. //if room is empty remove the room from rooms list
  101. if(this.users.length == 0)
  102. {
  103. rooms.remove(this.roomName);
  104. }
  105. }
  106. /**
  107. * Whether or not a user can join this room -- checks for number of people are
  108. * already in the room and the password
  109. * @param p
  110. * @returns {boolean}
  111. */
  112. this.canJoin = function(p)
  113. {
  114. if(this.password == null)
  115. {
  116. return (this.users.length < this.capacity);
  117. }
  118. else
  119. {
  120. return (this.users.length < this.capacity) && (p === this.password);
  121. }
  122. }
  123. this.newRound = function()
  124. {
  125. if(this.words.length == 0)
  126. {
  127. this.state == 4;
  128. }
  129. else
  130. {
  131. this.currentRound++;
  132. this.users.forEach(function(u)
  133. {
  134. u.sumbission = '';
  135. });
  136. this.currentRound = this.words.pop();
  137. this.state = 2;
  138. }
  139. }
  140. //updates room variables
  141. this.update = function()
  142. {
  143. switch(this.state)
  144. {
  145. case 1: //waiting for users to join
  146. {
  147. if(this.users.length == this.capacity)
  148. {
  149. this.newRound();
  150. }
  151. break;
  152. }
  153. case 2: // waiting for responses
  154. {
  155. var flag = true;
  156. this.users.forEach(function(u)
  157. {
  158. if(u.sumbission === '')
  159. {
  160. flag = false;
  161. }
  162. });
  163. if(flag)
  164. {
  165. this.state =3;
  166. this.sendRoomUpdate();
  167. setTimeout(function() {
  168. this.newRound();
  169. }, 4000);
  170. }
  171. break;
  172. }
  173. case 3: // showing results -- time out fnc
  174. {
  175. console.log("error &&&&&&&&&&&&&&&&&&");
  176. break;
  177. }
  178. case 4: //game over display final result
  179. {
  180. break;
  181. }
  182. default:
  183. {
  184. console.log("You don goof up")
  185. }
  186. }
  187. }
  188. }
  189. //list of all the rooms
  190. var rooms = {};
  191. var player = function(s)
  192. {
  193. //name of the user
  194. this.name = null;
  195. //players socket
  196. this.socket = s;
  197. //score of the player
  198. this.score = 0;
  199. //reference to the room -- might not need this
  200. this.room = null;
  201. //the word the user selected for current round
  202. this.sumbission = '';
  203. /**
  204. * generate the json object used in 'roomUpdate' socket io event
  205. *
  206. * return {name: score: word:}
  207. */
  208. this.genJASON = function()
  209. {
  210. var result = new Object();
  211. result.name = this.name;
  212. result.score = this.score;
  213. result.word = this.sumbission;
  214. }
  215. /**
  216. * data -- literally a string
  217. * @param data
  218. */
  219. this.selectWord = function(data)
  220. {
  221. this.sumbission = data;
  222. trendingAPI.getPopularity(data + " " + this.room.currentWord).then(function(result)
  223. {
  224. this.score += result;
  225. console.log("api result for " + result);
  226. this.room.update();
  227. })
  228. }
  229. }
  230. //list of all players --accessed using names like a dic
  231. var players = {};
  232. var generateSendRoomsJSON = function()
  233. {
  234. var obj = new Object();
  235. obj.rooms = [];
  236. //rooms.forEach(function(r)
  237. Object.keys(rooms).forEach(function(key)
  238. {
  239. console.log("**************");
  240. console.log(key);
  241. if(rooms[key] != null)
  242. {
  243. var roomObj = new Object();
  244. if(rooms[key].password == null)
  245. {
  246. roomObj.passwordBool = false;
  247. }
  248. else
  249. {
  250. roomObj.passwordBool = rooms[key].password;
  251. }
  252. roomObj.capacity = rooms[key].capacity;
  253. roomObj.occupents = rooms[key].users.length;
  254. obj.rooms.push(roomObj);
  255. }
  256. else
  257. {
  258. console.log("would not tough it with a 10ft pole");
  259. }
  260. });
  261. return obj;
  262. }
  263. var app = require('express')();
  264. var http = require('http').Server(app);
  265. var io = require('socket.io')(http);
  266. const port = 3000;
  267. //Whenever someone connects this gets executed
  268. io.on('connection', function(socket)
  269. {
  270. var p = new player(socket);
  271. console.log('A user connected');
  272. /**
  273. *Register user nickname/handle (register) Client => Server
  274. */
  275. socket.on('register', function(data) {
  276. console.log("Register event called");
  277. console.log(data);
  278. console.log(" ");
  279. //checks for user name in use
  280. if(serverUtils.userAvailable(data, players))
  281. {
  282. p.name = data;
  283. players[data] = p;
  284. socket.emit('sendRooms', generateSendRoomsJSON());
  285. console.log("send rooms called");
  286. console.log(generateSendRoomsJSON());
  287. }
  288. else
  289. {
  290. socket.emit('registerFailed', 'User name taken');
  291. console.log("registration failed sent");
  292. }
  293. console.log(player);
  294. });
  295. /**
  296. *Create Room (createRoom) Client => Server
  297. * data {password: , capacity: }
  298. */
  299. socket.on('createRoom', function(data) {
  300. console.log("create room event called");
  301. console.log(data);
  302. console.log(" ");
  303. rooms[p.name] = new room(data.capacity, data.password, p);
  304. });
  305. /**
  306. * Room Selection (joinRoom) Client => Server
  307. * data {roomName: , password: }
  308. */
  309. socket.on('joinRoom', function(data) {
  310. console.log("join room event called");
  311. console.log(data);
  312. console.log(" ");
  313. console.log(rooms);
  314. if(rooms[data.roomName] != null && rooms[data.roomName].canJoin(data.password))
  315. {
  316. rooms[data.roomName].addUser(p);
  317. console.log("user joined room");
  318. }
  319. else
  320. {
  321. socket.emit('registerFailed', 'Failed connecting to room');
  322. }
  323. console.log(rooms);
  324. });
  325. /**
  326. * data -- literally a string
  327. */
  328. socket.on('submitWord', function(data) {
  329. console.log("submitWord called");
  330. console.log(data);
  331. console.log(" ");
  332. p.selectWord(data);
  333. });
  334. //Whenever someone disconnects
  335. socket.on('disconnect', function () {
  336. console.log('A user disconnected');
  337. if(rooms[p.name] != null)
  338. {
  339. rooms[p.name] = null;
  340. }
  341. //leave the room
  342. if(p.room != null)
  343. {
  344. p.room.removeUser(p);
  345. }
  346. //players[p.name] = null;
  347. players.remove(p.name);
  348. });
  349. });
  350. http.listen(port, function() {
  351. console.log('listening on *:3000');
  352. });