summaryrefslogtreecommitdiff
path: root/src/http/Controller/Event.php
blob: 264fa26137fde4449c0b209987500f0737e090f5 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

namespace App\http\Controller;

use App\DB;
use App\Model\Event as Model;
use App\Model\Event\SendResources;
use App\Model\Event\SendUnits;
use App\Model\Event\TrainUnits;
use App\Model\Event\UpgradeBuilding;
use App\Model\Unit;
use App\Model\Unit\MailCarrier;
use App\Model\Village;
use App\View;
use App\http\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
  {
    $village = Village::getByCoordinates($request->get('x'), $request->get('y'));

    $totalAmount = $request->get('wood') + $request->get('clay') + $request->get('iron') + $request->get('food');

    $resourceCapabilities = MailCarrier::getResourceCapabilities($village);
    $necessaryMailCarriers = ceil($totalAmount / $resourceCapabilities);
    $mailCarriers = DB::fetch(MailCarrier::class, 'select sum(amount) as amount from village_units where type=:type and residence_village_id=:villageId and is_traveling=false', ['villageId' => $village->id, 'type' => 'MailCarrier'])[0]->amount ?? 0;
    if ($mailCarriers === 0 || $totalAmount > $resourceCapabilities) {
      return new Response(View::render('error.twig', ['message' => 'Insufficient Mail Carriers']), 403);
    }

    $destination = Village::get($request->get('village'));

    // event
    $event = new Model();
    $event->time = (new \DateTime())->add(
      \DateInterval::createFromDateString(
        Unit::getTravelTime(new MailCarrier(), Village::getDistance($village->x, $village->y, $destination->x, $destination->y))
        . ' seconds'
      )
    );
    $event->villageId = $village->id;
    $sendResourcesEvent = new SendResources();
    $sendResourcesEvent->event = $event;
    $sendResourcesEvent->wood = $request->get('wood');
    $sendResourcesEvent->clay = $request->get('clay');
    $sendResourcesEvent->iron = $request->get('iron');
    $sendResourcesEvent->food = $request->get('food');
    $sendResourcesEvent->source = $village->id;
    $sendResourcesEvent->destination = $destination->id;
    $sendResourcesEvent->dbInsert();


    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 UpgradeBuilding $upgradeBuildingEvent*/
    $upgradeBuildingEvent = DB::fetch(UpgradeBuilding::class, 'select * from events_upgrade_building where event_id=:id', ['id' => $event->id])[0] ?? null;
    /**@var TrainUnits $trainUnitsEvent*/
    $trainUnitsEvent = DB::fetch(TrainUnits::class, 'select * from events_train_units where event_id=:id', ['id' => $event->id])[0] ?? null;
    /**@var SendUnits $sendUnitsEvent*/
    $sendUnitsEvent = DB::fetch(SendUnits::class, 'select * from events_send_units where event_id=:id', ['id' => $event->id])[0] ?? null;
    /**@var SendUnits $sendUnitsEvent*/
    $sendResourcesEvent = DB::fetch(SendResources::class, 'select * from events_send_resources where event_id=:id', ['id' => $event->id])[0] ?? null;

    if (! empty($upgradeBuildingEvent)) {
      $upgradeBuildingEvent->cancel();
    }

    else if (! empty($trainUnitsEvent)) {
      $trainUnitsEvent->cancel();
    }

    else if (! empty($sendUnitsEvent)) {
      $sendUnitsEvent->cancel();
    }

    else if (! empty($sendResourcesEvent)) {
      $sendResourcesEvent->cancel();
    }


    return new RedirectResponse(
      Router::generate(
        'village.show',
        ['x' => $village->x, 'y' => $village->y]
      )
    );
  }
}