Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- include 'rust.rcon.php';
- $waiting_replies = array();
- $commands = array();
- function run()
- {
- $rcon = new RustRcon("host", 12345, "passwd");
- while (true) // Until SIGTERM
- {
- cron($rcon);
- $response = $rcon->Read();
- if ($response == null)
- continue;
- if ($response->ID() == 1)
- continue;
- // Handle "asynchronous" requests
- if (array_key_exists($response->ID(), $waiting_replies))
- {
- $waiting_replies[$response->ID()]($rcon, $response); // Call to callback
- unset($waiting_replies[$response->ID()]);
- }
- handle($rcon, $response);
- }
- }
- function wait_request($id, $callback)
- {
- global $waiting_replies;
- $waiting_replies[$id] = $callback;
- }
- $cron = array();
- function cron($rcon)
- {
- global $cron;
- $ncron = array();
- foreach ($cron as $function => $time)
- {
- if (time() >= $time)
- $ncron[$function] = $function($rcon); // Next time
- else
- $ncron[$function] = $time;
- }
- $cron = $ncron;
- }
- function handle($rcon, $command)
- {
- global $commands;
- // Handle chat messages
- if (preg_match('/^\\[CHAT\\] "(.*)":"(.*)"$/', $command->Response(), $matches))
- {
- // Command handling
- if (in_array($matches[2][0], array('/', '!')))
- {
- $user = $matches[1];
- // Silly users with quotation marks
- if (strpos($user, '"') !== false)
- {
- $rcon->Send('say "Sorry, ' . str_replace('"', "'", $user) . ', but your name contains a quotation mark and is therefore restricted in the use of commands."');
- return;
- }
- $command = substr($matches[2], 1, strpos($matches[2], ' ') - 1);
- $params = array();
- $param = "";
- if (strpos($matches[2], ' ') === false)
- $command = substr($matches[2], 1); // No parameters, only command
- else
- {
- $param = substr($matches[2], strpos($matches[2], ' ') + 1);
- $params = explode(' ', $param); // Parameters
- }
- $command = strtolower($command);
- echo "Received command $command from player $user.\n";
- if (array_key_exists($command, $commands))
- $commands[$command]($rcon, $user, $param, $params);
- else
- $rcon->Send('say "Invalid command."');
- }
- }
- }
- function cron_airdrop($rcon)
- {
- wait_request($rcon->Send('status'), 'cron_airdrop_response');
- return time() + (25 * 60); // In 25 minutes
- }
- $cron['cron_airdrop'] = time() + (25 * 60);
- function cron_airdrop_response($rcon, $response)
- {
- $players = parse_players($response);
- if ($players == null)
- return;
- if (count($players) > 5)
- $rcon->Send('airdrop.drop');
- }
- function cron_playercount($rcon)
- {
- wait_request($rcon->Send('status'), 'cron_playercount_response');
- return time() + (2.5 * 60);
- }
- $cron['cron_playercount'] = time() + (2.5 * 60);
- function cron_playercount_response($rcon, $response)
- {
- $players = parse_players($response);
- if ($players == null)
- return;
- if (count($players) == 1)
- $rcon->Send('say "Only you are currently online."');
- elseif (count($players) > 1)
- $rcon->Send('say "There are currently ' . num_to_str(count($players)) . ' players in this world."');
- }
- function cmd_kit($rcon, $user, $param, $params)
- {
- $rcon->Send('inv.giveplayer "' . $user . '" "Rock"');
- $rcon->Send('inv.giveplayer "' . $user . '" "Torch"');
- $rcon->Send('inv.giveplayer "' . $user . '" "Bandage" 2');
- $rcon->Send('inv.giveplayer "' . $user . '" "Chocolate Bar"');
- $rcon->Send('say "Kit delivered to ' . $user . '."');
- }
- $commands['kit'] = 'cmd_kit';
- // Parse status response to player array
- function parse_players($response)
- {
- if (preg_match_all('/[0-9]{6,17} +"(.*)" +[0-9]{1,4}/u', $response->Response(), $matches))
- return $matches[1];
- return null;
- }
- run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement