games-manager.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = GamesManager;
  7. var _onlineDuelSync = _interopRequireDefault(require("./online-duel-sync"));
  8. function GamesManager(ioServer, mariadbConn) {
  9. var currentGames = new Map(); // Method to add player into a game, create or add to game syncer
  10. var addPlayerInGame = function addPlayerInGame(player, gameId, joinCreatedGame) {
  11. var isNewGame = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  12. var gameOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
  13. if (currentGames.has(gameId)) {
  14. player.isPlayingGameId = gameId;
  15. return currentGames.get(gameId).addPlayer(player);
  16. } else if (joinCreatedGame === false) {
  17. player.isPlayingGameId = gameId;
  18. currentGames.set(gameId, new _onlineDuelSync.default(ioServer, mariadbConn, gameId, player, isNewGame, gameOptions));
  19. return true; // We are supposed to join a new game, but not there, must have been deleted in the meantime
  20. } else {
  21. console.log('error : ' + player.playerName + ' tries to join a created game that does not exist anymore');
  22. return false;
  23. }
  24. };
  25. var playerLeft = function playerLeft(player, disconnected) {
  26. var id = player.isPlayingGameId;
  27. if (currentGames.has(id)) {
  28. currentGames.get(id).playerLeft(player, disconnected);
  29. if (!currentGames.get(id).hasPlayers()) {
  30. currentGames.delete(id);
  31. mariadbConn.updateGameStatus(id, 'PAUSED');
  32. }
  33. }
  34. player.isPlayingGameId = -1;
  35. };
  36. return {
  37. addPlayerInGame: addPlayerInGame,
  38. playerLeft: playerLeft
  39. };
  40. }
  41. //# sourceMappingURL=games-manager.js.map