1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\Tal; |
6
|
|
|
|
7
|
|
|
use JBBCode\Parser; |
8
|
|
|
use PhpTal\Php\TalesInternal; |
9
|
|
|
use PhpTal\TalesRegistry; |
10
|
|
|
use Psr\Container\ContainerInterface; |
11
|
|
|
use Stu\Module\Colony\Lib\PlanetFieldTypeRetrieverInterface; |
12
|
|
|
use Stu\Module\Control\StuTime; |
13
|
|
|
use Stu\Module\Message\Lib\ContactListModeEnum; |
14
|
|
|
use Stu\Module\Tal\Exception\DiContainerNotSetException; |
15
|
|
|
use Stu\Orm\Entity\ColonyInterface; |
16
|
|
|
use Stu\Orm\Entity\PlanetFieldInterface; |
17
|
|
|
use Stu\Orm\Repository\ColonyTerraformingRepositoryInterface; |
18
|
|
|
|
19
|
|
|
final class TalHelper |
20
|
|
|
{ |
21
|
|
|
private static ?ContainerInterface $dic = null; |
22
|
|
|
|
23
|
4 |
|
private static function setDic(ContainerInterface $dic): void |
24
|
|
|
{ |
25
|
4 |
|
self::$dic = $dic; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @throws DiContainerNotSetException |
30
|
|
|
*/ |
31
|
|
|
private static function getDic(): ContainerInterface |
32
|
|
|
{ |
33
|
|
|
if (self::$dic === null) { |
34
|
|
|
throw new DiContainerNotSetException(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return self::$dic; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function formatProductionValue(int $value): string |
41
|
|
|
{ |
42
|
|
|
if ($value > 0) { |
43
|
|
|
return sprintf('<span class="positive">+%d</span>', $value); |
44
|
|
|
} elseif ($value < 0) { |
45
|
|
|
return sprintf('<span class="negative">%d</span>', $value); |
46
|
|
|
} |
47
|
|
|
return (string) $value; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function addPlusCharacter(string $value): string |
51
|
|
|
{ |
52
|
|
|
if ($value <= 0) { |
53
|
|
|
return $value; |
54
|
|
|
} |
55
|
|
|
return sprintf('+%d', $value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function getContactListModeDescription(ContactListModeEnum $mode): string |
59
|
|
|
{ |
60
|
|
|
return $mode->getTitle(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function getBBCodeParser(): Parser |
64
|
|
|
{ |
65
|
|
|
return self::getDic()->get(Parser::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function jsquote(string $str): string |
69
|
|
|
{ |
70
|
|
|
return str_replace( |
71
|
|
|
[ |
72
|
|
|
"\\", |
73
|
|
|
"'", |
74
|
|
|
], |
75
|
|
|
[ |
76
|
|
|
"\\\\", |
77
|
|
|
"\\'", |
78
|
|
|
], |
79
|
|
|
$str |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function formatSeconds(string $time): string |
84
|
|
|
{ |
85
|
|
|
$time = (int) $time; |
86
|
|
|
$h = floor($time / 3600); |
87
|
|
|
$time -= $h * 3600; |
88
|
|
|
$m = floor($time / 60); |
89
|
|
|
$time -= $m * 60; |
90
|
|
|
|
91
|
|
|
$ret = ''; |
92
|
|
|
if ($h > 0) { |
93
|
|
|
$ret .= $h . 'h'; |
94
|
|
|
} |
95
|
|
|
if ($m > 0) { |
96
|
|
|
$ret .= ' ' . $m . 'm'; |
97
|
|
|
} |
98
|
|
|
if ($time > 0) { |
99
|
|
|
$ret .= ' ' . $time . 's'; |
100
|
|
|
} |
101
|
|
|
return $ret; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public static function getNumberWithThousandSeperator(int $number): string |
105
|
|
|
{ |
106
|
|
|
return number_format((float) $number, 0, '', '.'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public static function getPlanetFieldTypeDescription( |
110
|
|
|
int $fieldTypeId |
111
|
|
|
): string { |
112
|
|
|
return self::getDic()->get(PlanetFieldTypeRetrieverInterface::class)->getDescription($fieldTypeId); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static function getPlanetFieldTitle( |
116
|
|
|
PlanetFieldInterface $planetField |
117
|
|
|
): string { |
118
|
|
|
$fieldTypeName = self::getPlanetFieldTypeDescription($planetField->getFieldType()); |
119
|
|
|
|
120
|
|
|
$building = $planetField->getBuilding(); |
121
|
|
|
|
122
|
|
|
if ($building === null) { |
123
|
|
|
$terraFormingState = null; |
124
|
|
|
$host = $planetField->getHost(); |
125
|
|
|
if ($host instanceof ColonyInterface) { |
126
|
|
|
$terraFormingState = self::getDic()->get(ColonyTerraformingRepositoryInterface::class)->getByColonyAndField( |
127
|
|
|
$host->getId(), |
128
|
|
|
$planetField->getId() |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
if ($terraFormingState !== null) { |
132
|
|
|
return sprintf( |
133
|
|
|
'%s läuft bis %s', |
134
|
|
|
$terraFormingState->getTerraforming()->getDescription(), |
135
|
|
|
date('d.m.Y H:i', $terraFormingState->getFinishDate()) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
return $fieldTypeName; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($planetField->isUnderConstruction()) { |
142
|
|
|
return sprintf( |
143
|
|
|
'In Bau: %s auf %s - Fertigstellung: %s', |
144
|
|
|
$building->getName(), |
145
|
|
|
$fieldTypeName, |
146
|
|
|
date('d.m.Y H:i', $planetField->getBuildtime()) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
if (!$planetField->isActivateable()) { |
150
|
|
|
return $building->getName() . " auf " . $fieldTypeName; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ($planetField->isActive()) { |
154
|
|
|
if ($planetField->isDamaged()) { |
155
|
|
|
return $building->getName() . " (aktiviert, beschädigt) auf " . $fieldTypeName; |
156
|
|
|
} |
157
|
|
|
return $building->getName() . " (aktiviert) auf " . $fieldTypeName; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($planetField->hasHighDamage()) { |
161
|
|
|
return $building->getName() . " (stark beschädigt) auf " . $fieldTypeName; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $building->getName() . " (deaktiviert) auf " . $fieldTypeName; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Registers global available tal methods |
169
|
|
|
*/ |
170
|
4 |
|
public static function register(ContainerInterface $dic): void |
171
|
|
|
{ |
172
|
4 |
|
self::setDic($dic); |
173
|
|
|
|
174
|
4 |
|
TalesRegistry::registerPrefix( |
175
|
4 |
|
'clmodeDescription', |
176
|
4 |
|
fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getContactListModeDescription((int) ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')' |
177
|
4 |
|
); |
178
|
4 |
|
TalesRegistry::registerPrefix( |
179
|
4 |
|
'addPlusCharacter', |
180
|
4 |
|
fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::addPlusCharacter((int)' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')' |
181
|
4 |
|
); |
182
|
4 |
|
TalesRegistry::registerPrefix( |
183
|
4 |
|
'isPositive', |
184
|
4 |
|
fn ($src, $nothrow): string => '(int) ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ' > 0' |
185
|
4 |
|
); |
186
|
4 |
|
TalesRegistry::registerPrefix( |
187
|
4 |
|
'isNegative', |
188
|
4 |
|
fn ($src, $nothrow): string => '(int) ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ' < 0' |
189
|
4 |
|
); |
190
|
4 |
|
TalesRegistry::registerPrefix( |
191
|
4 |
|
'numberWithThousandSeperator', |
192
|
4 |
|
fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getNumberWithThousandSeperator(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')' |
193
|
4 |
|
); |
194
|
4 |
|
TalesRegistry::registerPrefix( |
195
|
4 |
|
'bbcode', |
196
|
4 |
|
fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getBBCodeParser()->parse(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')->getAsHtml()' |
197
|
4 |
|
); |
198
|
4 |
|
TalesRegistry::registerPrefix( |
199
|
4 |
|
'bbcode2txt', |
200
|
4 |
|
fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getBBCodeParser()->parse(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')->getAsText()' |
201
|
4 |
|
); |
202
|
4 |
|
TalesRegistry::registerPrefix( |
203
|
4 |
|
'jsquote', |
204
|
4 |
|
fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::jsquote(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')' |
205
|
4 |
|
); |
206
|
4 |
|
TalesRegistry::registerPrefix( |
207
|
4 |
|
'datetime', |
208
|
4 |
|
fn ($src, $nothrow): string => 'date(\'d.m.\', ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ') . (date("Y", ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')+' . StuTime::STU_YEARS_IN_FUTURE_OFFSET . ') . " " . date("H:i", ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')' |
209
|
4 |
|
); |
210
|
4 |
|
TalesRegistry::registerPrefix( |
211
|
4 |
|
'date', |
212
|
4 |
|
fn ($src, $nothrow): string => 'date(\'d.m.\', ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ') . (date("Y", ' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')+' . StuTime::STU_YEARS_IN_FUTURE_OFFSET . ')' |
213
|
4 |
|
); |
214
|
4 |
|
TalesRegistry::registerPrefix( |
215
|
4 |
|
'nl2br', |
216
|
4 |
|
fn ($src, $nothrow): string => 'nl2br(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')' |
217
|
4 |
|
); |
218
|
4 |
|
TalesRegistry::registerPrefix( |
219
|
4 |
|
'nl2brBbCode', |
220
|
4 |
|
fn ($src, $nothrow): string => 'nl2br(\Stu\Module\Tal\TalHelper::getBBCodeParser()->parse(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')->getAsHtml())' |
221
|
4 |
|
); |
222
|
4 |
|
TalesRegistry::registerPrefix( |
223
|
4 |
|
'formatSeconds', |
224
|
4 |
|
fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::formatSeconds(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')' |
225
|
4 |
|
); |
226
|
4 |
|
TalesRegistry::registerPrefix( |
227
|
4 |
|
'planetFieldTypeDescription', |
228
|
4 |
|
fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getPlanetFieldTypeDescription(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')' |
229
|
4 |
|
); |
230
|
4 |
|
TalesRegistry::registerPrefix( |
231
|
4 |
|
'planetFieldTitle', |
232
|
4 |
|
fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::getPlanetFieldTitle(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')' |
233
|
4 |
|
); |
234
|
4 |
|
TalesRegistry::registerPrefix( |
235
|
4 |
|
'formatProductionValue', |
236
|
4 |
|
fn ($src, $nothrow): string => '\Stu\Module\Tal\TalHelper::formatProductionValue(' . TalesInternal::compileToPHPExpression($src, $nothrow) . ')' |
237
|
4 |
|
); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|