Пример #1
0
/**
 * calculateAttack()
 * Calculate the battle using OPBE.
 * 
 * OPBE ,to decrease memory usage, don't save both the initial and end state of fleets in a single round: only the end state is saved.
 * Then OPBE store the first round in BattleReport and don't start it, just to show the fleets before the battle.
 * Also,cause OPBE start the rounds without saving the initial state, the informations about how many shots were fired etc must be asked to the next round.
 * Logically, the last round can't ask the next round because there is not.
 * 
 * @param array &$attackers
 * @param array &$defenders
 * @param mixed $FleetTF
 * @param mixed $DefTF
 * @return array
 */
function calculateAttack(&$attackers, &$defenders, $FleetTF, $DefTF)
{
    //null == use default handlers
    $errorHandler = null;
    $exceptionHandler = null;
    $CombatCaps = $GLOBALS['CombatCaps'];
    $pricelist = $GLOBALS['pricelist'];
    /********** BUILDINGS MODELS **********/
    /** Note: we are transform array of data like
     *  fleetID => infos
     *  into object tree structure like
     *  playerGroup -> player -> fleet -> shipType
     */
    //attackers
    $attackerGroupObj = new PlayerGroup();
    foreach ($attackers as $fleetID => $attacker) {
        $player = $attacker['player'];
        //techs + bonus. Note that the bonus is divided by the factor because the result sum will be multiplied by the same inside OPBE
        list($attTech, $defenceTech, $shieldTech) = getTechsFromArray($player);
        //--
        $attackerPlayerObj = $attackerGroupObj->createPlayerIfNotExist($player['id'], array(), $attTech, $shieldTech, $defenceTech);
        $attackerFleetObj = new Fleet($fleetID);
        foreach ($attacker['unit'] as $element => $amount) {
            if (empty($amount)) {
                continue;
            }
            $shipType = getShipType($element, $amount);
            $attackerFleetObj->addShipType($shipType);
        }
        $attackerPlayerObj->addFleet($attackerFleetObj);
    }
    //defenders
    $defenderGroupObj = new PlayerGroup();
    foreach ($defenders as $fleetID => $defender) {
        $player = $defender['player'];
        //techs + bonus. Note that the bonus is divided by the factor because the result sum will be multiplied by the same inside OPBE
        list($attTech, $defenceTech, $shieldTech) = getTechsFromArray($player);
        //--
        $defenderPlayerObj = $defenderGroupObj->createPlayerIfNotExist($player['id'], array(), $attTech, $shieldTech, $defenceTech);
        $defenderFleetObj = getFleet($fleetID);
        foreach ($defender['unit'] as $element => $amount) {
            if (empty($amount)) {
                continue;
            }
            $shipType = getShipType($element, $amount);
            $defenderFleetObj->addShipType($shipType);
        }
        $defenderPlayerObj->addFleet($defenderFleetObj);
    }
    /********** BATTLE ELABORATION **********/
    $opbe = new Battle($attackerGroupObj, $defenderGroupObj);
    $startBattle = DebugManager::runDebugged(array($opbe, 'startBattle'), $errorHandler, $exceptionHandler);
    $startBattle();
    $report = $opbe->getReport();
    /********** WHO WON **********/
    if ($report->defenderHasWin()) {
        $won = DEFENDERS_WON;
    } elseif ($report->attackerHasWin()) {
        $won = ATTACKERS_WON;
    } elseif ($report->isAdraw()) {
        $won = DRAW;
    } else {
        throw new Exception('problem');
    }
    /********** ROUNDS INFOS **********/
    $ROUND = array();
    $lastRound = $report->getLastRoundNumber();
    for ($i = 0; $i <= $lastRound; $i++) {
        // in case of last round, ask for rebuilt defenses. to change rebuils prob see constants/battle_constants.php
        $attackerGroupObj = $lastRound == $i ? $report->getAfterBattleAttackers() : $report->getResultAttackersFleetOnRound($i);
        $defenderGroupObj = $lastRound == $i ? $report->getAfterBattleDefenders() : $report->getResultDefendersFleetOnRound($i);
        $attInfo = updatePlayers($attackerGroupObj, $attackers);
        $defInfo = updatePlayers($defenderGroupObj, $defenders);
        $ROUND[$i] = roundInfo($report, $attackers, $defenders, $attackerGroupObj, $defenderGroupObj, $i + 1, $attInfo, $defInfo);
    }
    /********** DEBRIS **********/
    //attackers
    $debAtt = $report->getAttackerDebris();
    $debAttMet = $debAtt[0];
    $debAttCry = $debAtt[1];
    //defenders
    $debDef = $report->getDefenderDebris();
    $debDefMet = $debDef[0];
    $debDefCry = $debDef[1];
    //total
    $debris = array('attacker' => array(METAL_ID => $debAttMet, CRYSTAL_ID => $debAttCry), 'defender' => array(METAL_ID => $debDefMet, CRYSTAL_ID => $debDefCry));
    /********** LOST UNITS **********/
    $totalLost = array('attacker' => $report->getTotalAttackersLostUnits(), 'defender' => $report->getTotalDefendersLostUnits());
    /********** RETURNS **********/
    return array('won' => $won, 'debris' => $debris, 'rw' => $ROUND, 'unitLost' => $totalLost);
}
Пример #2
0
/* 
Separated from the Update function to lighten the server-to-database communication.
Will usually be followed by it if all is done by code. It won't be if we set up the client-side 
	access and administration calls, then it should only be used once, at the end of the session,
	or every now and then to not lose all the changes.
*/
function exportPlayers()
{
    global $PlayersMMR;
    global $conn;
    $length = count($PlayersMMR);
    for ($i = 0; $i < $length; $i++) {
        $Player = $PlayersMMR[$i]['Name'];
        global ${$Player};
        $sqlUpdate = "UPDATE mmr SET \n\t\tRating='" . ${$Player}->rating . "',\n\t\tRD='" . ${$Player}->rd . "',\n\t\tmu='" . ${$Player}->mu . "',\n\t\tphi='" . ${$Player}->phi . "',\n\t\tsigma='" . ${$Player}->sigma . "'\n\t\tWHERE Name='" . $Player . "'";
        if ($conn->query($sqlUpdate) === TRUE) {
            echo "Updated player " . $Player . " in the database<br>";
        } else {
            echo "<br>Error: " . $sqlUpdate . "<br>" . $conn->error . "<br>";
        }
    }
}
initiate();
$Random0->AddWin($Random1);
$Random1->AddLoss($Random0);
updatePlayers();
echo $Random0->rating . '<br>';
echo $Random1->rating . '<br>';
exportPlayers();
// RIP Connection
$conn->close();