Completed
Pull Request — master (#104)
by
unknown
03:28
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 eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMatch;
19
use Maniaplanet\DedicatedServer\Structures\Map;
20
use Symfony\Component\Yaml\Yaml;
21
use eXpansion\Bundle\MxKarma\Plugins\Connection as MxConnection;
22
23
class MxKarma implements StatusAwarePluginInterface, ListenerInterfaceMpScriptMap,
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...
Coding Style introduced by
Only one interface may be specified per line in a multi-line implements declaration
Loading history...
24
    ListenerInterfaceMpLegacyChat, ListenerInterfaceExpApplication, ListenerInterfaceMxKarma
0 ignored issues
show
Coding Style introduced by
Only one interface may be specified per line in a multi-line implements declaration
Loading history...
25
{
26
27
28
    /** @var MxVote */
29
    protected $changedVotes = [];
30
31
    /** @var  int */
32
    private $startTime;
33
    /**
34
     * @var object
35
     */
36
    protected $config;
37
38
    /**
39
     * @var Console
40
     */
41
    protected $console;
42
    /**
43
     * @var Dispatcher
44
     */
45
    protected $dispatcher;
46
    /**
47
     * @var ChatNotification
48
     */
49
    protected $chatNotification;
50
51
    /**
52
     * @var MxConnection
53
     */
54
    private $mxKarma;
55
56
    /** @var  MxRating */
57
    private $mxRating;
58
59
    /** @var  MxVote[] */
60
    private $votes;
61
    /**
62
     * @var PlayerStorage
63
     */
64
    private $playerStorage;
65
66
    /**
67
     * MxKarma constructor.
68
     * @param Connection $mxKarma
69
     * @param Console $console
70
     * @param ChatNotification $chatNotification
71
     * @param Dispatcher $dispatcher
72
     * @param PlayerStorage $playerStorage
73
     */
74
    public function __construct(
75
        MxConnection $mxKarma,
76
        Console $console,
77
        ChatNotification $chatNotification,
78
        Dispatcher $dispatcher,
79
        PlayerStorage $playerStorage
80
    ) {
81
82
        $this->config = (object)Yaml::parse(file_get_contents('./app/config/plugins/mxkarma.yml'))['parameters'];
83
        $this->console = $console;
84
        $this->dispatcher = $dispatcher;
85
        $this->chatNotification = $chatNotification;
86
        $this->mxKarma = $mxKarma;
87
        $this->playerStorage = $playerStorage;
88
    }
89
90
    public function setVote($login, $vote)
91
    {
92
        $player = $this->playerStorage->getPlayerInfo($login);
93
94
        $obj = [
95
            "login" => $login,
96
            "nickname" => $player->getNickName(),
97
            "vote" => $vote,
98
        ];
99
100
        $this->changedVotes[$player->getLogin()] = new MxVote((object)$obj);
101
        $this->chatNotification->sendMessage('expansion_mxkarma.chat.votechanged', $login);
102
    }
103
104
105
    /**
106
     * Set the status of the plugin
107
     *
108
     * @param boolean $status
109
     *
110
     * @return null
111
     */
112
    public function setStatus($status)
113
    {
114
        // TODO: Implement setStatus() method.
115
    }
116
117
    /**
118
     * Called when a player chats.
119
     *
120
     * @param Player $player
121
     * @param $text
122
     *
123
     * @return void
124
     */
125
    public function onPlayerChat(Player $player, $text)
126
    {
127
        if ($player->getPlayerId() === 0) {
128
            return;
129
        }
130
        if (substr($text, 0, 1) == "/") {
131
            return;
132
        }
133
134
        if ($text == "++") {
135
            $this->setVote($player->getLogin(), 100);
136
        }
137
138
        if ($text == "--") {
139
            $this->setVote($player->getLogin(), 0);
140
        }
141
142
    }
143
144
145
    /**
146
     * called at eXpansion init
147
     *
148
     * @return void
149
     */
150
    public function onApplicationInit()
151
    {
152
        // TODO: Implement onApplicationInit() method.
153
    }
154
155
    /**
156
     * called when init is done and callbacks are enabled
157
     *
158
     * @return void
159
     */
160
    public function onApplicationReady()
161
    {
162
        $this->startTime = time();
163
        $this->mxKarma->connect($this->config->serverlogin, $this->config->apikey);
164
165
    }
166
167
    /**
168
     * called when requesting application stop
169
     *
170
     * @return void
171
     */
172
    public function onApplicationStop()
173
    {
174
        // TODO: Implement onApplicationStop() method.
175
    }
176
177
    /**
178
     *
179
     */
180
    public function onMxKarmaConnect()
181
    {
182
        $this->mxKarma->loadVotes(array_keys($this->playerStorage->getOnline()), false);
183
    }
184
185
    /**
186
     * @param MxRating $ratings
0 ignored issues
show
Bug introduced by
There is no parameter named $ratings. Was it maybe removed?

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 $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
187
     * @return mixed
188
     */
189
    public function onMxKarmaVoteLoad(MxRating $mxRating)
190
    {
191
192
        /** @var MxRating $mxRating */
193
        $this->mxRating = $mxRating;
194
        $this->changedVotes = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type object<eXpansion\Bundle\MxKarma\Entity\MxVote> of property $changedVotes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
195
        print_r($mxRating);
196
197
        $this->votes = $mxRating->getVotes();
198
199
    }
200
201
    /**
202
     * @param MxVote[] $updatedVotes
203
     * @return mixed
204
     */
205
    public function onMxKarmaVoteSave($updatedVotes)
206
    {
207
        // TODO: Implement onMxKarmaVoteSave() method.
208
    }
209
210
    public function onMxKarmaDisconnect()
211
    {
212
        // TODO: Implement onMxKarmaDisconnect() method.
213
    }
214
215
    /**
216
     * Callback sent when the "StartMap" section start.
217
     *
218
     * @param int $count Each time this section is played, this number is incremented by one
219
     * @param int $time Server time when the callback was sent
220
     * @param boolean $restarted true if the map was restarted, false otherwise
221
     * @param Map $map Map started with.
222
     *
223
     * @return mixed
224
     */
225
    public function onStartMapStart($count, $time, $restarted, Map $map)
226
    {
227
        // TODO: Implement onStartMapStart() method.
228
    }
229
230
    /**
231
     * Callback sent when the "StartMap" section end.
232
     *
233
     * @param int $count Each time this section is played, this number is incremented by one
234
     * @param int $time Server time when the callback was sent
235
     * @param boolean $restarted true if the map was restarted, false otherwise
236
     * @param Map $map Map started with.
237
     *
238
     * @return mixed
239
     */
240
    public function onStartMapEnd($count, $time, $restarted, Map $map)
241
    {
242
        $this->startTime = time();
243
        $this->mxKarma->loadVotes(array_keys($this->playerStorage->getOnline()), false);
244
    }
245
246
    /**
247
     * Callback sent when the "EndMap" section start.
248
     *
249
     * @param int $count Each time this section is played, this number is incremented by one
250
     * @param int $time Server time when the callback was sent
251
     * @param boolean $restarted true if the map was restarted, false otherwise
252
     * @param Map $map Map started with.
253
     *
254
     * @return mixed
255
     */
256
    public function onEndMapStart($count, $time, $restarted, Map $map)
257
    {
258
259
    }
260
261
    /**
262
     * Callback sent when the "EndMap" section end.
263
     *
264
     * @param int $count Each time this section is played, this number is incremented by one
265
     * @param int $time Server time when the callback was sent
266
     * @param boolean $restarted true if the map was restarted, false otherwise
267
     * @param Map $map Map started with.
268
     *
269
     * @return mixed
270
     */
271
    public function onEndMapEnd($count, $time, $restarted, Map $map)
272
    {
273
274
        if (!empty($this->changedVotes)) {
275
            $votes = [];
276
            foreach ($this->changedVotes as $vote) {
0 ignored issues
show
Bug introduced by
The expression $this->changedVotes of type object<eXpansion\Bundle\MxKarma\Entity\MxVote> is not traversable.
Loading history...
277
                $votes[] = $vote;
278
            }
279
280
            $this->mxKarma->saveVotes($map, (time() - $this->startTime), $votes);
281
        }
282
    }
283
284
}
285