diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-11-29 09:35:27 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-11-29 09:35:27 +0100 |
commit | 3afcaef927391db23fe23c6c8c26b8960e8dae32 (patch) | |
tree | 143b9f6df9e8c795c8c6ed901bffdc7119f40df1 /src/Controller/Event.php | |
parent | c4ce3e884a6aa527bcc138771617215cf03265a4 (diff) |
intermediate commit
Diffstat (limited to 'src/Controller/Event.php')
-rw-r--r-- | src/Controller/Event.php | 65 |
1 files changed, 65 insertions, 0 deletions
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 @@ +<?php + +namespace App\Controller; + +use App\DB; +use App\Model\Event as Model; +use App\Model\Village; +use App\Router; +use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Annotation\Route; + +class Event +{ + #[Route(path: '/village/{x}/{y}/send-resources', methods: ['POST'])] + public function sendResources(Request $request): Response + { + return new RedirectResponse( + Router::generate( + 'village.show', + ['x' => $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] + ) + ); + } +} |