SHARE
TWEET

get_tw_server_0_5-example

a guest Jan 22nd, 2014 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.         /******************************************************************\
  3.         |* Copyright (c) by Starlex.de
  4.         |*
  5.         |* Visit:
  6.         |*              http://teeworlds.com
  7.         |*              http://teeworlds-community.de
  8.         |*
  9.         |* License:
  10.         |*              http://creativecommons.org/licenses/by-nc-sa/3.0/deed.de
  11.         |*
  12.         |******************************************************************|
  13.         |*                                                   EXAMPLE USE
  14.         \******************************************************************/
  15.  
  16.         $test = gettwserver("starlex.de:8888");
  17. ?>
  18.  
  19. <table border="1">
  20.   <tr>
  21.     <td><b>Name</b></td>
  22.     <td><b>Map</b></td>
  23.     <td><b>Gametype</b></td>
  24.     <td><b>Players</b></td>
  25.   </tr>
  26.   <tr>
  27.     <td><?php echo $test["name"]; ?></td>
  28.     <td><?php echo $test["map"]; ?></td>
  29.     <td><?php echo $test["type"]; ?></td>
  30.     <td><?php echo $test["player_count"]."/".$test["max_players"]; ?></td>
  31.   </tr>
  32. </table>
  33.  
  34.  
  35. <table border="1">
  36.   <tr>
  37.     <td><b>Name</b></td>
  38.     <td><b>Score</b></td>
  39.   </tr>
  40.   <tr>
  41.  
  42. <?php foreach ($test["players"] as &$player) { ?>
  43.   <tr>
  44.     <td><?php echo $player["name"]; ?></td>
  45.     <td><?php echo $player["score"]; ?></td>
  46.   </tr>
  47. <?php } ?>
  48.  
  49.   </tr>
  50. </table>
  51. <?php
  52.  
  53. function get_tw_srv($server) {
  54.  
  55.         $socket = stream_socket_client('udp://'.$server , $errno, $errstr, 1);
  56.         stream_set_timeout($socket, 1);
  57.         fwrite($socket, "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xffgief");
  58.         $response = fread($socket, 2048);
  59.        
  60.         if ($response){
  61.                 $test = explode("\x00",$response);
  62.                 $players = array();
  63.  
  64.                 for ($i = 0; $i <= $test[6]*2 ; $i += 2) {
  65.                         $players[] = array("name" => $test[$i+8],"score" => $test[$i+8+1]);
  66.                 }
  67.                
  68.                 $tmp = array(
  69.                 "version" => str_replace("info","",str_replace("\xff","",$test[0])),
  70.                 "name" => $test[1],
  71.                 "map" => $test[2],
  72.                 "type" => $test[3],
  73.                 "flags" => $test[4],
  74.                 "progression" => $test[5],
  75.                 "player_count" => $test[6],
  76.                 "max_players" => $test[7],
  77.                 "players" => $players);
  78.                
  79.                 return $tmp;
  80.                
  81.         } else {
  82.                 return FALSE;
  83.         }
  84. }
  85. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top