Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php -q
- <?php
- include_once("internal/tracker.php");
- include_once("internal/helpers.php");
- include_once("internal/admin.php");
- // --------------------------------------------
- class BasicCommandHandler extends Tracker {
- protected $metagame = NULL;
- // --------------------------------------------
- function __construct($metagame) {
- $this->metagame = $metagame;
- }
- // ----------------------------------------------------
- protected function handle_chat_event($doc) {
- // player_id
- // player_name
- // message
- // global
- $event = $doc->firstChild;
- $message = $event->getAttribute("message");
- // for the most part, chat events aren't commands, so check that first
- if (!starts_with($message, "/")) {
- return;
- }
- $sender = $event->getAttribute("player_name");
- $sender_id = $event->getAttribute("player_id");
- if (check_command($message, "faction")) {
- $this->handle_change_faction($message, $sender_id);
- }
- // admin only from here on
- if (!is_admin($sender)) {
- return;
- }
- // it's a silent server command, check which one
- if (check_command($message, "test")) {
- $this->metagame->comms->send("say test yourself");
- } else if (check_command($message, "defend")) {
- // make ai defend only, both sides
- for ($i = 0; $i < 2; ++$i) {
- $command =
- "<command class='commander_ai'" .
- " faction='" . $i . "'" .
- " base_defense='1.0'" .
- " border_defense='0.0'>" .
- "</command>";
- $this->metagame->comms->send($command);
- }
- $this->metagame->comms->send("say defensive ai set");
- } else if (check_command($message, "grey_win")) {
- $this->metagame->comms->send("declare_winner 1");
- } else if (check_command($message, "kick")) {
- $this->handle_kick($message);
- }
- }
- // --------------------------------------------
- function has_ended() {
- // always on
- return false;
- }
- // --------------------------------------------
- function has_started() {
- // always on
- return true;
- }
- function handle_kick($message) {
- // TODO: just for debugging, this shows to everyone
- $this->metagame->comms->send("say kicking!");
- // get name given as parameter
- $name = substr($message, strlen("kick ") + 1);
- // ask for player list from the server
- $list_query_doc = new DOMDocument();
- $command =
- "<command class='make_query' id='player_list1'>\n" .
- " <data class='players' />\n" .
- "</command>";
- $list_query_doc->loadXML($command);
- $player_id = -1;
- // send the query command now
- $list_doc = $this->metagame->comms->query($list_query_doc);
- if ($list_doc) {
- $player_list = $list_doc->getElementsByTagName("player");
- echo "* " . $player_list->length . " players found\n";
- // go through the player list and match for the given name
- for ($i = 0; $i < $player_list->length; $i++) {
- $player = $player_list->item($i);
- $name2 = $player->getAttribute("name");
- // case insensitive
- if (strcasecmp($name2, $name) === 0) {
- // found it
- $player_id = $player->getAttribute("player_id");
- break;
- }
- }
- } else {
- echo "failed?\n";
- }
- if ($player_id >= 0) {
- // ok player id, now kick
- $this->metagame->comms->send("kick " . $player_id);
- } else {
- echo "* couldn't find a match for " . $name . "\n";
- // TODO: use XML chat command for private message back to who ever sent the message
- $this->metagame->comms->send("say kick missed!");
- }
- }
- function handle_change_faction($message, $sender_id) {
- // just for debugging, this shows to everyone
- //$this->metagame->comms->send("say switch!");
- // get faction as parameter, no validation here, server should rule out mistakes
- $faction_id = substr($message, stripos($message, " ") + 1);
- $command =
- "<command class='update_player' player_id='" . $sender_id . "' faction_id='" . $faction_id . "'/>\n";
- $this->metagame->comms->send($command);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement