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."');
- }
- }
- }
- run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement