Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  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. run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement