online-duel-sync.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = OnlineDuelSync;
  7. var _duelController = _interopRequireDefault(require("../client-server-shared/duel-controller"));
  8. var fs = require('fs');
  9. function OnlineDuelSync(ioServer, mariadbConnector, gameId, firstPlayer, isNewGame, gameOptions) {
  10. var io = ioServer;
  11. var mdb = mariadbConnector;
  12. var isNew = isNewGame;
  13. var players = new Map();
  14. var duelController = new _duelController.default();
  15. players.set(firstPlayer.playerName, firstPlayer); // If not new game, set status to PLAYING right now
  16. // Otherwise we wait second player
  17. if (!isNew) {
  18. mdb.updateGameStatus(gameId, 'PLAYING');
  19. duelController.resumeGame();
  20. }
  21. var loadGameData = function loadGameData() {
  22. var gameDetails = {};
  23. try {
  24. gameDetails = mdb.getGameById(gameId);
  25. } catch (err) {
  26. console.log('Error retreiving game : ', err.message);
  27. }
  28. return gameDetails;
  29. };
  30. var chat = function chat(name, message) {
  31. io.to(gameId).emit('message', {
  32. from: name,
  33. text: message
  34. });
  35. };
  36. firstPlayer.getSocket().join(gameId, function () {
  37. chat(firstPlayer.playerName, firstPlayer.playerName + ' has entered the game, waiting for second player...');
  38. }).emit('update-game-data', loadGameData().game_data).on('chat', function (message) {
  39. chat(firstPlayer.playerName, message);
  40. });
  41. var addPlayer = function addPlayer(player) {
  42. var res = true;
  43. if (players.size === 1) {
  44. if (players.has(player.playerName)) {
  45. console.log('ERROR : this player is already there ! ', player.playerName);
  46. return false;
  47. }
  48. players.set(player.playerName, player); // Both players connected to new game, set status to PLAYING
  49. if (isNew) {
  50. isNew = false;
  51. mdb.updateGameStatus(gameId, 'PLAYING', player.playerName);
  52. var allHeroesJson = JSON.parse(fs.readFileSync('../../src/server-shared/all-heroes.json'));
  53. duelController.startNewGame(firstPlayer.playerName, player.playerName, allHeroesJson, gameOptions);
  54. }
  55. player.getSocket().join(gameId, function () {
  56. chat(player.playerName, player.playerName + ' has entered the game');
  57. }).emit('update-game-data', loadGameData().game_data).on('chat', function (message) {
  58. chat(player.playerName, message);
  59. });
  60. } else {
  61. console.log('Already 2 players, weird :' + player.playerName + ' was trying to join : ', players);
  62. res = false;
  63. }
  64. return res;
  65. };
  66. var playerLeft = function playerLeft(player, disconnected) {
  67. var playerLeaving = players.get(player.playerName);
  68. if (playerLeaving) {
  69. if (!disconnected) {
  70. playerLeaving.getSocket().leave(gameId);
  71. }
  72. chat(player.playerName, player.playerName + ' has left the game');
  73. players.delete(player.playerName);
  74. }
  75. };
  76. var hasPlayers = function hasPlayers() {
  77. return players.size > 0;
  78. };
  79. return {
  80. addPlayer: addPlayer,
  81. playerLeft: playerLeft,
  82. hasPlayers: hasPlayers
  83. };
  84. }
  85. //# sourceMappingURL=online-duel-sync.js.map