Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var app = require('express')();
  2. var server = require('http').Server(app);
  3. var io = require('socket.io')(server);
  4.  
  5. server.listen(3000);
  6.  
  7. // global variables for the server
  8. var enemies = [];
  9. var playerSpawnPoints = [];
  10. var clients = [];
  11.  
  12. app.get('/', function(req, res) {
  13.     res.send('hey you got back get "/"');
  14. });
  15.  
  16. io.on('connection', function(socket) {
  17.    
  18.     var currentPlayer = {};
  19.     currentPlayer.name = 'unknown';
  20.  
  21.     socket.on('player connect', function() {
  22.         console.log(currentPlayer.name+' recv: player connect');
  23.         for(var i =0; i<clients.length;i++) {
  24.             var playerConnected = {
  25.                 name:clients[i].name,
  26.                 position:clients[i].position,
  27.                 rotation:clients[i].position,
  28.                 health:clients[i].health
  29.             };
  30.             // in your current game, we need to tell you about the other players.
  31.             socket.emit('other player connected', playerConnected);
  32.             console.log(currentPlayer.name+' emit: other player connected: '+JSON.stringify(playerConnected));
  33.         }
  34.     });
  35.  
  36.     socket.on('play', function(data) {
  37.         console.log(currentPlayer.name+' recv: play: '+JSON.stringify(data));
  38.         // if this is the first person to join the game init the enemies
  39.         if(clients.length === 0) {
  40.             numberOfEnemies = data.enemySpawnPoints.length;
  41.             enemies = [];
  42.             data.enemySpawnPoints.forEach(function(enemySpawnPoint) {
  43.                 var enemy = {
  44.                     name: guid(),
  45.                     position: enemySpawnPoint.position,
  46.                     rotation: enemySpawnPoint.rotation,
  47.                     health: 100
  48.                 };
  49.                 enemies.push(enemy);
  50.             });
  51.             playerSpawnPoints = [];
  52.             data.playerSpawnPoints.forEach(function(_playerSpawnPoint) {
  53.                 var playerSpawnPoint = {
  54.                     position: _playerSpawnPoint.position,
  55.                     rotation: _playerSpawnPoint.rotation
  56.                 };
  57.                 playerSpawnPoints.push(playerSpawnPoint);
  58.             });
  59.         }
  60.  
  61.         var enemiesResponse = {
  62.             enemies: enemies
  63.         };
  64.         // we always will send the enemies when the player joins
  65.         console.log(currentPlayer.name+' emit: enemies: '+JSON.stringify(enemiesResponse));
  66.         socket.emit('enemies', enemiesResponse);
  67.         var randomSpawnPoint = playerSpawnPoints[Math.floor(Math.random() * playerSpawnPoints.length)];
  68.         currentPlayer = {
  69.             name:data.name,
  70.             position: randomSpawnPoint.position,
  71.             rotation: randomSpawnPoint.rotation,
  72.             health: 100
  73.         };
  74.         clients.push(currentPlayer);
  75.         // in your current game, tell you that you have joined
  76.         console.log(currentPlayer.name+' emit: play: '+JSON.stringify(currentPlayer));
  77.         socket.emit('play', currentPlayer);
  78.         // in your current game, we need to tell the other players about you.
  79.         socket.broadcast.emit('other player connected', currentPlayer);
  80.     });
  81.  
  82.     socket.on('player move', function(data) {
  83.         console.log('recv: move: '+JSON.stringify(data));
  84.         currentPlayer.position = data.position;
  85.         socket.broadcast.emit('player move', currentPlayer);
  86.     });
  87.  
  88.     socket.on('player turn', function(data) {
  89.         console.log('recv: turn: '+JSON.stringify(data));
  90.         currentPlayer.rotation = data.rotation;
  91.         socket.broadcast.emit('player turn', currentPlayer);
  92.     });
  93.  
  94.     socket.on('player shoot', function() {
  95.         console.log(currentPlayer.name+' recv: shoot');
  96.         var data = {
  97.             name: currentPlayer.name
  98.         };
  99.         console.log(currentPlayer.name+' bcst: shoot: '+JSON.stringify(data));
  100.         socket.emit('player shoot', data);
  101.         socket.broadcast.emit('player shoot', data);
  102.     });
  103.  
  104.     socket.on('health', function(data) {
  105.         console.log(currentPlayer.name+' recv: health: '+JSON.stringify(data));
  106.         // only change the health once, we can do this by checking the originating player
  107.         if(data.from === currentPlayer.name) {
  108.             var indexDamaged = 0;
  109.             if(!data.isEnemy) {
  110.                 clients = clients.map(function(client, index) {
  111.                     if(client.name === data.name) {
  112.                         indexDamaged = index;
  113.                         client.health -= data.healthChange;
  114.                     }
  115.                     return client;
  116.                 });
  117.             } else {
  118.                 enemies = enemies.map(function(enemy, index) {
  119.                     if(enemy.name === data.name) {
  120.                         indexDamaged = index;
  121.                         enemy.health -= data.healthChange;
  122.                     }
  123.                     return enemy;
  124.                 });
  125.             }
  126.  
  127.             var response = {
  128.                 name: (!data.isEnemy) ? clients[indexDamaged].name : enemies[indexDamaged].name,
  129.                 health: (!data.isEnemy) ? clients[indexDamaged].health : enemies[indexDamaged].health
  130.             };
  131.             console.log(currentPlayer.name+' bcst: health: '+JSON.stringify(response));
  132.             socket.emit('health', response);
  133.             socket.broadcast.emit('health', response);
  134.         }
  135.     });
  136.  
  137.     socket.on('disconnect', function() {
  138.         console.log(currentPlayer.name+' recv: disconnect '+currentPlayer.name);
  139.         socket.broadcast.emit('other player disconnected', currentPlayer);
  140.         console.log(currentPlayer.name+' bcst: other player disconnected '+JSON.stringify(currentPlayer));
  141.         for(var i=0; i<clients.length; i++) {
  142.             if(clients[i].name === currentPlayer.name) {
  143.                 clients.splice(i,1);
  144.             }
  145.         }
  146.     });
  147.  
  148. });
  149.  
  150. console.log('--- server is running ...');
  151.  
  152. function guid() {
  153.     function s4() {
  154.         return Math.floor((1+Math.random()) * 0x10000).toString(16).substring(1);
  155.     }
  156.     return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement