Completed
Pull Request — master (#219)
by De Cramer
03:20
created

ScriptVariableUpdateFactory::getVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Plugins\Gui;
4
5
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
6
use eXpansion\Framework\Core\Model\Gui\ManialinkFactoryContext;
7
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
8
use eXpansion\Framework\Core\Model\Gui\Script\Variable;
9
use eXpansion\Framework\Core\Model\Gui\WidgetFactoryContext;
10
use eXpansion\Framework\Core\Model\UserGroups\Group;
11
use FML\Script\Script;
12
use FML\Script\ScriptLabel;
13
14
/**
15
 * Class ScriptVariableUpdateFactory
16
 *
17
 * @author    de Cramer Oliver<[email protected]>
18
 * @copyright 2018 eXpansion
19
 * @package eXpansion\Framework\Core\Plugins\Gui
20
 */
21
class ScriptVariableUpdateFactory extends WidgetFactory implements ListenerInterfaceExpTimer
22
{
23
    /** @var Variable[] */
24
    protected $variables = [];
25
26
    /** @var Variable */
27
    protected $checkVariable;
28
29
    /** @var int */
30
    protected $maxUpdateFrequency;
31
32
    /** @var Variable */
33
    protected $checkOldVariable;
34
35
    /** @var Group */
36
    protected $playerGroup;
37
38
    protected $queuedForUpdate = null;
39
40
    /**
41
     * ScriptVariableUpdateFactory constructor.
42
     *
43
     * @param                      $name
44
     * @param array                $variables
45
     * @param int                  $maxUpdateFrequency
46
     * @param Group                $playerGroup
47
     * @param WidgetFactoryContext $context
48
     */
49
    public function __construct($name, array  $variables, int $maxUpdateFrequency = 5, Group $playerGroup, WidgetFactoryContext $context)
50
    {
51
        parent::__construct($name, 0, 0, 0, 0, $context);
52
        $this->playerGroup = $playerGroup;
53
        $this->maxUpdateFrequency = $maxUpdateFrequency;
54
55
        foreach ($variables as $variable) {
56
            $this->variables[$variable['name']] = new Variable(
57
                $variable['name'],
58
                $variable['type'],
59
                'This',
60
                $variable['default']
61
            );
62
        }
63
64
        $uniqueId = uniqid('exp_');
65
        $this->checkVariable = new Variable('check', 'Text', 'This', "\"$uniqueId\"");
66
        $this->checkOldVariable = new Variable('check_old', 'Text', 'This', "\"$uniqueId\"");
67
    }
68
69
    /**
70
     * Update script value.
71
     *
72
     * @param $variable
73
     * @param $newValue
74
     */
75
    public function updateValue($variable, $newValue)
76
    {
77
        if ($this->variables[$variable]->getValue() != $newValue) {
78
            $this->variables[$variable]->setValue($newValue);
79
            $uniqueId = '"' . uniqid('exp_') . '"';
80
            $this->checkVariable->setValue($uniqueId);
81
82
            if (is_null($this->queuedForUpdate)) {
83
                $this->queuedForUpdate = time();
84
            }
85
        }
86
87
    }
88
89
    /**
90
     * Get a variable.
91
     *
92
     * @param $variable
93
     *
94
     * @return Variable
95
     */
96
    public function getVariable($variable)
97
    {
98
        return $this->variables[$variable];
99
    }
100
101
    /**
102
     * Get variable to use to check if data needs to be updated.
103
     *
104
     * @return Variable
105
     */
106
    public function getCheckVariable()
107
    {
108
        return $this->checkVariable;
109
    }
110
111
    /**
112
     * Get script to execute when there is a change.
113
     *
114
     * @param string $toExecute Script to execute.
115
     *
116
     * @return string
117
     */
118
    public function getScriptOnChange($toExecute)
119
    {
120
        return <<<EOL
121
            if ({$this->checkVariable->getVariableName()} != {$this->checkOldVariable->getVariableName()}) {
122
                {$this->checkOldVariable->getVariableName()} = {$this->checkVariable->getVariableName()};
123
                $toExecute
124
            }
125
EOL;
126
    }
127
128
    /**
129
     * Get initialization script.
130
     *
131
     * @return string
132
     */
133
    public function getScriptInitialization($defaultValues = false)
134
    {
135
        $scriptContent = '';
136
        foreach ($this->variables as $variable) {
137
            $scriptContent .= $variable->getScriptDeclaration() . "\n";
138
            if ($defaultValues) {
139
                $scriptContent .= $variable->getScriptValueSet() . "\n";
140
            }
141
        }
142
        $scriptContent .= $this->checkVariable->getScriptDeclaration() . "\n";
143
        $scriptContent .= $this->checkOldVariable->getScriptDeclaration() . "\n";
144
        if ($defaultValues) {
145
            $scriptContent .= $this->checkVariable->getScriptValueSet() . "\n";
146
            $scriptContent .= $this->checkOldVariable->getVariableName() . ' = "";';
147
        }
148
149
        return $scriptContent;
150
    }
151
152
    /**
153
     * @inheritdoc
154
     */
155
    protected function updateContent(ManialinkInterface $manialink)
156
    {
157
        // Empty existing script.
158
        parent::updateContent($manialink);
159
        $manialink->getFmlManialink()->removeAllChildren();
160
161
        // Get script with new values.
162
        $scriptContent = $this->getScriptInitialization(true);
163
164
        // Update FML Manialink
165
        $script = new Script();
166
        $script->addCustomScriptLabel(ScriptLabel::OnInit, $scriptContent);
167
        $manialink->getFmlManialink()->setScript($script);
168
169
        $this->queuedForUpdate = null;
170
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175
    public function create($group = null)
176
    {
177
        return parent::create($this->playerGroup);
178
    }
179
180
    /**
181
     * @inheritdoc
182
     */
183
    public function update($group = null)
184
    {
185
        parent::update($this->playerGroup);
186
    }
187
188
    /**
189
     * @inheritdoc
190
     */
191
    public function destroy(Group $group = null)
192
    {
193
        parent::destroy($this->playerGroup);
194
    }
195
196
    /**
197
     * @inheritdoc
198
     */
199
    public function onPreLoop()
200
    {
201
        // Nothing
202
    }
203
204
    /**
205
     * @inheritdoc
206
     */
207
    public function onPostLoop()
208
    {
209
        // Nothing
210
    }
211
212
    /**
213
     * @inheritdoc
214
     */
215
    public function onEverySecond()
216
    {
217
        if (!is_null($this->queuedForUpdate)) {
218
            if (time() - $this->queuedForUpdate > $this->maxUpdateFrequency) {
219
                $this->update($this->playerGroup);
220
            }
221
        }
222
    }
223
}
224