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\Lib\Colony\PlanetFieldHostInterface; |
11
|
|
|
use Stu\Module\Colony\Lib\ColonyEpsProductionPreviewWrapper; |
12
|
|
|
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface; |
13
|
|
|
use Stu\Module\Colony\Lib\ColonyProductionPreviewWrapper; |
14
|
|
|
use Stu\Module\Control\StuTime; |
15
|
|
|
use Stu\Module\Ship\Lib\Battle\FightLibInterface; |
16
|
|
|
use Stu\Module\Ship\Lib\ShipNfsItem; |
17
|
|
|
use Stu\Module\Tal\TalHelper; |
18
|
|
|
use Stu\Orm\Entity\BuildingInterface; |
19
|
|
|
use Stu\Orm\Entity\ShipInterface; |
20
|
|
|
use Twig\Environment; |
21
|
|
|
use Twig\TwigFilter; |
22
|
|
|
use Twig\TwigFunction; |
23
|
|
|
|
24
|
|
|
class TwigHelper |
25
|
|
|
{ |
26
|
|
|
private Environment $environment; |
27
|
|
|
|
28
|
|
|
private Parser $parser; |
29
|
|
|
|
30
|
|
|
private ConfigInterface $config; |
31
|
|
|
|
32
|
|
|
private FightLibInterface $fightLib; |
33
|
|
|
|
34
|
|
|
private ColonyLibFactoryInterface $colonyLibFactory; |
35
|
|
|
|
36
|
4 |
|
public function __construct( |
37
|
|
|
Environment $environment, |
38
|
|
|
Parser $parser, |
39
|
|
|
ConfigInterface $config, |
40
|
|
|
FightLibInterface $fightLib, |
41
|
|
|
ColonyLibFactoryInterface $colonyLibFactory |
42
|
|
|
) { |
43
|
4 |
|
$this->environment = $environment; |
44
|
4 |
|
$this->parser = $parser; |
45
|
4 |
|
$this->config = $config; |
46
|
4 |
|
$this->fightLib = $fightLib; |
47
|
4 |
|
$this->colonyLibFactory = $colonyLibFactory; |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
public function registerGlobalVariables(): void |
51
|
|
|
{ |
52
|
4 |
|
$this->environment->addGlobal( |
53
|
4 |
|
'ASSET_PATHS', |
54
|
4 |
|
[ |
55
|
4 |
|
'alliance' => $this->config->get('game.alliance_avatar_path'), |
56
|
4 |
|
'user' => $this->config->get('game.user_avatar_path'), |
57
|
4 |
|
'faction' => 'assets/rassen/', |
58
|
4 |
|
] |
59
|
4 |
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Registers global available twig methods and filters |
64
|
|
|
*/ |
65
|
4 |
|
public function registerFiltersAndFunctions(): void |
66
|
|
|
{ |
67
|
4 |
|
$this->registerFilters(); |
68
|
4 |
|
$this->registerFunctions(); |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
private function registerFilters(): void |
72
|
|
|
{ |
73
|
4 |
|
$bbcode2txtFilter = new TwigFilter('bbcode2txt', function ($string): string { |
74
|
|
|
return $this->parser->parse($string)->getAsText(); |
75
|
4 |
|
}); |
76
|
4 |
|
$this->environment->addFilter($bbcode2txtFilter); |
77
|
|
|
|
78
|
4 |
|
$bbcodeFilter = new TwigFilter('bbcode', function ($string): string { |
79
|
|
|
return $this->parser->parse($string)->getAsHTML(); |
80
|
4 |
|
}, ['is_safe' => ['html']]); |
81
|
4 |
|
$this->environment->addFilter($bbcodeFilter); |
82
|
|
|
|
83
|
4 |
|
$jsquoteFilter = new TwigFilter('jsquote', function ($string): string { |
84
|
|
|
return TalHelper::jsquote($string); |
85
|
4 |
|
}); |
86
|
4 |
|
$this->environment->addFilter($jsquoteFilter); |
87
|
|
|
|
88
|
4 |
|
$addPlusCharacterFilter = new TwigFilter('addPlusCharacter', function ($value): string { |
89
|
|
|
if (is_int($value)) { |
90
|
|
|
return TalHelper::addPlusCharacter((string) $value); |
91
|
|
|
} |
92
|
|
|
return TalHelper::addPlusCharacter($value); |
93
|
4 |
|
}); |
94
|
4 |
|
$this->environment->addFilter($addPlusCharacterFilter); |
95
|
|
|
|
96
|
4 |
|
$formatSecondsFilter = new TwigFilter('formatSeconds', function ($value): string { |
97
|
|
|
if (is_int($value)) { |
98
|
|
|
return TalHelper::formatSeconds((string) $value); |
99
|
|
|
} |
100
|
|
|
return TalHelper::formatSeconds($value); |
101
|
4 |
|
}); |
102
|
4 |
|
$this->environment->addFilter($formatSecondsFilter); |
103
|
|
|
|
104
|
4 |
|
$planetFieldTitleFilter = new TwigFilter('planetFieldTitle', function ($planetField): string { |
105
|
|
|
return TalHelper::getPlanetFieldTitle($planetField); |
106
|
4 |
|
}); |
107
|
4 |
|
$this->environment->addFilter($planetFieldTitleFilter); |
108
|
|
|
|
109
|
4 |
|
$planetFieldTypeDescriptionFilter = new TwigFilter('planetFieldTypeDescription', function ($id): string { |
110
|
|
|
return TalHelper::getPlanetFieldTypeDescription($id); |
111
|
4 |
|
}); |
112
|
4 |
|
$this->environment->addFilter($planetFieldTypeDescriptionFilter); |
113
|
|
|
|
114
|
4 |
|
$formatProductionValueFilter = new TwigFilter('formatProductionValue', function ($value): string { |
115
|
|
|
return TalHelper::formatProductionValue($value); |
116
|
4 |
|
}); |
117
|
4 |
|
$this->environment->addFilter($formatProductionValueFilter); |
118
|
|
|
|
119
|
4 |
|
$isPositiveFilter = new TwigFilter('isPositive', function (int $value): bool { |
120
|
|
|
return $value > 0; |
121
|
4 |
|
}); |
122
|
4 |
|
$this->environment->addFilter($isPositiveFilter); |
123
|
|
|
|
124
|
4 |
|
$datetimeFilter = new TwigFilter('datetime', function ($value): string { |
125
|
|
|
return sprintf( |
126
|
|
|
'%s%s %s', |
127
|
|
|
date('d.m.', $value), |
128
|
|
|
(int)date("Y", $value) + StuTime::STU_YEARS_IN_FUTURE_OFFSET, |
129
|
|
|
date("H:i", $value) |
130
|
|
|
); |
131
|
4 |
|
}); |
132
|
4 |
|
$this->environment->addFilter($datetimeFilter); |
133
|
|
|
|
134
|
4 |
|
$nl2brFilter = new TwigFilter('nl2br', function (string $value): string { |
135
|
|
|
return nl2br($value); |
136
|
4 |
|
}); |
137
|
4 |
|
$this->environment->addFilter($nl2brFilter); |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
private function registerFunctions(): void |
141
|
|
|
{ |
142
|
4 |
|
$canAttackTargetFunction = new TwigFunction('canAttackTarget', function (ShipInterface $ship, ShipInterface|ShipNfsItem $target): bool { |
143
|
|
|
return $this->fightLib->canAttackTarget($ship, $target); |
144
|
4 |
|
}); |
145
|
4 |
|
$this->environment->addFunction($canAttackTargetFunction); |
146
|
|
|
|
147
|
4 |
|
$getEpsProductionPreviewFunction = new TwigFunction('getEpsProductionPreview', function (PlanetFieldHostInterface $host, BuildingInterface $building): ColonyEpsProductionPreviewWrapper { |
148
|
|
|
return $this->colonyLibFactory->createEpsProductionPreviewWrapper($host, $building); |
149
|
4 |
|
}); |
150
|
4 |
|
$this->environment->addFunction($getEpsProductionPreviewFunction); |
151
|
|
|
|
152
|
4 |
|
$getCommodityProductionPreviewFunction = new TwigFunction('getCommodityProductionPreview', function (PlanetFieldHostInterface $host, BuildingInterface $building): ColonyProductionPreviewWrapper { |
153
|
|
|
return $this->colonyLibFactory->createColonyProductionPreviewWrapper($building, $host); |
154
|
4 |
|
}); |
155
|
4 |
|
$this->environment->addFunction($getCommodityProductionPreviewFunction); |
156
|
|
|
|
157
|
4 |
|
$getColonyMenuClassFunction = new TwigFunction('getColonyMenuClass', function (ColonyMenuEnum $currentMenu, int $value): string { |
158
|
|
|
return ColonyMenuEnum::getMenuClass($currentMenu, $value); |
159
|
4 |
|
}); |
160
|
4 |
|
$this->environment->addFunction($getColonyMenuClassFunction); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|