Advertisement
DoomMetal

basic_command_handler.php

May 25th, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.13 KB | None | 0 0
  1. #!/usr/bin/php -q
  2. <?php
  3.  
  4. include_once("internal/tracker.php");
  5. include_once("internal/helpers.php");
  6. include_once("internal/admin.php");
  7.  
  8. // --------------------------------------------
  9.  
  10. class BasicCommandHandler extends Tracker {
  11.         protected $metagame = NULL;
  12.  
  13.         // --------------------------------------------
  14.         function __construct($metagame) {
  15.                 $this->metagame = $metagame;
  16.         }
  17.  
  18.     // ----------------------------------------------------
  19.     protected function handle_chat_event($doc) {
  20.         // player_id
  21.         // player_name
  22.         // message
  23.         // global
  24.         $event = $doc->firstChild;
  25.  
  26.         $message = $event->getAttribute("message");
  27.         // for the most part, chat events aren't commands, so check that first
  28.         if (!starts_with($message, "/")) {
  29.             return;
  30.         }
  31.  
  32.         $sender = $event->getAttribute("player_name");
  33.         $sender_id = $event->getAttribute("player_id");
  34.        
  35.         if (check_command($message, "faction")) {
  36.             $this->handle_change_faction($message, $sender_id);
  37.         }
  38.  
  39.         // admin only from here on
  40.         if (!is_admin($sender)) {
  41.             return;
  42.         }
  43.  
  44.         // it's a silent server command, check which one
  45.         if (check_command($message, "test")) {
  46.             $this->metagame->comms->send("say test yourself");
  47.  
  48.         } else if (check_command($message, "defend")) {
  49.             // make ai defend only, both sides
  50.             for ($i = 0; $i < 2; ++$i) {
  51.                 $command =
  52.                     "<command class='commander_ai'" .
  53.                     "   faction='" . $i . "'" .
  54.                     "   base_defense='1.0'" .
  55.                     "   border_defense='0.0'>" .
  56.                     "</command>";
  57.                 $this->metagame->comms->send($command);
  58.             }
  59.             $this->metagame->comms->send("say defensive ai set");
  60.  
  61.         } else if (check_command($message, "grey_win")) {
  62.             $this->metagame->comms->send("declare_winner 1");
  63.         } else if (check_command($message, "kick")) {
  64.          $this->handle_kick($message);
  65.         }
  66.     }
  67.  
  68.         // --------------------------------------------
  69.         function has_ended() {
  70.                 // always on
  71.                 return false;
  72.         }
  73.  
  74.         // --------------------------------------------
  75.         function has_started() {
  76.                 // always on
  77.                 return true;
  78.         }
  79.        
  80.         function handle_kick($message) {
  81.             // TODO: just for debugging, this shows to everyone
  82.             $this->metagame->comms->send("say kicking!");
  83.  
  84.             // get name given as parameter
  85.             $name = substr($message, strlen("kick ") + 1);
  86.                
  87.             // ask for player list from the server
  88.             $list_query_doc = new DOMDocument();
  89.             $command =
  90.                 "<command class='make_query' id='player_list1'>\n" .
  91.                 "   <data class='players' />\n" .
  92.                 "</command>";
  93.              $list_query_doc->loadXML($command);
  94.  
  95.              $player_id = -1;
  96.  
  97.              // send the query command now
  98.              $list_doc = $this->metagame->comms->query($list_query_doc);
  99.             if ($list_doc) {
  100.                 $player_list = $list_doc->getElementsByTagName("player");
  101.                 echo "* " . $player_list->length . " players found\n";
  102.  
  103.                 // go through the player list and match for the given name
  104.                 for ($i = 0; $i < $player_list->length; $i++) {
  105.                     $player = $player_list->item($i);
  106.                     $name2 = $player->getAttribute("name");
  107.                     // case insensitive
  108.                     if (strcasecmp($name2, $name) === 0) {
  109.                         // found it
  110.                         $player_id = $player->getAttribute("player_id");
  111.                         break;
  112.                     }
  113.                 }
  114.             } else {
  115.                 echo "failed?\n";
  116.             }
  117.              
  118.             if ($player_id >= 0) {
  119.                 // ok player id, now kick
  120.                 $this->metagame->comms->send("kick " . $player_id);
  121.             } else {
  122.                 echo "* couldn't find a match for " . $name . "\n";
  123.                 // TODO: use XML chat command for private message back to who ever sent the message
  124.                 $this->metagame->comms->send("say kick missed!");
  125.             }
  126.         }
  127.        
  128.         function handle_change_faction($message, $sender_id) {
  129.             // just for debugging, this shows to everyone
  130.             //$this->metagame->comms->send("say switch!");
  131.  
  132.             // get faction as parameter, no validation here, server should rule out mistakes
  133.             $faction_id = substr($message, stripos($message, " ") + 1);
  134.  
  135.             $command =
  136.                 "<command class='update_player' player_id='" . $sender_id . "' faction_id='" . $faction_id . "'/>\n";
  137.  
  138.             $this->metagame->comms->send($command);
  139.         }
  140. }
  141.  
  142.  
  143. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement