<?php

class ObjectClass {

    private $idx = 0;
    private $name = "";
    private $type = array();
    private $subtype = array();
    private $supertype = array();
    private $ability = array();
    private $color = array();
    private $power = 0;
    private $toughness = 0;
    private $owner = "";
    private $controller = "";

    public function setIdx($value) {
        $this->idx = $value;
    }

    public function getIdx() {
        return $this->idx;
    }

    public function setName($value) {
        $this->name = $value;
    }

    public function getName() {
        return $this->name;
    }

    public function setPower($value) {
        $this->power = $value;
    }

    public function getPower() {
        return $this->power;
    }

    public function setToughness($value) {
        $this->toughness = $value;
    }

    public function getToughness() {
        return $this->toughness;
    }

    public function setOwner($value) {
        $this->owner = $value;
    }

    public function getOwner() {
        return $this->owner;
    }

    public function setController($value) {
        $this->controller = $value;
    }

    public function getController() {
        return $this->controller;
    }

    public function getType($typeIndex) {
        return $this->type[$typeIndex];
    }

    public function setType($typeIndex, $value) {
        $this->type[$typeIndex] = $value;
    }

    public function typeSize() {
        return sizeof($this->type);
    }

    public function addType($value) {
        $this->type[$this->typeSize()] = $value;
    }

    public function removeType($index) {
        unset($this->type[$index]);
    }

    public function getSubtype($subtypeIndex) {
        return $this->subtype[$subtypeIndex];
    }

    public function setSubtype($subtypeIndex, $value) {
        $this->subtype[$subtypeIndex] = $value;
    }

    public function subtypeSize() {
        return count($this->subtype);
    }

    public function addSubtype($value) {
        $this->subtype[$this->subtypeSize()] = $value;
    }

    public function removeSubtype($index) {
        unset($this->subtype[$index]);
    }

    public function getSupertype($supertypeIndex) {
        return $this->supertype[$supertypeIndex];
    }

    public function setSupertype($supertypeIndex, $value) {
        $this->supertype[$supertypeIndex] = $value;
    }

    public function supertypeSize() {
        return count($this->supertype);
    }

    public function addSupertype($value) {
        $this->supertype[$this->supertypeSize()] = $value;
    }

    public function removeSupertype($index) {
        unset($this->supertype[$index]);
    }

    public function getAbility($abilityIndex) {
        return $this->ability[$abilityIndex];
    }

    public function setAbility($abilityIndex, $value) {
        $this->ability[$abilityIndex] = $value;
    }

    public function abilitySize() {
        return count($this->ability);
    }

    public function addAbility($value) {
        $this->ability[$this->abilitySize()] = $value;
    }

    public function removeAbility($index) {
        unset($this->ability[$index]);
    }

    public function getColor($colorIndex) {
        return $this->color[$colorIndex];
    }

    public function setColor($colorIndex, $value) {
        $this->color[$colorIndex] = $value;
    }

    public function colorSize() {
        return count($this->color);
    }

    public function addColor($value) {
        $this->color[$this->colorSize()] = $value;
    }

    public function removeColor($index) {
        unset($this->color[$index]);
    }

    public function printInstance() {
        echo "<tr>";
        if (isset($this->idx))
            echo "<td class=\"little\">Object: " . $this->getIdx() . "</td>";
        if (isset($this->name))
            echo "<td class=\"mid\">Name: " . $this->getName() . "</td>";
        if (isset($this->power))
            echo "<td class=\"little\">Power: " . $this->getPower() . "</td>";
        if (isset($this->toughness))
            echo "<td class=\"little\">Toughness: " . $this->getToughness() . "</td>";
        if (isset($this->owner))
            echo "<td class=\"little\">Owner: " . $this->getOwner() . "</td>";
        if (isset($this->controller))
            echo "<td class=\"little\">Controller: " . $this->getController() . "</td>";
        if (isset($this->color)) {
            for ($i = 0; $i < $this->colorSize(); $i++) {
                echo "<td class=\"little\">Color " . $i . " : " . $this->getColor($i) . "</td>";
            }
        }
        if (isset($this->type)) {
            for ($i = 0; $i < $this->typeSize(); $i++) {
                echo "<td class=\"mid\">Type " . $i . " : " . $this->getType($i) . "</td>";
            }
        }
        if (isset($this->subtype)) {
            for ($i = 0; $i < $this->subtypeSize(); $i++) {
                echo "<td class=\"mid\">Subtype " . $i . " : " . $this->getSubtype($i) . "</td>";
            }
        }
        if (isset($this->supertype)) {
            for ($i = 0; $i < $this->supertypeSize(); $i++) {
                echo "<td class=\"mid\">Supertype " . $i . " : " . $this->getSupertype($i) . "</td>";
            }
        }
        if (isset($this->ability)) {
            for ($i = 0; $i < $this->abilitySize(); $i++) {
                echo "<td class=\"midbig\">Ability " . $i . " : " . str_replace("_", " ", $this->getAbility($i)) . "</td>";
            }
        }
        echo "</tr>\n";
    }

}

class Player {

    private $health;
    private $poison_counter;

    public function getHealth() {
        return $this->health;
    }

    public function setHealth($value) {
        $this->health = $value;
    }

    public function getPoisonCounter() {
        return $this->poison_counter;
    }

    public function setPoisonCounter($value) {
        $this->poison_counter = $value;
    }

    public function loose_life($amount) {
        $this->health = $this->health - $amount;
    }

    public function gain_life($amount) {
        $this->health = $this->health + $amount;
    }

    public function add_poison_counter($Amount) {
        $this->poison_counter = $this->poison_counter + $Amount;
    }

    public function remove_poison_counter($Amount) {
        $this->poison_counter = $this->poison_counter + $Amount;
    }

}

class ArrayHandler {

    public $theArray = array();

}

class Zone {

    private $name;
    private $objects_in_zone = array();
    private $visibility;

    public function getName() {
        return $this->name;
    }

    public function setName($value) {
        $this->name = $value;
    }

    public function getObjectsInZone($pos) {
        if (isset($this->objects_in_zone[$pos])) {
            return $this->objects_in_zone[$pos];
        }
    }

    public function setObjectsInZone($pos, $object) {
        $this->objects_in_zone[$pos] = $object;
    }

    public function objectsInZoneSize() {
        return count($this->objects_in_zone);
    }

    public function addObjectsInZone($object) {
        $this->objects_in_zone[$this->objectsInZoneSize()] = $object;
    }

    public function removeObjectInZone($pos) {
        unset($this->objects_in_zone[$pos]);
    }

    public function getAllObjects() {
        return $this->objects_in_zone;
    }

    public function getZoneVisibility() {
        return $this->visibility;
    }

    public function setZoneVisibility($value) {
        $this->visibility = $value;
    }

}

class Hand extends Zone {

    public function __construct() {
        $this->setZoneVisibility(FALSE);
    }

    public function cards_in_hand() {
        return $this->objectsInZoneSize();
    }

    public function getCardInHand($pos) {
        $this->getObjectsInZone($pos);
    }

}

class Graveyard extends Zone {

    public function __construct() {
        $this->setZoneVisibility(TRUE);
    }

}

class Library extends Zone {

    private $top_card;

    public function getTopCard() {
        return $this->top_card;
    }

    public function setTopCard($value) {
        $this->top_card = $value;
    }

    public function __construct() {
        $this->setZoneVisibility(FALSE);
    }

    public function cards_in_library() {
        $this->objectsInZoneSize();
    }

    public function shuffle() {
        ;
    }

    public function on_top_of_library() {
        return $this->getObjectsInZone($this->getTopCard());
    }

    public function remove_top_card() {
        $this->top_card++;
        $this->removeObjectInZone($this->getTopCard());
    }

}

class Stack extends Zone {

    private $top_of_stack = 0;

    public function __construct() {
        $this->setZoneVisibility(TRUE);
    }

    public function getTop_of_stack() {
        return $this->top_of_stack;
    }

    public function setTop_of_stack($pos) {
        $this->top_of_stack = $pos;
    }

    public function resolve_spell() {
        //resolve
        $this->removeObjectInZone($this->top_of_stack);
        $this->top_of_stack--;
    }

    public function put_on_stack($object) {
        $this->addObjectsInZone($object);
        $this->top_of_stack++;
    }

}

class Battlefield extends Zone {

    public function __construct() {
        $this->setZoneVisibility(TRUE);
    }

}

class Exile extends Zone {

    public function __construct() {
        $this->setZoneVisibility(TRUE);
    }

}

class FlagHandler {

    private $fluentsArray = array();
    public $histedFlags = array();
    public $modules_array = array();
    public $card_array = array();
    public $handler;
    public $reset_flag;

    public function __construct($inputArray, $flag_array, $modules_array1) {
        $this->setWholeArray($inputArray);
        $this->histedFlags = $flag_array;
        $this->reset_flag = FALSE;
        $this->card_array = array();
        $this->modules_array = $modules_array1->a_list;
        $_SESSION["petko1"] = $this->modules_array;
        if (isset($this->histedFlags["reduce_game_plan"])) {
            $this->callFunc("reduce_game_plan");
            unset($this->histedFlags["reduce_game_plan"]);
        }
        foreach ($this->histedFlags as $key => $value) {
            $this->callFunc($key);
        }
    }

    public function modules() {
        transferCoreCode();
        $fluentsArrayAfterSum = array();
        $w_file = fopen(realpath("./Game/") . "/always", 'a');
        if (count($this->modules_array) > 0) {
            foreach ($this->modules_array as $name) {
                $r_file = fopen(realpath("./Game/Modules/") . "/" . $name . ".plan", 'r');
                $str = fgets($r_file);
                while ($str != FALSE) {
                    fputs($w_file, $str);
                    $str = fgets($r_file);
                }
                fclose($r_file);
            }
        }
        foreach ($this->fluentsArray as $fluent) {
            if ($fluent->getName() === "object_name") {
                $new_array = array();
                $new_array[0] = $fluent->getParameterArray(0);
                $new_array[1] = $fluent->getParameterArray(1);
                $this->card_array[count($this->card_array)] = $new_array;
            }
            if ($fluent->getName() != "update_card_database") {
                $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
            } else {
                $new_fluent = new FluentClass();
                $new_fluent->setName("update_card_database_guard");
                $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
            }
        }
        $this->fluentsArray = $fluentsArrayAfterSum;
        if (count($this->card_array) > 0) {
            foreach ($this->card_array as $name) {
                $r_file = fopen(realpath("./Game/Modules/Decks/") . "/plan_" . $name[1], 'r');
                $str = fgets($r_file);
                while ($str != FALSE) {
                    $str = str_replace("last_object()", $name[0], $str);
                    fputs($w_file, $str);
                    $str = fgets($r_file);
                }
                fclose($r_file);
            }
        }
        fclose($w_file);
        $w_file = fopen(realpath("./Game/") . "/TheGame.dl", 'a');
        if (count($this->card_array) > 0) {
            foreach ($this->card_array as $name) {
                $r_file = fopen(realpath("./Game/Modules/Decks/") . "/dl_" . $name[1], 'r');
                $str = fgets($r_file);
                while ($str != FALSE) {
                    $str = str_replace("last_object()", $name[0], $str);
                    fputs($w_file, $str);
                    $str = fgets($r_file);
                }
                fclose($r_file);
            }
        }
        fclose($w_file);
    }

    public function getFluentsArray($index) {
        $this->fluentsArray[$index];
    }

    public function setFluentsArray($index, $value) {
        $this->fluentsArray[$index] = $value;
    }

    public function getWholeArray() {
        return $this->fluentsArray;
    }

    public function setWholeArray($fArray) {
        $this->fluentsArray = $fArray;
    }

    public function callFunc($input) {
        if (method_exists($this, $input)) {
            $this->$input();
        }
    }

    public function save_state() {
        $fluentsArrayAfterSum = array();
        foreach ($this->fluentsArray as $fluent) {
            if ($fluent->getName() === "save_state") {
                unlink(realpath("./Game/") . "/restart_action" . $fluent->getParameterArray(0));
                copy(realpath("./Game/") . "/LastState", realpath("./Game/") . "/restart_action" . $fluent->getParameterArray(0));
            }else{
                    $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
            }
        }
        $this->fluentsArray = $fluentsArrayAfterSum;
    }

    public function reduce_game_plan() {
        $fluentsArrayAfterSum = array();
        setModules();
        foreach ($this->fluentsArray as $fluent) {
            if ($fluent->getName() != "reduce_game_plan") {
                $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
            }
        }
        $this->fluentsArray = $fluentsArrayAfterSum;
        unset($this->histedFlags["reduce_game_plan"]);
        $this->histedFlags["modules"] = 1;
    }

    public function restart_action() {
        setModules();
        foreach($this->fluentsArray as $fluent){
            if($fluent->getName() === "restart_action"){
                copy(realpath("./Game/") . "/restart_action" . $fluent->getParameterArray(0), realpath("./Game/") . "/CurrentState");
                $handler = fopen(realpath("./Game/") . "/restart_action" . $fluent->getParameterArray(0), 'r') or die("couldn't open file: ./Game/restart_action");
                $w_file = fopen(realpath("./Game/") . "/initially", 'w') or die("couldn't open file: ./Game/initially");
                $str = fgets($handler);
                if ($str != FALSE) {
                    fputs($w_file, "initially: \n");
                    fputs($w_file, "    update_card_database. \n");
                }
                while ($str != FALSE) {
                    fputs($w_file, $str);
                    $str = fgets($handler);
                }
                fclose($handler);
                fclose($w_file);
                $this->reset_flag = TRUE;
                unlink(realpath("./Game/") . "/restart_action" . $fluent->getParameterArray(0));
            }
        }
    }
    
    public function sum_cards_in_hand() {
        $fluentsArrayAfterSum = array();
        $p0 = 0;
        $p1 = 0;
        foreach($this->fluentsArray as $fluent){
            if ($fluent->getName() === "hand"){
                if($fluent->getParameterArray(0) === "p0"){
                    $p0++;
                    $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
                }else{
                    $p1++;
                    $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
                }
            }else{
                if($fluent->getName() === "last_object"){
                    if(($_SESSION['GameHandler']->last_object - $fluent->getParameterArray(0)) < 10){
                        $_SESSION['GameHandler']->last_object+=50;
                    }
                    $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
                }else{
                    if (($fluent->getName() != "sum_cards_in_hand")&&($fluent->getName() != "noone_receives_priority")){
                    $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
                    }
                }
            }
        }
        $new_fluent = new FluentClass();
        $new_fluent->setName("cards_in_hand");
        $new_fluent->setParameterArray(0, "p0");
        $new_fluent->setParameterArray(1, $p0);
        $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
        $new_fluent = new FluentClass();
        $new_fluent->setName("cards_in_hand");
        $new_fluent->setParameterArray(0, "p1");
        $new_fluent->setParameterArray(1, $p1);
        $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
        $this->fluentsArray = $fluentsArrayAfterSum;
    }
    
    public function sum_partial_costs() {
        $fluentsArrayAfterSum = array();
        $f_array = array();
        foreach ($this->fluentsArray as $fluent) {
            if ($fluent->getName() === "object_partial_mana_cost") {
                $objectNumber = $fluent->getParameterArray(0);
                if (isset($f_array[$objectNumber])) {
                    $f_array[$objectNumber]->setParameterArray(1, $f_array[$objectNumber]->getParameterArray(1) + $fluent->getParameterArray(2));
                    $f_array[$objectNumber]->setParameterArray(2, $f_array[$objectNumber]->getParameterArray(2) + $fluent->getParameterArray(3));
                    $f_array[$objectNumber]->setParameterArray(3, $f_array[$objectNumber]->getParameterArray(3) + $fluent->getParameterArray(4));
                    $f_array[$objectNumber]->setParameterArray(4, $f_array[$objectNumber]->getParameterArray(4) + $fluent->getParameterArray(5));
                    $f_array[$objectNumber]->setParameterArray(5, $f_array[$objectNumber]->getParameterArray(5) + $fluent->getParameterArray(6));
                    $f_array[$objectNumber]->setParameterArray(6, $f_array[$objectNumber]->getParameterArray(6) + $fluent->getParameterArray(7));
                } else {
                    $newfluent = new FluentClass();
                    $newfluent->setName("total_mana_cost");
                    $newfluent->addToParameterArray($objectNumber);
                    for ($i = 2; $i < $fluent->parameterArraySize(); $i++) {
                        $newfluent->addToParameterArray($fluent->getParameterArray($i));
                    }
                    $f_array[$objectNumber] = $newfluent;
                }
            }
            if (!($fluent->getName() === "sum_partial_costs")) {
                $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
            }
        }
        foreach ($f_array as $fluent) {
            $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
        }
        $fluent = new FluentClass();
        $fluent->setName("locked_in");
        $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
        $this->fluentsArray = $fluentsArrayAfterSum;
    }

    public function sum_object_power_toughness() {
        $fluentsArrayAfterSum = array();
        $fluentsToIncrementPower = array();
        foreach ($this->fluentsArray as $fluent) {
            if ($fluent->getName() === "increment_power_toughness") {
                if (isset($fluentsToIncrementPower[$fluent->getParameterArray(0)])) {
                    $fluentsToIncrementPower[$fluent->getParameterArray(0)]+=$fluent->getParameterArray(1);
                } else {
                    $fluentsToIncrementPower[$fluent->getParameterArray(0)] = $fluent->getParameterArray(1);
                }
            } else {
                if ($fluent->getName() != "sum_object_power_toughness") {
                    $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
                }
            }
        }
        $new_fluent = new FluentClass();
        $new_fluent->setName("increment_guard");
        $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
        foreach ($fluentsArrayAfterSum as $fluent) {
            if (($fluent->getName() === "object_power") || ($fluent->getName() === "object_toughness")) {
                if (isset($fluentsToIncrementPower[$fluent->getParameterArray(0)])) {
                    $fluent->setParameterArray(1, $fluent->getParameterArray(1) + $fluentsToIncrementPower[$fluent->getParameterArray(0)]);
                }
            }
        }
        $this->fluentsArray = $fluentsArrayAfterSum;
    }

    public function shuffle_library() {
        $library_after_sum0 = array();
        $library_after_sum1 = array();
        $shuff_deck = array();
        $not_deck = array();
        $ample = TRUE;
        $player;
        $fluentsArrayAfterSum = array();
        foreach ($this->fluentsArray as $fluent) {
            $ample = TRUE;
            if ($fluent->getName() === "library") {
                $ample = FALSE;
                if ($fluent->getParameterArray(0) === "p0") {
                    $library_after_sum0[count($library_after_sum0)] = $fluent;
                } else {
                    if ($fluent->getParameterArray(0) === "p1") {
                        $library_after_sum1[count($library_after_sum1)] = $fluent;
                    }
                }
            }
            if ($fluent->getName() === "shuffle_library") {
                $ample = FALSE;
                if ($fluent->getParameterArray(0) === "p0") {
                    $player = FALSE;
                } else {
                    $player = TRUE;
                }
            }
            if ($ample) {
                $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
            }
        }
        $new_fluent = new FluentClass();
        $new_fluent->setName("shuffle_guard");
        $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
        if ($player) {
            $shuff_deck = $library_after_sum1;
            $not_deck = $library_after_sum0;
        } else {
            $not_deck = $library_after_sum1;
            $shuff_deck = $library_after_sum0;
        }
        foreach ($not_deck as $fluent) {
            $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
        }
        $count = count($shuff_deck);
        $i = 1;
        if ($count > 0) {
            $rand = mt_rand(0, $count - 1);
            $new_fluent = new FluentClass();
            $new_fluent->setName("on_top_of_library");
            if ($player) {
                $new_fluent->setParameterArray(0, "p1");
            } else {
                $new_fluent->setParameterArray(0, "p0");
            }
            $new_fluent->setParameterArray(1, $shuff_deck[$rand]->getParameterArray(1));
            $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;

            $new_fluent = new FluentClass();
            $new_fluent->setName("library");
            if ($player) {
                $new_fluent->setParameterArray(0, "p1");
            } else {
                $new_fluent->setParameterArray(0, "p0");
            }
            $new_fluent->setParameterArray(1, $shuff_deck[$rand]->getParameterArray(1));
            $new_fluent->setParameterArray(2, $i++);
            $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
            unset($shuff_deck[$rand]);
            $shuff_deck = array_values($shuff_deck);
        }
        while ($i <= $count) {
            if ($count > $i) {
                $rand = mt_rand(0, $count - $i - 1);
            } else {
                $rand = 0;
            }
            //echo "count:".$count."rand:".$rand."|i:".$i."lib:".$shuff_deck[$rand]->getParameterArray(1)."<br/>";
            $new_fluent = new FluentClass();
            $new_fluent->setName("library");
            if ($player) {
                $new_fluent->setParameterArray(0, "p1");
            } else {
                $new_fluent->setParameterArray(0, "p0");
            }
            $new_fluent->setParameterArray(1, $shuff_deck[$rand]->getParameterArray(1));
            $new_fluent->setParameterArray(2, $i);
            $i++;
            $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
            unset($shuff_deck[$rand]);
            $shuff_deck = array_values($shuff_deck);
        }
        $this->fluentsArray = $fluentsArrayAfterSum;
    }

    public function parser_sum_damage() {
        $fluentsArrayAfterSUm = array();
        $dmg_fluents = array();
        foreach ($this->fluentsArray as $fluent) {
            if ($fluent->getName() === "assigned_combat_damage") {
                $dmg_fluents[$fluent->getParameterArray(0)]+=$fluent->getParameterArray(2);
            } else {
                if ($fluent->getName() != "sum_assigned_damage_from") {
                    $fluentsArrayAfterSUm[count($fluentsArrayAfterSUm)] = $fluent;
                }
            }
        }
    }

    public function loose_game() {
        foreach ($this->fluentsArray as $fluent) {
            if ($fluent->getName() === "loose_game") {
                $_SESSION["lostGame"] = $fluent->getParameterArray(0);
                $url = "endGame.php";
                endScript($url);
            }
        }
    }

    public function sum_damage() {
        $fluentsArrayAfterSum = array();
        $dmg_fluents_player = array();
        $dmg_fluents_creature = array();
        $infect_fluents_player = array();
        $infect_fluents_creature = array();
        $lethalAssignment = FALSE;
        $dmg_marked = array();
        $final_damage_assignment = array();
        // echo "working";
        if (isset($this->histedFlags["parser_lethal_assignment"])) {
            $lethalAssignment = TRUE;
        }
        if ($lethalAssignment) {
            //$dmgOrder = array();
            foreach ($this->fluentsArray as $key => $fluent) {
                $ample = TRUE;
                if ($fluent->getName() === "damage_to_assign") {
                    $fluent->printInstance();
                    $ample = FALSE;
                    if (isset($final_damage_assignment[$fluent->getParameterArray(0)])) {
                        $final_damage_assignment[$fluent->getParameterArray(0)]->theArray[0] = $fluent->getParameterArray(1);
                    } else {
                        $newFluent = new ArrayHandler();
                        $newFluent->theArray[0] = $fluent->getParameterArray(1);
                        $final_damage_assignment[$fluent->getParameterArray(0)] = $newFluent;
                        echo $newFluent->theArray;
                    }
                }
                if ($fluent->getName() === "order_of_damage_assignment") {
                    //$fluent->printInstance();
                    $ample = FALSE;
                    if (isset($final_damage_assignment[$fluent->getParameterArray(0)])) {
                        $theIndex = $final_damage_assignment[$fluent->getParameterArray(0)];
                        $theIndex->theArray[$fluent->getParameterArray(2) * 2 + 1] = "object" . $fluent->getParameterArray(1);
                        //$dmgOrder[count($dmgOrder)] = $fluent->getParameterArray(1);
                    } else {
                        $newFluent = new ArrayHandler();
                        $newFluent->theArray[$fluent->getParameterArray(2) * 2 + 1] = "object" . $fluent->getParameterArray(1);
                        $final_damage_assignment[$fluent->getParameterArray(0)] = $newFluent;
                    }
                    $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
                }
                if ($fluent->getName() === "parser_lethal_assignment") {
                    $ample = FALSE;
                }
                if ($ample) {
                    $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
                }
            }
            $this->fluentsArray = $fluentsArrayAfterSum;
            $fluentsArrayAfterSum = array();
            foreach ($this->fluentsArray as $key => $fluent) {
                if ($fluent->getName() === "lethal_damage") {
                    foreach ($final_damage_assignment as $key => $dmg_array) {
                        if (in_array("object" . $fluent->getParameterArray(0), $dmg_array->theArray)) {
                            $searchVal = array_search("object" . $fluent->getParameterArray(0), $dmg_array->theArray);
                            $dmg_array->theArray[$searchVal + 1] = $fluent->getParameterArray(1);
                        }
                    }
                }
            }
            foreach ($final_damage_assignment as $key => $arrayList) {
                $count = 1;
                $final_count = count($arrayList->theArray);
                if (isset($arrayList->theArray[0])) {
                    $dmgNumber = $arrayList->theArray[0];
                    $condition = true;
                    while ($condition) {
                        // echo "dmgNum:".$dmgNumber."|";
                        $newFluent = new FluentClass();
                        $newFluent->setName("damage_dealt");
                        $newFluent->setParameterArray(0, $key);
                        // echo "theArray:".$final_count."|".$count."<c|";
                        $newFluent->setParameterArray(1, substr($arrayList->theArray[$count], 6));
                        $newFluent->setParameterArray(2, "normal");
                        if ($dmgNumber > $arrayList->theArray[$count + 1]) {
                            $newFluent->setParameterArray(3, $arrayList->theArray[$count + 1]);
                            $dmgNumber -= $arrayList->theArray[$count + 1];
                        } else {
                            $newFluent->setParameterArray(3, $dmgNumber);
                            $condition = FALSE;
                        }
                        $this->fluentsArray[count($this->fluentsArray)] = $newFluent;
                        //  $newFluent->printInstance();
                        $count += 2;
                        if ($count >= $final_count) {
                            $condition = FALSE;
                        }
                    }
                }
            }
        }
        foreach ($this->fluentsArray as $key => $fluent) {
            if ($fluent->getName() === "active_player") {
                $new_fluent = new FluentClass();
                $new_fluent->setName("next_in_getting_priority");
                $new_fluent->setParameterArray(0, $fluent->getParameterArray(0));
                $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
            }
            if ($fluent->getName() === "damage_marked") {
                $dmg_marked[$fluent->getParameterArray(0)] = $fluent;
            }
            if ($fluent->getName() === "damage_dealt") {
                $name = $fluent->getParameterArray(1);
                $dmg_type = $fluent->getParameterArray(2);
                $amount = $fluent->getParameterArray(3);
                if ($dmg_type === "wither") {
                    //if the target is a player, then normal dmg is assigned
                    if ($this->isAPlayer($name)) {
                        if ($this->posInFluentArray($dmg_fluents_player, $name) === FALSE) {
                            //the player is not in the array
                            $new_fluent = new FluentClass();
                            $new_fluent->setName($name);
                            $new_fluent->addToParameterArray($amount);
                            $dmg_fluents_player[$name] = $new_fluent;
                        } else {
                            //the player is in the array
                            $dmg_fluents_player[$name] = $dmg_fluents_player[$name] + $amount;
                        }
                    } else {
                        //if it's a creature, it gets a -1/-1 counter
                        if ($this->posInFluentArray($infect_fluents_creature, $name) === FALSE) {
                            //the player is not in the array
                            $new_fluent = new FluentClass();
                            $new_fluent->setName($name);
                            $new_fluent->addToParameterArray($amount);
                            $infect_fluents_creature[$name] = $new_fluent;
                        } else {
                            //the player is in the array
                            $infect_fluents_creature[$name]->setParameterArray(0, $infect_fluents_creature[$name]->getParameterArray(0) + $amount);
                        }
                    }
                }
                if ($dmg_type === "infect") {
                    //if the target is a player, then player gets a poison counter
                    if ($this->isAPlayer($name)) {
                        if ($this->posInFluentArray($infect_fluents_player, $name) === FALSE) {
                            //the player is not in the array
                            $new_fluent = new FluentClass();
                            $new_fluent->setName($name);
                            $new_fluent->addToParameterArray($amount);
                            $infect_fluents_player[$name] = $new_fluent;
                        } else {
                            //the player is in the array
                            $infect_fluents_player[$name]->setParameterArray(0, $infect_fluents_player[$name]->getParameterArray(0) + $amount);
                        }
                    } else {
                        //if it's a creature, it gets a -1/-1 counter
                        if ($this->posInFluentArray($infect_fluents_creature, $name) === FALSE) {
                            //the player is not in the array
                            $new_fluent = new FluentClass();
                            $new_fluent->setName($name);
                            $new_fluent->addToParameterArray($amount);
                            $infect_fluents_creature[$name] = $new_fluent;
                        } else {
                            //the creature is in the array
                            $infect_fluents_creature[$name]->setParameterArray(0, $infect_fluents_creature[$name]->getParameterArray(0) + $amount);
                        }
                    }
                }
                if ($dmg_type === "normal") {
                    //if the target is a player, then player gets loose_life
                    if ($this->isAPlayer($name)) {
                        if ($this->posInFluentArray($dmg_fluents_player, $name) === FALSE) {
                            //the player is not in the array
                            $new_fluent = new FluentClass();
                            $new_fluent->setName($name);
                            $new_fluent->addToParameterArray($amount);
                            $dmg_fluents_player[$name] = $new_fluent;
                        } else {
                            //the player is in the array
                            $dmg_fluents_player[$name]->setParameterArray(0, $dmg_fluents_player[$name]->getParameterArray(0) + $amount);
                        }
                    } else {
                        //if it's a creature, it gets a -1/-1 counter
                        if ($this->posInFluentArray($dmg_fluents_creature, $name) === FALSE) {
                            //the creature is not in the array
                            $new_fluent = new FluentClass();
                            $new_fluent->setName($name);
                            $new_fluent->addToParameterArray($amount);
                            $dmg_fluents_creature[$name] = $new_fluent;
                        } else {
                            //the creature is in the array
                            $dmg_fluents_creature[$name]->setParameterArray(0, $dmg_fluents_creature[$name]->getParameterArray(0) + $amount);
                        }
                    }
                }
            } else {
                if (!(($fluent->getName() === "beginning_of_step") || ($fluent->getName() === "parser_processing") || ($fluent->getName() === "sum_damage") || ($fluent->getName() === "dealing_damage") || ($fluent->getName() === "lethal_damage") || ($fluent->getName() === "damage_marked"))) {
                    $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $fluent;
                }
            }
        }
        //arrays are full now we need to make the final damage fluents
        foreach ($dmg_fluents_creature as $key => $value) {
            if (isset($dmg_marked[$key])) {
                $dmg_marked[$key]->setParameterArray(1, $dmg_marked[$key]->getParameterArray(1) + $value->getParameterArray(0));
                $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $dmg_marked[$key];
                unset($dmg_marked[$key]);
            } else {
                $new_fluent = new FluentClass();
                $new_fluent->setName("damage_marked");
                $new_fluent->addToParameterArray($key);
                $new_fluent->addToParameterArray($value->getParameterArray(0));
                $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
            }
        }
        foreach ($dmg_marked as $value) {
            $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $value;
        }
        foreach ($dmg_fluents_player as $key => $value) {
            $new_fluent = new FluentClass();
            $new_fluent->setName("loose_life");
            $new_fluent->addToParameterArray($key);
            $new_fluent->addToParameterArray($value->getParameterArray(0));
            $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
        }
        foreach ($infect_fluents_creature as $key => $value) {
            $new_fluent = new FluentClass();
            $new_fluent->setName("add_counter");
            $new_fluent->addToParameterArray($key);
            $new_fluent->addToParameterArray("neg1_neg1");
            $new_fluent->addToParameterArray($value->getParameterArray(0));
            $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
        }
        foreach ($infect_fluents_player as $key => $value) {
            $new_fluent = new FluentClass();
            $new_fluent->setName("get_poison_counters");
            $new_fluent->addToParameterArray($key);
            $new_fluent->addToParameterArray($value->getParameterArray(0));
            $fluentsArrayAfterSum[count($fluentsArrayAfterSum)] = $new_fluent;
        }
        $this->fluentsArray = $fluentsArrayAfterSum;
    }

    public function isAPlayer($name) {
        if (($name === "p0") || ($name === "p1")) {
            return TRUE;
        }else
            return FALSE;
    }

    public function posInFluentArray($haystack, $needle) {
        foreach ($haystack as $key => $value) {
            if ($value->getName() === $needle) {
                return $key;
            }
        }
        return FALSE;
    }

}

class FluentClass {

    private $name;
    private $parameterArray;

    function getName() {
        return $this->name;
    }

    function setName($value) {
        $this->name = $value;
    }

    function getParameterArray($index) {
        return $this->parameterArray[$index];
    }

    function setParameterArray($index, $value) {
        $this->parameterArray[$index] = $value;
    }

    function parameterArraySize() {
        return count($this->parameterArray);
    }

    function addToParameterArray($value) {
        $this->parameterArray[count($this->parameterArray)] = $value;
    }

    function removeFromParameterArray($index) {
        unset($this->parameterArray[$index]);
    }

    function removeLastParameterFromArray() {
        unset($this->parameterArray[count($this->parameterArray) - 1]);
    }

    function printInstance() {
        $str = $this->name;
        if ($this->parameterArraySize() != 0) {
            $str = $str . "(";
            foreach ($this->parameterArray as $param) {
                $str = $str . $param . ",";
            }
            $str = substr($str, 0, strlen($str) - 1);
            echo $str . ").";
        } else {
            echo $str . ".";
        }
    }

}

class GameHandler {

    private $objectArray;
    public $fluentsArray;
    private $readstr;
    private $handler;
    private $flagsArray;
    private $processFlag = FALSE;
    public $display_array;
    public $module_list;
    public $card_list;
    public $graveyard;
    public $battlefield;
    public $hand;
    public $stack;
    public $library;
    public $tapped;
    public $last_object;
    
    public function printPage() {

        $_SESSION["sel_action"] = FALSE;
        for ($i = 0; $i < $_SESSION["action_count"]; $i++) {
            if ($_SESSION["action_$i"] != "(no") {
                if (isset($_SESSION["engine_processing"])) {
                    if (($_SESSION["engine_processing"] === TRUE) && ($_SESSION["action_$i"] === "engine_next_step")) {
                        $_SESSION["sel_action"] = TRUE;
                        $_SESSION["print"] = FALSE;
                        endScript("engine.php");
                    }
                    if (($_SESSION["engine_processing"] === TRUE) && ($_SESSION["action_$i"] === "system_next_move")) {
                        $_SESSION["sel_action"] = TRUE;
                        $_SESSION["print"] = FALSE;
                        endScript("engine.php");
                    }
                }
            }
        }
        $this->sortFluents();
        $this->fillZones();
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sk"> 
            <head> 
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
                <meta http-equiv="Content-Style-Type" content="text/css" /> 
                <meta http-equiv="Content-Script-Type" content="text/javascript" /> 
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
                <meta name="revisit-after" content="1 days" /> 
                <title>Magic The Gathering</title> 
                <link rel="stylesheet" type="text/css" href="./Game/GUI/main.css?v=1.5" />
            </head> 
            <body> 
                <?php
                //Game Info
                echo "<div class=\"gameinfo\"> \n";
                echo "<p>Current Phase: ";
                if (isset($this->display_array["current_phase"])) {
                    echo "<span>" . $this->display_array["current_phase"]->getParameterArray(0) . "</span></p> \n";
                }
                echo "<p>Current Step: ";
                if (isset($this->display_array["current_step"])) {
                    echo "<span>" . $this->display_array["current_step"]->getParameterArray(0) . "</span></p> \n";
                } else {
                    echo "</p>";
                }
                echo "<p>Active Player: <span>";
                if (isset($this->display_array["active_player"])) {
                    $str = str_replace("p1", "p2", $this->display_array["active_player"]->getParameterArray(0));
                    $str = str_replace("p0", "p1", $str);
                    $str = str_replace("_", " ", $str);
                    $str = strtoupper($str);
                    echo $str . "</span></p> \n";
                } else {
                    echo "</p>";
                }
                if (isset($this->display_array["life_totalp0"])) {

                    echo "<p>Life P1: <span>" . $this->display_array["life_totalp0"]->getParameterArray(1) . "</span></p> \n";
                }

                if (isset($this->display_array["life_totalp1"])) {

                    echo "<p>Life P2: <span>" . $this->display_array["life_totalp1"]->getParameterArray(1) . "</span></p> \n";
                }
                if (isset($this->display_array["mana_poolp0"])) {

                    echo "<p>Mana Pool P1:<span>" . $this->display_array["mana_poolp0"]->getParameterArray(2) . "</span> (green)</p> \n";
                }
                if (isset($this->display_array["mana_poolp1"])) {

                    echo "<p>Mana Pool P2:<span>" . $this->display_array["mana_poolp1"]->getParameterArray(5) . "</span> (red)</p> \n";
                }
                if (isset($this->display_array["current_static_phase"])) {

                    $str = str_replace("p1", "p2", $this->display_array["current_static_phase"]->getParameterArray(0));
                    $str = str_replace("p0", "p1", $str);
                    $str = str_replace("_", " ", $str);
                    echo "<p>Current Static Phase: <span>" . $str . "</span></p> \n";
                }
                if (isset($this->display_array["current_resolve_phase"])) {

                    $str = str_replace("p1", "p2", $this->display_array["current_resolve_phase"]->getParameterArray(1));
                    $str = str_replace("p0", "p1", $str);
                    $str = str_replace("_", " ", $str);
                    echo "<p>Current Resolve Phase: <span>" . $str . "</span></p> \n";
                }
                if (isset($this->display_array["current_casting_phase"])) {
                    $str = str_replace("p1", "p2", $this->display_array["current_casting_phase"]->getParameterArray(1));
                    $str = str_replace("p0", "p1", $str);
                    $str = str_replace("_", " ", $str);
                    echo "<p>Current Casting Phase: <span>" . $str . "</span></p> \n";
                }
                if (isset($this->display_array["current_activating_phase"])) {
                    $str = str_replace("p1", "p2", $this->display_array["current_activating_phase"]->getParameterArray(1));
                    $str = str_replace("p0", "p1", $str);
                    $str = str_replace("_", " ", $str);
                    echo "<p>Current Activating Phase: <span>" . $str . "</span></p> \n";
                }
                echo "</div> \n";
                ?>
                <div class="game">  
                    <div class="graveyard">
                        <div class="p1">

                        </div>
                        <div class="p2">

                        </div>
                    </div>
                    <div class="battlefield">
                        <div class="player1">
                            <div class="col1">
                                <?php
                                $count = 1;
                                foreach ($this->battlefield as $fluent) {
                                    if ($this->objectArray[$fluent->getParameterArray(0)]->getController() === "p0") {
                                        if ($this->objectArray[$fluent->getParameterArray(0)]->getType(0) != "land") {
                                            if ($count === 6) {
                                                echo "</div> \n";
                                                echo "<div class=\"col2\">";
                                            }
                                            echo "<div class=\"c" . $count . "\">";
                                            echo "     <img ";
                                            if (isset($this->tapped[$fluent->getParameterArray(0)])) {
                                                echo "class=\"tap\"";
                                            }
                                            echo " src=\"./Game/GUI/img/" . $this->objectArray[$fluent->getParameterArray(0)]->getName() . ".jpg\" title =\"ID: " . $fluent->getParameterArray(0) . "-" . $this->objectArray[$fluent->getParameterArray(0)]->getName() . " (" . $this->objectArray[$fluent->getParameterArray(0)]->getType(0) . ") " . $this->objectArray[$fluent->getParameterArray(0)]->getPower() . "/" . $this->objectArray[$fluent->getParameterArray(0)]->getToughness() . ", abilities: ";
                                            for ($i = 0; $i < $this->objectArray[$fluent->getParameterArray(0)]->abilitySize(); $i++) {
                                                echo ($i+1) . ". " . $this->objectArray[$fluent->getParameterArray(0)]->getAbility($i) . ",";
                                            }
                                            echo "\" />";
                                            echo "</div> \n";
                                            $count++;
                                        }
                                    }
                                }
                                if ($count < 7) {
                                    echo "</div> \n";
                                    echo "<div class=\"col2\"> \n";
                                }
                                echo "</div> \n";
                                echo "<div class=\"col3\">";
                                $count = 1;
                                foreach ($this->battlefield as $fluent) {
                                    if ($this->objectArray[$fluent->getParameterArray(0)]->getController() === "p0") {
                                        if ($this->objectArray[$fluent->getParameterArray(0)]->getType(0) === "land") {
                                            echo "<div class=\"c" . $count . "\">";
                                            echo "     <img ";
                                            if (isset($this->tapped[$fluent->getParameterArray(0)])) {
                                                echo "class=\"tap\"";
                                            }
                                            echo " src=\"./Game/GUI/img/" . $this->objectArray[$fluent->getParameterArray(0)]->getName() . ".jpg\" title =\"ID: " . $fluent->getParameterArray(0) . "-" . $this->objectArray[$fluent->getParameterArray(0)]->getName() . " (" . $this->objectArray[$fluent->getParameterArray(0)]->getType(0) . "), abilities: ";
                                            for ($i = 0; $i < $this->objectArray[$fluent->getParameterArray(0)]->abilitySize(); $i++) {
                                                echo ($i+1) . ". " . $this->objectArray[$fluent->getParameterArray(0)]->getAbility($i) . ",";
                                            }
                                            echo "\" />";
                                            echo "</div> \n";
                                            $count++;
                                        }
                                    }
                                }
                                echo "</div> \n";
                                echo "</div> \n";
                                echo "<div class=\"player2\"> \n";
                                echo "<div class=\"col1\"> \n";
                                $count = 1;
                                foreach ($this->battlefield as $fluent) {
                                    if ($this->objectArray[$fluent->getParameterArray(0)]->getController() === "p1") {
                                        if ($this->objectArray[$fluent->getParameterArray(0)]->getType(0) != "land") {
                                            if ($count === 6) {
                                                echo "</div> \n";
                                                echo "<div class=\"col2\">";
                                            }
                                            echo "<div class=\"c" . $count . "\">";
                                            echo "     <img ";
                                            if (isset($this->tapped[$fluent->getParameterArray(0)])) {
                                                echo "class=\"tap\"";
                                            }
                                            echo " src=\"./Game/GUI/img/" . $this->objectArray[$fluent->getParameterArray(0)]->getName() . ".jpg\" title =\"ID: " . $fluent->getParameterArray(0) . "-" . $this->objectArray[$fluent->getParameterArray(0)]->getName() . " (" . $this->objectArray[$fluent->getParameterArray(0)]->getType(0) . ") " . $this->objectArray[$fluent->getParameterArray(0)]->getPower() . "/" . $this->objectArray[$fluent->getParameterArray(0)]->getToughness() . ", abilities: ";
                                            for ($i = 0; $i < $this->objectArray[$fluent->getParameterArray(0)]->abilitySize(); $i++) {
                                                echo ($i+1) . ". " . $this->objectArray[$fluent->getParameterArray(0)]->getAbility($i) . ",";
                                            }
                                            echo "\" />";
                                            echo "</div> \n";
                                            $count++;
                                        }
                                    }
                                }
                                if ($count < 7) {
                                    echo "</div> \n";
                                    echo "<div class=\"col2\"> \n";
                                }
                                echo "</div> \n";
                                echo "<div class=\"col3\"> \n";
                                $count = 1;
                                foreach ($this->battlefield as $fluent) {
                                    if ($this->objectArray[$fluent->getParameterArray(0)]->getController() === "p1") {
                                        if ($this->objectArray[$fluent->getParameterArray(0)]->getType(0) === "land") {
                                            echo "<div class=\"c" . $count . "\">";
                                            echo "     <img ";
                                            if (isset($this->tapped[$fluent->getParameterArray(0)])) {
                                                echo "class=\"tap\"";
                                            }
                                            echo " src=\"./Game/GUI/img/" . $this->objectArray[$fluent->getParameterArray(0)]->getName() . ".jpg\" title =\"ID: " . $fluent->getParameterArray(0) . "-" . $this->objectArray[$fluent->getParameterArray(0)]->getName() . " (" . $this->objectArray[$fluent->getParameterArray(0)]->getType(0) . "), abilities: ";
                                            for ($i = 0; $i < $this->objectArray[$fluent->getParameterArray(0)]->abilitySize(); $i++) {
                                                echo ($i+1) . "." . $this->objectArray[$fluent->getParameterArray(0)]->getAbility($i) . ",";
                                            }
                                            echo "\" />";
                                            echo "</div> \n";
                                            $count++;
                                        }
                                    }
                                }
                                echo "</div> \n";
                                echo "</div> \n";
                                echo "</div> \n";
                                echo "<div class=\"stack\"> \n";
                                $order = array();
                                foreach ($this->stack as $fluent) {
                                    $order[$fluent->getParameterArray(1)] = $fluent->getParameterArray(0);
                                }
                                for ($i = 0; $i < count($order); $i++) {
                                    echo "<div class=\"c" . ($i + 1) . "\">";
                                    if($order[$i]>120){
                                        if(isset($this->display_array["activated" . $order[$i]])){
                                            echo "<img class=\"tap\" src=\"./Game/GUI/img/ability.jpg\" title = \"ID: " . $order[$i]. " (activated ability) - " . $this->display_array["activated" . $order[$i]]->getParameterArray(0);
                                        }
                                        if(isset($this->display_array["triggered" . $order[$i]])){
                                            echo "<img class=\"tap\" src=\"./Game/GUI/img/ability.jpg\" title = \"ID: " . $order[$i] . " (triggered ability) - " . $this->display_array["triggered" . $order[$i]]->getParameterArray(0);
                                        }
                                    }else{
                                        echo "<img class=\"tap\" src=\"./Game/GUI/img/" . $this->objectArray[$order[$i]]->getName() . ".jpg\" title = \"ID: " . $order[$i] . "-" . $this->objectArray[$order[$i]]->getName() . ", abilities: ";
                                    }
                                    for ($j = 0; $j < $this->objectArray[$order[$i]]->abilitySize(); $j++) {
                                        echo ($j+1) . ". " . $this->objectArray[$order[$i]]->getAbility($j) . ",";
                                    }
                                    echo "\" />";
                                    echo "</div> \n";
                                }
                                echo "</div> \n";
                                echo "<div class=\"hand\"> \n";
                                echo "<div class=\"p1\"> \n";
                                $count = 1;
                                foreach ($this->hand as $fluent) {
                                    if ($fluent->getParameterArray(0) === "p0") {
                                        echo "<div class=\"c" . $count++ . "\">";
                                        echo "<img src=\"./Game/GUI/img/" . $this->objectArray[$fluent->getParameterArray(1)]->getName() . ".jpg\" title =\"ID: " . $fluent->getParameterArray(1) . "-" . $this->objectArray[$fluent->getParameterArray(1)]->getName() . " (" . $this->objectArray[$fluent->getParameterArray(1)]->getType(0) . "), abilities: ";
                                        for ($i = 0; $i < $this->objectArray[$fluent->getParameterArray(1)]->abilitySize(); $i++) {
                                            echo ($i+1) . ". " . $this->objectArray[$fluent->getParameterArray(1)]->getAbility($i) . ",";
                                        }
                                        echo "\" />";
                                        echo "</div> \n";
                                    }
                                }
                                echo "</div> \n";
                                echo "<div class=\"p2\"> \n";
                                $count = 1;
                                foreach ($this->hand as $fluent) {
                                    if ($fluent->getParameterArray(0) === "p1") {
                                        echo "<div class=\"c" . $count++ . "\">";
                                        echo "<img src=\"./Game/GUI/img/" . $this->objectArray[$fluent->getParameterArray(1)]->getName() . ".jpg\" title =\"ID: " . $fluent->getParameterArray(1) . "-" . $this->objectArray[$fluent->getParameterArray(1)]->getName() . " (" . $this->objectArray[$fluent->getParameterArray(1)]->getType(0) . "), abilities: ";
                                        for ($i = 0; $i < $this->objectArray[$fluent->getParameterArray(1)]->abilitySize(); $i++) {
                                            echo ($i+1) . ". " . $this->objectArray[$fluent->getParameterArray(1)]->getAbility($i) . ",";
                                        }
                                        echo "\" />";
                                        echo "</div> \n";
                                    }
                                }
                                echo "</div> \n";
                                echo "</div> \n";
                                echo "</div> \n"; //game end div
                                echo "<div class=\"options\"> \n";
                                echo "<table id= \"actionchoice\"><tr>";
                                echo "<form action=\"engine.php\" method=\"post\" onsubmit=\"if (this.getAttribute('submitted')) return false; this.setAttribute('submitted','true');\">";
                                $val = 1;
                                $_SESSION["sel_action"] = FALSE;
                                for ($i = 0; $i < $_SESSION["action_count"]; $i++) {
                                    if ($_SESSION["action_$i"] != "(no") {
                                        $str = str_replace("p1", "p2", $_SESSION["action_$i"]);
                                        $str = str_replace("p0", "p1", $str);
                                        $str = $this->substituteNames($str);
                                        $str = str_replace("_", " ", $str);
                                        echo '<tr><button type="submit" name="sel_action" label="action' . $i . '" value="' . $val++ . '.' . $_SESSION["action_$i"] . '"> ' . $str . '</button></tr>';
                                        if (isset($_SESSION["engine_processing"])) {
                                            if (($_SESSION["engine_processing"] === TRUE) && ($_SESSION["action_$i"] === "engine_next_step")) {
                                                $_SESSION["sel_action"] = TRUE;
                                                $_SESSION["print"] = FALSE;
                                                endScript("engine.php");
                                            }
                                            if (($_SESSION["engine_processing"] === TRUE) && ($_SESSION["action_$i"] === "system_next_move")) {
                                                $_SESSION["sel_action"] = TRUE;
                                                $_SESSION["print"] = FALSE;
                                                endScript("engine.php");
                                            }
                                        }
                                    } else {
                                        $val++;
                                    }
                                }
                                echo "</tr>\n";
                                echo "</form>";
                                echo "<tr><form action=\"options.php\" method=\"post\" onsubmit=\"if (this.getAttribute('submitted')) return false; this.setAttribute('submitted','true');\">";
                                echo '<td class="midsize"><input type="submit" name="snapshot" label="action1" value="1"/>New Game</td>';
                                echo '<td class="midsize"><input type="submit" name="snapshot" label="action2" value="2"/>Load Game</td>';
                                echo '<td class="midsize"><input type="submit" name="snapshot" label="action3" value="3"/>Save Game</td>';
                                echo "</form></tr>\n</table>";
                                echo "</div> \n";
                                echo "</body> \n";
                                echo "</html> \n";
                            }
                            
                            function substituteNames($str){
                                if((strpos($str, "no_modal_choice") === 0)||(strpos($str, "restart_casting_spell") === 0)||(strpos($str, "no_mana_drawing") === 0)||(strpos($str, "start_casting_spell") === 0)||(strpos($str, "play_land_card") === 0)||(strpos($str, "declare_attacker") === 0)||(strpos($str, "discard_card") === 0)){
                                    $str2 = substr($str, strpos($str, ",")+1);
                                    $result = substr($str, 0, strpos($str, ","));
                                    $obj = substr($str2, 0, strpos($str2, ")"));
                                    //echo "|" . $obj . "|";
                                    if(isset($this->objectArray[$obj])){
                                        return $result . ", ID:" . $obj . " - " . $this->objectArray[$obj]->getName() . ")";
                                    }else{
                                        return $str;
                                    }
                                }
                                if(strpos($str, "declare_blocker") === 0){
                                    $str2 = substr($str, strpos($str, "(")+1);
                                    $result = substr($str, 0, strpos($str, "("));
                                    $obj1 = substr($str2, 0, strpos($str2, ","));
                                    $str2 = substr($str2, strpos($str2, ",")+1);
                                    $obj2 = substr($str2, 0, strpos($str2, ","));
                                    $str2 = substr($str2, strpos($str2, ",")+1);
                                    $obj3 = substr($str2, 0, strpos($str2, ")"));
                                    //echo "|$obj1|$obj2|$obj3|";
                                    if(isset($this->objectArray[$obj2])){
                                        $result = $result . "( Att: " . $obj2 . "- " . $this->objectArray[$obj2]->getName() . ",";
                                    }else{
                                        $result = $result . "( Att: " . $obj2 . ",";
                                    }
                                    if(isset($this->objectArray[$obj3])){
                                        return $result = $result . " Block: " . $obj3 . "- " . $this->objectArray[$obj3]->getName() . ")";
                                    }else{
                                        return $result = $result . " Block: " . $obj3 . ")";
                                    }
                                    return $str;
                                }
                                if(strpos($str, "choose_target") === 0){
                                    $str2 = substr($str, strpos($str, "(")+1);
                                    $result = substr($str, 0, strpos($str, "("));
                                    $obj1 = substr($str2, 0, strpos($str2, ","));
                                    $str2 = substr($str2, strpos($str2, ",")+1);
                                    $obj2 = substr($str2, 0, strpos($str2, ")"));
                                    //echo "|" . $obj1 . "|" . $obj2 . "|";
                                    if(isset($this->objectArray[$obj1])){
                                        $result = $result . "(Target: " . $obj1 . " - " . $this->objectArray[$obj1]->getName() . ",";
                                    }else{
                                        $result = $result . "(Target: " . $obj1 . ",";
                                    }
                                    if(isset($this->objectArray[$obj2])){
                                        return $result . "Source: " . $obj2 . " - " . $this->objectArray[$obj2]->getName() . ")";
                                    }else{
                                        return $result . "Source: " . $obj2 . ")";
                                    }
                                }
                                return $str;
                            }
                            
                            function getFluentArray($name) {
                                return $this->fluentsArray[$name];
                            }

                            function setFluentArray($index, $value) {
                                $this->fluentsArray[$index] = $value;
                            }

                            function addToFluentArray($value) {
                                $this->fluentsArray[count($this->fluentsArray)] = $value;
                            }

                            function removeFromFluentArray($index) {
                                unset($this->fluentsArray[$index]);
                            }

                            function fluentArraySize() {
                                return count($this->fluentsArray);
                            }

                            function update_card_database() {
                                $this->flagsArray["modules"] = 1;
                                $this->setFlag();
                            }

                            function fillZones() {
                                $this->battlefield = array();
                                $this->tapped = array();
                                $this->graveyard = array();
                                $this->hand = array();
                                $this->stack = array();
                                foreach ($this->fluentsArray as $fluent) {
                                    switch ($fluent->getName()) {
                                        case "graveyard":
                                            $this->graveyard[$fluent->getParameterArray(0)] = $fluent;
                                            break;
                                        case "hand":
                                            $this->hand[$fluent->getParameterArray(1)] = $fluent;
                                            break;
                                        case "battlefield":
                                            $this->battlefield[$fluent->getParameterArray(0)] = $fluent;
                                            break;
                                        case "stack":
                                            $this->stack[$fluent->getParameterArray(0)] = $fluent;
                                            break;
                                        case "tapped":
                                            $this->tapped[$fluent->getParameterArray(0)] = $fluent;
                                            break;
                                    }
                                }
                            }

                            function printFluents($rows) {
                                if ($this->fluentsArray) {
                                    $i = 0;
                                    $end = $rows - 1;
                                    foreach ($this->fluentsArray as $fluent) {
                                        if ((substr($fluent->getName(), 0, 6) != "object") && ($fluent->getName() != "library")) {
                                            $i = $i % $rows;
                                            if ($i === 0) {
                                                echo "<tr>";
                                            }
                                            echo "<td>";
                                            echo $fluent->printInstance();
                                            echo "</td>";
                                            if ($i === $end) {
                                                echo "</tr>\n";
                                            }
                                            $i++;
                                        }
                                    }
                                }
                            }

                            function sortFluents() {
                                sort($this->fluentsArray);
                            }

                            function issetFlag() {
                                return $this->processFlag;
                            }

                            function setFlag() {
                                $this->processFlag = TRUE;
                            }

                            function resetFlag() {
                                $this->processFlag = FALSE;
                            }

                            function addFlag($flag) {
                                $this->flagsArray[count($this->flagsArray)] = $flag;
                            }

                            function removeFlag($index) {
                                unset($this->flagsArray[$index]);
                            }

                            function handleFlags() {
                                if ($this->issetFlag()) {
                                    $newFlagHandler = new FlagHandler($this->fluentsArray, $this->flagsArray, $this->module_list);
                                    //reset for future use
                                    $this->fluentsArray = $newFlagHandler->getWholeArray();
                                    $this->flagsArray = array();
                                    if ($newFlagHandler->reset_flag) {
                                        $this->resetFlag();
                                        endScript("turn.php");
                                    }
                                }
                            }

                            function callfunc($str) {
                                $this->readstr = $str;
                                $idx = strpos($str, "(");
                                if ($idx === FALSE) {
                                    $idx = strpos($str, ".");
                                }
                                $input = substr($str, 0, $idx);
                                if (method_exists($this, $input)) {
                                    //we need it as it is a fluent and it is relevant to the status
                                    $original_readstr = $this->readstr;
                                    $this->fluents($input);
                                    //now some fluents are effects which have a special meaning and those have their own method
                                    $this->readstr = $original_readstr;
                                    $this->$input();
                                } else {
                                    //it's a fact or a fluent, and ofc those are practically of same importance and have the same composition
                                    $this->fluents($input);
                                }
                            }

                            function printObject() {
                                foreach ($this->objectArray as $val) {
                                    if ($val instanceof ObjectClass) {
                                        $val->printInstance();
                                    }
                                }
                            }

                            function object_power() {
                                $this->readstr = trim($this->readstr);
                                $this->readstr = substr($this->readstr, 0, strlen($this->readstr) - 1);
                                //we need the arguments from the facts, they are in the form of (arg1, arg2,..., argn).
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $obj = substr($this->readstr, strpos($this->readstr, "(") + 1, $idx - strpos($this->readstr, "(") - 1);
                                }
                                $this->readstr = substr($this->readstr, $idx + 1, strlen($this->readstr) - $idx);
                                //we cut the string to the position of the first arg
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $val = substr($this->readstr, 0, $idx);
                                } else {
                                    //there simply has to be a ")" so it's no sense in checking
                                    $val = substr($this->readstr, 0, strpos($this->readstr, ")"));
                                }
                                //now we just fill in the array
                                if ($this->objectArray[$obj]) {
                                    $handler = $this->objectArray[$obj];
                                    $handler->setPower($val);
                                } else {
                                    $newObject = new ObjectClass();
                                    $newObject->setPower($val);
                                    //need to set idx too
                                    $newObject->setIdx($obj);
                                    $this->objectArray[$obj] = $newObject;
                                }
                            }

                            function object_toughness() {
                                $this->readstr = trim($this->readstr);
                                $this->readstr = substr($this->readstr, 0, strlen($this->readstr) - 1);
                                //we need the arguments from the facts, they are in the form of (arg1, arg2,..., argn).
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $obj = substr($this->readstr, strpos($this->readstr, "(") + 1, $idx - strpos($this->readstr, "(") - 1);
                                }
                                $this->readstr = substr($this->readstr, $idx + 1, strlen($this->readstr) - $idx);
                                //we cut the string to the position of the first arg
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $val = substr($this->readstr, 0, $idx);
                                } else {
                                    //there simply has to be a ")" so it's no sense in checking
                                    $val = substr($this->readstr, 0, strpos($this->readstr, ")"));
                                }
                                //now we just fill in the array
                                if (isset($this->objectArray[$obj])) {
                                    $handler = $this->objectArray[$obj];
                                    $handler->setToughness($val);
                                } else {
                                    $newObject = new ObjectClass();
                                    $newObject->setToughness($val);
                                    //need to set idx too
                                    $newObject->setIdx($obj);
                                    $this->objectArray[$obj] = $newObject;
                                }
                            }

                            function object_name() {
                                $this->readstr = trim($this->readstr);
                                $this->readstr = substr($this->readstr, 0, strlen($this->readstr) - 1);
                                //we need the arguments from the facts, they are in the form of (arg1, arg2,..., argn).
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $obj = substr($this->readstr, strpos($this->readstr, "(") + 1, $idx - strpos($this->readstr, "(") - 1);
                                }
                                $this->readstr = substr($this->readstr, $idx + 1, strlen($this->readstr) - $idx);
                                //we cut the string to the position of the first arg
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $val = substr($this->readstr, 0, $idx);
                                } else {
                                    //there simply has to be a ")" so it's no sense in checking
                                    $val = substr($this->readstr, 0, strpos($this->readstr, ")"));
                                }
                                //now we just fill in the array
                                if (isset($this->objectArray[$obj])) {
                                    $handler = $this->objectArray[$obj];
                                    $handler->setName($val);
                                } else {
                                    $newObject = new ObjectClass();
                                    $newObject->setName($val);
                                    //need to set idx too
                                    $newObject->setIdx($obj);
                                    $this->objectArray[$obj] = $newObject;
                                }
                            }

                            function object_owner() {
                                $this->readstr = trim($this->readstr);
                                $this->readstr = substr($this->readstr, 0, strlen($this->readstr) - 1);
                                //we need the arguments from the facts, they are in the form of (arg1, arg2,..., argn).
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $obj = substr($this->readstr, strpos($this->readstr, "(") + 1, $idx - strpos($this->readstr, "(") - 1);
                                }
                                $this->readstr = substr($this->readstr, $idx + 1, strlen($this->readstr) - $idx);
                                //we cut the string to the position of the first arg
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $val = substr($this->readstr, 0, $idx);
                                } else {
                                    //there simply has to be a ")" so it's no sense in checking
                                    $val = substr($this->readstr, 0, strpos($this->readstr, ")"));
                                }
                                //now we just fill in the array
                                if (isset($this->objectArray[$obj])) {
                                    $handler = $this->objectArray[$obj];
                                    $handler->setOwner($val);
                                } else {
                                    $newObject = new ObjectClass();
                                    $newObject->setOwner($val);
                                    //need to set idx too
                                    $newObject->setIdx($obj);
                                    $this->objectArray[$obj] = $newObject;
                                }
                            }

                            function object_controller() {
                                $this->readstr = trim($this->readstr);
                                $this->readstr = substr($this->readstr, 0, strlen($this->readstr) - 1);
                                //we need the arguments from the facts, they are in the form of (arg1, arg2,..., argn).
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $obj = substr($this->readstr, strpos($this->readstr, "(") + 1, $idx - strpos($this->readstr, "(") - 1);
                                }
                                $this->readstr = substr($this->readstr, $idx + 1, strlen($this->readstr) - $idx);
                                //we cut the string to the position of the first arg
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $val = substr($this->readstr, 0, $idx);
                                } else {
                                    //there simply has to be a ")" so it's no sense in checking
                                    $val = substr($this->readstr, 0, strpos($this->readstr, ")"));
                                }
                                //now we just fill in the array
                                if (isset($this->objectArray[$obj])) {
                                    $handler = $this->objectArray[$obj];
                                    $handler->setController($val);
                                } else {
                                    $newObject = new ObjectClass();
                                    $newObject->setController($val);
                                    //need to set idx too
                                    $newObject->setIdx($obj);
                                    $this->objectArray[$obj] = $newObject;
                                }
                            }

                            function object_color() {
                                $this->readstr = trim($this->readstr);
                                $this->readstr = substr($this->readstr, 0, strlen($this->readstr) - 1);
                                //we need the arguments from the facts, they are in the form of (arg1, arg2,..., argn).
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $obj = substr($this->readstr, strpos($this->readstr, "(") + 1, $idx - strpos($this->readstr, "(") - 1);
                                }
                                $this->readstr = substr($this->readstr, $idx + 1, strlen($this->readstr) - $idx);
                                //we cut the string to the position of the first arg
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $val = substr($this->readstr, 0, $idx);
                                } else {
                                    //there simply has to be a ")" so it's no sense in checking
                                    $val = substr($this->readstr, 0, strpos($this->readstr, ")"));
                                }
                                //now we just fill in the array
                                if (isset($this->objectArray[$obj])) {
                                    $handler = $this->objectArray[$obj];
                                    $handler->addColor($val);
                                } else {
                                    $newObject = new ObjectClass();
                                    $newObject->addColor($val);
                                    //need to set idx too
                                    $newObject->setIdx($obj);
                                    $this->objectArray[$obj] = $newObject;
                                }
                            }

                            function object_type() {
                                $this->readstr = trim($this->readstr);
                                $this->readstr = substr($this->readstr, 0, strlen($this->readstr) - 1);
                                //we need the arguments from the facts, they are in the form of (arg1, arg2,..., argn).
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $obj = substr($this->readstr, strpos($this->readstr, "(") + 1, $idx - strpos($this->readstr, "(") - 1);
                                }
                                $this->readstr = substr($this->readstr, $idx + 1, strlen($this->readstr) - $idx);
                                //we cut the string to the position of the first arg
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $val = substr($this->readstr, 0, $idx);
                                } else {
                                    //there simply has to be a ")" so it's no sense in checking
                                    $val = substr($this->readstr, 0, strpos($this->readstr, ")"));
                                }
                                //now we just fill in the array
                                if (isset($this->objectArray[$obj])) {
                                    $handler = $this->objectArray[$obj];
                                    $handler->addType($val);
                                } else {
                                    $newObject = new ObjectClass();
                                    $newObject->addType($val);
                                    //need to set idx too
                                    $newObject->setIdx($obj);
                                    $this->objectArray[$obj] = $newObject;
                                }
                            }

                            function object_subtype() {
                                $this->readstr = trim($this->readstr);
                                $this->readstr = substr($this->readstr, 0, strlen($this->readstr) - 1);
                                //we need the arguments from the facts, they are in the form of (arg1, arg2,..., argn).
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $obj = substr($this->readstr, strpos($this->readstr, "(") + 1, $idx - strpos($this->readstr, "(") - 1);
                                }
                                $this->readstr = substr($this->readstr, $idx + 1, strlen($this->readstr) - $idx);
                                //we cut the string to the position of the first arg
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $val = substr($this->readstr, 0, $idx);
                                } else {
                                    //there simply has to be a ")" so it's no sense in checking
                                    $val = substr($this->readstr, 0, strpos($this->readstr, ")"));
                                }
                                //now we just fill in the array
                                if (isset($this->objectArray[$obj])) {
                                    $handler = $this->objectArray[$obj];
                                    $handler->addSubtype($val);
                                } else {
                                    $newObject = new ObjectClass();
                                    $newObject->addSubtype($val);
                                    //need to set idx too
                                    $newObject->setIdx($obj);
                                    $this->objectArray[$obj] = $newObject;
                                }
                            }

                            function object_supertype() {
                                $this->readstr = trim($this->readstr);
                                $this->readstr = substr($this->readstr, 0, strlen($this->readstr) - 1);
                                //we need the arguments from the facts, they are in the form of (arg1, arg2,..., argn).
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $obj = substr($this->readstr, strpos($this->readstr, "(") + 1, $idx - strpos($this->readstr, "(") - 1);
                                }
                                $this->readstr = substr($this->readstr, $idx + 1, strlen($this->readstr) - $idx);
                                //we cut the string to the position of the first arg
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $val = substr($this->readstr, 0, $idx);
                                } else {
                                    //there simply has to be a ")" so it's no sense in checking
                                    $val = substr($this->readstr, 0, strpos($this->readstr, ")"));
                                }
                                //now we just fill in the array
                                if (isset($this->objectArray[$obj])) {
                                    $handler = $this->objectArray[$obj];
                                    $handler->addSupertype($val);
                                } else {
                                    $newObject = new ObjectClass();
                                    $newObject->addSupertype($val);
                                    //need to set idx too
                                    $newObject->setIdx($obj);
                                    $this->objectArray[$obj] = $newObject;
                                }
                            }

                            function object_ability() {
                                $this->readstr = trim($this->readstr);
                                $this->readstr = substr($this->readstr, 0, strlen($this->readstr) - 1);
                                //we need the arguments from the facts, they are in the form of (arg1, arg2,..., argn).
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $obj = substr($this->readstr, strpos($this->readstr, "(") + 1, $idx - strpos($this->readstr, "(") - 1);
                                }
                                $this->readstr = substr($this->readstr, $idx + 1, strlen($this->readstr) - $idx);
                                //we cut the string to the position of the first arg
                                $idx = strpos($this->readstr, ",");
                                if ($idx != FALSE) {
                                    $val = substr($this->readstr, 0, $idx);
                                } else {
                                    //there simply has to be a ")" so it's no sense in checking
                                    $val = substr($this->readstr, 0, strpos($this->readstr, ")"));
                                }
                                //now we just fill in the array
                                if (isset($this->objectArray[$obj])) {
                                    $handler = $this->objectArray[$obj];
                                    $handler->addAbility($val);
                                } else {
                                    $newObject = new ObjectClass();
                                    $newObject->addAbility($val);
                                    //need to set idx too
                                    $newObject->setIdx($obj);
                                    $this->objectArray[$obj] = $newObject;
                                }
                            }

                            function sum_object_power_toughness() {
                                $this->flagsArray["sum_object_power_toughness"] = 1;
                                $this->setFlag();
                            }

                            function parser_lethal_assignment() {
                                $this->flagsArray["parser_lethal_assignment"] = 1;
                                $this->setFlag();
                            }

                            function sum_cards_in_hand() {
                                $this->flagsArray["sum_cards_in_hand"] = 1;
                                $this->setFlag();
                            }
                            
                            function sum_damage() {
                                $this->flagsArray["sum_damage"] = 1;
                                $this->setFlag();
                            }

                            function loose_game() {
                                $this->flagsArray["loose_game"] = 1;
                                $this->setFlag();
                            }

                            function sum_partial_costs() {
                                $this->flagsArray["sum_partial_costs"] = 1;
                                $this->setFlag();
                            }

                            function restart_action() {
                                $this->flagsArray["restart_action"] = 1;
                                $this->setFlag();
                            }

                            function shuffle_library_() {
                                $this->flagsArray["shuffle_library"] = 1;
                                $this->setFlag();
                            }

                            function reduce_game_plan() {
                                $this->flagsArray["reduce_game_plan"] = 1;
                                $this->setFlag();
                            }

                            function drawing_cards() {
                                $this->module_list->add("drawing_cards");
                                if ($_SESSION["modules"]["drawing_cards"] != TRUE) {
                                    $_SESSION["modules"]["drawing_cards"] = TRUE;
                                    $this->flagsArray["modules"] = 1;
                                    $this->setFlag();
                                }
                            }

                            function save_state() {
                                $this->flagsArray["save_state"] = 1;
                                $this->setFlag();
                            }

                            function combat_phase() {
                                $this->module_list->add("combat_phase");
                                if ($_SESSION["modules"]["combat_phase"] != TRUE) {
                                    $_SESSION["modules"]["combat_phase"] = TRUE;
                                    $this->flagsArray["modules"] = 1;
                                    $this->setFlag();
                                }
                            }

                            function casting_process() {
                                $this->module_list->add("casting_process");
                                if ($_SESSION["modules"]["casting_process"] != TRUE) {
                                    $_SESSION["modules"]["casting_process"] = TRUE;
                                    $this->flagsArray["modules"] = 1;
                                    $this->setFlag();
                                }
                            }

                            function resolve_process() {
                                $this->module_list->add("resolve_process");
                                if ($_SESSION["modules"]["resolve_process"] != TRUE) {
                                    $_SESSION["modules"]["resolve_process"] = TRUE;
                                    $this->flagsArray["modules"] = 1;
                                    $this->setFlag();
                                }
                            }

                            function static_abilities() {
                                $this->module_list->add("static_abilities");
                                if ($_SESSION["modules"]["static_abilities"] != TRUE) {
                                    $_SESSION["modules"]["static_abilities"] = TRUE;
                                    $this->flagsArray["modules"] = 1;
                                    $this->setFlag();
                                }
                            }

                            function abilities() {
                                $this->module_list->add("abilities");
                                if ($_SESSION["modules"]["abilities"] != TRUE) {
                                    $_SESSION["modules"]["abilities"] = TRUE;
                                    $this->flagsArray["modules"] = 1;
                                    $this->setFlag();
                                }
                            }

                            function triggered_abilities() {
                                $this->module_list->add("triggered_abilities");
                                if ($_SESSION["modules"]["triggered_abilities"] != TRUE) {
                                    $_SESSION["modules"]["triggered_abilities"] = TRUE;
                                    $this->flagsArray["modules"] = 1;
                                    $this->setFlag();
                                }
                            }

                            function fluents($name) {
                                $this->readstr = substr($this->readstr, 0, strlen($this->readstr) - 1);
                                $newFluent = new FluentClass;
                                $newFluent->setName($name);
                                $idx = strpos($this->readstr, "(");
                                if ($idx != FALSE) {
                                    $this->readstr = substr($this->readstr, $idx + 1);
                                }
                                //if it has more than one parameter, it has a comma separating them
                                $idx = strpos($this->readstr, ",");
                                while ($idx != FALSE) {
                                    $newFluent->addToParameterArray(substr($this->readstr, 0, $idx));
                                    $this->readstr = substr($this->readstr, $idx + 1);
                                    $idx = strpos($this->readstr, ",");
                                }
                                //now the last parameter
                                $idx = strpos($this->readstr, ")");
                                if ($idx != FALSE) {
                                    $newFluent->addToParameterArray(substr($this->readstr, 0, $idx));
                                }
                                //prezitok  $newFluent->removeLastParameterFromArray();
                                $this->addToFluentArray($newFluent);
                                switch ($newFluent->getName()) {
                                    case "life_total":
                                        $this->display_array["life_total" . $newFluent->getParameterArray(0)] = $newFluent;
                                        break;
                                    case "mana_pool":
                                        $this->display_array["mana_pool" . $newFluent->getParameterArray(0)] = $newFluent;
                                        break;
                                    case "triggered_ability":
                                        $this->display_array["triggered" . $newFluent->getParameterArray(1)] = $newFluent;
                                        break;
                                    case "activated_ability":
                                        $this->display_array["activated" . $newFluent->getParameterArray(1)] = $newFluent;
                                        break;
                                    default:
                                        $this->display_array[$newFluent->getName()] = $newFluent;
                                }
                            }

                            function resetFluentsArray() {
                                $this->fluentsArray = array();
                            }

                            function resetObjectArray() {
                                $this->objectArray = array();
                            }

                            function resetDisplayArray() {
                                $this->display_array = array();
                            }

                        }

                        function ConditionMin($str) {
                            //returns the minmal nonfalse value of the both variables or false if both are
                            $colon = strpos($str, ",");
                            $bracket = strpos($str, "(");
                            if (($colon === FALSE) || ($bracket === FALSE)) {
                                if (($colon === FALSE) && ($bracket === FALSE)) {
                                    return FALSE;
                                } else {
                                    if ($colon === FALSE) {
                                        return strpos($str, ")") + 3;
                                    } else {
                                        return $colon + 2;
                                    }
                                }
                            } else {
                                if ($colon < $bracket) {
                                    return $colon + 2;
                                } else {
                                    return strpos($str, ")") + 3;
                                }
                            }
                        }

                        function handleOutput($newGhandler) {
                            //now we are going to play with the state of the game, we need to parse all information of some value, which of course are all that are here :)
                            $inputfile = fopen(realpath("./Game/") . "/ActionFork", 'r') or die("Couldn't open action file");
                            $str = fgets($inputfile);
                            //the first ':' in the file is at the end of STATE 0:, which is the starting point for the parser
                            $condition = strpos($str, ":");
                            //here only +2 cause we searched for only a one letter match
                            $str = substr($str, $condition + 2);
                            $condition = ConditionMin($str);
                            while ($condition) {
                                $fact = substr($str, 0, $condition - 2);
                                $str = substr($str, $condition);
                                $newGhandler->callfunc($fact . ".");
                                $condition = ConditionMin($str);
                            }
                            fclose($inputfile);
                            $newGhandler->handleFlags();
                        }

                        class a_list {

                            public $a_list;

                            public function add($var) {
                                $this->a_list[count($this->a_list)] = $var;
                            }

                            public function remove($pos) {
                                unset($this->a_list[$pos]);
                                array_values($this->a_list);
                            }

                            public function removeLast() {
                                unset($this->a_list[count($this->a_list)]);
                            }

                            public function get($pos) {
                                return $this->a_list[$pos];
                            }

                            public function set($pos, $value) {
                                $this->a_list[$pos] = $value;
                            }

                        }

                        ;

                        function MainOutputParser() {
                            $gameHandler = $_SESSION['GameHandler'];
                            if (!isset($gameHandler)) {
                                $gameHandler = new GameHandler();
                            }
                            $gameHandler->resetFlag();
                            $gameHandler->resetFluentsArray();
                            $gameHandler->module_list = new a_list();
                            $gameHandler->resetObjectArray();
                            $gameHandler->resetDisplayArray();
                            handleOutput($gameHandler);
                            $_SESSION['GameHandler'] = $gameHandler;
                        }

                        function MainPagePrint() {
                            $gameHandler = $_SESSION['GameHandler'];
                            if (isset($_SESSION["print"])) {
                                if ($_SESSION["print"] === TRUE) {
                                    $gameHandler->printPage();
                                }else
                                    echo "go fish";
                            } else {
                                $gameHandler->printPage();
                            }
                        }
                        ?>
