summaryrefslogtreecommitdiff
path: root/src/Controller/Village.php
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-10-04 11:32:04 +0200
committerDaniel Weipert <code@drogueronin.de>2023-10-04 11:32:04 +0200
commit94a3dd52da3ae180af37c6fd0e8c24b3562da388 (patch)
treeacced055660bfd65ca3e955ea26d412457ba4507 /src/Controller/Village.php
parentfa00b957378a393f8edbfc98ef111d35d18ecb09 (diff)
initial commit 2
Diffstat (limited to 'src/Controller/Village.php')
-rw-r--r--src/Controller/Village.php79
1 files changed, 76 insertions, 3 deletions
diff --git a/src/Controller/Village.php b/src/Controller/Village.php
index 3854d29..dfca298 100644
--- a/src/Controller/Village.php
+++ b/src/Controller/Village.php
@@ -3,9 +3,13 @@
namespace App\Controller;
use App\DB;
+use App\Model\Event\SendUnits;
+use App\Model\Event\TrainUnits;
use App\Model\Event\UpgradeBuilding;
use App\Model\Village as Model;
+use App\Router;
use App\View;
+use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
@@ -26,8 +30,9 @@ class Village
public function show(Request $request): Response
{
$village = Model::getByCoordinates($request->get('x'), $request->get('y'));
+ $events = [];
- $results = DB::query(
+ $eventsBuilding = DB::query(
<<<SQL
select events.*, village_buildings.type as building from events
join village_buildings
@@ -36,8 +41,7 @@ class Village
SQL, ['id' => $village->id, 'type' => 'UpgradeBuilding']
)->fetchAll();
- $events = [];
- foreach ($results as $row) {
+ foreach ($eventsBuilding as $row) {
$events[$row['type']][] = [
'event' => DB::convertToModel(UpgradeBuilding::class, $row),
'data' => [
@@ -46,9 +50,78 @@ class Village
];
}
+ $eventsUnits = DB::query(
+ <<<SQL
+ select * from events
+ where type=:type and village_id=:id
+ SQL, ['type' => 'TrainUnits', 'id' => $village->id]
+ )->fetchAll();
+
+ foreach ($eventsUnits as $row) {
+ $events[$row['type']][] = [
+ 'event' => DB::convertToModel(TrainUnits::class, $row),
+ 'data' => json_decode($row['payload'], true),
+ ];
+ }
+
+ $eventsUnitsSend = DB::query(
+ <<<SQL
+ select * from events
+ where type=:type and (village_id=:id or (payload->>'destination')::bigint=:id)
+ SQL, ['type' => 'SendUnits', 'id' => $village->id]
+ )->fetchAll();
+
+ foreach ($eventsUnitsSend as $row) {
+ $events[$row['type']][] = [
+ 'event' => DB::convertToModel(SendUnits::class, $row),
+ 'data' => json_decode($row['payload'], true),
+ ];
+ }
+
return new Response(View::render('village.twig', [
'village' => $village,
'events' => $events,
+ 'villages' => DB::fetch(Model::class, "select * from villages where id!=:id", ['id' => $village->id]),
]));
}
+
+ #[Route(path: '/village/{x}/{y}/storage/config', methods: ['POST'])]
+ public function storageConfig(Request $request): Response
+ {
+ $village = Model::getByCoordinates($request->get('x'), $request->get('y'));
+
+ // calculate to max 100%
+ $wood = intval($request->get('wood'));
+ $clay = intval($request->get('clay'));
+ $iron = intval($request->get('iron'));
+ $food = intval($request->get('food'));
+ $total = $wood + $clay + $iron + $food;
+ $woodPercent = $wood / $total;
+ $clayPercent = $clay / $total;
+ $ironPercent = $iron / $total;
+ $foodPercent = $food / $total;
+
+ $wood = round($woodPercent * 100);
+ $clay = round($clayPercent * 100);
+ $iron = round($ironPercent * 100);
+ $food = round($foodPercent * 100);
+ $newTotal = $wood+$clay+$iron+$food;
+ $food += (100 - $newTotal);
+
+ DB::query(
+ <<<SQL
+ update village_storage_config
+ set wood=:wood, clay=:clay, iron=:iron, food=:food
+ where village_id=:id
+ SQL,
+ ['wood' => $wood, 'clay' => $clay, 'iron' => $iron, 'food' => $food, 'id' => $village->id]
+ );
+
+ return new RedirectResponse(
+ Router::generate(
+ 'village.show',
+ ['x' => $request->get('x'), 'y' => $request->get('y')]
+ )
+ );
+ }
}