<commit_before><?php

/**
 *  OPBE
 *  Copyright (C) 2013  Jstar
 *
 * This file is part of OPBE.
 * 
 * OPBE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OPBE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OPBE.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OPBE
 * @author Jstar <<EMAIL>>
 * @copyright 2013 Jstar <<EMAIL>>
 * @license http://www.gnu.org/licenses/ GNU AGPLv3 License
 * @version alpha(2013-2-4)
 * @link https://github.com/jstar88/opbe
 */
class BattleReport
{
    private $rounds;
    private $roundsCount;

    private $attackersLostUnits;
    private $defendersLostUnits;

    public function __construct()
    {
        $this->rounds = array();
        $this->roundsCount = 0;
        $this->attackersLostUnits = null;
        $this->defendersLostUnits = null;
    }
    public function addRound(Round $round)
    {
        if (ONLY_FIRST_AND_LAST_ROUND && $this->roundsCount == 2)
        {
            $this->rounds[1] = $round;
            return;
        }
        $this->rounds[$this->roundsCount++] = $round;
    }
    public function getRound($number)
    {
        if ($number === 'END')
        {
            return $this->rounds[$this->roundsCount - 1];
        }
        elseif ($number === 'START')
        {
            return $this->rounds[0];
        }
        elseif (intval($number) < 0 || intval($number) > $this->getLastRoundNumber())
        {
            throw new Exception('Invalid round number');
        }
        else
        {
            return $this->rounds[intval($number)];
        }
    }
    public function setBattleResult($att, $def)
    {
        $this->getRound('END')->getAttachersAfterRound()->battleResult = $att;
        $this->getRound('END')->getDefendersAfterRound()->battleResult = $def;
    }
    public function attackerHasWin()
    {
        return $this->getRound('END')->getAttachersAfterRound()->battleResult === BATTLE_WIN;
    }
    public function defenderHasWin()
    {
        return $this->getRound('END')->getDefendersAfterRound()->battleResult === BATTLE_WIN;
    }
    public function isAdraw()
    {
        return $this->getRound('END')->getAttachersAfterRound()->battleResult === BATTLE_DRAW;
    }
    private function getPresentationRound($number)
    {
        if ($number !== 'START')
        {
            $number -= 1;
        }
        return $this->getRound($number);
    }
    private function getResultRound($number)
    {
        return $this->getRound($number);
    }
    public function getPresentationAttackersFleetOnRound($number)
    {
        return $this->getPresentationRound($number)->getAttachersAfterRound();
    }
    public function getPresentationDefendersFleetOnRound($number)
    {
        return $this->getPresentationRound($number)->getDefendersAfterRound();
    }
    public function getResultAttackersFleetOnRound($number)
    {
        return $this->getResultRound($number)->getAttachersAfterRound();
    }
    public function getResultDefendersFleetOnRound($number)
    {
        return $this->getResultRound($number)->getDefendersAfterRound();
    }
    public function getTotalAttackersLostUnits()
    {
        return Math::recursive_sum($this->getAttackersLostUnits());
    }
    public function getTotalDefendersLostUnits()
    {
        return Math::recursive_sum($this->getDefendersLostUnits());
    }
    public function getAttackersLostUnits()
    {
        if ($this->attackersLostUnits !== null)
        {
            return $this->attackersLostUnits;
        }
        $attackersBefore = $this->getRound('START')->getAttachersAfterRound();
        $attackersAfter = $this->getRound('END')->getAttachersAfterRound();
        return $this->getPlayersLostUnits($attackersBefore, $attackersAfter);
    }
    public function getDefendersLostUnits()
    {
        if ($this->defendersLostUnits !== null)
        {
            return $this->defendersLostUnits;
        }
        $defendersBefore = $this->getRound('START')->getDefendersAfterRound();
        $defendersAfter = $this->getRound('END')->getDefendersAfterRound();
        return $this->getPlayersLostUnits($defendersBefore, $defendersAfter);
    }
    private function getPlayersLostUnits(PlayerGroup $playersBefore, PlayerGroup $playersAfter)
    {
        $lostShips = $this->getPlayersLostShips($playersBefore, $playersAfter);
        $return = array();
        foreach ($lostShips->getIterator() as $idPlayer => $player)
        {
            foreach ($player->getIterator() as $idFleet => $fleet)
            {
                foreach ($fleet->getIterator() as $idFighters => $fighters)
                {
                    $cost = $fighters->getCost();
                    $return[$idPlayer][$idFleet][$idFighters] = array($cost[0] * $fighters->getCount(), $cost[1] * $fighters->getCount());
                }
            }
        }
        return $return;
    }
    public function tryMoon()
    {
        $prob = $this->getMoonProb();
        return Math::tryEvent($prob, 'Events::event_moon', $prob);
    }
    public function getMoonProb()
    {
        return min(round(array_sum($this->getDebris()) / MOON_UNIT_PROB), MAX_MOON_PROB);
    }
    public function getAttackerDebris()
    {
        $metal = 0;
        $crystal = 0;
        foreach ($this->getAttackersLostUnits() as $idPlayer => $player)
        {
            foreach ($player as $idFleet => $fleet)
            {
                foreach ($fleet as $idFighters => $lost)
                {<commit_msg>fixed debris<commit_after><?php

/**
 *  OPBE
 *  Copyright (C) 2013  Jstar
 *
 * This file is part of OPBE.
 * 
 * OPBE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OPBE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OPBE.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OPBE
 * @author Jstar <<EMAIL>>
 * @copyright 2013 Jstar <<EMAIL>>
 * @license http://www.gnu.org/licenses/ GNU AGPLv3 License
 * @version alpha(2013-2-4)
 * @link https://github.com/jstar88/opbe
 */
class BattleReport
{
    private $rounds;
    private $roundsCount;

    private $attackersLostUnits;
    private $defendersLostUnits;

    public function __construct()
    {
        $this->rounds = array();
        $this->roundsCount = 0;
        $this->attackersLostUnits = null;
        $this->defendersLostUnits = null;
    }
    public function addRound(Round $round)
    {
        if (ONLY_FIRST_AND_LAST_ROUND && $this->roundsCount == 2)
        {
            $this->rounds[1] = $round;
            return;
        }
        $this->rounds[$this->roundsCount++] = $round;
    }
    public function getRound($number)
    {
        if ($number === 'END')
        {
            return $this->rounds[$this->roundsCount - 1];
        }
        elseif ($number === 'START')
        {
            return $this->rounds[0];
        }
        elseif (intval($number) < 0 || intval($number) > $this->getLastRoundNumber())
        {
            throw new Exception('Invalid round number');
        }
        else
        {
            return $this->rounds[intval($number)];
        }
    }
    public function setBattleResult($att, $def)
    {
        $this->getRound('END')->getAttachersAfterRound()->battleResult = $att;
        $this->getRound('END')->getDefendersAfterRound()->battleResult = $def;
    }
    public function attackerHasWin()
    {
        return $this->getRound('END')->getAttachersAfterRound()->battleResult === BATTLE_WIN;
    }
    public function defenderHasWin()
    {
        return $this->getRound('END')->getDefendersAfterRound()->battleResult === BATTLE_WIN;
    }
    public function isAdraw()
    {
        return $this->getRound('END')->getAttachersAfterRound()->battleResult === BATTLE_DRAW;
    }
    private function getPresentationRound($number)
    {
        if ($number !== 'START')
        {
            $number -= 1;
        }
        return $this->getRound($number);
    }
    private function getResultRound($number)
    {
        return $this->getRound($number);
    }
    public function getPresentationAttackersFleetOnRound($number)
    {
        return $this->getPresentationRound($number)->getAttachersAfterRound();
    }
    public function getPresentationDefendersFleetOnRound($number)
    {
        return $this->getPresentationRound($number)->getDefendersAfterRound();
    }
    public function getResultAttackersFleetOnRound($number)
    {
        return $this->getResultRound($number)->getAttachersAfterRound();
    }
    public function getResultDefendersFleetOnRound($number)
    {
        return $this->getResultRound($number)->getDefendersAfterRound();
    }
    public function getTotalAttackersLostUnits()
    {
        return Math::recursive_sum($this->getAttackersLostUnits());
    }
    public function getTotalDefendersLostUnits()
    {
        return Math::recursive_sum($this->getDefendersLostUnits());
    }
    public function getAttackersLostUnits()
    {
        if ($this->attackersLostUnits !== null)
        {
            return $this->attackersLostUnits;
        }
        $attackersBefore = $this->getRound('START')->getAttachersAfterRound();
        $attackersAfter = $this->getRound('END')->getAttachersAfterRound();
        return $this->getPlayersLostUnits($attackersBefore, $attackersAfter);
    }
    public function getDefendersLostUnits()
    {
        if ($this->defendersLostUnits !== null)
        {
            return $this->defendersLostUnits;
        }
        $defendersBefore = $this->getRound('START')->getDefendersAfterRound();
        $defendersAfter = $this->getRound('END')->getDefendersAfterRound();
        return $this->getPlayersLostUnits($defendersBefore, $defendersAfter);
    }
    private function getPlayersLostUnits(PlayerGroup $playersBefore, PlayerGroup $playersAfter)
    {
        $lostShips = $this->getPlayersLostShips($playersBefore, $playersAfter);
        $defRepaired = $this->getPlayerRepaired($playersBefore, $playersAfter);
        $return = array();
        foreach ($lostShips->getIterator() as $idPlayer => $player)
        {
            foreach ($player->getIterator() as $idFleet => $fleet)
            {
                foreach ($fleet->getIterator() as $idFighters => $fighters)
                {
                    $cost = $fighters->getCost();
                    $count = $fighters->getCount() - $defRepaired->getPlayer($idPlayer)->getFleet($idFleet)->getFighters($idFighters)->getCount();
                    if ($count > 0)
                    {
                        $return[$idPlayer][$idFleet][$idFighters] = array($cost[0] * $count, $cost[1] * $count);
                    }
                    elseif ($count < 0)
                    {
                        throw new Exception('Count negative');
                    }
                }
            }
        }
        return $return;
    }
    public function tryMoon()
    {
        $prob = $this->getMoonProb();
        return Math::tryEvent($prob, 'Events::event_moon', $prob);
    }
    public function getMoonProb()
    {
        return min(round(array_sum($this->getDebris()) / MOON_UNIT_PROB), MAX_MOON_PROB);
    }
    public function getAttackerDebris()
    {
        $metal = 0;
        $crystal = 0;
        foreach ($this->getAttackersLostUnits() as $idPlayer => $player)
        {
            foreach ($player as $idFleet => $fleet)
            {
                foreach ($fleet as $idFighters => $lost)
                {
{'fraction_non_alphanumeric': 0.12812626467017402, 'fraction_numerical': 0.004856333468231485, 'mean_word_length': 2.4864559819413095, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 160, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6096846', 'n_tokens_mistral': 3687, 'n_tokens_neox': 3528, 'n_words': 745}