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

namespace App\gemini\Controller;

use App\Model\Building as Model;
use App\Model\Event;
use App\Model\Event\UpgradeBuilding;
use App\Model\Village;
use GeminiFoundation\Request;
use GeminiFoundation\Response;
use GeminiFoundation\Status;

class Building
{
  //#[Route(path: '/village/{x}/{y}/building/{type}/level-up', methods: ['POST'])]
  public function levelUp(Request $request): Response
  {
    $village = Village::getByCoordinates($request->get('x'), $request->get('y'));
    $building = Model::getByVillage($village->id, $request->get('type')) ?? Model::getEmpty($village->id, $request->get('type'));

    // resources
    foreach ($building->getResourceRequirements() as $resourceType => $resourceValue) {
      $village->{$resourceType} -= $resourceValue;
    }
    $village->updateResources();

    // event
    $event = new Event();
    $event->time = (new \DateTime())->add(\DateInterval::createFromDateString(
      $building->getBuildTimeForLevel($building->getEffectiveLevel() + 1) . ' seconds'
    ));
    $event->villageId = $building->villageId;
    $upgradeBuildingEvent = new UpgradeBuilding();
    $upgradeBuildingEvent->event = $event;
    $upgradeBuildingEvent->type = $building->type;
    $upgradeBuildingEvent->dbInsert();


    return new Response(
      statusCode: Status::REDIRECT_TEMPORARY, # correct response code?
      meta: "/village/{$village->x}/{$village->y}"
    );
  }
}