Completed
Pull Request — master (#219)
by De Cramer
05:06
created

getScriptInitialization()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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