Completed
Push — master ( cac559...b1b643 )
by De Cramer
12s
created

PlayerStorage::onPlayerDisconnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace eXpansion\Core\Storage;
4
5
use eXpansion\Core\DataProviders\Listener\PlayerDataListenerInterface;
6
use eXpansion\Core\Storage\Data\Player;
7
use eXpansion\Core\Storage\Data\PlayerFactory;
8
use Maniaplanet\DedicatedServer\Connection;
9
10
/**
11
 * PlayerStorage keeps in storage player data in order to minimize amounts of calls done to the dedicated server.
12
 *
13
 * @package eXpansion\Core\Storage
14
 */
15
class PlayerStorage implements PlayerDataListenerInterface
16
{
17
    /** @var  Connection */
18
    protected $connection;
19
20
    /** @var PlayerFactory  */
21
    protected $playerFactory;
22
23
    /** @var Player[] List of all the players on the server. */
24
    protected $online = [];
25
26
    /** @var Player[] List of all the players playing on the server. */
27
    protected $players = [];
28
29
    /** @var Player[] List of all spectators on the server. */
30
    protected $spectators = [];
31
32
    /**
33
     * PlayerDataProvider constructor.
34
     *
35
     * @param Connection $connection
36
     */
37 6
    public function __construct(Connection $connection, PlayerFactory $playerFactory)
38
    {
39 6
        $this->connection = $connection;
40 6
        $this->playerFactory = $playerFactory;
41 6
    }
42
43
    /**
44
     * Get information about a player.
45
     *
46
     * @param $login
47
     *
48
     * @return Player
49
     */
50 3
    public function getPlayerInfo($login, $forceNew = false)
51
    {
52 3
        if (!isset($this->online[$login]) || $forceNew) {
53 1
            $playerInformation = $this->connection->getPlayerInfo($login);
54 1
            $playerDetails = $this->connection->getDetailedPlayerInfo($login);
55
56 1
            return $this->playerFactory->createPlayer($playerInformation, $playerDetails);
57
        }
58
59 2
        return $this->online[$login];
60
    }
61
62
    /**
63
     * Fetch player data & store it when player connects.
64
     *
65
     * @inheritdoc
66
     */
67 4
    public function onPlayerConnect(Player $playerData)
68
    {
69 4
        $login = $playerData->getLogin();
70
71 4
        $this->online[$login] = $playerData;
72
73 4
        if ($playerData->isSpectator()) {
74 2
            $this->spectators[$login] = $playerData;
75 2
        } else {
76 4
            $this->players[$login] = $playerData;
77
        }
78 4
    }
79
80
    /**
81
     * Remove player data when he disconnects.
82
     *
83
     * @inheritdoc
84
     */
85 1
    public function onPlayerDisconnect(Player $playerData, $disconnectionReason)
86
    {
87 1
        unset($this->online[$playerData->getLogin()]);
88 1
        unset($this->spectators[$playerData->getLogin()]);
89 1
        unset($this->players[$playerData->getLogin()]);
90 1
    }
91
92
    /**
93
     * Change the status of the players.
94
     *
95
     * @inheritdoc
96
     */
97 1
    public function onPlayerInfoChanged(Player $oldPlayer, Player $player)
98
    {
99 1
        unset($this->players[$player->getLogin()]);
100 1
        unset($this->spectators[$player->getLogin()]);
101
102 1
        $this->onPlayerConnect($player);
103 1
    }
104
105
    /**
106
     * Modify the player object.
107
     *
108
     * @inheritdoc
109
     */
110 1
    public function onPlayerAlliesChanged(Player $oldPlayer, Player $player)
111
    {
112 1
        $this->onPlayerConnect($player);
113 1
    }
114
115
    /**
116
     * @return Player[]
117
     */
118 4
    public function getOnline()
119
    {
120 4
        return $this->online;
121
    }
122
123
    /**
124
     * @return Player[]
125
     */
126 4
    public function getPlayers()
127
    {
128 4
        return $this->players;
129
    }
130
131
    /**
132
     * @return Player[]
133
     */
134 1
    public function getSpectators()
135
    {
136 1
        return $this->spectators;
137
    }
138
}