Completed
Push — master ( 586b8e...cb0e4b )
by De Cramer
04:39 queued 53s
created

Player::onPlayerDisconnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
namespace eXpansion\Framework\PlayersBundle\Plugins;
4
5
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceMpLegacyPlayer;
6
use eXpansion\Framework\Core\Helpers\TMString;
7
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
8
use \eXpansion\Framework\Core\Storage\Data\Player as PlayerData;
9
use eXpansion\Framework\Core\Storage\PlayerStorage;
10
use eXpansion\Framework\GameManiaplanet\DataProviders\Listener\ListenerInterfaceMpScriptMatch;
11
use eXpansion\Framework\GameManiaplanet\ScriptMethods\GetScores;
12
use eXpansion\Framework\PlayersBundle\Repository\PlayerRepository;
13
use \eXpansion\Framework\PlayersBundle\Entity\Player as PlayerEntity;
14
15
16
/**
17
 * Class Player
18
 *
19
 * @package eXpansion\Framework\PlayersBundle\Plugins;
20
 * @author  oliver de Cramer <[email protected]>
21
 */
22
class Player implements ListenerInterfaceMpLegacyPlayer, ListenerInterfaceMpScriptMatch, StatusAwarePluginInterface
23
{
24
    /** @var PlayerRepository */
25
    protected $playerRepository;
26
27
    /** @var GetScores  */
28
    protected $getScores;
29
30
    /** @var PlayerStorage */
31
    protected $playerStorage;
32
33
    /** @var PlayerEntity[] */
34
    protected $loggedInPlayers = [];
35
36
    /** @var int[] Timestamp at which player play time was last updated in DB. */
37
    protected $playerLastUpTime = [];
38
39
    /**
40
     * Player constructor.
41
     *
42
     * @param PlayerRepository $playerRepository
43
     * @param GetScores $getScores
44
     * @param PlayerStorage $playerStorage
45
     */
46 7
    public function __construct(
47
        PlayerRepository $playerRepository,
48
        GetScores $getScores,
49
        PlayerStorage $playerStorage
50
    ) {
51 7
        $this->playerRepository = $playerRepository;
52 7
        $this->getScores = $getScores;
53 7
        $this->playerStorage = $playerStorage;
54 7
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 1
    public function setStatus($status)
60
    {
61 1
        if ($status) {
62 1
            foreach ($this->playerStorage->getOnline() as $player) {
63 1
                $this->onPlayerConnect($player);
64
            }
65
        }
66 1
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71 6
    public function onPlayerConnect(PlayerData $playerData)
72
    {
73 6
        $player = $this->playerRepository->findByLogin($playerData->getLogin());
74 6
        $update = false;
75
76 6
        if (is_null($player)) {
77 3
            $player = new PlayerEntity();
78 3
            $player->setLogin($playerData->getLogin());
79 3
            $player->setNickname($playerData->getNickName());
80 3
            $player->setNicknameStripped(TMString::trimStyles($playerData->getNickName()));
81
82 3
            $update = true;
83
        }
84 6
        $player->setPath($playerData->getPath());
85
86 6
        $this->loggedInPlayers[$player->getLogin()] = $player;
87 6
        $this->playerLastUpTime[$player->getLogin()] = time();
88 6
        $player->setLastOnline(new \DateTime('now'));
89
90 6
        if ($update) {
91 3
            $this->updatePlayer($player);
92 3
            $this->playerRepository->save([$player]);
93
        }
94 6
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99 2
    public function onPlayerDisconnect(PlayerData $player, $disconnectionReason)
100
    {
101 2
        $playerEntity = $this->getPlayer($player->getLogin());
102 2
        $this->updatePlayer($playerEntity);
0 ignored issues
show
Bug introduced by
It seems like $playerEntity defined by $this->getPlayer($player->getLogin()) on line 101 can be null; however, eXpansion\Framework\Play...\Player::updatePlayer() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
103 2
        $this->playerRepository->save([$playerEntity]);
0 ignored issues
show
Documentation introduced by
array($playerEntity) is of type array<integer,object<eXp...Entity\\Player>|null"}>, but the function expects a array<integer,object<eXp...sBundle\Entity\Player>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
105 2
        unset($this->playerLastUpTime[$player->getLogin()]);
106 2
        unset($this->loggedInPlayers[$player->getLogin()]);
107 2
    }
108
109
    /**
110
     * @inheritdoc
111
     */
112 1
    public function onEndMatchEnd($count, $time)
113
    {
114 1
        $object = $this;
115 1
        $this->getScores->get(function($scores) use($object) {
116
117 1
            var_dump($scores);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($scores); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
118
119 1
            $object->updateWithScores($scores);
120 1
        });
121 1
    }
122
123
    /**
124
     * Update when scores is available.
125
     *
126
     * @param $scores
127
     */
128 1
    public function updateWithScores($scores)
129
    {
130
        // Update the winner player.
131 1
        $player = $this->getPlayer($scores['winnerplayer']);
132 1
        if ($player) {
133 1
            $player->setWins($player->getWins() + 1);
134 1
            $this->updatePlayer($player);
135 1
            $this->playerRepository->save([$player]);
136
        }
137
138
        // Update remaining players.
139 1
        foreach ($this->loggedInPlayers as $player) {
140 1
            $this->updatePlayer($player);
141
        }
142
143 1
        $this->playerRepository->save(array_values($this->loggedInPlayers));
144 1
    }
145
146
    /**
147
     * Update player information.
148
     *
149
     * @param PlayerEntity $player Login of the player.
150
     */
151 5
    protected function updatePlayer($player)
152
    {
153 5
        $time = time();
154 5
        $upTime = $time - $this->playerLastUpTime[$player->getLogin()];
155 5
        $this->playerLastUpTime[$player->getLogin()] = $time;
156
157 5
        $player->setOnlineTime($player->getOnlineTime() + $upTime);
158 5
    }
159
160
    /**
161
     * Get data on a player.
162
     *
163
     * @param string $login Login of the player.
164
     *
165
     * @return PlayerEntity
166
     */
167 6
    public function getPlayer($login)
168
    {
169 6
        if (isset($this->loggedInPlayers[$login])) {
170 6
            return $this->loggedInPlayers[$login];
171
        }
172
173 3
        return $this->playerRepository->findByLogin($login);
174
    }
175
176
    /**
177
     * @inheritdoc
178
     */
179 1
    public function onPlayerInfoChanged(PlayerData $oldPlayer, PlayerData $player)
180
    {
181
        // Nothing to do here.
182 1
    }
183
184
    /**
185
     * @inheritdoc
186
     */
187 1
    public function onPlayerAlliesChanged(PlayerData $oldPlayer, PlayerData $player)
188
    {
189
        // Nothing to do here.
190 1
    }
191
192
    /**
193
     * @inheritdoc
194
     */
195 1
    public function onStartMatchStart($count, $time)
196
    {
197
        // Nothing to do here.
198 1
    }
199
200
    /**
201
     * @inheritdoc
202
     */
203 1
    public function onStartMatchEnd($count, $time)
204
    {
205
        // Nothing to do here.
206 1
    }
207
208
    /**
209
     * @inheritdoc
210
     */
211 1
    public function onEndMatchStart($count, $time)
212
    {
213
        // Nothing to do here.
214 1
    }
215
216
217
    /**
218
     * @inheritdoc
219
     */
220 1
    public function onStartTurnStart($count, $time)
221
    {
222
        // Nothing to do here.
223 1
    }
224
225
    /**
226
     * @inheritdoc
227
     */
228 1
    public function onStartTurnEnd($count, $time)
229
    {
230
        // Nothing to do here.
231 1
    }
232
233
    /**
234
     * @inheritdoc
235
     */
236 1
    public function onEndTurnStart($count, $time)
237
    {
238
        // Nothing to do here.
239 1
    }
240
241
    /**
242
     * @inheritdoc
243
     */
244 1
    public function onEndTurnEnd($count, $time)
245
    {
246
        // Nothing to do here.
247 1
    }
248
249
    /**
250
     * @inheritdoc
251
     */
252 1
    public function onStartRoundStart($count, $time)
253
    {
254
        // Nothing to do here.
255 1
    }
256
257
    /**
258
     * @inheritdoc
259
     */
260 1
    public function onStartRoundEnd($count, $time)
261
    {
262
        // Nothing to do here.
263 1
    }
264
265
    /**
266
     * @inheritdoc
267
     */
268 1
    public function onEndRoundStart($count, $time)
269
    {
270
        // Nothing to do here.
271 1
    }
272
273
    /**
274
     * @inheritdoc
275
     */
276 1
    public function onEndRoundEnd($count, $time)
277
    {
278
        // Nothing to do here.
279 1
    }
280
}
281