From 3afcaef927391db23fe23c6c8c26b8960e8dae32 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 29 Nov 2023 09:35:27 +0100 Subject: intermediate commit --- .env.example | 1 + .gitignore | 1 + bin/db.php | 160 ++++++++++++++++++++++++++++++++++++ bin/villages.php | 147 --------------------------------- public/assets/img/map.jpg | Bin 0 -> 1149770 bytes public/assets/style.css | 82 ++++++++++++++++++ src/Controller/Building.php | 8 +- src/Controller/Event.php | 65 +++++++++++++++ src/Controller/Unit.php | 2 +- src/Controller/Village.php | 27 ++++-- src/DB.php | 2 +- src/EventRunner.php | 9 ++ src/Model.php | 2 +- 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 +++++++++-- views/components/timer.twig | 3 +- views/map.twig | 28 +++++-- views/village.twig | 52 +++++++++--- views/villages.twig | 4 +- 33 files changed, 675 insertions(+), 224 deletions(-) create mode 100644 bin/db.php delete mode 100644 bin/villages.php create mode 100644 public/assets/img/map.jpg create mode 100644 src/Controller/Event.php 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 diff --git a/.env.example b/.env.example index 072c25f..6cd359f 100644 --- a/.env.example +++ b/.env.example @@ -10,3 +10,4 @@ BASE_UNIT_BUILD_TIME_FACTOR=256 BASE_UNIT_TRAVEL_TIME_FACTOR=60 BASE_UNIT_RESOURCE_REQUIREMENT_FACTOR=64 BASE_RESOURCE_GENERATION_FACTOR=128 +BASE_STORAGE_CAPACITY_FACTOR=2048 diff --git a/.gitignore b/.gitignore index 02166b7..c87432b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .env +config.yml /vendor/ diff --git a/bin/db.php b/bin/db.php new file mode 100644 index 0000000..951c0b3 --- /dev/null +++ b/bin/db.php @@ -0,0 +1,160 @@ +load(dirname(__DIR__) . '/.env'); + +DB::init(); + +DB::query(<<load(dirname(__DIR__) . '/.env'); - -DB::init(); - -DB::query(<<get('x'), $request->get('y')); - $building = Model::getByVillage($village->id, $request->get('type')); + $building = Model::getByVillage($village->id, $request->get('type')) ?? Model::getEmpty($village->id, $request->get('type')); // resources foreach ($building->getResourceRequirements() as $resourceType => $resourceValue) { @@ -27,11 +28,12 @@ class Building $village->updateResources(); // event - $event = new Event(); + $event = new UpgradeBuilding(); $event->type = 'UpgradeBuilding'; $event->time = (new \DateTime())->add(\DateInterval::createFromDateString($building->getBuildTime() . ' seconds')); $event->payload = json_encode([ - 'id' => $building->id, + 'type' => $building->type, + 'village_id' => $building->villageId, ]); DB::query( diff --git a/src/Controller/Event.php b/src/Controller/Event.php new file mode 100644 index 0000000..a6538fe --- /dev/null +++ b/src/Controller/Event.php @@ -0,0 +1,65 @@ + $request->get('x'), 'y' => $request->get('y')] + ) + ); + } + + #[Route(path: '/event/{id}/cancel', methods: ['POST'])] + public function cancel(Request $request): Response + { + $event = DB::fetch(Model::class, 'select * from events where id=:id', ['id' => $request->get('id')])[0] ?? null; + $village = Village::get($event->villageId); + + if ($event->type === 'SendUnits') { + $payload = json_decode($event->payload, true); + + if ($payload['type'] === 'SendBack') { + $cancelTimeDiff = $event->createdAt->diff(new \DateTime()); + $cancelTime = (new \DateTime())->add($cancelTimeDiff); + + $cancelPayload = array_replace_recursive($payload, [ + 'cancel' => [ + 'home' => $payload['destination'], + 'residence' => $payload['source'], + ], + ]); + + DB::query( + 'update events set time=:time, payload=:payload where id=:id', + ['time' => $cancelTime->format('c'), 'payload' => json_encode($cancelPayload), 'id' => $request->get('id')] + ); + } + } + + else { + DB::query('delete from event where id=:id', ['id' => $request->get('id')]); + } + + return new RedirectResponse( + Router::generate( + 'village.show', + ['x' => $village->x, 'y' => $village->y] + ) + ); + } +} diff --git a/src/Controller/Unit.php b/src/Controller/Unit.php index 602c6e8..c57ff01 100644 --- a/src/Controller/Unit.php +++ b/src/Controller/Unit.php @@ -226,7 +226,7 @@ class Unit ) ); $event->payload = json_encode([ - 'type' => 'Borrow', + 'type' => $request->get('type'), 'unit' => $request->get('unit'), 'amount' => $amount, 'source' => $village->id, diff --git a/src/Controller/Village.php b/src/Controller/Village.php index dfca298..59e6d4b 100644 --- a/src/Controller/Village.php +++ b/src/Controller/Village.php @@ -34,9 +34,7 @@ class Village $eventsBuilding = DB::query( <<'id')::bigint + select * from events where events.village_id=:id and events.type=:type SQL, ['id' => $village->id, 'type' => 'UpgradeBuilding'] )->fetchAll(); @@ -44,9 +42,7 @@ class Village foreach ($eventsBuilding as $row) { $events[$row['type']][] = [ 'event' => DB::convertToModel(UpgradeBuilding::class, $row), - 'data' => [ - 'building' => $row['building'], - ], + 'data' => json_decode($row['payload']), ]; } @@ -64,23 +60,36 @@ class Village ]; } - $eventsUnitsSend = DB::query( + $eventsUnitsSendOwn = DB::query( <<>'destination')::bigint=:id) + where type=:type and village_id=:id SQL, ['type' => 'SendUnits', 'id' => $village->id] )->fetchAll(); - foreach ($eventsUnitsSend as $row) { + $eventsUnitsSendOther = DB::query( + <<>'destination')::bigint=:id and (payload->>'cancel') is null + SQL, ['type' => 'SendUnits', 'id' => $village->id] + )->fetchAll(); + + foreach ([...$eventsUnitsSendOwn, ...$eventsUnitsSendOther] as $row) { $events[$row['type']][] = [ 'event' => DB::convertToModel(SendUnits::class, $row), 'data' => json_decode($row['payload'], true), ]; } + $buildings = []; + foreach (Model::getBuildings($village->id, true) as $building) { + $buildings[$building->type] = $building; + } + return new Response(View::render('village.twig', [ 'village' => $village, 'events' => $events, + 'buildings' => $buildings, 'villages' => DB::fetch(Model::class, "select * from villages where id!=:id", ['id' => $village->id]), ])); } diff --git a/src/DB.php b/src/DB.php index 4e34d32..82a81c4 100644 --- a/src/DB.php +++ b/src/DB.php @@ -3,7 +3,7 @@ namespace App; class DB { - private static \PDO $connection; + public static \PDO $connection; public static function init(): void { diff --git a/src/EventRunner.php b/src/EventRunner.php index 4f3bce9..a071d32 100644 --- a/src/EventRunner.php +++ b/src/EventRunner.php @@ -31,6 +31,15 @@ class EventRunner ); } + # Upgrade Building + + $results = DB::query(<<fetchAll(); + // Resources diff --git a/src/Model.php b/src/Model.php index 5f45ed4..2720486 100644 --- a/src/Model.php +++ b/src/Model.php @@ -9,7 +9,7 @@ class Model $object = new $cast(); foreach (get_class_vars(get_class($original)) as $property => $_) { - if (! empty($original->$property) && empty($object->$property)) { + if (isset($original->$property) && ! isset($object->$property)) { $object->$property = $original->$property; } } 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); diff --git a/views/components/timer.twig b/views/components/timer.twig index 97977da..ccb31a7 100644 --- a/views/components/timer.twig +++ b/views/components/timer.twig @@ -9,8 +9,9 @@ document.addEventListener('DOMContentLoaded', function (ev) { const interval = setInterval(setTime, 1000); function setTime() { let diff = time - new Date(); - if (diff <= -1) { + if (diff <= 0) { clearInterval(interval); + window.location.reload(); } const hh = Math.floor(diff/1000/60/60); diff --git a/views/map.twig b/views/map.twig index 43f1a5b..29f0294 100644 --- a/views/map.twig +++ b/views/map.twig @@ -3,30 +3,40 @@ {% block main %}
- Up + + +
{% for row in range(-range, range) %} {% for column in range(-range, range) %} {% set village = map[x + column][y + row] %} - +
+ {% if village %} + + {{ map[x + column][y + row].name }} + + {% endif %} +
{% endfor %} {% endfor %}
{% endblock %} diff --git a/views/village.twig b/views/village.twig index ea6635c..b843ef4 100644 --- a/views/village.twig +++ b/views/village.twig @@ -49,7 +49,7 @@

Storage Config