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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
<?php
namespace App\gemini\Controller;
use App\DB;
use App\Model\Event;
use App\Model\Event\SendUnits;
use App\Model\Event\TrainUnits;
use App\Model\Unit as Model;
use App\Model\Village;
use App\View;
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}"
);
}
// #[Route(path: '/village/{x}/{y}/send-units', methods: ['POST'])]
public function sendUnits(Request $request): Response
{
$village = Village::getByCoordinates($request->get('x'), $request->get('y'));
$selectedUnit = $request->get('selectedUnit');
if (empty($selectedUnit)) {
return new Response(body: View::render('send-units/01-units.twig', [
'village' => $village,
]));
}
if (empty($request->get('selectedVillageX'))) {
return new Response(body: View::render('send-units/02-villages.twig', [
'village' => $village,
'villages' => DB::fetch(Village::class, "select * from villages where id!=:id", ['id' => $village->id]),
'selectedUnit' => $selectedUnit,
]));
}
$selectedVillage = Village::getByCoordinates($request->get('selectedVillageX'), $request->get('selectedVillageY'));
$selectedAction = $request->get('selectedAction');
if (empty($selectedAction)) {
return new Response(body: View::render('send-units/03-actions.twig', [
'village' => $village,
'selectedUnit' => $selectedUnit,
'selectedVillage' => $selectedVillage,
]));
}
$amount = intval($request->get('input'));
if (empty($amount)) {
return new Response(statusCode: Status::INPUT, meta: 'Amount');
}
$destination = $selectedVillage;
/**@var Model $unit*/
$unit = new (Model::resolveType($selectedUnit))();
$amountUnits = DB::query(
'select amount from village_units where home_village_id=:home and residence_village_id=:home and type=:type',
['home' => $village->id, 'type' => $selectedUnit]
)->fetchColumn();
if ($amountUnits - $amount > 0) {
$statement = DB::query(
<<<SQL
update village_units set amount=:amount where home_village_id=:home and residence_village_id=:home and type=:type
SQL,
['amount' => $amountUnits - $amount, 'home' => $village->id, 'type' => $selectedUnit]
);
} else if ($amountUnits - $amount === 0) {
DB::query(
<<<SQL
delete from village_units where home_village_id=:home and residence_village_id=:home and type=:type
SQL,
['home' => $village->id, 'type' => $selectedUnit]
);
}
DB::query(
'insert into village_units (amount, type, home_village_id, residence_village_id, is_traveling) values (:amount, :type, :home, :home, true)',
['amount' => $amount, 'type' => $selectedUnit, 'home' => $village->id]
);
// event
$event = new Event();
$event->time = (new \DateTime())->add(
\DateInterval::createFromDateString(
Model::getTravelTime($unit, Village::getDistance($village->x, $village->y, $destination->x, $destination->y))
. ' seconds'
)
);
$event->villageId = $village->id;
$sendUnitsEvent = new SendUnits();
$sendUnitsEvent->event = $event;
$sendUnitsEvent->type = $selectedAction;
$sendUnitsEvent->unit = $selectedUnit;
$sendUnitsEvent->amount = $amount;
$sendUnitsEvent->source = $village->id;
$sendUnitsEvent->destination = $destination->id;
$sendUnitsEvent->dbInsert();
return new Response(
statusCode: Status::REDIRECT_TEMPORARY,
meta: "/village/{$village->x}/{$village->y}"
);
}
}
|