From 3afcaef927391db23fe23c6c8c26b8960e8dae32 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 29 Nov 2023 09:35:27 +0100 Subject: intermediate commit --- src/Model/Building.php | 10 +++++ src/Model/Building/ClayPit.php | 2 + src/Model/Building/Embassy.php | 17 ++++++++ src/Model/Building/Farm.php | 2 + src/Model/Building/IronMine.php | 2 + src/Model/Building/Marketplace.php | 17 ++++++++ src/Model/Building/Storage.php | 4 +- src/Model/Building/WoodCutter.php | 4 +- src/Model/Event.php | 3 ++ src/Model/Event/BaseEvent.php | 14 ++++++ src/Model/Event/SendUnits.php | 87 ++++++++++++++++++++++++++++--------- src/Model/Event/UpgradeBuilding.php | 32 +++++++++++--- src/Model/Unit.php | 30 ++++++++++++- src/Model/Unit/Diplomat.php | 18 ++++++++ src/Model/Unit/Merchant.php | 18 ++++++++ src/Model/Village.php | 46 +++++++++++++++++--- 16 files changed, 269 insertions(+), 37 deletions(-) create mode 100644 src/Model/Building/Embassy.php create mode 100644 src/Model/Building/Marketplace.php create mode 100644 src/Model/Event/BaseEvent.php create mode 100644 src/Model/Unit/Diplomat.php create mode 100644 src/Model/Unit/Merchant.php (limited to 'src/Model') 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 @@ + 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 @@ + 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 @@ +payload, true); - if ($payload['type'] === 'Recall' || $payload['type'] === 'SendBack') { - DB::query( - << $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( - << $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( + << $payload['amount'], 'type' => $payload['unit'], 'id' => $payload['destination']] + ); + } + + /** + * @param array $payload + */ + private function borrow(array $payload): void + { + DB::query( + << $payload['amount'], 'type' => $payload['unit'], 'home' => $payload['source'], 'residence' => $payload['destination']] + ); + } + + /** + * @param array $payload + */ + private function gift(array $payload): void + { + DB::query( + << $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( + << $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 @@ + 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 @@ + 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 */ - 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); -- cgit v1.2.3