Completed
Pull Request — master (#171)
by
unknown
08:37 queued 05:06
created

PlayerStorage::getPlayerInfo()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 8.125

Importance

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