diff options
Diffstat (limited to 'src/Model')
-rw-r--r-- | src/Model/Building.php | 10 | ||||
-rw-r--r-- | src/Model/Building/ClayPit.php | 2 | ||||
-rw-r--r-- | src/Model/Building/Embassy.php | 17 | ||||
-rw-r--r-- | src/Model/Building/Farm.php | 2 | ||||
-rw-r--r-- | src/Model/Building/IronMine.php | 2 | ||||
-rw-r--r-- | src/Model/Building/Marketplace.php | 17 | ||||
-rw-r--r-- | src/Model/Building/Storage.php | 4 | ||||
-rw-r--r-- | src/Model/Building/WoodCutter.php | 4 | ||||
-rw-r--r-- | src/Model/Event.php | 3 | ||||
-rw-r--r-- | src/Model/Event/BaseEvent.php | 14 | ||||
-rw-r--r-- | src/Model/Event/SendUnits.php | 87 | ||||
-rw-r--r-- | src/Model/Event/UpgradeBuilding.php | 32 | ||||
-rw-r--r-- | src/Model/Unit.php | 30 | ||||
-rw-r--r-- | src/Model/Unit/Diplomat.php | 18 | ||||
-rw-r--r-- | src/Model/Unit/Merchant.php | 18 | ||||
-rw-r--r-- | src/Model/Village.php | 46 |
16 files changed, 269 insertions, 37 deletions
diff --git a/src/Model/Building.php b/src/Model/Building.php index 78d1aa5..7cae067 100644 --- a/src/Model/Building.php +++ b/src/Model/Building.php @@ -54,6 +54,16 @@ class Building return isset($results[0]) ? $results[0]->cast() : null; } + public static function getEmpty(int $villageId, string $buildingType): Building + { + $building = new Building(); + $building->type = $buildingType; + $building->level = 0; + $building->villageId = $villageId; + + return $building->cast(); + } + public function getBuildTime(): int { diff --git a/src/Model/Building/ClayPit.php b/src/Model/Building/ClayPit.php index 8127818..b4905eb 100644 --- a/src/Model/Building/ClayPit.php +++ b/src/Model/Building/ClayPit.php @@ -10,6 +10,8 @@ class ClayPit extends ResourceGenerator public array $resourceRequirements = [ 'wood' => 1.0, + 'clay' => 2.0, + 'iron' => 1.0, ]; public string $resourceType = 'clay'; diff --git a/src/Model/Building/Embassy.php b/src/Model/Building/Embassy.php new file mode 100644 index 0000000..3be1f7f --- /dev/null +++ b/src/Model/Building/Embassy.php @@ -0,0 +1,17 @@ +<?php + +namespace App\Model\Building; + +use App\Model\Building; + +class Embassy extends Building +{ + public int $buildTimeFactor = 1; + public int $maxLevel = 25; + + public array $resourceRequirements = [ + 'wood' => 25.0, + 'clay' => 25.0, + 'iron' => 30.0, + ]; +} diff --git a/src/Model/Building/Farm.php b/src/Model/Building/Farm.php index aaa58b5..222d8cd 100644 --- a/src/Model/Building/Farm.php +++ b/src/Model/Building/Farm.php @@ -13,6 +13,8 @@ class Farm extends ResourceGenerator public array $resourceRequirements = [ 'wood' => 1.0, + 'clay' => 1.0, + 'iron' => 1.0, ]; public string $resourceType = 'food'; diff --git a/src/Model/Building/IronMine.php b/src/Model/Building/IronMine.php index 4bf5cc6..d240e3a 100644 --- a/src/Model/Building/IronMine.php +++ b/src/Model/Building/IronMine.php @@ -10,6 +10,8 @@ class IronMine extends ResourceGenerator public array $resourceRequirements = [ 'wood' => 1.0, + 'clay' => 1.0, + 'iron' => 2.0, ]; public string $resourceType = 'iron'; diff --git a/src/Model/Building/Marketplace.php b/src/Model/Building/Marketplace.php new file mode 100644 index 0000000..714de0d --- /dev/null +++ b/src/Model/Building/Marketplace.php @@ -0,0 +1,17 @@ +<?php + +namespace App\Model\Building; + +use App\Model\Building; + +class Marketplace extends Building +{ + public int $buildTimeFactor = 1; + public int $maxLevel = 25; + + public array $resourceRequirements = [ + 'wood' => 10.0, + 'clay' => 10.0, + 'iron' => 8.0, + ]; +} diff --git a/src/Model/Building/Storage.php b/src/Model/Building/Storage.php index fde4c4e..de2df92 100644 --- a/src/Model/Building/Storage.php +++ b/src/Model/Building/Storage.php @@ -12,11 +12,13 @@ class Storage extends Building public array $resourceRequirements = [ 'wood' => 1.0, + 'clay' => 1.0, + 'iron' => 1.0, ]; public function getCapacity(): int { - return $this->level * 2560; + return $this->level * $_ENV['BASE_STORAGE_CAPACITY_FACTOR']; } public function getResourceCapacity(string $resourceType): int diff --git a/src/Model/Building/WoodCutter.php b/src/Model/Building/WoodCutter.php index 86bde9b..726cdbc 100644 --- a/src/Model/Building/WoodCutter.php +++ b/src/Model/Building/WoodCutter.php @@ -9,7 +9,9 @@ class WoodCutter extends ResourceGenerator public int $maxLevel = 25; public array $resourceRequirements = [ - 'wood' => 1.0, + 'wood' => 2.0, + 'clay' => 1.0, + 'iron' => 1.0, ]; public string $resourceType = 'wood'; diff --git a/src/Model/Event.php b/src/Model/Event.php index aa235f9..b4c131e 100644 --- a/src/Model/Event.php +++ b/src/Model/Event.php @@ -14,6 +14,9 @@ class Event public int $villageId; + public \DateTime $createdAt; + public \DateTime $updatedAt; + /* OOP */ diff --git a/src/Model/Event/BaseEvent.php b/src/Model/Event/BaseEvent.php new file mode 100644 index 0000000..d3cc3fb --- /dev/null +++ b/src/Model/Event/BaseEvent.php @@ -0,0 +1,14 @@ +<?php + +namespace App\Model\Event; + +abstract class BaseEvent +{ + public int $id; + public int $eventId; + + public \DateTime $createdAt; + public \DateTime $updatedAt; + + abstract function sqlInsert(): void; +} diff --git a/src/Model/Event/SendUnits.php b/src/Model/Event/SendUnits.php index f104a08..bf81031 100644 --- a/src/Model/Event/SendUnits.php +++ b/src/Model/Event/SendUnits.php @@ -14,28 +14,75 @@ class SendUnits extends Event { $payload = json_decode($this->payload, true); - if ($payload['type'] === 'Recall' || $payload['type'] === 'SendBack') { - DB::query( - <<<SQL - insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id) - values (:amount, :type, false, :id, :id) - on conflict (type, home_village_id, residence_village_id) - do update set amount = village_units.amount+:amount - SQL, - ['amount' => $payload['amount'], 'type' => $payload['unit'], 'id' => $payload['destination']] - ); + if (isset($payload['cancel'])) { + if ($payload['type'] === 'SendBack') { + $payload['source'] = $payload['cancel']['home']; + $payload['destination'] = $payload['cancel']['residence']; + + $this->borrow($payload); + } } - else if ($payload['type'] === 'Borrow') { - DB::query( - <<<SQL - insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id, created_at, updated_at) - values (:amount, :type, false, :home, :residence, now(), now()) - on conflict (type, home_village_id, residence_village_id) - do update set amount = village_units.amount+:amount - SQL, - ['amount' => $payload['amount'], 'type' => $payload['unit'], 'home' => $this->villageId, 'residence' => $payload['destination']] - ); + else { + if ($payload['type'] === 'Recall' || $payload['type'] === 'SendBack') { + $this->return($payload); + } + + else if ($payload['type'] === 'Borrow') { + $this->borrow($payload); + } + + else if ($payload['type'] === 'Gift') { + $this->gift($payload); + } } } + + /** + * @param array $payload + */ + private function return(array $payload): void + { + DB::query( + <<<SQL + insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id) + values (:amount, :type, false, :id, :id) + on conflict (type, home_village_id, residence_village_id) + do update set amount = village_units.amount+:amount + SQL, + ['amount' => $payload['amount'], 'type' => $payload['unit'], 'id' => $payload['destination']] + ); + } + + /** + * @param array $payload + */ + private function borrow(array $payload): void + { + DB::query( + <<<SQL + insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id) + values (:amount, :type, false, :home, :residence) + on conflict (type, home_village_id, residence_village_id) + do update set amount = village_units.amount+:amount + SQL, + ['amount' => $payload['amount'], 'type' => $payload['unit'], 'home' => $payload['source'], 'residence' => $payload['destination']] + ); + } + + /** + * @param array $payload + */ + private function gift(array $payload): void + { + DB::query( + <<<SQL + insert into village_units (amount, type, is_traveling, home_village_id, residence_village_id) + values (:amount, :type, false, :home, :residence) + on conflict (type, home_village_id, residence_village_id) + do update set amount = village_units.amount+:amount + SQL, + ['amount' => $payload['amount'], 'type' => $payload['unit'], 'home' => $payload['destination'], 'residence' => $payload['destination']] + ); + } } diff --git a/src/Model/Event/UpgradeBuilding.php b/src/Model/Event/UpgradeBuilding.php index c014cfe..f4f427d 100644 --- a/src/Model/Event/UpgradeBuilding.php +++ b/src/Model/Event/UpgradeBuilding.php @@ -5,23 +5,43 @@ namespace App\Model\Event; use App\DB; use App\Model\Event; -class UpgradeBuilding extends Event +class UpgradeBuilding extends BaseEvent { + public Event $event; + public string $type; + + public function __construct(Event $event, string $type) + { + $this->event = $event; + $this->type = $type; + } + /** * @return void */ public function __invoke(): void { - $payload = json_decode($this->payload, true); + DB::query( + <<<SQL + insert into village_buildings (level, type, village_id) + values (1, :type, :village_id) + on conflict (type, village_id) + do update set level = village_buildings.level+1 + SQL, + ['type' => $this->type, 'village_id' => $this->event->villageId] + ); + } + public function sqlInsert(): void + { DB::query( - 'update village_buildings set level=level+1 where id=:id', - ['id' => $payload['id']] + 'insert into events (time, village_id) values (:time, :village_id)', + ['time' => $this->event->time->format('c'), 'village_id' => $this->event->villageId] ); DB::query( - 'delete from events where id=:id', - ['id' => $this->id] + 'insert into events_upgrade_building (event_id, type) values (:event_id, :type)', + ['event_id' => DB::$connection->lastInsertId(), $this->type] ); } } diff --git a/src/Model/Unit.php b/src/Model/Unit.php index 621651c..d563682 100644 --- a/src/Model/Unit.php +++ b/src/Model/Unit.php @@ -25,9 +25,26 @@ class Unit public array $resourceRequirements = []; + public static function getEmpty(int $villageId, string $unitType): Unit + { + $unit = new Unit(); + $unit->type = $unitType; + $unit->homeVillageId = $villageId; + $unit->amount = 0; + + return $unit->cast(); + } + + public function getBuildTime(int $amount): int { - return intval(($_ENV['BASE_UNIT_BUILD_TIME_FACTOR'] / ($this->getBuilding()->level ?: 1)) * $amount); + $building = $this->getBuilding(); + + if (! $building) { + return -1; + } + + return intval(($_ENV['BASE_UNIT_BUILD_TIME_FACTOR'] / ($building->level ?: 1)) * $amount); } public static function getTravelTime(Unit $unit, int $distance): int @@ -63,7 +80,16 @@ class Unit function ($resourceRequirement) use ($amount, $currentAmount, $building) { $r = 0; for ($i = 0; $i <= $amount; $i++) { - $r += ceil((pow($_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_BASE'], $currentAmount + 1) * $resourceRequirement * $_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_FACTOR']) / ($building->level ?? 1)); + $r += ceil( + ( + pow( + $_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_BASE'], + $currentAmount + 1 + ) + * $resourceRequirement * $_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_FACTOR'] + ) + / ($building->level ?? 1) + ); } return $r; diff --git a/src/Model/Unit/Diplomat.php b/src/Model/Unit/Diplomat.php new file mode 100644 index 0000000..e263cd3 --- /dev/null +++ b/src/Model/Unit/Diplomat.php @@ -0,0 +1,18 @@ +<?php + +namespace App\Model\Unit; + +use App\Model\Unit; + +class Diplomat extends Unit +{ + public string $buildingType = 'Embassy'; + public int $travelTime = 1; + public int $populationDemandFactor = 1; + public array $resourceRequirements = [ + 'wood' => 5.0, + 'clay' => 5.0, + 'iron' => 5.0, + 'food' => 5.0, + ]; +} diff --git a/src/Model/Unit/Merchant.php b/src/Model/Unit/Merchant.php new file mode 100644 index 0000000..61e2090 --- /dev/null +++ b/src/Model/Unit/Merchant.php @@ -0,0 +1,18 @@ +<?php + +namespace App\Model\Unit; + +use App\Model\Unit; + +class Merchant extends Unit +{ + public string $buildingType = 'Marketplace'; + public int $travelTime = 1; + public int $populationDemandFactor = 1; + public array $resourceRequirements = [ + 'wood' => 2.0, + 'clay' => 2.0, + 'iron' => 2.0, + 'food' => 2.0, + ]; +} diff --git a/src/Model/Village.php b/src/Model/Village.php index b1ab19e..6c80ed0 100644 --- a/src/Model/Village.php +++ b/src/Model/Village.php @@ -80,10 +80,21 @@ class Village /* DB - Relations */ - public static function getBuildings(int $villageId): array + public static function getBuildings(int $villageId, bool $withEmpty = false): array { $buildings = DB::fetch(Building::class, 'select * from village_buildings where village_id=:id', ['id' => $villageId]); + if ($withEmpty) { + $nonBuiltBuildings = array_diff( + ['TownHall', 'Embassy', 'Marketplace', 'Storage', 'WoodCutter', 'ClayPit', 'IronMine', 'Farm'], + array_column($buildings, 'type'), + ); + + foreach ($nonBuiltBuildings as $type) { + $buildings[] = Building::getEmpty($villageId, $type); + } + } + return array_map(function (Building $building) { return $building->cast(); }, $buildings); @@ -121,30 +132,51 @@ class Village public const FETCH_UNIT_SUPPORT_AT_HOME = 3; public const FETCH_UNIT_RESIDENCE = 4; + public const RETURN_UNIT_EXISTING = 1; + public const RETURN_UNIT_ALL = 2; + public const RETURN_UNIT_TRAINABLE = 3; + public static function getUnit(string $unitType, int $flag): ?Unit { } /** - * @param int $flag + * @param int $fetchFlag * * @return array<int, Unit> */ - public static function getUnits(int $villageId, $flag = Village::FETCH_UNIT_ALL): array + public static function getUnits(int $villageId, int $fetchFlag = Village::FETCH_UNIT_ALL, int $returnFlag = Village::RETURN_UNIT_EXISTING): array { - if ($flag == Village::FETCH_UNIT_HOME_AT_HOME) { + if ($fetchFlag == Village::FETCH_UNIT_HOME_AT_HOME) { $units = DB::fetch(Unit::class, 'select * from village_units where home_village_id=:id and residence_village_id=:id', ['id' => $villageId]); } - else if ($flag == Village::FETCH_UNIT_HOME_AT_SUPPORT) { + else if ($fetchFlag == Village::FETCH_UNIT_HOME_AT_SUPPORT) { $units = DB::fetch(Unit::class, 'select * from village_units where home_village_id=:id and residence_village_id!=:id', ['id' => $villageId]); } - else if ($flag == Village::FETCH_UNIT_SUPPORT_AT_HOME) { + else if ($fetchFlag == Village::FETCH_UNIT_SUPPORT_AT_HOME) { $units = DB::fetch(Unit::class, 'select * from village_units where home_village_id!=:id and residence_village_id=:id', ['id' => $villageId]); } - else if ($flag == Village::FETCH_UNIT_RESIDENCE) { + else if ($fetchFlag == Village::FETCH_UNIT_RESIDENCE) { $units = DB::fetch(Unit::class, 'select * from village_units where residence_village_id=:id', ['id' => $villageId]); } + if ($returnFlag == Village::RETURN_UNIT_ALL || $returnFlag == Village::RETURN_UNIT_TRAINABLE) { + $nonExistingUnits = array_diff( + ['WoodCutter', 'PitWorker', 'Miner', 'Farmer', 'Merchant', 'Diplomat'], + array_column($units, 'type'), + ); + + foreach ($nonExistingUnits as $type) { + $units[] = Unit::getEmpty($villageId, $type); + } + + if ($returnFlag == Village::RETURN_UNIT_TRAINABLE) { + $units = array_filter($units, function (Unit $unit) { + return !! $unit->cast()->getBuilding(); + }); + } + } + return array_map(function (Unit $unit) { return $unit->cast(); }, $units); |