<?php

include_once("internal/tracker.php");
include_once("internal/helpers.php");
include_once("internal/admin.php");
include_once("internal/log.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");

		// admin only from here on
		if (!is_admin($sender)) {
			return;
		}

		// it's a silent server command, check which one
		if (check_command($message, "test2")) {
			$command = "<command class='set_marker' faction_id='0' position='512 0 512' color='0 0 1' atlas_index='0' text='hello!' />";
			$this->metagame->comms->send($command);
		} else 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, "0_attack")) {
			// make ai defend only, both sides
			$command =
				"<command class='commander_ai'" .
				"	faction='0'" .
				"	base_defense='0.0'" .
				"	border_defense='0.0'>" .
				"</command>";
			$this->metagame->comms->send($command);
			$this->metagame->comms->send("say attack green ai set");

		} else if (check_command($message, "0_win")) {
			$this->metagame->comms->send("declare_winner 0");
		} else if (check_command($message, "1_win")) {
			$this->metagame->comms->send("declare_winner 1");
		} else if (check_command($message, "kick")) {
			$this->handle_kick($message);
		} else if (check_command($message, "whereami")) {
			_log("whereami received");
			$info = $this->metagame->get_player_info($sender_id);
			if ($info && $info->hasAttribute("character_id")) {
				$character_id = $info->getAttribute("character_id");
				$info = $this->metagame->get_character_info($character_id);
				if ($info) {
					$pos_str = $info->getAttribute("position");
					$pos_array = parse_position($pos_str);
					$region = $this->metagame->get_region($pos_array);

					$text = $pos_str . ", " . $region;

					$this->metagame->send_private_message($sender_id, $text);
				} else {
					_log("character info not ok");
				}
			} else {
				_log("player info not ok");
			}
		}
	}

	// --------------------------------------------
	function has_ended() {
			// always on
			return false;
	}

	// --------------------------------------------
	function has_started() {
			// always on
			return true;
	}

	// --------------------------------------------
	function handle_kick($message) {
		// TODO: just for debuging, this shows to everyone
		$this->metagame->comms->send("say kicking!");

		// get name given as parameter
		$name = substr($message, strlen("kick ") + 1);

		// assuming player name 
		// 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);

		// send the query command now
		$list_doc = $this->metagame->comms->query($list_query_doc);
		if ($list_doc) {
			$player_list = $list_doc->getElementsByTagName("player");
			_log("* " . $player_list->length . " players found");
		} else {
			_log("failed?");
		}
		
		// go through the player list and match for the given name
		$player_id = -1;
		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;
			}
		}

		if ($player_id >= 0) {
			// ok player id, now kick
			// TODO: use XML chat command for private message back to who ever sent the message 
			$this->metagame->comms->send("kick " . $player_id);
		} else {
			_log("* couldn't find a match for " . $name);
			// TODO: again, this shows to everyone
			$this->metagame->comms->send("say kick missed!");
		}
	}
}


?>