summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-01-08 15:10:10 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-01-08 15:10:10 +0100
commit82875448c485d26375ed6dea4e64e940f6e10f74 (patch)
treeff2580447429309824e7d64401ad75e7f756e45e /src
parentb21316248572cb27ed1f504529ad6680a473022e (diff)
gemini
Diffstat (limited to 'src')
-rw-r--r--src/Model/Building.php16
-rw-r--r--src/Model/Building/ClayPit.php7
-rw-r--r--src/Model/Building/Embassy.php7
-rw-r--r--src/Model/Building/Farm.php9
-rw-r--r--src/Model/Building/IronMine.php7
-rw-r--r--src/Model/Building/Marketplace.php7
-rw-r--r--src/Model/Building/Storage.php7
-rw-r--r--src/Model/Building/TownHall.php7
-rw-r--r--src/Model/Building/WoodCutter.php7
-rw-r--r--src/Model/Unit.php43
-rw-r--r--src/Model/Unit/Diplomat.php9
-rw-r--r--src/Model/Unit/Farmer.php9
-rw-r--r--src/Model/Unit/Merchant.php9
-rw-r--r--src/Model/Unit/Miner.php9
-rw-r--r--src/Model/Unit/PitWorker.php9
-rw-r--r--src/Model/Unit/WoodCutter.php9
-rw-r--r--src/gemini/Controller/Unit.php94
-rw-r--r--src/gemini/Controller/Village.php2
-rw-r--r--src/gemini/Gemini.php26
-rw-r--r--src/http/Controller/Village.php2
20 files changed, 253 insertions, 42 deletions
diff --git a/src/Model/Building.php b/src/Model/Building.php
index eefb4df..e42af03 100644
--- a/src/Model/Building.php
+++ b/src/Model/Building.php
@@ -18,7 +18,8 @@ class Building
public string $unitType;
public int $buildTimeFactor;
- public array $resourceRequirements;
+ public array $resourceRequirementsBase;
+ public array $resourceRequirementsFactor;
public array $buildingRequirements;
public array $techRequirements;
public int $maxLevel;
@@ -104,10 +105,15 @@ class Building
*/
public function getResourceRequirementsForLevel(int $level): array
{
- return array_map(
- fn ($resourceRequirement) => ceil(log($level * 2) * $resourceRequirement * $_ENV['BASE_BUILDING_RESOURCE_REQUIREMENT_FACTOR'] * $level),
- $this->resourceRequirements
- );
+ $resourceRequirements = [];
+ foreach (['wood', 'clay', 'iron'] as $resourceType) {
+ $base = $this->resourceRequirementsBase[$resourceType];
+ $factor = $this->resourceRequirementsFactor[$resourceType];
+
+ $resourceRequirements[$resourceType] = $base + ceil(log($level * 2) * $factor * $_ENV['BASE_BUILDING_RESOURCE_REQUIREMENT_FACTOR'] * $level);
+ }
+
+ return $resourceRequirements;
}
diff --git a/src/Model/Building/ClayPit.php b/src/Model/Building/ClayPit.php
index b4905eb..dfead72 100644
--- a/src/Model/Building/ClayPit.php
+++ b/src/Model/Building/ClayPit.php
@@ -8,7 +8,12 @@ class ClayPit extends ResourceGenerator
public int $buildTimeFactor = 1;
public int $maxLevel = 25;
- public array $resourceRequirements = [
+ public array $resourceRequirementsBase = [
+ 'wood' => 100,
+ 'clay' => 200,
+ 'iron' => 100,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 1.0,
'clay' => 2.0,
'iron' => 1.0,
diff --git a/src/Model/Building/Embassy.php b/src/Model/Building/Embassy.php
index 3be1f7f..a8a1ec4 100644
--- a/src/Model/Building/Embassy.php
+++ b/src/Model/Building/Embassy.php
@@ -9,7 +9,12 @@ class Embassy extends Building
public int $buildTimeFactor = 1;
public int $maxLevel = 25;
- public array $resourceRequirements = [
+ public array $resourceRequirementsBase = [
+ 'wood' => 2500,
+ 'clay' => 2500,
+ 'iron' => 3000,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 25.0,
'clay' => 25.0,
'iron' => 30.0,
diff --git a/src/Model/Building/Farm.php b/src/Model/Building/Farm.php
index 222d8cd..1a69c89 100644
--- a/src/Model/Building/Farm.php
+++ b/src/Model/Building/Farm.php
@@ -11,7 +11,12 @@ class Farm extends ResourceGenerator
public int $buildTimeFactor = 1;
public int $maxLevel = 25;
- public array $resourceRequirements = [
+ public array $resourceRequirementsBase = [
+ 'wood' => 100,
+ 'clay' => 100,
+ 'iron' => 100,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 1.0,
'clay' => 1.0,
'iron' => 1.0,
@@ -22,7 +27,7 @@ class Farm extends ResourceGenerator
public function getResourceIncrementor(): int
{
$populationDemand = array_reduce(
- Village::getUnits($this->villageId, Village::FETCH_UNIT_RESIDENCE),
+ Village::getUnits($this->villageId, Village::FETCH_UNIT_RESIDENCE, Village::RETURN_UNIT_EXISTING),
function ($carry, Unit $unit) {
return $carry + $unit->getPopulationDemand();
}
diff --git a/src/Model/Building/IronMine.php b/src/Model/Building/IronMine.php
index d240e3a..8e70ee0 100644
--- a/src/Model/Building/IronMine.php
+++ b/src/Model/Building/IronMine.php
@@ -8,7 +8,12 @@ class IronMine extends ResourceGenerator
public int $buildTimeFactor = 1;
public int $maxLevel = 25;
- public array $resourceRequirements = [
+ public array $resourceRequirementsBase = [
+ 'wood' => 100,
+ 'clay' => 100,
+ 'iron' => 200,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 1.0,
'clay' => 1.0,
'iron' => 2.0,
diff --git a/src/Model/Building/Marketplace.php b/src/Model/Building/Marketplace.php
index 714de0d..2d87379 100644
--- a/src/Model/Building/Marketplace.php
+++ b/src/Model/Building/Marketplace.php
@@ -9,7 +9,12 @@ class Marketplace extends Building
public int $buildTimeFactor = 1;
public int $maxLevel = 25;
- public array $resourceRequirements = [
+ public array $resourceRequirementsBase = [
+ 'wood' => 1000,
+ 'clay' => 1000,
+ 'iron' => 800,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 10.0,
'clay' => 10.0,
'iron' => 8.0,
diff --git a/src/Model/Building/Storage.php b/src/Model/Building/Storage.php
index de2df92..d75b80d 100644
--- a/src/Model/Building/Storage.php
+++ b/src/Model/Building/Storage.php
@@ -10,7 +10,12 @@ class Storage extends Building
public int $buildTimeFactor = 1;
public int $maxLevel = 25;
- public array $resourceRequirements = [
+ public array $resourceRequirementsBase = [
+ 'wood' => 250,
+ 'clay' => 250,
+ 'iron' => 250,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 1.0,
'clay' => 1.0,
'iron' => 1.0,
diff --git a/src/Model/Building/TownHall.php b/src/Model/Building/TownHall.php
index 608f083..33fd105 100644
--- a/src/Model/Building/TownHall.php
+++ b/src/Model/Building/TownHall.php
@@ -9,7 +9,12 @@ class TownHall extends Building
public int $buildTimeFactor = 1;
public int $maxLevel = 25;
- public array $resourceRequirements = [
+ public array $resourceRequirementsBase = [
+ 'wood' => 250,
+ 'clay' => 250,
+ 'iron' => 250,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 1.0,
'clay' => 1.0,
'iron' => 1.0,
diff --git a/src/Model/Building/WoodCutter.php b/src/Model/Building/WoodCutter.php
index 726cdbc..6e1ce8c 100644
--- a/src/Model/Building/WoodCutter.php
+++ b/src/Model/Building/WoodCutter.php
@@ -8,7 +8,12 @@ class WoodCutter extends ResourceGenerator
public int $buildTimeFactor = 1;
public int $maxLevel = 25;
- public array $resourceRequirements = [
+ public array $resourceRequirementsBase = [
+ 'wood' => 200,
+ 'clay' => 100,
+ 'iron' => 100,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 2.0,
'clay' => 1.0,
'iron' => 1.0,
diff --git a/src/Model/Unit.php b/src/Model/Unit.php
index d563682..206f5dd 100644
--- a/src/Model/Unit.php
+++ b/src/Model/Unit.php
@@ -22,7 +22,8 @@ class Unit
public string $buildingType;
public int $travelTime;
public int $populationDemandFactor;
- public array $resourceRequirements = [];
+ public array $resourceRequirementsBase = [];
+ public array $resourceRequirementsFactor = [];
public static function getEmpty(int $villageId, string $unitType): Unit
@@ -75,27 +76,29 @@ class Unit
['type' => $unit->type, 'id' => $unit->homeVillageId]
)->fetchColumn();
-
- return array_map(
- 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']
+ $resourceRequirements = [];
+ foreach (['wood', 'clay', 'iron', 'food'] as $resourceType) {
+ $base = $unit->resourceRequirementsBase[$resourceType];
+ $factor = $unit->resourceRequirementsFactor[$resourceType];
+
+ $r = $base;
+ for ($i = 0; $i <= $amount; $i++) {
+ $r += ceil(
+ (
+ pow(
+ $_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_BASE'],
+ $currentAmount + 1
)
+ * $factor * $_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_FACTOR']
+ )
/ ($building->level ?? 1)
- );
- }
+ );
+ }
- return $r;
- },
- $unit->resourceRequirements
- );
+ $resourceRequirements[$resourceType] = $r;
+ }
+
+ return $resourceRequirements;
}
public function getPopulationDemand(): int
@@ -144,7 +147,7 @@ class Unit
public static function getAmountResiding(string $unitType, int $villageId): int
{
$statement = DB::query(
- 'select SUM(amount) from village_units where type=:type and residence_village_id=:id',
+ 'select SUM(amount) from village_units where type=:type and residence_village_id=:id and is_traveling=false',
['type' => $unitType, 'id' => $villageId]
);
$result = $statement->fetch()['sum'];
diff --git a/src/Model/Unit/Diplomat.php b/src/Model/Unit/Diplomat.php
index e263cd3..baee88a 100644
--- a/src/Model/Unit/Diplomat.php
+++ b/src/Model/Unit/Diplomat.php
@@ -9,7 +9,14 @@ class Diplomat extends Unit
public string $buildingType = 'Embassy';
public int $travelTime = 1;
public int $populationDemandFactor = 1;
- public array $resourceRequirements = [
+
+ public array $resourceRequirementsBase = [
+ 'wood' => 500,
+ 'clay' => 500,
+ 'iron' => 500,
+ 'food' => 500,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 5.0,
'clay' => 5.0,
'iron' => 5.0,
diff --git a/src/Model/Unit/Farmer.php b/src/Model/Unit/Farmer.php
index 8e3c1ea..5256bf1 100644
--- a/src/Model/Unit/Farmer.php
+++ b/src/Model/Unit/Farmer.php
@@ -9,7 +9,14 @@ class Farmer extends Unit
public string $buildingType = 'Farm';
public int $travelTime = 1;
public int $populationDemandFactor = 1;
- public array $resourceRequirements = [
+
+ public array $resourceRequirementsBase = [
+ 'wood' => 100,
+ 'clay' => 100,
+ 'iron' => 100,
+ 'food' => 0,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 1.0,
'clay' => 1.0,
'iron' => 1.0,
diff --git a/src/Model/Unit/Merchant.php b/src/Model/Unit/Merchant.php
index 61e2090..8958d27 100644
--- a/src/Model/Unit/Merchant.php
+++ b/src/Model/Unit/Merchant.php
@@ -9,7 +9,14 @@ class Merchant extends Unit
public string $buildingType = 'Marketplace';
public int $travelTime = 1;
public int $populationDemandFactor = 1;
- public array $resourceRequirements = [
+
+ public array $resourceRequirementsBase = [
+ 'wood' => 200,
+ 'clay' => 200,
+ 'iron' => 200,
+ 'food' => 200,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 2.0,
'clay' => 2.0,
'iron' => 2.0,
diff --git a/src/Model/Unit/Miner.php b/src/Model/Unit/Miner.php
index 2d246ef..06c6dda 100644
--- a/src/Model/Unit/Miner.php
+++ b/src/Model/Unit/Miner.php
@@ -9,7 +9,14 @@ class Miner extends Unit
public string $buildingType = 'IronMine';
public int $travelTime = 1;
public int $populationDemandFactor = 1;
- public array $resourceRequirements = [
+
+ public array $resourceRequirementsBase = [
+ 'wood' => 100,
+ 'clay' => 100,
+ 'iron' => 200,
+ 'food' => 200,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 1.0,
'clay' => 1.0,
'iron' => 2.0,
diff --git a/src/Model/Unit/PitWorker.php b/src/Model/Unit/PitWorker.php
index d2a52c3..f2a27b6 100644
--- a/src/Model/Unit/PitWorker.php
+++ b/src/Model/Unit/PitWorker.php
@@ -9,7 +9,14 @@ class PitWorker extends Unit
public string $buildingType = 'ClayPit';
public int $travelTime = 1;
public int $populationDemandFactor = 1;
- public array $resourceRequirements = [
+
+ public array $resourceRequirementsBase = [
+ 'wood' => 100,
+ 'clay' => 200,
+ 'iron' => 100,
+ 'food' => 200,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 1.0,
'clay' => 2.0,
'iron' => 1.0,
diff --git a/src/Model/Unit/WoodCutter.php b/src/Model/Unit/WoodCutter.php
index d8c2c42..02aea85 100644
--- a/src/Model/Unit/WoodCutter.php
+++ b/src/Model/Unit/WoodCutter.php
@@ -9,7 +9,14 @@ class WoodCutter extends Unit
public string $buildingType = 'WoodCutter';
public int $travelTime = 1;
public int $populationDemandFactor = 1;
- public array $resourceRequirements = [
+
+ public array $resourceRequirementsBase = [
+ 'wood' => 200,
+ 'clay' => 100,
+ 'iron' => 100,
+ 'food' => 200,
+ ];
+ public array $resourceRequirementsFactor = [
'wood' => 2.0,
'clay' => 1.0,
'iron' => 1.0,
diff --git a/src/gemini/Controller/Unit.php b/src/gemini/Controller/Unit.php
index c04079a..9aa6968 100644
--- a/src/gemini/Controller/Unit.php
+++ b/src/gemini/Controller/Unit.php
@@ -2,10 +2,13 @@
namespace App\gemini\Controller;
+use App\DB;
use App\Model\Event;
+use App\Model\Event\SendUnits;
use App\Model\Event\TrainUnits;
use App\Model\Unit as Model;
use App\Model\Village;
+use App\View;
use GeminiFoundation\Request;
use GeminiFoundation\Response;
use GeminiFoundation\Status;
@@ -57,4 +60,95 @@ class Unit
meta: "/village/{$village->x}/{$village->y}"
);
}
+
+ // #[Route(path: '/village/{x}/{y}/send-units', methods: ['POST'])]
+ public function sendUnits(Request $request): Response
+ {
+ $village = Village::getByCoordinates($request->get('x'), $request->get('y'));
+
+ $selectedUnit = $request->get('selectedUnit');
+ if (empty($selectedUnit)) {
+ return new Response(body: View::render('send-units/01-units.twig', [
+ 'village' => $village,
+ ]));
+ }
+
+ if (empty($request->get('selectedVillageX'))) {
+ return new Response(body: View::render('send-units/02-villages.twig', [
+ 'village' => $village,
+ 'villages' => DB::fetch(Village::class, "select * from villages where id!=:id", ['id' => $village->id]),
+ 'selectedUnit' => $selectedUnit,
+ ]));
+ }
+ $selectedVillage = Village::getByCoordinates($request->get('selectedVillageX'), $request->get('selectedVillageY'));
+
+ $selectedAction = $request->get('selectedAction');
+ if (empty($selectedAction)) {
+ return new Response(body: View::render('send-units/03-actions.twig', [
+ 'village' => $village,
+ 'selectedUnit' => $selectedUnit,
+ 'selectedVillage' => $selectedVillage,
+ ]));
+ }
+
+ $amount = intval($request->get('input'));
+ if (empty($amount)) {
+ return new Response(statusCode: Status::INPUT, meta: 'Amount');
+ }
+
+ $destination = $selectedVillage;
+
+ /**@var Model $unit*/
+ $unit = new (Model::resolveType($selectedUnit))();
+
+ $amountUnits = DB::query(
+ 'select amount from village_units where home_village_id=:home and residence_village_id=:home and type=:type',
+ ['home' => $village->id, 'type' => $selectedUnit]
+ )->fetchColumn();
+
+ if ($amountUnits - $amount > 0) {
+ $statement = DB::query(
+ <<<SQL
+ update village_units set amount=:amount where home_village_id=:home and residence_village_id=:home and type=:type
+ SQL,
+ ['amount' => $amountUnits - $amount, 'home' => $village->id, 'type' => $selectedUnit]
+ );
+ } else if ($amountUnits - $amount === 0) {
+ DB::query(
+ <<<SQL
+ delete from village_units where home_village_id=:home and residence_village_id=:home and type=:type
+ SQL,
+ ['home' => $village->id, 'type' => $selectedUnit]
+ );
+ }
+
+ DB::query(
+ 'insert into village_units (amount, type, home_village_id, residence_village_id, is_traveling) values (:amount, :type, :home, :home, true)',
+ ['amount' => $amount, 'type' => $selectedUnit, 'home' => $village->id]
+ );
+
+ // event
+ $event = new Event();
+ $event->time = (new \DateTime())->add(
+ \DateInterval::createFromDateString(
+ Model::getTravelTime($unit, Village::getDistance($village->x, $village->y, $destination->x, $destination->y))
+ . ' seconds'
+ )
+ );
+ $event->villageId = $village->id;
+ $sendUnitsEvent = new SendUnits();
+ $sendUnitsEvent->event = $event;
+ $sendUnitsEvent->type = $selectedAction;
+ $sendUnitsEvent->unit = $selectedUnit;
+ $sendUnitsEvent->amount = $amount;
+ $sendUnitsEvent->source = $village->id;
+ $sendUnitsEvent->destination = $destination->id;
+ $sendUnitsEvent->dbInsert();
+
+
+ return new Response(
+ statusCode: Status::REDIRECT_TEMPORARY,
+ meta: "/village/{$village->x}/{$village->y}"
+ );
+ }
}
diff --git a/src/gemini/Controller/Village.php b/src/gemini/Controller/Village.php
index 9b27561..85b9205 100644
--- a/src/gemini/Controller/Village.php
+++ b/src/gemini/Controller/Village.php
@@ -21,7 +21,7 @@ class Village
$villages = DB::fetch(
Model::class,
<<<SQL
- select * from villages
+ select *,villages.id from villages
join user_villages on villages.id = user_villages.village_id
where user_villages.user_id=:id
SQL,
diff --git a/src/gemini/Gemini.php b/src/gemini/Gemini.php
index 41b8716..507b962 100644
--- a/src/gemini/Gemini.php
+++ b/src/gemini/Gemini.php
@@ -103,6 +103,32 @@ class Gemini
$response = $unitController->train($request);
}
+ else if (preg_match('@village/(\d+)/(\d+)/send-units@', $request->getPath(), $routeMatch)) {
+ $request
+ ->set('x', $routeMatch[1])
+ ->set('y', $routeMatch[2]);
+
+ if (preg_match('@village/(\d+)/(\d+)/send-units/type/(\w+)$@', $request->getPath(), $routeMatch)) {
+ $request->set('selectedUnit', $routeMatch[3]);
+ }
+ else if (preg_match('@village/(\d+)/(\d+)/send-units/type/(\w+)/village/(\d+)/(\d+)$@', $request->getPath(), $routeMatch)) {
+ $request
+ ->set('selectedUnit', $routeMatch[3])
+ ->set('selectedVillageX', $routeMatch[4])
+ ->set('selectedVillageY', $routeMatch[5]);
+ }
+ else if (preg_match('@village/(\d+)/(\d+)/send-units/type/(\w+)/village/(\d+)/(\d+)/action/(\w+)$@', $request->getPath(), $routeMatch)) {
+ $request
+ ->set('selectedUnit', $routeMatch[3])
+ ->set('selectedVillageX', $routeMatch[4])
+ ->set('selectedVillageY', $routeMatch[5])
+ ->set('selectedAction', $routeMatch[6]);
+ }
+
+ $unitController = new Unit();
+ $response = $unitController->sendUnits($request);
+ }
+
else if (preg_match('@village/(\d+)/(\d+)@', $request->getPath(), $routeMatch)) {
$request
->set('x', $routeMatch[1])
diff --git a/src/http/Controller/Village.php b/src/http/Controller/Village.php
index cd38442..7b1f227 100644
--- a/src/http/Controller/Village.php
+++ b/src/http/Controller/Village.php
@@ -23,7 +23,7 @@ class Village
$villages = DB::fetch(
Model::class,
<<<SQL
- select * from villages
+ select *,villages.id from villages
join user_villages on villages.id = user_villages.village_id
where user_villages.user_id=:id
SQL,