summaryrefslogtreecommitdiff
path: root/src/Controller/Event.php
blob: 1fd304dd3ba47cb22c708865f453ce4fd20c42c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php

namespace App\Controller;

use App\DB;
use App\Model\Event as Model;
use App\Model\Event\SendUnits;
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);

    /**@var SendUnits $sendUnitsEvent*/
    $sendUnitsEvent = DB::fetch(SendUnits::class, 'select * from events_send_units where event_id=:id', ['id' => $event->id])[0] ?? null;
    if (! empty($sendUnitsEvent)) {
      if ($sendUnitsEvent->type === 'SendBack') {
        $cancelTimeDiff = $event->createdAt->diff(new \DateTime());
        $cancelTime = (new \DateTime())->add($cancelTimeDiff);

        $sendUnitsEvent->isCanceled = true;
        $sendUnitsEvent->home = $sendUnitsEvent->destination;
        $sendUnitsEvent->residence = $sendUnitsEvent->source;

        DB::query(
          'update events set time=:time where id=:id',
          ['time' => $cancelTime->format('c'), 'id' => $request->get('id')]
        );
        DB::query(
          'update events_send_units set is_canceled=:is_canceled, home=:home, residence=:residence where id=:id',
          ['is_canceled' => $sendUnitsEvent->isCanceled, 'home' => $sendUnitsEvent->home, 'residence' => $sendUnitsEvent->residence, 'id' => $sendUnitsEvent->id]
        );
      }

      else if ($sendUnitsEvent->type === 'Recall') {
        $cancelTimeDiff = $event->createdAt->diff(new \DateTime());
        $cancelTime = (new \DateTime())->add($cancelTimeDiff);

        $sendUnitsEvent->isCanceled = true;
        $sendUnitsEvent->home = $sendUnitsEvent->destination;
        $sendUnitsEvent->residence = $sendUnitsEvent->source;

        DB::query(
          'update events set time=:time where id=:id',
          ['time' => $cancelTime->format('c'), 'id' => $request->get('id')]
        );
        DB::query(
          'update events_send_units set is_canceled=:is_canceled, home=:home, residence=:residence where id=:id',
          ['is_canceled' => $sendUnitsEvent->isCanceled, 'home' => $sendUnitsEvent->home, 'residence' => $sendUnitsEvent->residence, 'id' => $sendUnitsEvent->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]
      )
    );
  }
}