Gachl

Untitled

Apr 14th, 2014
2,406
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?PHP
  2.  
  3. include 'rust.rcon.php';
  4.  
  5. $waiting_replies = array();
  6. $commands = array();
  7.  
  8. function run()
  9. {
  10.     $rcon = new RustRcon("host", 12345, "passwd");
  11.     while (true) // Until SIGTERM
  12.     {
  13.         cron($rcon);
  14.  
  15.         $response = $rcon->Read();
  16.         if ($response == null)
  17.             continue;
  18.         if ($response->ID() == 1)
  19.             continue;
  20.        
  21.         // Handle "asynchronous" requests
  22.         if (array_key_exists($response->ID(), $waiting_replies))
  23.         {
  24.             $waiting_replies[$response->ID()]($rcon, $response); // Call to callback
  25.             unset($waiting_replies[$response->ID()]);
  26.         }
  27.  
  28.         handle($rcon, $response);
  29.     }
  30. }
  31.  
  32. function wait_request($id, $callback)
  33. {
  34.     global $waiting_replies;
  35.     $waiting_replies[$id] = $callback;
  36. }
  37.  
  38. $cron = array();
  39. function cron($rcon)
  40. {
  41.     global $cron;
  42.  
  43.     $ncron = array();
  44.     foreach ($cron as $function => $time)
  45.     {
  46.         if (time() >= $time)
  47.             $ncron[$function] = $function($rcon); // Next time
  48.         else
  49.             $ncron[$function] = $time;
  50.     }
  51.  
  52.     $cron = $ncron;
  53. }
  54.  
  55. function handle($rcon, $command)
  56. {
  57.     global $commands;
  58.  
  59.     // Handle chat messages
  60.     if (preg_match('/^\\[CHAT\\] "(.*)":"(.*)"$/', $command->Response(), $matches))
  61.     {
  62.         // Command handling
  63.         if (in_array($matches[2][0], array('/', '!')))
  64.         {
  65.             $user = $matches[1];
  66.  
  67.             // Silly users with quotation marks
  68.             if (strpos($user, '"') !== false)
  69.             {
  70.                 $rcon->Send('say "Sorry, ' . str_replace('"', "'", $user) . ', but your name contains a quotation mark and is therefore restricted in the use of commands."');
  71.                 return;
  72.             }
  73.  
  74.             $command = substr($matches[2], 1, strpos($matches[2], ' ') - 1);
  75.             $params = array();
  76.             $param = "";
  77.             if (strpos($matches[2], ' ') === false)
  78.                 $command = substr($matches[2], 1); // No parameters, only command
  79.             else
  80.             {
  81.                 $param = substr($matches[2], strpos($matches[2], ' ') + 1);
  82.                 $params = explode(' ', $param); // Parameters
  83.             }
  84.             $command = strtolower($command);
  85.  
  86.             echo "Received command $command from player $user.\n";
  87.  
  88.             if (array_key_exists($command, $commands))
  89.                 $commands[$command]($rcon, $user, $param, $params);
  90.             else
  91.                 $rcon->Send('say "Invalid command."');
  92.         }
  93.     }
  94. }
  95.  
  96. function cron_airdrop($rcon)
  97. {
  98.     wait_request($rcon->Send('status'), 'cron_airdrop_response');
  99.     return time() + (25 * 60); // In 25 minutes
  100. }
  101.  
  102. $cron['cron_airdrop'] = time() + (25 * 60);
  103.  
  104. function cron_airdrop_response($rcon, $response)
  105. {
  106.     $players = parse_players($response);
  107.     if ($players == null)
  108.         return;
  109.  
  110.     if (count($players) > 5)
  111.         $rcon->Send('airdrop.drop');
  112. }
  113.  
  114. function cron_playercount($rcon)
  115. {
  116.     wait_request($rcon->Send('status'), 'cron_playercount_response');
  117.     return time() + (2.5 * 60);
  118. }
  119.  
  120. $cron['cron_playercount'] = time() + (2.5 * 60);
  121.  
  122. function cron_playercount_response($rcon, $response)
  123. {
  124.     $players = parse_players($response);
  125.     if ($players == null)
  126.         return;
  127.  
  128.     if (count($players) == 1)
  129.         $rcon->Send('say "Only you are currently online."');
  130.     elseif (count($players) > 1)
  131.         $rcon->Send('say "There are currently ' . num_to_str(count($players)) . ' players in this world."');
  132. }
  133.  
  134. function cmd_kit($rcon, $user, $param, $params)
  135. {
  136.     $rcon->Send('inv.giveplayer "' . $user . '" "Rock"');
  137.     $rcon->Send('inv.giveplayer "' . $user . '" "Torch"');
  138.     $rcon->Send('inv.giveplayer "' . $user . '" "Bandage" 2');
  139.     $rcon->Send('inv.giveplayer "' . $user . '" "Chocolate Bar"');
  140.     $rcon->Send('say "Kit delivered to ' . $user . '."');
  141. }
  142.  
  143. $commands['kit'] = 'cmd_kit';
  144.  
  145. // Parse status response to player array
  146. function parse_players($response)
  147. {
  148.     if (preg_match_all('/[0-9]{6,17} +"(.*)" +[0-9]{1,4}/u', $response->Response(), $matches))
  149.         return $matches[1];
  150.     return null;
  151. }
  152.  
  153. run();
RAW Paste Data