Completed
Push — master ( 220ce5...be682d )
by
unknown
17s
created

MxKarma::onPlayerChat()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 8.8571
ccs 0
cts 15
cp 0
cc 5
eloc 9
nc 6
nop 2
crap 30
1
<?php
2
3
namespace eXpansion\Bundle\MxKarma\Plugins;
4
5
6
use eXpansion\Bundle\MxKarma\DataProviders\Listeners\ListenerInterfaceMxKarma;
7
use eXpansion\Bundle\MxKarma\Entity\MxRating;
8
use eXpansion\Bundle\MxKarma\Entity\MxVote;
9
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpApplication;
10
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceMpLegacyChat;
11
use eXpansion\Framework\Core\Helpers\ChatNotification;
12
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
13
use eXpansion\Framework\Core\Services\Application\Dispatcher;
14
use eXpansion\Framework\Core\Services\Console;
15
use eXpansion\Framework\Core\Storage\Data\Player;
16
use eXpansion\Framework\Core\Storage\PlayerStorage;
17
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMap;
18
use Maniaplanet\DedicatedServer\Structures\Map;
19
use Symfony\Component\Yaml\Yaml;
20
use eXpansion\Bundle\MxKarma\Plugins\Connection as MxConnection;
21
22
class MxKarma implements StatusAwarePluginInterface,
0 ignored issues
show
Coding Style introduced by
The first item in a multi-line implements list must be on the line following the implements keyword
Loading history...
23
    ListenerInterfaceMpScriptMap,
24
    ListenerInterfaceMpLegacyChat,
25
    ListenerInterfaceExpApplication,
26
    ListenerInterfaceMxKarma
27
{
28
29
30
    /** @var MxVote[] */
31
    protected $changedVotes = [];
32
33
    /** @var  int */
34
    private $startTime;
35
    /**
36
     * @var object
37
     */
38
    protected $config;
39
40
    /**
41
     * @var Console
42
     */
43
    protected $console;
44
    /**
45
     * @var Dispatcher
46
     */
47
    protected $dispatcher;
48
    /**
49
     * @var ChatNotification
50
     */
51
    protected $chatNotification;
52
53
    /**
54
     * @var MxConnection
55
     */
56
    private $mxKarma;
57
58
    /** @var  MxRating */
59
    private $mxRating;
60
61
    /** @var  MxVote[] */
62
    private $votes;
63
    /**
64
     * @var PlayerStorage
65
     */
66
    private $playerStorage;
67
68
    /**
69
     * MxKarma constructor.
70
     * @param Connection $mxKarma
71
     * @param Console $console
72
     * @param ChatNotification $chatNotification
73
     * @param Dispatcher $dispatcher
74
     * @param PlayerStorage $playerStorage
75
     */
76
    public function __construct(
77
        MxConnection $mxKarma,
78
        Console $console,
79
        ChatNotification $chatNotification,
80
        Dispatcher $dispatcher,
81
        PlayerStorage $playerStorage
82
    ) {
83
84
        $this->config = (object)Yaml::parse(file_get_contents('./app/config/plugins/mxkarma.yml'))['parameters'];
85
        $this->console = $console;
86
        $this->dispatcher = $dispatcher;
87
        $this->chatNotification = $chatNotification;
88
        $this->mxKarma = $mxKarma;
89
        $this->playerStorage = $playerStorage;
90
    }
91
92
    /**
93
     * sets vote value
94
     * @param $login
95
     * @param $vote
96
     */
97
    public function setVote($login, $vote)
98
    {
99
        $player = $this->playerStorage->getPlayerInfo($login);
100
101
        $obj = [
102
            "login" => $login,
103
            "nickname" => $player->getNickName(),
104
            "vote" => $vote,
105
        ];
106
107
        $this->changedVotes[$player->getLogin()] = new MxVote((object)$obj);
108
        $this->chatNotification->sendMessage('expansion_mxkarma.chat.votechanged', $login);
109
    }
110
111
112
    /**
113
     * Set the status of the plugin
114
     *
115
     * @param boolean $status
116
     *
117
     * @return null
118
     */
119
    public function setStatus($status)
120
    {
121
        // TODO: Implement setStatus() method.
122
    }
123
124
    /**
125
     * Called when a player chats.
126
     *
127
     * @param Player $player
128
     * @param $text
129
     *
130
     * @return void
131
     */
132
    public function onPlayerChat(Player $player, $text)
133
    {
134
        if ($player->getPlayerId() === 0) {
135
            return;
136
        }
137
        if (substr($text, 0, 1) == "/") {
138
            return;
139
        }
140
141
        if ($text == "++") {
142
            $this->setVote($player->getLogin(), 100);
143
        }
144
145
        if ($text == "--") {
146
            $this->setVote($player->getLogin(), 0);
147
        }
148
149
    }
150
151
152
    /**
153
     * called at eXpansion init
154
     *
155
     * @return void
156
     */
157
    public function onApplicationInit()
158
    {
159
        // TODO: Implement onApplicationInit() method.
160
    }
161
162
    /**
163
     * called when init is done and callbacks are enabled
164
     *
165
     * @return void
166
     */
167
    public function onApplicationReady()
168
    {
169
        $this->startTime = time();
170
        $this->mxKarma->connect($this->config->serverlogin, $this->config->apikey);
171
172
    }
173
174
    /**
175
     * called when requesting application stop
176
     *
177
     * @return void
178
     */
179
    public function onApplicationStop()
180
    {
181
        // TODO: Implement onApplicationStop() method.
182
    }
183
184
    /**
185
     *
186
     */
187
    public function onMxKarmaConnect()
188
    {
189
        $this->mxKarma->loadVotes(array_keys($this->playerStorage->getOnline()), false);
190
    }
191
192
    /**
193
     * @param MxRating $mxRating
194
     * @return mixed
195
     */
196
    public function onMxKarmaVoteLoad(MxRating $mxRating)
197
    {
198
199
        $this->mxRating = $mxRating;
200
        $this->changedVotes = [];
201
        $this->votes = $mxRating->getVotes();
202
203
    }
204
205
    /**
206
     * @param MxVote[] $updatedVotes
207
     * @return mixed
208
     */
209
    public function onMxKarmaVoteSave($updatedVotes)
210
    {
211
        // TODO: Implement onMxKarmaVoteSave() method.
212
    }
213
214
    public function onMxKarmaDisconnect()
215
    {
216
        // TODO: Implement onMxKarmaDisconnect() method.
217
    }
218
219
    /**
220
     * Callback sent when the "StartMap" section start.
221
     *
222
     * @param int $count Each time this section is played, this number is incremented by one
223
     * @param int $time Server time when the callback was sent
224
     * @param boolean $restarted true if the map was restarted, false otherwise
225
     * @param Map $map Map started with.
226
     *
227
     * @return mixed
228
     */
229
    public function onStartMapStart($count, $time, $restarted, Map $map)
230
    {
231
        // TODO: Implement onStartMapStart() method.
232
    }
233
234
    /**
235
     * Callback sent when the "StartMap" section end.
236
     *
237
     * @param int $count Each time this section is played, this number is incremented by one
238
     * @param int $time Server time when the callback was sent
239
     * @param boolean $restarted true if the map was restarted, false otherwise
240
     * @param Map $map Map started with.
241
     *
242
     * @return mixed
243
     */
244
    public function onStartMapEnd($count, $time, $restarted, Map $map)
245
    {
246
        $this->startTime = time();
247
        $this->mxKarma->loadVotes(array_keys($this->playerStorage->getOnline()), false);
248
    }
249
250
    /**
251
     * Callback sent when the "EndMap" section start.
252
     *
253
     * @param int $count Each time this section is played, this number is incremented by one
254
     * @param int $time Server time when the callback was sent
255
     * @param boolean $restarted true if the map was restarted, false otherwise
256
     * @param Map $map Map started with.
257
     *
258
     * @return mixed
259
     */
260
    public function onEndMapStart($count, $time, $restarted, Map $map)
261
    {
262
263
    }
264
265
    /**
266
     * Callback sent when the "EndMap" section end.
267
     *
268
     * @param int $count Each time this section is played, this number is incremented by one
269
     * @param int $time Server time when the callback was sent
270
     * @param boolean $restarted true if the map was restarted, false otherwise
271
     * @param Map $map Map started with.
272
     *
273
     * @return mixed
274
     */
275
    public function onEndMapEnd($count, $time, $restarted, Map $map)
276
    {
277
278
        if (!empty($this->changedVotes)) {
279
            $votes = [];
280
            foreach ($this->changedVotes as $vote) {
281
                $votes[] = $vote;
282
            }
283
284
            $this->mxKarma->saveVotes($map, (time() - $this->startTime), $votes);
285
        }
286
    }
287
288
}
289