1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Colony\Lib\Gui; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use Stu\Component\Colony\ColonyMenuEnum; |
9
|
|
|
use Stu\Lib\Colony\PlanetFieldHostInterface; |
10
|
|
|
use Stu\Module\Colony\Lib\Gui\Component\GuiComponentProviderInterface; |
11
|
|
|
use Stu\Module\Control\GameControllerInterface; |
12
|
|
|
use Stu\Orm\Entity\ColonyInterface; |
13
|
|
|
use Stu\Orm\Entity\ColonySandboxInterface; |
14
|
|
|
|
15
|
|
|
final class ColonyGuiHelper implements ColonyGuiHelperInterface |
16
|
|
|
{ |
17
|
|
|
/** @var array<int, GuiComponentProviderInterface> */ |
18
|
|
|
private array $guiComponentProviders; |
19
|
|
|
|
20
|
|
|
/** @param array<int, GuiComponentProviderInterface> $guiComponentProviders */ |
21
|
|
|
public function __construct(array $guiComponentProviders) |
22
|
|
|
{ |
23
|
|
|
$this->guiComponentProviders = $guiComponentProviders; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function registerMenuComponents( |
27
|
|
|
ColonyMenuEnum $menu, |
28
|
|
|
PlanetFieldHostInterface $host, |
29
|
|
|
GameControllerInterface $game |
30
|
|
|
): void { |
31
|
|
|
|
32
|
|
|
foreach ($menu->getNecessaryGuiComponents() as $component) { |
33
|
|
|
$this->processComponent($component, $host, $game); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$game->setTemplateVar('HOST', $host); |
37
|
|
|
$game->setTemplateVar('CURRENT_MENU', $menu); |
38
|
|
|
|
39
|
|
|
if ($host instanceof ColonyInterface) { |
40
|
|
|
$game->setTemplateVar('COLONY', $host); |
41
|
|
|
$game->setTemplateVar('FORM_ACTION', 'colony.php'); |
42
|
|
|
} |
43
|
|
|
if ($host instanceof ColonySandboxInterface) { |
44
|
|
|
$game->setTemplateVar('COLONY', $host->getColony()); |
45
|
|
|
$game->setTemplateVar('FORM_ACTION', '/admin/index.php'); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function registerComponents( |
50
|
|
|
PlanetFieldHostInterface $host, |
51
|
|
|
GameControllerInterface $game, |
52
|
|
|
array $guiComponents |
53
|
|
|
): void { |
54
|
|
|
foreach ($guiComponents as $component) { |
55
|
|
|
$this->processComponent($component, $host, $game); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function processComponent( |
60
|
|
|
GuiComponentEnum $guiComponent, |
61
|
|
|
PlanetFieldHostInterface $host, |
62
|
|
|
GameControllerInterface $game, |
63
|
|
|
): void { |
64
|
|
|
if (!array_key_exists($guiComponent->value, $this->guiComponentProviders)) { |
65
|
|
|
throw new RuntimeException(sprintf('guiComponentProvider with follwing id does not exist: %d', $guiComponent->value)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$componentProvider = $this->guiComponentProviders[$guiComponent->value]; |
69
|
|
|
$componentProvider->setTemplateVariables($host, $game); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|