|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Module\Colony\View\ShowBuilding; |
|
6
|
|
|
|
|
7
|
|
|
use Stu\Lib\Colony\PlanetFieldHostInterface; |
|
8
|
|
|
use Stu\Lib\Colony\PlanetFieldHostProviderInterface; |
|
9
|
|
|
use Stu\Module\Control\GameControllerInterface; |
|
10
|
|
|
use Stu\Module\Control\ViewControllerInterface; |
|
11
|
|
|
use Stu\Orm\Entity\BuildingInterface; |
|
12
|
|
|
use Stu\Orm\Entity\ColonyInterface; |
|
13
|
|
|
use Stu\Orm\Entity\PlanetFieldTypeBuildingInterface; |
|
14
|
|
|
use Stu\Orm\Repository\BuildingFieldAlternativeRepositoryInterface; |
|
15
|
|
|
use Stu\Orm\Repository\BuildingRepositoryInterface; |
|
16
|
|
|
use Stu\Orm\Repository\PlanetFieldRepositoryInterface; |
|
17
|
|
|
|
|
18
|
|
|
final class ShowBuilding implements ViewControllerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
public const VIEW_IDENTIFIER = 'SHOW_BUILDING'; |
|
21
|
|
|
|
|
22
|
|
|
private PlanetFieldHostProviderInterface $planetFieldHostProvider; |
|
23
|
|
|
|
|
24
|
|
|
private PlanetFieldRepositoryInterface $planetFieldRepository; |
|
25
|
|
|
|
|
26
|
|
|
private ShowBuildingRequestInterface $showBuildingRequest; |
|
27
|
|
|
|
|
28
|
|
|
private BuildingFieldAlternativeRepositoryInterface $buildingFieldAlternativeRepository; |
|
29
|
|
|
|
|
30
|
|
|
private BuildingRepositoryInterface $buildingRepository; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
PlanetFieldHostProviderInterface $planetFieldHostProvider, |
|
34
|
|
|
PlanetFieldRepositoryInterface $planetFieldRepository, |
|
35
|
|
|
ShowBuildingRequestInterface $showBuildingRequest, |
|
36
|
|
|
BuildingFieldAlternativeRepositoryInterface $buildingFieldAlternativeRepository, |
|
37
|
|
|
BuildingRepositoryInterface $buildingRepository |
|
38
|
|
|
) { |
|
39
|
|
|
$this->planetFieldHostProvider = $planetFieldHostProvider; |
|
40
|
|
|
$this->planetFieldRepository = $planetFieldRepository; |
|
41
|
|
|
$this->showBuildingRequest = $showBuildingRequest; |
|
42
|
|
|
$this->buildingFieldAlternativeRepository = $buildingFieldAlternativeRepository; |
|
43
|
|
|
$this->buildingRepository = $buildingRepository; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function handle(GameControllerInterface $game): void |
|
47
|
|
|
{ |
|
48
|
|
|
$userId = $game->getUser()->getId(); |
|
49
|
|
|
|
|
50
|
|
|
$host = $this->planetFieldHostProvider->loadHostViaRequestParameters($game->getUser()); |
|
51
|
|
|
|
|
52
|
|
|
$building = $this->buildingRepository->find($this->showBuildingRequest->getBuildingId()); |
|
53
|
|
|
if ($building === null) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$alternativeBuildings = $this->buildingFieldAlternativeRepository->getByBuildingIdAndResearchedByUser( |
|
58
|
|
|
$building->getId(), |
|
59
|
|
|
$userId |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
if ($alternativeBuildings === []) { |
|
63
|
|
|
$useableFieldTypes = $building->getBuildableFields(); |
|
64
|
|
|
} else { |
|
65
|
|
|
$useableFieldTypes = current($alternativeBuildings)->getBuilding()->getBuildableFields(); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
//filter by view |
|
69
|
|
|
$useableFieldTypes = array_filter( |
|
70
|
|
|
$useableFieldTypes->toArray(), |
|
71
|
|
|
fn (PlanetFieldTypeBuildingInterface $pftb): bool => $pftb->getView() |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
$game->setPageTitle($building->getName()); |
|
75
|
|
|
$game->setMacroInAjaxWindow('html/colony/component/buildingInfo.twig'); |
|
76
|
|
|
$game->setTemplateVar('buildingdata', $building); |
|
77
|
|
|
$game->setTemplateVar('HOST', $host); |
|
78
|
|
|
$game->setTemplateVar('ALTERNATIVE_BUILDINGS', $alternativeBuildings); |
|
79
|
|
|
$game->setTemplateVar('USEABLE_FIELD_TYPES', $useableFieldTypes); |
|
80
|
|
|
|
|
81
|
|
|
$this->setBuildingLimit($building, $host, $game); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function setBuildingLimit(BuildingInterface $building, PlanetFieldHostInterface $host, GameControllerInterface $game): void |
|
85
|
|
|
{ |
|
86
|
|
|
$buildingcount = null; |
|
87
|
|
|
|
|
88
|
|
|
if ($host instanceof ColonyInterface) { |
|
89
|
|
|
$storage = $host->getStorage(); |
|
90
|
|
|
$buildingcount = $host->getEps() / $building->getEpsCost(); |
|
91
|
|
|
foreach ($building->getCosts() as $cost) { |
|
92
|
|
|
if ($storage[$cost->getCommodityId()] != null) { |
|
93
|
|
|
$need = $storage[$cost->getCommodityId()]->getAmount() / $cost->getAmount(); |
|
94
|
|
|
$buildingcount = min($need, $buildingcount); |
|
95
|
|
|
} else { |
|
96
|
|
|
$buildingcount = 0; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($building->hasLimitColony()) { |
|
102
|
|
|
if ($this->planetFieldRepository->getCountByHostAndBuilding($host, $building->getId()) >= $building->getLimitColony()) { |
|
103
|
|
|
$buildingcount = 0; |
|
104
|
|
|
} else { |
|
105
|
|
|
$buildingcount = min($buildingcount, $building->getLimitColony()); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
if ($building->hasLimit()) { |
|
109
|
|
|
if ($this->planetFieldRepository->getCountByBuildingAndUser($building->getId(), $host->getUser()->getId()) >= $building->getLimit()) { |
|
110
|
|
|
$buildingcount = 0; |
|
111
|
|
|
} else { |
|
112
|
|
|
$buildingcount = min($buildingcount, $building->getLimit()); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
if ($buildingcount < 0) { |
|
116
|
|
|
$buildingcount = 0; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if ($buildingcount !== null) { |
|
120
|
|
|
$game->setTemplateVar('BUILDING_COUNT', floor($buildingcount)); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|