Completed
Pull Request — master (#75)
by
unknown
03:05
created

PlayerStorage::onPlayerAlliesChanged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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