main.js 983 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. import PlayerId from './players/player-id';
  3. import DuelController from './game-server/duel-controller';
  4. const io = require('socket.io'); //(http)
  5. const server = io.listen(4000);
  6. let players = [];
  7. let addNewPlayer = function (playerSocket, playerName) {
  8. console.log('players length : ' + players.length + ' : ' + players);
  9. if (players.length < 2) {
  10. let newPlayer = new PlayerId(playerSocket,playerName);
  11. console.log('push player : ' + newPlayer);
  12. players.push(newPlayer);
  13. }
  14. if (players.length === 2) {
  15. let duelController = new DuelController(players[0], players[1]);
  16. }
  17. }
  18. server.on('connection', function (socket) {
  19. console.log("A player connected with id : " + socket.id);
  20. socket.on ('disconnect', () => {
  21. console.log("A player disconnected with id : " + socket.id);
  22. });
  23. socket.on('auth', (playerName) => {
  24. console.log(' Received auth message, player name : ' + playerName);
  25. addNewPlayer(socket, playerName);
  26. });
  27. })