Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace WebSocket\Application;
- /**
- * Websocket-Server demo and test application.
- *
- * @author Snabg based on the DemoApplication supplied
- * but the way the onData method dispatches a request to
- * a handler function has been simplified!
- */
- class TrackerApplication extends Application {
- // These data used to assign a colour tag to client
- private static $colourNamesArray = array('red', 'green', 'blue', 'aqua',
- 'fuschia', 'lime', 'maroon', 'navy', 'yellow');
- private static $colourcounter = 0;
- private $_clients = array();
- private $maze = array(
- 0 =>"10111111011111011111111111111111111011111111111111",
- 1 =>"10000000010000010000010001000001000000010000000001",
- 2 =>"00111101010111010111000100011100011101010111011100",
- 3 =>"10100001010100010100010101000001000001010000010001",
- 4 =>"10101101000101010101010101011011101101010111000111",
- 5 =>"10100000011101010001010100010010001001010100010001",
- 6 =>"10101111000001000111010101010110111011000101111011",
- 7 =>"00100010011011010000010001010100000010010100010000",
- 8 =>"11101010010010000111000111010101111010110001000001",
- 9 =>"00001010110110110100010100010001000010100111011100",
- 10 =>"10111010100100100001000100000101011010101100010001",
- 11 =>"10100000101101101101010101110001010000101001110111",
- 12 =>"10101110101001001001010101000101000110101011000001",
- 13 =>"10101000001011011011010001011101011110100011011101",
- 14 =>"10001011101011000011010111010001010000101111011101",
- 15 =>"11101010001011011111010100010111010111101000011101",
- 16 =>"11101010111010010000010001110110010100001011000001",
- 17 =>"00001010110010110111110111110100110101111010011100",
- 18 =>"10111000110110110011100100000101110101000010111101",
- 19 =>"10111011110100100110001101110001000101011110000101",
- 20 =>"10100010000101101110111101110111011101010000101101",
- 21 =>"10001110111100001110100000000111011100010111100001",
- 22 =>"10111000100001000110101111111100000101110111101111",
- 23 =>"10111011101111010100001000000001110101110111101111",
- 24 =>"10111000101111010101111011111111110001110111101111",
- 25 =>"10000010001000000001000011111111110111110011100111",
- 26 =>"11011111011011111101011000001000000100000000000111",
- 27 =>"10011111010011111100010011101011111101111111110011",
- 28 =>"10111111010111110001110111100011000001110000111011",
- 29 =>"10111110010110000111100111101111011101110110000011",
- 30 =>"10111110110110111100001111101000000000000110111001",
- 31 =>"10001110100100100001101000000011111111110110111101",
- 32 =>"00101000101101101101101011110001000111000110000000",
- 33 =>"10100011101101101101101010000101010100010100111111",
- 34 =>"10101111101101101101101010110001010101110001111111",
- 35 =>"10101111101100001101001010110101010100000100000001",
- 36 =>"10100000001101100000011000110100010001110101111101",
- 37 =>"10001111100001101111011011110101010111110101111101",
- 38 =>"11101111101111101111010011000101000100000101000001",
- 39 =>"10001000001111100000010111011101110101110001011101",
- 40 =>"10111011111000001111010111010001000101110111011101",
- 41 =>"10111011111011111110000100010111011100110011000001",
- 42 =>"10111011111010000000100001110111011000111011111101",
- 43 =>"00111000000000111110101101110111000011111001111100",
- 44 =>"10000011111010111110101100100110011011111100111001",
- 45 =>"10111010000010100000101110101110111011111110000011",
- 46 =>"10011010111110001110001000101110110011111000111011",
- 47 =>"00111010011110111000111011100000110000000011111000",
- 48 =>"10000011000000000011111000001111111011111000000011",
- 49 =>"10111111011111011111111111111111111011111111111111",
- );
- private $rows = 50;
- private $cols = 50;
- public function onConnect($client) {
- $id = $client->getClientId();
- $this->_clients[$id] = $client;
- // Extra code to set up application specific client data
- $info = array();
- $info['colour'] = TrackerApplication::$colourNamesArray[TrackerApplication::$colourcounter];
- TrackerApplication::$colourcounter++;
- if (TrackerApplication::$colourcounter == 9)
- TrackerApplication::$colourcounter = 0;
- $client->setClientInfo($info);
- }
- public function onDisconnect($client) {
- $id = $client->getClientId();
- unset($this->_clients[$id]);
- }
- public function onData($data, $client) {
- $decodedData = $this->_decodeData($data);
- $action = $decodedData['action'];
- $info = $decodedData['data'];
- $this->_process($client, $action, $info);
- }
- private function _process($client, $action, $info) {
- // Currently the only action defined is "Move"
- // First, update the client record
- $infoarray = $client->getClientInfo();
- if ($action == "Connect") {
- echo "Client registering\n";
- $infoarray['name'] = $info['name'];
- $xVal = rand(0,49);
- $yVal = rand(0,49);
- while ($this->maze[$yVal][$xVal] == "1") {
- $xVal = rand(0,49);
- $yVal = rand(0,49);
- }
- $infoarray['x'] = $xVal;
- $infoarray['y'] = $yVal;
- $infoarray['dirFacing'] = $info['dirFacing'];
- $infoarray['health'] = $info['health'];
- $client->setClientInfo($infoarray);
- $msgdata = array();
- foreach ($this->_clients as $aclient) {
- $info = $aclient->getClientInfo();
- $msgdata[] = $info;
- }
- $encodedUpdate = $this->_encodeData('Update', $msgdata);
- // Now send that to all connected clients to notify someone has joined
- foreach ($this->_clients as $sendto) {
- $sendto->send($encodedUpdate);
- }
- return;
- }
- if ($action == "GetMap"){
- echo "Get map function recieved\n";
- $position['x'] = $infoarray['x'];
- $position['y'] = $infoarray['y'];
- $position['dirFacing'] = $infoarray['dirFacing'];
- $dataFrom['pos'] = $position;
- $dataFrom['maze'] = $this->maze;
- $encoded = $this->_encodeData('Maze', $dataFrom);
- $client->send($encoded);
- $msgdata = array();
- foreach ($this->_clients as $aclient) {
- $info = $aclient->getClientInfo();
- $msgdata[] = $info;
- }
- $encodedUpdate = $this->_encodeData('Update', $msgdata);
- // Now send that to all connected clients
- foreach ($this->_clients as $sendto) {
- $sendto->send($encodedUpdate);
- }
- return;
- }
- if ($action == "Move") {
- echo "Move function recieved from client\n";
- $toSend = $info;
- switch ($info['dirPressed']) {
- case "up":
- if ($info['dirFacing'] == 0) { //If the player is facing the right direction
- if ($info['y'] - 1 >= 0) { //if the player will move to a position in bounds
- if ($this->maze[$info['y'] - 1][$info['x']] != 1) {
- $toSend['y']--;
- }
- }
- else {
- if ($this->maze[$this->rows - 1][$info['x']] != 1) {
- $toSend['y'] = $this->rows - 1;
- }
- }
- }
- else
- $toSend['dirFacing'] = 0;
- break;
- case "down":
- if ($info['dirFacing'] == 2) { //If the player is facing the right direction
- if ($info['y'] + 1 < $this->cols) { //if the player will move to a position in bounds
- if ($this->maze[$info['y'] + 1][$info['x']] != 1) {
- $toSend['y']++;
- }
- }
- else {
- if ($this->maze[0][$info['x']] != 1) {
- $toSend['y'] = 0;
- }
- }
- }
- else
- $toSend['dirFacing'] = 2;
- break;
- case "right":
- if ($info['dirFacing'] == 1) { //If the player is facing the right direction
- if ($info['x'] + 1 < $this->cols) { //if the player will move to a position in bounds
- if ($this->maze[$info['y']][$info['x'] + 1] != 1) {
- $toSend['x']++;
- }
- }
- else {
- if ($this->maze[$info['y']][0] != 1) {
- $toSend['x'] = 0;
- }
- }
- }
- else
- $toSend['dirFacing'] = 1;
- break;
- case "left":
- if ($info['dirFacing'] == 3) { //If the player is facing the right direction
- if ($info['x'] - 1 >= 0) { //if the player will move to a position in bounds
- if ($this->maze[$info['y']][$info['x'] - 1] != 1) {
- $toSend['x']--;
- }
- }
- else {
- if ($this->maze[$info['y']][$this->cols - 1] != 1) {
- $toSend['x'] = $this->cols - 1;
- }
- }
- }
- else
- $toSend['dirFacing'] = 3;
- break;
- }
- $encoded = $this->_encodeData('Move', $toSend);
- //update client information
- $infoarray['x'] = $toSend['x'];
- $infoarray['y'] = $toSend['y'];
- $infoarray['dirFacing'] = $toSend['dirFacing'];
- $client->setClientInfo($infoarray);
- //Update all the clients that the person has moved
- $msgdata = array();
- foreach ($this->_clients as $aclient) {
- $info = $aclient->getClientInfo();
- $msgdata[] = $info;
- }
- $encodedUpdate = $this->_encodeData('Update', $msgdata);
- // Now send that to all connected clients
- foreach ($this->_clients as $sendto) {
- $sendto->send($encodedUpdate);
- }
- $client->send($encoded);
- return;
- }
- }
- }
Add Comment
Please, Sign In to add comment