예제 #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);
}
if ($player->turns < 3) {
    create_error("Insufficient turns to perform that action");
}
if (DEBUG) {
    print "Opening<br>";
}
//$players contains all player info for the trigger and his alliance IS
$players = getPlayerArray();
//$weapons contains info for weapons being used this battle
$weapons = getWeapons(getHardware($players));
if (!sizeof($players[$player->account_id][WEAPONS])) {
    create_error("What are you going to do?  Insult it to death?");
}
$port = getPortArray();
//in case there are more than 10 players IS.  $fleet contains account_ids of the attackers
$fleet = getFleet($players, $weapons);
if (DEBUG) {
    print "Pre news<br>";
}
hofTracker($players, $port);
// Take off the 3 turns for attacking
$player->take_turns(3);
$player->update();
// fire shots
if (DEBUG) {
    print "Pre Shots<br>";
}
portFires($fleet, $port, $players);
fleetFires($fleet, $port, $players, $weapons);
//get results in a way we want them
$results = processResults($players, $port, $fleet, $weapons);
예제 #3
0
<?php

use App\Battleships\Exceptions\Exception;
// Default home route
$app->get('/', function () use($app) {
    $fleet = getFleet();
    // flag to backdoor cheats
    $debug = isset($_GET['debug']) ? true : false;
    $flash_messages = getFlashMessage();
    $app->view->setData('fleet', $fleet);
    $app->render('layout.php', ['grid' => $fleet->getGridCoordinates(), 'hits' => $fleet->getShotCoordinates(), 'debug' => $debug, 'hit' => 'X', 'miss' => '--', 'no_shot' => '.', 'flash_messages' => $flash_messages]);
});
// Handle post request
$app->post('/', function () use($app) {
    if (empty($_POST['co-ordinates'])) {
        $app->redirect('/');
    }
    $coordinates = $_POST['co-ordinates'];
    $row = strtolower(substr($coordinates, 0, 1));
    $column = intval(substr($coordinates, 1));
    $fleet = getFleet();
    try {
        $status = $fleet->fire($row, $column);
        $status ? setFlashMessage('hit') : setFlashMessage('miss');
        updateFleet($fleet);
    } catch (Exception $e) {
        setFlashMessage($e->getMessage());
    }
    $app->redirect('/');
});
return $app;
예제 #4
0
function fleet_copy($src, $dst = 0)
{
    if (!$src || !is_numeric($src)) {
        return;
    }
    $fleet = getFleet($src);
    if (!$fleet) {
        return;
    }
    if ($dst) {
        return fleet_update($dst, $fleet);
    } else {
        return fleet_add($fleet);
    }
}