Guest User

Untitled

a guest
Jul 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.30 KB | None | 0 0
  1. <?php
  2.  
  3. namespace WebSocket\Application;
  4.  
  5. /**
  6.  * Websocket-Server demo and test application.
  7.  *
  8.  * @author Snabg based on the DemoApplication supplied
  9.  *   but the way the onData method dispatches a request to
  10.  *   a handler function has been simplified!
  11.  */
  12. class TrackerApplication extends Application {
  13.  
  14. // These data used to assign a colour tag to client
  15.     private static $colourNamesArray = array('red', 'green', 'blue', 'aqua',
  16.         'fuschia', 'lime', 'maroon', 'navy', 'yellow');
  17.     private static $colourcounter = 0;
  18.     private $_clients = array();
  19.    
  20.     private $maze = array(
  21.       0 =>"10111111011111011111111111111111111011111111111111",
  22.       1 =>"10000000010000010000010001000001000000010000000001",
  23.       2 =>"00111101010111010111000100011100011101010111011100",
  24.       3 =>"10100001010100010100010101000001000001010000010001",
  25.       4 =>"10101101000101010101010101011011101101010111000111",
  26.       5 =>"10100000011101010001010100010010001001010100010001",
  27.       6 =>"10101111000001000111010101010110111011000101111011",
  28.       7 =>"00100010011011010000010001010100000010010100010000",
  29.       8 =>"11101010010010000111000111010101111010110001000001",
  30.       9 =>"00001010110110110100010100010001000010100111011100",
  31.      10 =>"10111010100100100001000100000101011010101100010001",
  32.      11 =>"10100000101101101101010101110001010000101001110111",
  33.      12 =>"10101110101001001001010101000101000110101011000001",
  34.      13 =>"10101000001011011011010001011101011110100011011101",
  35.      14 =>"10001011101011000011010111010001010000101111011101",
  36.      15 =>"11101010001011011111010100010111010111101000011101",
  37.      16 =>"11101010111010010000010001110110010100001011000001",
  38.      17 =>"00001010110010110111110111110100110101111010011100",
  39.      18 =>"10111000110110110011100100000101110101000010111101",
  40.      19 =>"10111011110100100110001101110001000101011110000101",
  41.      20 =>"10100010000101101110111101110111011101010000101101",
  42.      21 =>"10001110111100001110100000000111011100010111100001",
  43.      22 =>"10111000100001000110101111111100000101110111101111",
  44.      23 =>"10111011101111010100001000000001110101110111101111",
  45.      24 =>"10111000101111010101111011111111110001110111101111",
  46.      25 =>"10000010001000000001000011111111110111110011100111",
  47.      26 =>"11011111011011111101011000001000000100000000000111",
  48.      27 =>"10011111010011111100010011101011111101111111110011",
  49.      28 =>"10111111010111110001110111100011000001110000111011",
  50.      29 =>"10111110010110000111100111101111011101110110000011",
  51.      30 =>"10111110110110111100001111101000000000000110111001",
  52.      31 =>"10001110100100100001101000000011111111110110111101",
  53.      32 =>"00101000101101101101101011110001000111000110000000",
  54.      33 =>"10100011101101101101101010000101010100010100111111",
  55.      34 =>"10101111101101101101101010110001010101110001111111",
  56.      35 =>"10101111101100001101001010110101010100000100000001",
  57.      36 =>"10100000001101100000011000110100010001110101111101",
  58.      37 =>"10001111100001101111011011110101010111110101111101",
  59.      38 =>"11101111101111101111010011000101000100000101000001",
  60.      39 =>"10001000001111100000010111011101110101110001011101",
  61.      40 =>"10111011111000001111010111010001000101110111011101",
  62.      41 =>"10111011111011111110000100010111011100110011000001",
  63.      42 =>"10111011111010000000100001110111011000111011111101",
  64.      43 =>"00111000000000111110101101110111000011111001111100",
  65.      44 =>"10000011111010111110101100100110011011111100111001",
  66.      45 =>"10111010000010100000101110101110111011111110000011",
  67.      46 =>"10011010111110001110001000101110110011111000111011",
  68.      47 =>"00111010011110111000111011100000110000000011111000",
  69.      48 =>"10000011000000000011111000001111111011111000000011",
  70.      49 =>"10111111011111011111111111111111111011111111111111",
  71.     );
  72.    
  73.    private $rows = 50;
  74.    private $cols = 50;
  75.        
  76.  
  77.     public function onConnect($client) {
  78.        
  79.         $id = $client->getClientId();
  80.         $this->_clients[$id] = $client;
  81.  
  82.         // Extra code to set up application specific client data
  83.         $info = array();
  84.         $info['colour'] = TrackerApplication::$colourNamesArray[TrackerApplication::$colourcounter];
  85.  
  86.         TrackerApplication::$colourcounter++;
  87.         if (TrackerApplication::$colourcounter == 9)
  88.             TrackerApplication::$colourcounter = 0;
  89.         $client->setClientInfo($info);
  90.     }
  91.  
  92.     public function onDisconnect($client) {
  93.         $id = $client->getClientId();
  94.         unset($this->_clients[$id]);
  95.     }
  96.  
  97.    
  98.     public function onData($data, $client) {
  99.         $decodedData = $this->_decodeData($data);
  100.         $action = $decodedData['action'];  
  101.         $info = $decodedData['data'];
  102.         $this->_process($client, $action, $info);
  103.     }
  104.  
  105.     private function _process($client, $action, $info) {
  106.         // Currently the only action defined is "Move"
  107.         // First, update the client record
  108.         $infoarray = $client->getClientInfo();
  109.        
  110.         if ($action == "Connect") {
  111.         echo "Client registering\n";
  112.                 $infoarray['name'] = $info['name'];
  113.        
  114.         $xVal = rand(0,49);
  115.         $yVal = rand(0,49);
  116.         while ($this->maze[$yVal][$xVal] == "1") {
  117.             $xVal = rand(0,49);
  118.             $yVal = rand(0,49);
  119.         }
  120.        
  121.                 $infoarray['x'] = $xVal;
  122.                 $infoarray['y'] = $yVal;
  123.         $infoarray['dirFacing'] = $info['dirFacing'];
  124.                 $infoarray['health'] = $info['health'];
  125.                 $client->setClientInfo($infoarray);
  126.        
  127.         $msgdata = array();
  128.         foreach ($this->_clients as $aclient) {
  129.             $info = $aclient->getClientInfo();
  130.             $msgdata[] = $info;
  131.         }
  132.         $encodedUpdate = $this->_encodeData('Update', $msgdata);
  133.  
  134.         // Now send that to all connected clients to notify someone has joined
  135.         foreach ($this->_clients as $sendto) {
  136.             $sendto->send($encodedUpdate);
  137.                 }
  138.                 return;
  139.         }
  140.        
  141.     if ($action == "GetMap"){
  142.         echo "Get map function recieved\n";
  143.        
  144.         $position['x'] = $infoarray['x'];
  145.         $position['y'] = $infoarray['y'];
  146.         $position['dirFacing'] = $infoarray['dirFacing'];
  147.        
  148.         $dataFrom['pos'] = $position;
  149.         $dataFrom['maze'] = $this->maze;
  150.        
  151.         $encoded = $this->_encodeData('Maze', $dataFrom);
  152.         $client->send($encoded);
  153.                
  154.                 $msgdata = array();
  155.         foreach ($this->_clients as $aclient) {
  156.             $info = $aclient->getClientInfo();
  157.             $msgdata[] = $info;
  158.         }
  159.         $encodedUpdate = $this->_encodeData('Update', $msgdata);
  160.  
  161.         // Now send that to all connected clients
  162.         foreach ($this->_clients as $sendto) {
  163.             $sendto->send($encodedUpdate);
  164.                 }
  165.                
  166.         return;
  167.     }
  168.    
  169.     if ($action == "Move") {
  170.         echo "Move function recieved from client\n";
  171.         $toSend = $info;
  172.        
  173.         switch ($info['dirPressed']) {
  174.             case "up":
  175.                                 if ($info['dirFacing'] == 0) { //If the player is facing the right direction
  176.                     if ($info['y'] - 1 >= 0) { //if the player will move to a position in bounds
  177.                         if ($this->maze[$info['y'] - 1][$info['x']] != 1) {
  178.                             $toSend['y']--;
  179.                         }
  180.                     }
  181.                     else {
  182.                         if ($this->maze[$this->rows - 1][$info['x']] != 1) {
  183.                             $toSend['y'] = $this->rows - 1;
  184.                         }
  185.                     }
  186.                 }
  187.                 else
  188.                     $toSend['dirFacing'] = 0;
  189.                 break;
  190.             case "down":
  191.                 if ($info['dirFacing'] == 2) { //If the player is facing the right direction
  192.                     if ($info['y'] + 1 < $this->cols) { //if the player will move to a position in bounds
  193.                         if ($this->maze[$info['y'] + 1][$info['x']] != 1) {
  194.                             $toSend['y']++;
  195.                         }
  196.                     }
  197.                     else {
  198.                         if ($this->maze[0][$info['x']] != 1) {
  199.                             $toSend['y'] = 0;
  200.                         }
  201.                     }
  202.                 }
  203.                 else
  204.                     $toSend['dirFacing'] = 2;              
  205.                 break;
  206.             case "right":
  207.                                 if ($info['dirFacing'] == 1) { //If the player is facing the right direction
  208.                     if ($info['x'] + 1 < $this->cols) { //if the player will move to a position in bounds
  209.                         if ($this->maze[$info['y']][$info['x'] + 1] != 1) {
  210.                             $toSend['x']++;
  211.                         }
  212.                     }
  213.                     else {
  214.                         if ($this->maze[$info['y']][0] != 1) {
  215.                             $toSend['x'] = 0;
  216.                         }
  217.                     }
  218.                 }
  219.                 else
  220.                     $toSend['dirFacing'] = 1;
  221.                 break;
  222.             case "left":
  223.                                 if ($info['dirFacing'] == 3) { //If the player is facing the right direction
  224.                     if ($info['x'] - 1 >= 0) { //if the player will move to a position in bounds
  225.                         if ($this->maze[$info['y']][$info['x'] - 1] != 1) {
  226.                             $toSend['x']--;
  227.                         }
  228.                     }
  229.                     else {
  230.                         if ($this->maze[$info['y']][$this->cols - 1] != 1) {
  231.                             $toSend['x'] = $this->cols - 1;
  232.                         }
  233.                     }
  234.                 }
  235.                 else
  236.                     $toSend['dirFacing'] = 3;
  237.                 break;
  238.         }
  239.         $encoded = $this->_encodeData('Move', $toSend);
  240.                
  241.                 //update client information
  242.                 $infoarray['x'] = $toSend['x'];
  243.                 $infoarray['y'] = $toSend['y'];
  244.                 $infoarray['dirFacing'] = $toSend['dirFacing'];
  245.                 $client->setClientInfo($infoarray);
  246.                
  247.                 //Update all the clients that the person has moved
  248.                 $msgdata = array();
  249.         foreach ($this->_clients as $aclient) {
  250.             $info = $aclient->getClientInfo();
  251.             $msgdata[] = $info;
  252.         }
  253.         $encodedUpdate = $this->_encodeData('Update', $msgdata);
  254.  
  255.         // Now send that to all connected clients
  256.         foreach ($this->_clients as $sendto) {
  257.             $sendto->send($encodedUpdate);
  258.                 }
  259.                
  260.         $client->send($encoded);
  261.         return;
  262.     }
  263.     }
  264. }
Add Comment
Please, Sign In to add comment