1 |
- {"version":3,"sources":["../../src/game-server/online-duel-sync.js"],"names":["fs","require","OnlineDuelSync","ioServer","mariadbConnector","gameId","firstPlayer","isNewGame","gameOptions","io","mdb","isNew","players","Map","duelController","DuelController","set","playerName","updateGameStatus","resumeGame","loadGameData","gameDetails","getGameById","err","console","log","message","chat","name","to","emit","from","text","getSocket","join","game_data","on","addPlayer","player","res","size","has","allHeroesJson","JSON","parse","readFileSync","startNewGame","playerLeft","disconnected","playerLeaving","get","leave","delete","hasPlayers"],"mappings":";;;;;;;;;AACA;;AADA,IAAMA,EAAE,GAAGC,OAAO,CAAC,IAAD,CAAlB;;AAGe,SAASC,cAAT,CACbC,QADa,EAEbC,gBAFa,EAGbC,MAHa,EAIbC,WAJa,EAKbC,SALa,EAMbC,WANa,EAOb;AACA,MAAIC,EAAE,GAAGN,QAAT;AACA,MAAIO,GAAG,GAAGN,gBAAV;AACA,MAAIO,KAAK,GAAGJ,SAAZ;AAEA,MAAIK,OAAO,GAAG,IAAIC,GAAJ,EAAd;AACA,MAAIC,cAAc,GAAG,IAAIC,uBAAJ,EAArB;AAEAH,EAAAA,OAAO,CAACI,GAAR,CAAYV,WAAW,CAACW,UAAxB,EAAoCX,WAApC,EARA,CAUA;AACA;;AACA,MAAI,CAACK,KAAL,EAAY;AACVD,IAAAA,GAAG,CAACQ,gBAAJ,CAAqBb,MAArB,EAA6B,SAA7B;AACAS,IAAAA,cAAc,CAACK,UAAf;AACD;;AAED,MAAIC,YAAY,GAAG,SAAfA,YAAe,GAAW;AAC5B,QAAIC,WAAW,GAAG,EAAlB;;AACA,QAAI;AACFA,MAAAA,WAAW,GAAGX,GAAG,CAACY,WAAJ,CAAgBjB,MAAhB,CAAd;AACD,KAFD,CAEE,OAAOkB,GAAP,EAAY;AACZC,MAAAA,OAAO,CAACC,GAAR,CAAY,0BAAZ,EAAwCF,GAAG,CAACG,OAA5C;AACD;;AACD,WAAOL,WAAP;AACD,GARD;;AAUA,MAAIM,IAAI,GAAG,SAAPA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FD;;AAGA,SAAO;AACLH,IAAAA,SAAS,EAATA,SADK;AAELU,IAAAA,UAAU,EAAVA,UAFK;AAGLM,IAAAA,UAAU,EAAVA;AAHK,GAAP;AAKD","sourcesContent":["const fs = require('fs');\r\nimport DuelController from '../client-server-shared/duel-controller';\r\n\r\nexport default function OnlineDuelSync(\r\n ioServer,\r\n mariadbConnector,\r\n gameId,\r\n firstPlayer,\r\n isNewGame,\r\n gameOptions\r\n) {\r\n let io = ioServer;\r\n let mdb = mariadbConnector;\r\n let isNew = isNewGame;\r\n\r\n let players = new Map();\r\n let duelController = new DuelController();\r\n\r\n players.set(firstPlayer.playerName, firstPlayer);\r\n\r\n // If not new game, set status to PLAYING right now\r\n // Otherwise we wait second player\r\n if (!isNew) {\r\n mdb.updateGameStatus(gameId, 'PLAYING');\r\n duelController.resumeGame();\r\n }\r\n\r\n let loadGameData = function() {\r\n let gameDetails = {};\r\n try {\r\n gameDetails = mdb.getGameById(gameId);\r\n } catch (err) {\r\n console.log('Error retreiving game : ', err.message);\r\n }\r\n return gameDetails;\r\n };\r\n\r\n let chat = function(name, message) {\r\n io.to(gameId).emit('message', {\r\n from: name,\r\n text: message\r\n });\r\n };\r\n\r\n firstPlayer\r\n .getSocket()\r\n .join(gameId, () => {\r\n chat(\r\n firstPlayer.playerName,\r\n firstPlayer.playerName +\r\n ' has entered the game, waiting for second player...'\r\n );\r\n })\r\n .emit('update-game-data', loadGameData().game_data)\r\n .on('chat', message => {\r\n chat(firstPlayer.playerName, message);\r\n });\r\n\r\n let addPlayer = function(player) {\r\n let res = true;\r\n if (players.size === 1) {\r\n if (players.has(player.playerName)) {\r\n console.log(\r\n 'ERROR : this player is already there ! ',\r\n player.playerName\r\n );\r\n return false;\r\n }\r\n players.set(player.playerName, player);\r\n // Both players connected to new game, set status to PLAYING\r\n if (isNew) {\r\n isNew = false;\r\n mdb.updateGameStatus(gameId, 'PLAYING', player.playerName);\r\n let allHeroesJson = JSON.parse(\r\n fs.readFileSync('../../src/server-shared/all-heroes.json')\r\n );\r\n duelController.startNewGame(\r\n firstPlayer.playerName,\r\n player.playerName,\r\n allHeroesJson,\r\n gameOptions\r\n );\r\n }\r\n\r\n player\r\n .getSocket()\r\n .join(gameId, () => {\r\n chat(player.playerName, player.playerName + ' has entered the game');\r\n })\r\n .emit('update-game-data', loadGameData().game_data)\r\n .on('chat', message => {\r\n chat(player.playerName, message);\r\n });\r\n } else {\r\n console.log(\r\n 'Already 2 players, weird :' +\r\n player.playerName +\r\n ' was trying to join : ',\r\n players\r\n );\r\n res = false;\r\n }\r\n return res;\r\n };\r\n\r\n let playerLeft = function(player, disconnected) {\r\n let playerLeaving = players.get(player.playerName);\r\n if (playerLeaving) {\r\n if (!disconnected) {\r\n playerLeaving.getSocket().leave(gameId);\r\n }\r\n\r\n chat(player.playerName, player.playerName + ' has left the game');\r\n players.delete(player.playerName);\r\n }\r\n };\r\n\r\n let hasPlayers = function() {\r\n return players.size > 0;\r\n };\r\n return {\r\n addPlayer,\r\n playerLeft,\r\n hasPlayers\r\n };\r\n}\r\n"],"file":"online-duel-sync.js"}
|