1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Twig; |
6
|
|
|
|
7
|
|
|
use JBBCode\Parser; |
8
|
|
|
use Noodlehaus\ConfigInterface; |
9
|
|
|
use Stu\Component\Colony\ColonyMenuEnum; |
10
|
|
|
use Stu\Component\Game\ModuleViewEnum; |
11
|
|
|
use Stu\Lib\Colony\PlanetFieldHostInterface; |
12
|
|
|
use Stu\Lib\ModuleScreen\GradientColorInterface; |
13
|
|
|
use Stu\Module\Colony\Lib\ColonyEpsProductionPreviewWrapper; |
14
|
|
|
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface; |
15
|
|
|
use Stu\Module\Colony\Lib\ColonyProductionPreviewWrapper; |
16
|
|
|
use Stu\Module\Control\StuTime; |
17
|
|
|
use Stu\Module\Ship\Lib\Battle\FightLibInterface; |
18
|
|
|
use Stu\Module\Ship\Lib\ShipNfsItem; |
19
|
|
|
use Stu\Module\Tal\TalHelper; |
20
|
|
|
use Stu\Orm\Entity\AnomalyInterface; |
21
|
|
|
use Stu\Orm\Entity\BuildingInterface; |
22
|
|
|
use Stu\Orm\Entity\ShipInterface; |
23
|
|
|
use Twig\Environment; |
24
|
|
|
use Twig\TwigFilter; |
25
|
|
|
use Twig\TwigFunction; |
26
|
|
|
|
27
|
|
|
class TwigHelper |
28
|
|
|
{ |
29
|
|
|
private Environment $environment; |
30
|
|
|
|
31
|
|
|
private Parser $parser; |
32
|
|
|
|
33
|
|
|
private ConfigInterface $config; |
34
|
|
|
|
35
|
|
|
private FightLibInterface $fightLib; |
36
|
|
|
|
37
|
|
|
private ColonyLibFactoryInterface $colonyLibFactory; |
38
|
|
|
|
39
|
|
|
private GradientColorInterface $gradientColor; |
40
|
|
|
|
41
|
4 |
|
public function __construct( |
42
|
|
|
Environment $environment, |
43
|
|
|
Parser $parser, |
44
|
|
|
ConfigInterface $config, |
45
|
|
|
FightLibInterface $fightLib, |
46
|
|
|
ColonyLibFactoryInterface $colonyLibFactory, |
47
|
|
|
GradientColorInterface $gradientColor |
48
|
|
|
) { |
49
|
4 |
|
$this->environment = $environment; |
50
|
4 |
|
$this->parser = $parser; |
51
|
4 |
|
$this->config = $config; |
52
|
4 |
|
$this->fightLib = $fightLib; |
53
|
4 |
|
$this->colonyLibFactory = $colonyLibFactory; |
54
|
4 |
|
$this->gradientColor = $gradientColor; |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
public function registerGlobalVariables(): void |
58
|
|
|
{ |
59
|
4 |
|
$this->environment->addGlobal( |
60
|
4 |
|
'ASSET_PATHS', |
61
|
4 |
|
[ |
62
|
4 |
|
'alliance' => $this->config->get('game.alliance_avatar_path'), |
63
|
4 |
|
'user' => $this->config->get('game.user_avatar_path'), |
64
|
4 |
|
'faction' => 'assets/rassen/', |
65
|
4 |
|
] |
66
|
4 |
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Registers global available twig methods and filters |
71
|
|
|
*/ |
72
|
4 |
|
public function registerFiltersAndFunctions(): void |
73
|
|
|
{ |
74
|
4 |
|
$this->registerFilters(); |
75
|
4 |
|
$this->registerFunctions(); |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
private function registerFilters(): void |
79
|
|
|
{ |
80
|
4 |
|
$bbcode2txtFilter = new TwigFilter('bbcode2txt', function ($string): string { |
81
|
|
|
return $this->parser->parse($string)->getAsText(); |
82
|
4 |
|
}); |
83
|
4 |
|
$this->environment->addFilter($bbcode2txtFilter); |
84
|
|
|
|
85
|
4 |
|
$bbcodeFilter = new TwigFilter('bbcode', function ($string): string { |
86
|
|
|
return $this->parser->parse($string)->getAsHTML(); |
87
|
4 |
|
}, ['is_safe' => ['html']]); |
88
|
4 |
|
$this->environment->addFilter($bbcodeFilter); |
89
|
|
|
|
90
|
4 |
|
$jsquoteFilter = new TwigFilter('jsquote', function ($string): string { |
91
|
|
|
return TalHelper::jsquote($string); |
92
|
4 |
|
}); |
93
|
4 |
|
$this->environment->addFilter($jsquoteFilter); |
94
|
|
|
|
95
|
4 |
|
$addPlusCharacterFilter = new TwigFilter('addPlusCharacter', function ($value): string { |
96
|
|
|
if (is_int($value)) { |
97
|
|
|
return TalHelper::addPlusCharacter((string) $value); |
98
|
|
|
} |
99
|
|
|
return TalHelper::addPlusCharacter($value); |
100
|
4 |
|
}); |
101
|
4 |
|
$this->environment->addFilter($addPlusCharacterFilter); |
102
|
|
|
|
103
|
4 |
|
$formatSecondsFilter = new TwigFilter('formatSeconds', function ($value): string { |
104
|
|
|
if (is_int($value)) { |
105
|
|
|
return TalHelper::formatSeconds((string) $value); |
106
|
|
|
} |
107
|
|
|
return TalHelper::formatSeconds($value); |
108
|
4 |
|
}); |
109
|
4 |
|
$this->environment->addFilter($formatSecondsFilter); |
110
|
|
|
|
111
|
4 |
|
$planetFieldTitleFilter = new TwigFilter('planetFieldTitle', function ($planetField): string { |
112
|
|
|
return TalHelper::getPlanetFieldTitle($planetField); |
113
|
4 |
|
}); |
114
|
4 |
|
$this->environment->addFilter($planetFieldTitleFilter); |
115
|
|
|
|
116
|
4 |
|
$planetFieldTypeDescriptionFilter = new TwigFilter('planetFieldTypeDescription', function ($id): string { |
117
|
|
|
return TalHelper::getPlanetFieldTypeDescription($id); |
118
|
4 |
|
}); |
119
|
4 |
|
$this->environment->addFilter($planetFieldTypeDescriptionFilter); |
120
|
|
|
|
121
|
4 |
|
$formatProductionValueFilter = new TwigFilter('formatProductionValue', function ($value): string { |
122
|
|
|
return TalHelper::formatProductionValue($value); |
123
|
4 |
|
}); |
124
|
4 |
|
$this->environment->addFilter($formatProductionValueFilter); |
125
|
|
|
|
126
|
4 |
|
$isPositiveFilter = new TwigFilter('isPositive', function (int $value): bool { |
127
|
|
|
return $value > 0; |
128
|
4 |
|
}); |
129
|
4 |
|
$this->environment->addFilter($isPositiveFilter); |
130
|
|
|
|
131
|
4 |
|
$stuDateTimeFilter = new TwigFilter('stuDateTime', function ($value): string { |
132
|
|
|
return sprintf( |
133
|
|
|
'%s%s %s', |
134
|
|
|
date('d.m.', $value), |
135
|
|
|
(int)date("Y", $value) + StuTime::STU_YEARS_IN_FUTURE_OFFSET, |
136
|
|
|
date("H:i", $value) |
137
|
|
|
); |
138
|
4 |
|
}); |
139
|
4 |
|
$this->environment->addFilter($stuDateTimeFilter); |
140
|
|
|
|
141
|
4 |
|
$stuDateFilter = new TwigFilter('stuDate', function ($value): string { |
142
|
|
|
return sprintf( |
143
|
|
|
'%s%s', |
144
|
|
|
date('d.m.', $value), |
145
|
|
|
(int)date("Y", $value) + StuTime::STU_YEARS_IN_FUTURE_OFFSET |
146
|
|
|
); |
147
|
4 |
|
}); |
148
|
4 |
|
$this->environment->addFilter($stuDateFilter); |
149
|
|
|
|
150
|
4 |
|
$nl2brFilter = new TwigFilter('nl2br', function (string $value): string { |
151
|
|
|
return nl2br($value); |
152
|
4 |
|
}); |
153
|
4 |
|
$this->environment->addFilter($nl2brFilter); |
154
|
|
|
|
155
|
4 |
|
$htmlSafeFilter = new TwigFilter('htmlSafe', function (string $text): string { |
156
|
|
|
return htmlspecialchars($text); |
157
|
4 |
|
}); |
158
|
4 |
|
$this->environment->addFilter($htmlSafeFilter); |
159
|
|
|
|
160
|
4 |
|
$adventDoorFilter = new TwigFilter('adventDoor', function (AnomalyInterface $anomaly): int { |
161
|
|
|
return (int)((120 - $anomaly->getRemainingTicks()) / 5) + 1; |
162
|
4 |
|
}); |
163
|
4 |
|
$this->environment->addFilter($adventDoorFilter); |
164
|
|
|
} |
165
|
|
|
|
166
|
4 |
|
private function registerFunctions(): void |
167
|
|
|
{ |
168
|
4 |
|
$canAttackTargetFunction = new TwigFunction('canAttackTarget', function (ShipInterface $ship, ShipInterface|ShipNfsItem $target): bool { |
169
|
|
|
return $this->fightLib->canAttackTarget($ship, $target); |
170
|
4 |
|
}); |
171
|
4 |
|
$this->environment->addFunction($canAttackTargetFunction); |
172
|
|
|
|
173
|
4 |
|
$getEpsProductionPreviewFunction = new TwigFunction('getEpsProductionPreview', function (PlanetFieldHostInterface $host, BuildingInterface $building): ColonyEpsProductionPreviewWrapper { |
174
|
|
|
return $this->colonyLibFactory->createEpsProductionPreviewWrapper($host, $building); |
175
|
4 |
|
}); |
176
|
4 |
|
$this->environment->addFunction($getEpsProductionPreviewFunction); |
177
|
|
|
|
178
|
4 |
|
$getCommodityProductionPreviewFunction = new TwigFunction('getCommodityProductionPreview', function (PlanetFieldHostInterface $host, BuildingInterface $building): ColonyProductionPreviewWrapper { |
179
|
|
|
return $this->colonyLibFactory->createColonyProductionPreviewWrapper($building, $host); |
180
|
4 |
|
}); |
181
|
4 |
|
$this->environment->addFunction($getCommodityProductionPreviewFunction); |
182
|
|
|
|
183
|
4 |
|
$getColonyMenuClassFunction = new TwigFunction('getColonyMenuClass', function (ColonyMenuEnum $currentMenu, int $value): string { |
184
|
|
|
return ColonyMenuEnum::getMenuClass($currentMenu, $value); |
185
|
4 |
|
}); |
186
|
4 |
|
$this->environment->addFunction($getColonyMenuClassFunction); |
187
|
|
|
|
188
|
4 |
|
$getViewFunction = new TwigFunction('getView', function (string $value): ModuleViewEnum { |
189
|
|
|
return ModuleViewEnum::from($value); |
190
|
4 |
|
}); |
191
|
4 |
|
$this->environment->addFunction($getViewFunction); |
192
|
|
|
|
193
|
4 |
|
$getUniqIdFunction = new TwigFunction('getUniqId', function (): string { |
194
|
|
|
return uniqid(); |
195
|
4 |
|
}); |
196
|
4 |
|
$this->environment->addFunction($getUniqIdFunction); |
197
|
|
|
|
198
|
4 |
|
$gradientColorFunction = new TwigFunction('gradientColor', function (int $value, int $lowest, int $highest): string { |
199
|
|
|
return $this->gradientColor->calculateGradientColor($value, $lowest, $highest); |
200
|
4 |
|
}); |
201
|
4 |
|
$this->environment->addFunction($gradientColorFunction); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|