<?php

include_once("internal/tracker.php");
include_once("internal/log.php");

// --------------------------------------------

class Overtime extends Tracker {
	protected $metagame = NULL;
	protected $friendly_id = 0;
	protected $started = false;
	protected $match_win_timer = 100000.0;
	protected $match_winner = -1;
	protected $timer = 0.0;
	protected $REFRESH_TIME = 10.0;
	protected $THRESHOLD = 80.0;

	// --------------------------------------------
	function __construct($metagame) {
		$this->metagame = $metagame;
	}

	// --------------------------------------------
	function start() {
		_log("starting Overtime tracker");
		$this->started = true;
		
		$this->refresh();
	}

    // ----------------------------------------------------
	public function update($time) {
		// recheck every 10 seconds
		$this->timer -= $time;
		if ($this->timer < 0.0) {
			$this->refresh();
		}
	}

    // ----------------------------------------------------
	protected function refresh() {
		// query for game win timer 
		// if used to be higher than threshold and is now lower than threshold, decrease attacker faction capacities
		// if used to be lower than threshold and is now higher than threshold, set attacker faction capacities back to normal
	
        $list_query_doc = new DOMDocument();

        // query about bases
        $command =
                "<command class='make_query' id='overtime_query_1'>\n" .
                "       <data class='general'/>\n" .
                "</command>";
        $list_query_doc->loadXML($command);

        // send the query command now
        $list_doc = $this->metagame->comms->query($list_query_doc);

		$last_win_timer = $this->match_win_timer;
		$last_winner = $this->match_winner;

        if ($list_doc) {
            $list = $list_doc->getElementsByTagName("general");
			if ($list && $list->length > 0) {
				$node = $list->item(0);
				$this->match_winner = $node->getAttribute("match_winner");
				$this->match_win_timer = $node->getAttribute("match_win_timer");
			}
            _log("* " . $this->match_win_timer . " win timer, " . $this->match_winner . " about to win");
        }

		$factions = $this->metagame->get_factions();
		// if invalid winner, not set 
		if (($this->match_winner < 0) ||
			// or if winner changed since last time
			($last_winner != $this->match_winner) ||
			// or current winner is neutral
			($factions[$this->match_winner]->is_neutral()) ||
			// or win time used to be below threshold, and now went back up
			($last_win_timer <= $this->THRESHOLD && $this->match_win_timer > $this->THRESHOLD)) {

			// go back to default
			$this->set_loser_capacity_default();

			$this->set_all_default_mode();

		} else if ($last_win_timer > $this->THRESHOLD && $this->match_win_timer <= $this->THRESHOLD) {
			$this->set_loser_capacity_down();

			$this->set_loser_offensive_mode();
		}

		$this->timer = $this->REFRESH_TIME;
	}

	// ----------------------------------------------------
    protected function handle_base_owner_change_event($doc) {
        // base_id
        // owner_id (faction)

		$this->refresh();
    }


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

	// --------------------------------------------
	function has_started() {
		return $this->started;
	}

	// --------------------------------------------
	function set_loser_capacity_down() {
		$command =
			"<command class='change_game_settings'>";
		
		foreach ($this->metagame->get_factions() as $key => $f) {
			if ($key == $this->match_winner) {
				$command .=
				"    <faction capacity_multiplier='" . $f->capacity_multiplier . "' />";
			} else {
				// if capacity multiplier was 0 (neutral), it will continue to be
				$capacity = min($f->capacity_multiplier, 0.05);
				$command .=
				"    <faction capacity_multiplier='" . $capacity . "' />";
			}
		}

		$command .= 
			"</command>";

		$this->metagame->comms->send($command);

		if ($this->friendly_id == $this->match_winner) {
			// player is winning
			$this->metagame->send_faction_message_key($this->friendly_id, "overtime, we are winning");

		} else {
			// player is losing
			$this->metagame->send_faction_message_key($this->friendly_id, "overtime, we are losing");
		}
	}

	// --------------------------------------------
	function set_loser_capacity_default() {
		$command =
			"<command class='change_game_settings'>";
		
		foreach ($this->metagame->get_factions() as $key => $f) {
			$command .=
			"    <faction capacity_multiplier='" . $f->capacity_multiplier . "' />";
		}

		$command .= 
			"</command>";

		$this->metagame->comms->send($command);
	}

	// --------------------------------------------
	function set_all_default_mode() {
		foreach ($this->metagame->get_factions() as $key => $value) {
			$this->apply_default_mode($key);
		}
	}

	// --------------------------------------------
	function apply_default_mode($enemy_id) {
		$factions = $this->metagame->get_factions();

		$command = $factions[$enemy_id]->default_commander_ai_command;
		$this->metagame->comms->send($command);
	}

	// --------------------------------------------
	function set_loser_offensive_mode() {
		foreach ($this->metagame->get_factions() as $key => $f) {
			if ($key != $this->match_winner) {
				$this->apply_offensive_mode($key);
			} 
		}
	}

	// --------------------------------------------
	function apply_offensive_mode($id) {
		$command =
			"<command class='commander_ai'" .
 			"       faction='" . $id . "'" .
 			"       base_defense='0.0'" .
 			"       border_defense='0.0'>" .
        	        "</command>";
		$this->metagame->comms->send($command);
	}

}

?>