Completed
Pull Request — master (#112)
by De Cramer
11:22
created

Player::onStartTurnStart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
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 1
            $object->updateWithScores($scores);
117 1
        });
118 1
    }
119
120
    /**
121
     * Update when scores is available.
122
     *
123
     * @param $scores
124
     */
125 1
    public function updateWithScores($scores)
126
    {
127
        // Update the winner player.
128 1
        $player = $this->getPlayer($scores['winnerplayer']);
129 1
        $player->setWins($player->getWins() + 1);
130 1
        $this->updatePlayer($player);
0 ignored issues
show
Bug introduced by
It seems like $player defined by $this->getPlayer($scores['winnerplayer']) on line 128 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...
131 1
        $this->playerRepository->save([$player]);
0 ignored issues
show
Documentation introduced by
array($player) 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...
132
133
        // Update remaining players.
134 1
        foreach ($this->loggedInPlayers as $player) {
135 1
            $this->updatePlayer($player);
136
        }
137
138 1
        $this->playerRepository->save(array_values($this->loggedInPlayers));
139 1
    }
140
141
    /**
142
     * Update player information.
143
     *
144
     * @param PlayerEntity $player Login of the player.
145
     */
146 5
    protected function updatePlayer($player)
147
    {
148 5
        $time = time();
149 5
        $upTime = $time - $this->playerLastUpTime[$player->getLogin()];
150 5
        $this->playerLastUpTime[$player->getLogin()] = $time;
151
152 5
        $player->setOnlineTime($player->getOnlineTime() + $upTime);
153 5
    }
154
155
    /**
156
     * Get data on a player.
157
     *
158
     * @param string $login Login of the player.
159
     *
160
     * @return PlayerEntity
161
     */
162 6
    public function getPlayer($login)
163
    {
164 6
        if (isset($this->loggedInPlayers[$login])) {
165 6
            return $this->loggedInPlayers[$login];
166
        }
167
168 3
        return $this->playerRepository->findByLogin($login);
169
    }
170
171
    /**
172
     * @inheritdoc
173
     */
174 1
    public function onPlayerInfoChanged(PlayerData $oldPlayer, PlayerData $player)
175
    {
176
        // Nothing to do here.
177 1
    }
178
179
    /**
180
     * @inheritdoc
181
     */
182 1
    public function onPlayerAlliesChanged(PlayerData $oldPlayer, PlayerData $player)
183
    {
184
        // Nothing to do here.
185 1
    }
186
187
    /**
188
     * @inheritdoc
189
     */
190 1
    public function onStartMatchStart($count, $time)
191
    {
192
        // Nothing to do here.
193 1
    }
194
195
    /**
196
     * @inheritdoc
197
     */
198 1
    public function onStartMatchEnd($count, $time)
199
    {
200
        // Nothing to do here.
201 1
    }
202
203
    /**
204
     * @inheritdoc
205
     */
206 1
    public function onEndMatchStart($count, $time)
207
    {
208
        // Nothing to do here.
209 1
    }
210
211
212
    /**
213
     * @inheritdoc
214
     */
215 1
    public function onStartTurnStart($count, $time)
216
    {
217
        // Nothing to do here.
218 1
    }
219
220
    /**
221
     * @inheritdoc
222
     */
223 1
    public function onStartTurnEnd($count, $time)
224
    {
225
        // Nothing to do here.
226 1
    }
227
228
    /**
229
     * @inheritdoc
230
     */
231 1
    public function onEndTurnStart($count, $time)
232
    {
233
        // Nothing to do here.
234 1
    }
235
236
    /**
237
     * @inheritdoc
238
     */
239 1
    public function onEndTurnEnd($count, $time)
240
    {
241
        // Nothing to do here.
242 1
    }
243
244
    /**
245
     * @inheritdoc
246
     */
247 1
    public function onStartRoundStart($count, $time)
248
    {
249
        // Nothing to do here.
250 1
    }
251
252
    /**
253
     * @inheritdoc
254
     */
255 1
    public function onStartRoundEnd($count, $time)
256
    {
257
        // Nothing to do here.
258 1
    }
259
260
    /**
261
     * @inheritdoc
262
     */
263 1
    public function onEndRoundStart($count, $time)
264
    {
265
        // Nothing to do here.
266 1
    }
267
268
    /**
269
     * @inheritdoc
270
     */
271 1
    public function onEndRoundEnd($count, $time)
272
    {
273
        // Nothing to do here.
274 1
    }
275
}
276