blob: c04079a5832bd557c7b41fddf94c16cda609aec2 (
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
|
<?php
namespace App\gemini\Controller;
use App\Model\Event;
use App\Model\Event\TrainUnits;
use App\Model\Unit as Model;
use App\Model\Village;
use GeminiFoundation\Request;
use GeminiFoundation\Response;
use GeminiFoundation\Status;
class Unit
{
// #[Route(path: '/village/{x}/{y}/unit/{type}/create', methods: ['POST'])]
public function train(Request $request): Response
{
if (empty($request->get('input'))) {
return new Response(statusCode: Status::INPUT, meta: 'Amount');
}
$village = Village::getByCoordinates($request->get('x'), $request->get('y'));
/**@var Model $unit*/
$unit = new (Model::resolveType($request->get('type')))();
$unit->type = $request->get('type');
$unit->homeVillageId = $village->id;
$amount = intval($request->get('input'));
if (! Village::canTrain($village, $unit, $amount)) {
return new Response(
statusCode: Status::REDIRECT_TEMPORARY,
meta: "/village/{$village->x}/{$village->y}"
);
}
// resources
foreach (Model::getResourceRequirements($unit, $amount) as $resourceType => $resourceValue) {
$village->{$resourceType} -= $resourceValue;
}
$village->updateResources();
// event
$event = new Event();
$event->time = (new \DateTime())->add(\DateInterval::createFromDateString($unit->getBuildTime($amount) . ' seconds'));
$event->villageId = $village->id;
$trainUnitsEvent = new TrainUnits();
$trainUnitsEvent->event = $event;
$trainUnitsEvent->type = $request->get('type');
$trainUnitsEvent->amount = $amount;
$trainUnitsEvent->dbInsert();
return new Response(
statusCode: Status::REDIRECT_TEMPORARY,
meta: "/village/{$village->x}/{$village->y}"
);
}
}
|