|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Framework\Core\Plugins\Gui; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer; |
|
6
|
|
|
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpUserGroup; |
|
7
|
|
|
use eXpansion\Framework\Core\Model\Gui\ManialinkFactoryContext; |
|
8
|
|
|
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface; |
|
9
|
|
|
use eXpansion\Framework\Core\Model\Gui\Script\Variable; |
|
10
|
|
|
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext; |
|
11
|
|
|
use eXpansion\Framework\Core\Model\UserGroups\Group; |
|
12
|
|
|
use FML\Script\Script; |
|
13
|
|
|
use FML\Script\ScriptLabel; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ScriptVariableUpdateFactory |
|
17
|
|
|
* |
|
18
|
|
|
* @author de Cramer Oliver<[email protected]> |
|
19
|
|
|
* @copyright 2018 eXpansion |
|
20
|
|
|
* @package eXpansion\Framework\Core\Plugins\Gui |
|
21
|
|
|
*/ |
|
22
|
|
|
class ScriptVariableUpdateFactory extends WidgetFactory implements ListenerInterfaceExpTimer, ListenerInterfaceExpUserGroup |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var Variable[] */ |
|
25
|
|
|
protected $variables = []; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Variable */ |
|
28
|
|
|
protected $checkVariable; |
|
29
|
|
|
|
|
30
|
|
|
/** @var int */ |
|
31
|
|
|
protected $maxUpdateFrequency; |
|
32
|
|
|
|
|
33
|
|
|
/** @var Variable */ |
|
34
|
|
|
protected $checkOldVariable; |
|
35
|
|
|
|
|
36
|
|
|
/** @var mixed[][] */ |
|
37
|
|
|
protected $queuedForUpdate = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* ScriptVariableUpdateFactory constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param $name |
|
43
|
|
|
* @param array $variables |
|
44
|
|
|
* @param int $maxUpdateFrequency |
|
45
|
|
|
* @param Group $playerGroup |
|
|
|
|
|
|
46
|
|
|
* @param WidgetFactoryContext $context |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($name, array $variables, int $maxUpdateFrequency = 1, WidgetFactoryContext $context) |
|
49
|
|
|
{ |
|
50
|
|
|
parent::__construct($name, 0, 0, 0, 0, $context); |
|
51
|
|
|
$this->maxUpdateFrequency = $maxUpdateFrequency; |
|
52
|
|
|
|
|
53
|
|
|
foreach ($variables as $variable) { |
|
54
|
|
|
$this->variables[$variable['name']] = new Variable( |
|
55
|
|
|
$variable['name'], |
|
56
|
|
|
$variable['type'], |
|
57
|
|
|
'This', |
|
58
|
|
|
$variable['default'] |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$uniqueId = uniqid('exp_',true); |
|
63
|
|
|
$this->checkVariable = new Variable('check', 'Text', 'This', "\"$uniqueId\""); |
|
64
|
|
|
$this->checkOldVariable = new Variable('check_old', 'Text', 'Page', "\"$uniqueId\""); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Update script value. |
|
69
|
|
|
* |
|
70
|
|
|
* @param Group $group |
|
71
|
|
|
* @param string $variableCode |
|
72
|
|
|
* @param string $newValue |
|
73
|
|
|
*/ |
|
74
|
|
|
public function updateValue($group, $variableCode, $newValue) |
|
75
|
|
|
{ |
|
76
|
|
|
$variable = clone $this->getVariable($variableCode); |
|
77
|
|
|
$variable->setValue($newValue); |
|
78
|
|
|
|
|
79
|
|
|
if (!isset($this->queuedForUpdate[$group->getName()])) { |
|
80
|
|
|
$this->queuedForUpdate[$group->getName()]['time'] = time(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$checkVariable = clone $this->checkVariable; |
|
84
|
|
|
$uniqueId = uniqid('exp_',true); |
|
85
|
|
|
$checkVariable->setValue($uniqueId); |
|
86
|
|
|
|
|
87
|
|
|
$this->queuedForUpdate[$group->getName()]['group'] = $group; |
|
88
|
|
|
$this->queuedForUpdate[$group->getName()]['variables'][$variableCode] = $variable; |
|
89
|
|
|
$this->queuedForUpdate[$group->getName()]['check'] = $checkVariable; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get a variable. |
|
94
|
|
|
* |
|
95
|
|
|
* @param $variable |
|
96
|
|
|
* |
|
97
|
|
|
* @return Variable |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getVariable($variable) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->variables[$variable]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get variable to use to check if data needs to be updated. |
|
106
|
|
|
* |
|
107
|
|
|
* @return Variable |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getCheckVariable() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->checkVariable; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get script to execute when there is a change. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $toExecute Script to execute. |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getScriptOnChange($toExecute) |
|
122
|
|
|
{ |
|
123
|
|
|
return <<<EOL |
|
124
|
|
|
if ({$this->checkVariable->getVariableName()} != {$this->checkOldVariable->getVariableName()}) { |
|
125
|
|
|
{$this->checkOldVariable->getVariableName()} = {$this->checkVariable->getVariableName()}; |
|
126
|
|
|
$toExecute |
|
127
|
|
|
} |
|
128
|
|
|
EOL; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get initialization script. |
|
133
|
|
|
* |
|
134
|
|
|
* @param bool $defaultValues |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getScriptInitialization($defaultValues = false) |
|
138
|
|
|
{ |
|
139
|
|
|
$scriptContent = ''; |
|
140
|
|
|
foreach ($this->variables as $variable) { |
|
141
|
|
|
$scriptContent .= $variable->getScriptDeclaration() . "\n"; |
|
142
|
|
|
if ($defaultValues) { |
|
143
|
|
|
$scriptContent .= $variable->getScriptValueSet() . "\n"; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
$scriptContent .= $this->checkVariable->getScriptDeclaration() . "\n"; |
|
147
|
|
|
$scriptContent .= $this->checkOldVariable->getScriptDeclaration() . "\n"; |
|
148
|
|
|
if ($defaultValues) { |
|
149
|
|
|
$scriptContent .= $this->checkVariable->getScriptValueSet() . "\n"; |
|
150
|
|
|
$scriptContent .= $this->checkOldVariable->getVariableName() . ' = "";'; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $scriptContent; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @inheritdoc |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function updateContent(ManialinkInterface $manialink) |
|
160
|
|
|
{ |
|
161
|
|
|
// Empty existing script. |
|
162
|
|
|
parent::updateContent($manialink); |
|
163
|
|
|
$manialink->getFmlManialink()->removeAllChildren(); |
|
164
|
|
|
|
|
165
|
|
|
// Get script with new values. |
|
166
|
|
|
$scriptContent = $this->getScriptInitialization(true); |
|
167
|
|
|
|
|
168
|
|
|
// Update FML Manialink |
|
169
|
|
|
$script = new Script(); |
|
170
|
|
|
$script->addCustomScriptLabel(ScriptLabel::OnInit, $scriptContent); |
|
171
|
|
|
$manialink->getFmlManialink()->setScript($script); |
|
172
|
|
|
|
|
173
|
|
|
$this->queuedForUpdate = null; |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @inheritdoc |
|
178
|
|
|
*/ |
|
179
|
|
|
public function onPreLoop() |
|
180
|
|
|
{ |
|
181
|
|
|
// Nothing |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @inheritdoc |
|
186
|
|
|
*/ |
|
187
|
|
|
public function onPostLoop() |
|
188
|
|
|
{ |
|
189
|
|
|
// Nothing |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @inheritdoc |
|
194
|
|
|
*/ |
|
195
|
|
|
public function onEverySecond() |
|
196
|
|
|
{ |
|
197
|
|
|
if (!empty($this->queuedForUpdate)) { |
|
198
|
|
|
foreach ($this->queuedForUpdate as $groupName => $updateData) { |
|
199
|
|
|
if (time() - $updateData['time'] > $this->maxUpdateFrequency) { |
|
200
|
|
|
$variables = $this->variables; |
|
201
|
|
|
$checkVariable = $this->checkVariable; |
|
202
|
|
|
|
|
203
|
|
|
// Update variables temporarily with player data. |
|
204
|
|
|
$this->variables = []; |
|
205
|
|
|
foreach ($updateData['variables'] as $variableCode => $variable) { |
|
206
|
|
|
$this->variables[$variableCode] = $variable; |
|
207
|
|
|
} |
|
208
|
|
|
$this->checkVariable = $updateData['check']; |
|
209
|
|
|
$this->update($updateData['group']); |
|
210
|
|
|
|
|
211
|
|
|
// Put back original data. |
|
212
|
|
|
$this->variables = $variables; |
|
213
|
|
|
$this->checkVariable = $checkVariable; |
|
214
|
|
|
|
|
215
|
|
|
unset($this->queuedForUpdate[$groupName]); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @inheritdoc |
|
223
|
|
|
*/ |
|
224
|
|
|
public function onExpansionGroupAddUser(Group $group, $loginAdded) |
|
225
|
|
|
{ |
|
226
|
|
|
// This will be handled by the gui handler. |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @inheritdoc |
|
231
|
|
|
*/ |
|
232
|
|
|
public function onExpansionGroupRemoveUser(Group $group, $loginRemoved) |
|
233
|
|
|
{ |
|
234
|
|
|
// This will be handled by the gui handler. |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @inheritdoc |
|
239
|
|
|
*/ |
|
240
|
|
|
public function onExpansionGroupDestroy(Group $group, $lastLogin) |
|
241
|
|
|
{ |
|
242
|
|
|
if (isset($this->queuedForUpdate[$group->getName()])) { |
|
243
|
|
|
unset($this->queuedForUpdate[$group->getName()]); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.