ThirdPartyServer.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const MatchService = require('./MatchService.js');
  2. const RandomFreeQueue = require('./queues/RandomFreeQueue.js');
  3. const io = require('./sockets.js');
  4. let Users = require('../database').Repositories.Users;
  5. const GameMachine= require('../game').GameMachine;
  6. class ThirdPartyServer {
  7. constructor() {
  8. console.log('TPS');
  9. let randomFreeQueue = new RandomFreeQueue();
  10. randomFreeQueue.onMatchFound = (s1, s2, channel, matchInfo) => {
  11. console.log("[TPS]Match Found: " + channel)
  12. let match = new MatchService(s1,s2, channel, matchInfo);
  13. match.onMatchFinished = () => {
  14. console.log("[TPS] -- MATCH FINISHED");
  15. this._dropPlayers(channel);
  16. };
  17. this.matches[channel] = match;
  18. };
  19. this.queues = [ randomFreeQueue ];
  20. //setup socket
  21. // is always called
  22. io.on('connection', (socket) => {
  23. console.log("Default connection:" + socket.id);
  24. // User connected !
  25. // check if user is in game!
  26. this.userInGame(socket).then(() => socket.join(socket._user.id)); ///////
  27. // Setup Events
  28. socket.on('disconnect', this._updateLobby);
  29. socket.on('chatMsg', this._chatMsg);
  30. // create join queue Events
  31. this.queues.forEach((queue) => socket.on('join_' + queue.getName(), () => {
  32. return queue.queue(socket);
  33. }));
  34. this._updateLobby();
  35. });
  36. this.matches = {};
  37. }
  38. _dropPlayers(channel) {
  39. console.log("Dropping channel: " + channel );
  40. return Users.dropChannel(channel);
  41. }
  42. _setupSocket(socket, channel) {
  43. socket.on('broadcast', (data) => this._broadcast(data, channel));
  44. socket.on('abandon', (data) => this._abandon(data, channel));
  45. socket.on('gameOver', (data) => this._gameOver(data, channel));
  46. socket.on('dispute', (data) => this._dispute(data, channel));
  47. socket.leave('queue', this.errorHandler);
  48. socket.join(channel, this.errorHandler);
  49. }
  50. _updateLobby() {
  51. io.clients((err, clients) => {
  52. io.emit('lobbySize', clients.length);
  53. });
  54. if(this.queues) {
  55. this.queues.forEach((queue) => {
  56. queue._updateLobby();
  57. });
  58. }
  59. }
  60. _chatMsg(msg) {
  61. msg.time = new Date().toISOString();
  62. let rId = msg.receiverId;
  63. if(!rId) {
  64. console.log("No receiverID.");
  65. return;
  66. }
  67. io.to(rId).emit('chatMsg');
  68. }
  69. // createMatch(Match) { // sockets
  70. // this.matches.push(match); // create Garbage collector
  71. // }
  72. // queue(socket) {
  73. // // add socket to this queue
  74. // // add Socket to Queues
  75. // // Handle all queues
  76. // }
  77. // match() {
  78. // // Check queue to make matchmaking
  79. // }
  80. userInGame(socket) {
  81. let token = socket.request._query['token'];
  82. return Users.getUser(token)
  83. .then((user) => {
  84. socket._user = user;
  85. if(user.currentGame !== null && user.currentGame !== undefined) {
  86. if(this.matches[user.currentGame.channel]) {
  87. console.log("Game in progress... Sending details...")
  88. this.matches[user.currentGame.channel].reconnect(socket, user.currentGame); // send GameID
  89. return user;
  90. }
  91. }
  92. return false;
  93. });
  94. }
  95. }
  96. module.exports = ThirdPartyServer;