Completed
Pull Request — master (#131)
by De Cramer
05:13 queued 02:26
created

Player::updatePlayer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
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\Model\Player as PlayerModel;
13
use eXpansion\Framework\PlayersBundle\Model\PlayerQueryBuilder;
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 PlayerQueryBuilder */
25
    protected $playerQueryBuilder;
26
27
    /** @var GetScores  */
28
    protected $getScores;
29
30
    /** @var PlayerStorage */
31
    protected $playerStorage;
32
33
    /** @var PlayerModel[] */
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 GetScores $getScores
43
     * @param PlayerStorage $playerStorage
44
     */
45 7
    public function __construct(
46
        PlayerQueryBuilder $playerQueryBuilder,
47
        GetScores $getScores,
48
        PlayerStorage $playerStorage
49
    ) {
50 7
        $this->playerQueryBuilder = $playerQueryBuilder;
51 7
        $this->getScores = $getScores;
52 7
        $this->playerStorage = $playerStorage;
53 7
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 1
    public function setStatus($status)
59
    {
60 1
        if ($status) {
61 1
            foreach ($this->playerStorage->getOnline() as $player) {
62 1
                $this->onPlayerConnect($player);
63
            }
64
        }
65 1
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 6
    public function onPlayerConnect(PlayerData $playerData)
71
    {
72 6
        $player = $this->playerQueryBuilder->findByLogin($playerData->getLogin());
73 6
        $update = false;
74
75 6
        if (is_null($player)) {
76 3
            $player = new PlayerModel();
77 3
            $player->setLogin($playerData->getLogin());
78 3
            $update = true;
79
        }
80 6
        $player->setNickname($playerData->getNickName());
81 6
        $player->setNicknameStripped(TMString::trimStyles($playerData->getNickName()));
82 6
        $player->setPath($playerData->getPath());
83
84 6
        $this->loggedInPlayers[$player->getLogin()] = $player;
85 6
        $this->playerLastUpTime[$player->getLogin()] = time();
86 6
        $player->setLastOnline(new \DateTime('now'));
87
88 6
        if ($update) {
89 3
            $this->updatePlayer($player);
90 3
            $this->playerQueryBuilder->save($player);
91
        }
92 6
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97 2
    public function onPlayerDisconnect(PlayerData $player, $disconnectionReason)
98
    {
99 2
        $playerModel = $this->getPlayer($player->getLogin());
100 2
        $this->updatePlayer($playerModel);
101 2
        $this->playerQueryBuilder->save($playerModel);
102
103 2
        unset($this->playerLastUpTime[$player->getLogin()]);
104 2
        unset($this->loggedInPlayers[$player->getLogin()]);
105 2
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110 1
    public function onEndMatchEnd($count, $time)
111
    {
112 1
        $object = $this;
113 1
        $this->getScores->get(function($scores) use($object) {
114 1
            $object->updateWithScores($scores);
115 1
        });
116 1
    }
117
118
    /**
119
     * Update when scores is available.
120
     *
121
     * @param $scores
122
     */
123 1
    public function updateWithScores($scores)
124
    {
125
        // Update the winner player.
126 1
        $player = $this->getPlayer($scores['winnerplayer']);
127 1
        if ($player) {
128 1
            $this->playerQueryBuilder->save($player);
129
        }
130
131
        // Update remaining players.
132 1
        foreach ($this->loggedInPlayers as $player) {
133 1
            $this->updatePlayer($player);
134
        }
135 1
        $this->playerQueryBuilder->saveAll($this->loggedInPlayers);
136 1
    }
137
138
    /**
139
     * Update player information.
140
     *
141
     * @param PlayerModel $player Login of the player.
142
     */
143 5
    protected function updatePlayer($player)
144
    {
145 5
        $time = time();
146 5
        $upTime = $time - $this->playerLastUpTime[$player->getLogin()];
147 5
        $this->playerLastUpTime[$player->getLogin()] = $time;
148
149 5
        $player->setOnlineTime($player->getOnlineTime() + $upTime);
150 5
    }
151
152
    /**
153
     * Get data on a player.
154
     *
155
     * @param string $login Login of the player.
156
     *
157
     * @return PlayerModel
158
     */
159 6
    public function getPlayer($login)
160
    {
161 6
        if (isset($this->loggedInPlayers[$login])) {
162 6
            return $this->loggedInPlayers[$login];
163
        }
164
165 3
        return $this->playerQueryBuilder->findByLogin($login);
166
    }
167
168
    /**
169
     * @inheritdoc
170
     */
171 1
    public function onPlayerInfoChanged(PlayerData $oldPlayer, PlayerData $player)
172
    {
173
        // Nothing to do here.
174 1
    }
175
176
    /**
177
     * @inheritdoc
178
     */
179 1
    public function onPlayerAlliesChanged(PlayerData $oldPlayer, PlayerData $player)
180
    {
181
        // Nothing to do here.
182 1
    }
183
184
    /**
185
     * @inheritdoc
186
     */
187 1
    public function onStartMatchStart($count, $time)
188
    {
189
        // Nothing to do here.
190 1
    }
191
192
    /**
193
     * @inheritdoc
194
     */
195 1
    public function onStartMatchEnd($count, $time)
196
    {
197
        // Nothing to do here.
198 1
    }
199
200
    /**
201
     * @inheritdoc
202
     */
203 1
    public function onEndMatchStart($count, $time)
204
    {
205
        // Nothing to do here.
206 1
    }
207
208
209
    /**
210
     * @inheritdoc
211
     */
212 1
    public function onStartTurnStart($count, $time)
213
    {
214
        // Nothing to do here.
215 1
    }
216
217
    /**
218
     * @inheritdoc
219
     */
220 1
    public function onStartTurnEnd($count, $time)
221
    {
222
        // Nothing to do here.
223 1
    }
224
225
    /**
226
     * @inheritdoc
227
     */
228 1
    public function onEndTurnStart($count, $time)
229
    {
230
        // Nothing to do here.
231 1
    }
232
233
    /**
234
     * @inheritdoc
235
     */
236 1
    public function onEndTurnEnd($count, $time)
237
    {
238
        // Nothing to do here.
239 1
    }
240
241
    /**
242
     * @inheritdoc
243
     */
244 1
    public function onStartRoundStart($count, $time)
245
    {
246
        // Nothing to do here.
247 1
    }
248
249
    /**
250
     * @inheritdoc
251
     */
252 1
    public function onStartRoundEnd($count, $time)
253
    {
254
        // Nothing to do here.
255 1
    }
256
257
    /**
258
     * @inheritdoc
259
     */
260 1
    public function onEndRoundStart($count, $time)
261
    {
262
        // Nothing to do here.
263 1
    }
264
265
    /**
266
     * @inheritdoc
267
     */
268 1
    public function onEndRoundEnd($count, $time)
269
    {
270
        // Nothing to do here.
271 1
    }
272
}
273