server-tool-listener.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. 'use strict';
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  8. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  9. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  10. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  11. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  12. var _socket = _interopRequireDefault(require("socket.io"));
  13. var ServerToolListner = /*#__PURE__*/function () {
  14. function ServerToolListner(server) {
  15. (0, _classCallCheck2.default)(this, ServerToolListner);
  16. this.server = server;
  17. }
  18. (0, _createClass2.default)(ServerToolListner, [{
  19. key: "listen",
  20. value: function listen(port) {
  21. var server = this.server;
  22. this.serverTool = _socket.default.listen(port);
  23. this.serverTool.on('connection', function (socket) {
  24. console.log('tool connected');
  25. socket.on('players', function (option, callback) {
  26. callback(server.getCurrentListAuthorizedPlayers());
  27. socket.disconnect(false);
  28. });
  29. socket.on('syncdb', /*#__PURE__*/function () {
  30. var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(option, callback) {
  31. var response;
  32. return _regenerator.default.wrap(function _callee$(_context) {
  33. while (1) {
  34. switch (_context.prev = _context.next) {
  35. case 0:
  36. _context.next = 2;
  37. return server.updatePlayersFromDb();
  38. case 2:
  39. response = _context.sent;
  40. callback(response);
  41. socket.disconnect(false);
  42. case 5:
  43. case "end":
  44. return _context.stop();
  45. }
  46. }
  47. }, _callee);
  48. }));
  49. return function (_x, _x2) {
  50. return _ref.apply(this, arguments);
  51. };
  52. }());
  53. socket.on('add-player', /*#__PURE__*/function () {
  54. var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(username, callback) {
  55. var response;
  56. return _regenerator.default.wrap(function _callee2$(_context2) {
  57. while (1) {
  58. switch (_context2.prev = _context2.next) {
  59. case 0:
  60. _context2.next = 2;
  61. return server.addPlayerDb.apply(server, (0, _toConsumableArray2.default)(username));
  62. case 2:
  63. response = _context2.sent;
  64. callback(response);
  65. socket.disconnect(false);
  66. case 5:
  67. case "end":
  68. return _context2.stop();
  69. }
  70. }
  71. }, _callee2);
  72. }));
  73. return function (_x3, _x4) {
  74. return _ref2.apply(this, arguments);
  75. };
  76. }());
  77. socket.on('remove-player', /*#__PURE__*/function () {
  78. var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(username, callback) {
  79. var response;
  80. return _regenerator.default.wrap(function _callee3$(_context3) {
  81. while (1) {
  82. switch (_context3.prev = _context3.next) {
  83. case 0:
  84. _context3.next = 2;
  85. return server.removePlayerDb.apply(server, (0, _toConsumableArray2.default)(username));
  86. case 2:
  87. response = _context3.sent;
  88. callback(response);
  89. socket.disconnect(false);
  90. case 5:
  91. case "end":
  92. return _context3.stop();
  93. }
  94. }
  95. }, _callee3);
  96. }));
  97. return function (_x5, _x6) {
  98. return _ref3.apply(this, arguments);
  99. };
  100. }());
  101. socket.on('add-game', /*#__PURE__*/function () {
  102. var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(game, callback) {
  103. var response;
  104. return _regenerator.default.wrap(function _callee4$(_context4) {
  105. while (1) {
  106. switch (_context4.prev = _context4.next) {
  107. case 0:
  108. response = '';
  109. _context4.prev = 1;
  110. _context4.next = 4;
  111. return server.addGameDb.apply(server, (0, _toConsumableArray2.default)(game));
  112. case 4:
  113. response = _context4.sent;
  114. response = 'OK - inserted at id :' + response;
  115. _context4.next = 11;
  116. break;
  117. case 8:
  118. _context4.prev = 8;
  119. _context4.t0 = _context4["catch"](1);
  120. response = 'KO ' + _context4.t0.message;
  121. case 11:
  122. callback(JSON.stringify(response));
  123. socket.disconnect(false);
  124. case 13:
  125. case "end":
  126. return _context4.stop();
  127. }
  128. }
  129. }, _callee4, null, [[1, 8]]);
  130. }));
  131. return function (_x7, _x8) {
  132. return _ref4.apply(this, arguments);
  133. };
  134. }());
  135. socket.on('remove-game', /*#__PURE__*/function () {
  136. var _ref5 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(options, callback) {
  137. var response;
  138. return _regenerator.default.wrap(function _callee5$(_context5) {
  139. while (1) {
  140. switch (_context5.prev = _context5.next) {
  141. case 0:
  142. _context5.next = 2;
  143. return server.removeGameDb.apply(server, (0, _toConsumableArray2.default)(options));
  144. case 2:
  145. response = _context5.sent;
  146. callback(response);
  147. socket.disconnect(false);
  148. case 5:
  149. case "end":
  150. return _context5.stop();
  151. }
  152. }
  153. }, _callee5);
  154. }));
  155. return function (_x9, _x10) {
  156. return _ref5.apply(this, arguments);
  157. };
  158. }());
  159. });
  160. }
  161. }]);
  162. return ServerToolListner;
  163. }();
  164. exports.default = ServerToolListner;
  165. //# sourceMappingURL=server-tool-listener.js.map