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.

146 lines
2.9 KiB

  1. //list of all the rooms
  2. var rooms = [];
  3. var room = function(capacityP, roomN)
  4. {
  5. //max capacity of room -- default is 4 for now
  6. this.capacity = capacityP;
  7. //name of the room
  8. this.roomName = roomN;
  9. //list of words used in the game
  10. this.words = [];
  11. //list players -- so we can push requests to them
  12. this.users = [];
  13. //increments when rounds pass
  14. this.currentRoom = 0;
  15. // the password of the room -- null if no password
  16. this.password = null;
  17. /**
  18. * adds a user to a room
  19. * @param p
  20. * return 0 if they could join
  21. */
  22. this.addUser = function(player)
  23. {
  24. //check if room is not full
  25. if(this.users.length != this.capacity)
  26. {
  27. this.users.push(player)
  28. }
  29. else
  30. {
  31. }
  32. }
  33. /**
  34. *
  35. * @param p
  36. */
  37. this.removeUser = function(p)
  38. {
  39. this.users.remove(p);
  40. //if room is empty remove the room from rooms list
  41. rooms.remove(this);
  42. }
  43. }
  44. var player = function(s)
  45. {
  46. //name of the user
  47. this.name = null;
  48. //players socket
  49. this.socket = s;
  50. //score of the player
  51. this.score = 0;
  52. //reference to the room -- might not need this
  53. this.room = null;
  54. //
  55. this.sumbission = null;
  56. }
  57. var app = require('express')();
  58. var http = require('http').Server(app);
  59. var io = require('socket.io')(http);
  60. const port = 3000;
  61. const utils = require('./serverUtils.js');
  62. //Whenever someone connects this gets executed
  63. io.on('connection', function(socket)
  64. {
  65. var player = new player(socket);
  66. console.log('A user connected');
  67. /**
  68. *Register user nickname/handle (register) Client => Server
  69. */
  70. socket.on('register', function(data) {
  71. console.log("Register event called");
  72. console.log(data);
  73. console.log(" ");
  74. //checks for user name in use
  75. if(utils.userAvailable(data, rooms))
  76. {
  77. player.name = data;
  78. }
  79. else
  80. {
  81. socket.emit('registerFailed', 'User name taken');
  82. }
  83. });
  84. /**
  85. *Create Room (createRoom) Client => Server
  86. * data {password: , capacity: }
  87. */
  88. socket.on('createRoom', function(data) {
  89. console.log("create room event called");
  90. console.log(data);
  91. console.log(" ");
  92. });
  93. /**
  94. *
  95. */
  96. socket.on('joinRoom', function(data) {
  97. console.log("join room event called");
  98. console.log(data);
  99. console.log(" ");
  100. });
  101. socket.on('submitWord', function(data) {
  102. console.log("submitWord called");
  103. console.log(data);
  104. console.log(" ");
  105. });
  106. //Whenever someone disconnects this piece of code executed
  107. socket.on('disconnect', function () {
  108. console.log('A user disconnected');
  109. });
  110. });
  111. http.listen(port, function() {
  112. console.log('listening on *:3000');
  113. });