summaryrefslogtreecommitdiff
path: root/src/gemini/Controller/Event.php
blob: ab4463b478f1edf74540d8dbadeecad76af8033b (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
<?php

namespace App\gemini\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\Village;
use GeminiFoundation\Request;
use GeminiFoundation\Response;
use GeminiFoundation\Status;

class Event
{
  // #[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 SendResources $sendResourcesEvent*/
    $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 Response(
      statusCode: Status::REDIRECT_TEMPORARY,
      meta: "/village/{$village->x}/{$village->y}"
    );
  }
}