%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /data/www_bck/varak.net_bck/warhammer.varak.net/app/model/
Upload File :
Create Path :
Current File : //data/www_bck/varak.net_bck/warhammer.varak.net/app/model/CycleModel.php

<?php
/**
 * Created by PhpStorm.
 * User: waritko
 * Date: 9. 8. 2018
 * Time: 22:12
 */

namespace App\Model;


use Tracy\Debugger;

class CycleModel extends BaseModel
{
    private $worldData;
    private $shipData;

    private $logData = "";

    protected function onCreate()
    {
        $this->worldData = json_decode(file_get_contents("../app/model/cycleData/world.json"));
        $this->shipData = json_decode(file_get_contents("../app/model/cycleData/ship.json"));
    }

    /**
     * Advances all planets and ships by one cycle
     */
    public function advance($commit = false)
    {
        $logData = "";
        $this->database->beginTransaction();
        $players = $this->database->query("SELECT `id`, `mineral`, `food`, `credit`, `consumer`, `luxury`, `plasteel`, `plastcrete`, `influence`, `imperialInfluence`, `manhour` FROM `player`");
        foreach ($players as $player)
        {
            $worlds = $this->getAllWorlds($player->id);
            Debugger::barDump($this->worldData);
            foreach ($worlds as $world)
            {
                $logData .= $this->processWorld($world, $player);
            }
            $this->savePlayer($player);
        }

        $cycleId = $this->database->query("SELECT (max(`cycle`) + 1) `maxid` FROM `cycle_log`")->fetch()->maxid;
        $cycleId = $cycleId == null ? 1 : $cycleId;
        $this->database->query("INSERT INTO `cycle_log`", ['cycle' => $cycleId, 'log' => $logData]);

        if($commit)
            $this->database->commit();
        else
            $this->database->rollBack();
        return $logData;
    }

    private function savePlayer($player)
    {
        $data = array(
            'mineral' => $player->mineral,
            'food' => $player->food,
            'credit' => $player->credit,
            'consumer' => $player->consumer,
            'luxury' => $player->luxury,
            'plasteel' => $player->plasteel,
            'plastcrete' => $player->plastcrete,
            'influence' => $player->influence,
            'imperialInfluence' => $player->imperialInfluence,
            'manhour' => $player->manhour
        );
        $this->database->query("UPDATE `player` SET", $data, "WHERE `id`=?", $player->id);
    }

    private function processWorld($world, &$player)
    {
        $this->logMessage("Processing world " . $world->name . "(#" . $world->id . ")");
        if(!isset($this->worldData->{$world->type}))
        {
            $this->logMessage("Could not process world of type " . $world->type);
            return $this->dumpLog();
        }
        $prod = $this->worldData->{$world->type}->generation;
        $cons = $this->worldData->{$world->type}->consumption;

        // Process food
        $died = 0;
        if(isset($prod->food))
        {
            $generated = ceil($world->population * $prod->food * $this->happinessCoef($world->happiness));
            $this->logMessage("Generated food: " . number_format($generated, 0, ".", " "));
            $player->food += $generated;
        }
        if(isset($cons->food))
        {
            $consumed = floor($world->population * $cons->food);
            $this->logMessage("Consumed food: " . number_format($consumed, 0, ".", " "));
            $player->food -= $consumed;
            if($player->food < 0)
            {
                $died += floor(-($player->food / $cons->food));
                $this->logMessage("Due to starvation, this much people died: " . number_format($generated, 0, ".", " "));
                $this->database->query("UPDATE `world` SET `population`=`population`-? WHERE `id`=?", $died, $world->id);
                $player->food = 0;
            }
        }

        // Consumer & Luxury Goods
        $missingConsumer = 0;
        $missingLuxury = 0;
        $missingPlasteel = 0;
        if(isset($cons->consumerGoods))
        {
            $wouldBeConsumed = $world->population * $cons->consumerGoods;
            $missingConsumer = max(0, floor(-($player->consumer - $wouldBeConsumed) /  $cons->consumerGoods));
            $consumed = ($world->population - $missingConsumer) * $cons->consumerGoods;
            $this->logMessage("Consumed Consumer Goods: " . number_format($consumed, 0, ".", " "));
            $player->consumer -= $consumed;
        }
        if(isset($cons->luxuryGoods))
        {
            $wouldBeConsumed = $world->population * $cons->luxuryGoods;
            $missingLuxury = max(0, floor(-($player->consumer - $wouldBeConsumed) /  $cons->luxuryGoods));
            $consumed = ($world->population - $missingLuxury) * $cons->luxuryGoods;
            $this->logMessage("Consumed luxury goods: " . number_format($consumed, 0, ".", " "));
            $player->luxury -= $consumed;
        }
        if(isset($cons->plasteel))
        {
            $wouldBeConsumed = $world->population * $cons->plasteel;
            $missingPlasteel = max(0, floor(-($player->consumer - $wouldBeConsumed) /  $cons->plasteel));
            $consumed = ($world->population - $missingPlasteel) * $cons->plasteel;
            $this->logMessage("Consumed plasteel: " . number_format($consumed, 0, ".", " "));
            $player->plasteel -= $consumed;
        }
        //$this->logMessage("$missingPlasteel | $missingLuxury | $missingConsumer");
        $generatingPop = max(0, $world->population - max($missingLuxury, $missingConsumer, $missingPlasteel)); // Population contributing credit, influence, ...

        // Minerals
        if(isset($prod->mineral))
        {
            $generated = ceil($world->population * $prod->mineral * $this->happinessCoef($world->happiness));
            $this->logMessage("Generated minerals: " . number_format($generated, 0, ".", " "));
            $player->mineral += $generated;
        }
        $usableMineral = 0;
        if(isset($cons->mineral))
        {
            $wouldBeConsumed = $world->population * $cons->mineral;
            $usableMineral = floor($wouldBeConsumed > $player->mineral ? $player->mineral : $wouldBeConsumed);
            $this->logMessage("Consumed minerals: " . number_format($usableMineral, 0, ".", " "));
            $player->mineral -= $usableMineral;
        }

        // Production of resources
        if(isset($prod->resource))
        {
            $rate = $prod->resource;
            if($world->resource == "luxury")
                $rate /= 100;
            $productivePop = ($usableMineral / $cons->mineral) > $world->population ? $world->population : ceil($usableMineral / $cons->mineral);
            $produced = ceil($productivePop * $rate * $this->happinessCoef($world->happiness));
            $this->logMessage("Generated resource - $world->resource: " . number_format($produced, 0, ".", " "));
            $player->{$world->resource} += $produced;
            // For industrial worlds, cap credit generation by minerals also
            $generatingPop = min($generatingPop, $productivePop);
        }

        // Credits, manhours, Imperial Influence
        if(isset($prod->credit))
        {
            $generated = $generatingPop * $prod->credit * $this->happinessCoef($world->happiness) * $world->tax;
            $this->logMessage("Generated credits: " . number_format($generated, 0, ".", " "));
            $player->credit += $generated;
        }
        if(isset($prod->imperialInfluence))
        {
            $generated = $generatingPop * $prod->imperialInfluence * $this->happinessCoef($world->happiness);
            $this->logMessage("Generated Imperial Influence: " . number_format($generated, 7, ".", " "));
            $player->imperialInfluence += $generated;
        }
        if(isset($prod->manhour))
        {
            $generated = $generatingPop * $prod->manhour * $this->happinessCoef($world->happiness);
            $this->logMessage("Generated manhours: " . number_format($generated, 0, ".", " "));
            $player->manhour += $generated;
        }



        // Population
        // Change population last
        if(isset($prod->population) && $player->food > 5)
        {
            // Planet population increases no-one died of starvation
            $newPop = floor(min($world->populationCap, $world->population * (1 + ($prod->population/100))));
            $this->logMessage("Population increased from $world->population to $newPop (" . ($newPop - $world->population) .")");
            $this->database->query("UPDATE `world` SET `population`=? WHERE `id`=?", $newPop, $world->id);
        }
        if(isset($cons->population))
        {
            // Planet population increases no-one died of starvation
            $newPop = floor(max(0, $world->population * (1 - ($cons->population/100))));
            $this->logMessage("Population decreased from $world->population to $newPop (" . ($world->population - $newPop) .")");
            $this->database->query("UPDATE `world` SET `population`=? WHERE `id`=?", $newPop, $world->id);
        }

        $this->logMessage(print_r($player, true));
        return $this->dumpLog();
    }

    private function happinessCoef($happiness)
    {
        if($happiness > 95)
            return 1;
        if($happiness > 85)
            return 0.9;
        if($happiness > 50)
            return 0.7;
        return 0.3;
    }

    private function logMessage($message)
    {
        $this->logData .= date("[Y-m-d H:i:s] ") . $message . "\n";
    }

    private function dumpLog()
    {
        $retVal = $this->logData;
        $this->logData = "";
        return $retVal;
    }

    private function getAllWorlds($playerId)
    {
        // Get all worlds in correct order
        // Agrar, Feudal, Feral ==> No generated food gets off the planned if population is not fed, but also gets consumer goods first
        // Civilized, Pleasure, Holy, Shipyard, Sanctum, Armory ==> first use resources before generating them
        // Industrial ==> process minerals from last round
        // Mining ==> Poor miners...
        $worlds = array();

        $result = $this->database->query("SELECT `id`, `name`, `population`, `populationCap`, `type`, `happiness`, `resource`, `tax` FROM `world` WHERE `player`=? AND `type` IN ('agrar', 'feudal', 'feral')", $playerId);
        foreach ($result as $row)
        {
            $worlds[] = $row;
        }
        $result = $this->database->query("SELECT `id`, `name`, `population`, `populationCap`, `type`, `happiness`, `resource`, `tax` FROM `world` WHERE `player`=? AND `type` IN ('civilized', 'pleasure', 'holy', 'shipyard', 'sanctum', 'armory')", $playerId);
        foreach ($result as $row)
        {
            $worlds[] = $row;
        }
        $result = $this->database->query("SELECT `id`, `name`, `population`, `populationCap`, `type`, `happiness`, `resource`, `tax` FROM `world` WHERE `player`=? AND `type` IN ('industrial')", $playerId);
        foreach ($result as $row)
        {
            $worlds[] = $row;
        }
        $result = $this->database->query("SELECT `id`, `name`, `population`, `populationCap`, `type`, `happiness`, `resource`, `tax` FROM `world` WHERE `player`=? AND `type` IN ('mining')", $playerId);
        foreach ($result as $row)
        {
            $worlds[] = $row;
        }

        return $worlds;
    }
}

Zerion Mini Shell 1.0