<?php
require ("includer.php");
require (OPBEPATH . "tests".DIRECTORY_SEPARATOR."runnable".DIRECTORY_SEPARATOR."langs".DIRECTORY_SEPARATOR."XGLangImplementation.php");

class MyTest{
    private $time;
    private $memory;
    private $report;
    public static $reslist, $pricelist, $requeriments, $resource, $CombatCaps;
    public function __construct($debug = false)
    {
        if(empty(self::$reslist))
        {
            self::includeVars('XG');
        }
        if(!LangManager::getInstance()->implementationExist())
        {
            LangManager::getInstance()->setImplementation(new XGLangImplementation());
        }
        $attackers = $this->getAttachers();
        $defenders = $this->getDefenders();
        $memory1 = memory_get_usage();
        $micro1 = microtime();
        $engine = new Battle($attackers, $defenders);
        $micro1 = microtime() - $micro1;
        $memory1 = memory_get_usage() - $memory1;
        $this->report = $engine->getReport();
        $this->time = round(1000 * $micro1, 2);
        $this->memory = round($memory1 / 1000);
        echo $this;
    }

    public function getAttachers()
    {
        $fleet = new Fleet(1,array(
            $this->getShipType(206, 50),
            $this->getShipType(207, 50),
            $this->getShipType(204, 150)));
        $player = new Player(1, array($fleet));
        return new PlayerGroup(array($player));
    }
    public function getDefenders()
    {
        $fleet = new Fleet(2,array(
            $this->getShipType(210, 150),
            $this->getShipType(215, 50),
            $this->getShipType(207, 20)));
        $player = new Player(2, array($fleet));
        return new PlayerGroup(array($player));
    }
     public static function includeVars($name)
        {
            require (OPBEPATH."tests".DIRECTORY_SEPARATOR."runnable".DIRECTORY_SEPARATOR."vars".DIRECTORY_SEPARATOR."$name.php");
            MyTest::$reslist = $reslist;
            MyTest::$pricelist = $pricelist;
            MyTest::$requeriments = $requeriments;
            MyTest::$resource = $resource;
            MyTest::$CombatCaps = $CombatCaps;
        }
        public function getShipType($id, $count)
            {
                $rf = self::$CombatCaps[$id]['sd'];
                $shield = self::$CombatCaps[$id]['shield'];
                $cost = array(self::$pricelist[$id]['metal'], self::$pricelist[$id]['crystal']);
                $power = self::$CombatCaps[$id]['attack'];
                if (in_array($id, self::$reslist['fleet']))
                {
                    return new Ship($id, $count, $rf, $shield, $cost, $power);
                }
                return new Defense($id, $count, $rf, $shield, $cost, $power);
            }
            public function __toString(){
                $micro = $this->time;
                $memory = $this->memory;
                $defendersLost = $this->report->getTotalDefendersLostUnits();
                return $this->report . <<< EOT
$defendersLost
Battle calculated in $micro ms.
Memory used: $memory KB

EOT;
}
}
new MyTest();
?>