1 | <?php |
||
23 | class PlayerStorage implements ListenerInterfaceMpLegacyPlayer, ListenerInterfaceExpTimer |
||
24 | { |
||
25 | /** @var Factory */ |
||
26 | protected $factory; |
||
27 | |||
28 | /** @var PlayerFactory */ |
||
29 | protected $playerFactory; |
||
30 | |||
31 | /** @var Player[] List of all the players on the server. */ |
||
32 | protected $online = []; |
||
33 | |||
34 | /** @var Player[] List of all the players playing on the server. */ |
||
35 | protected $players = []; |
||
36 | |||
37 | /** @var Player[] List of all spectators on the server. */ |
||
38 | protected $spectators = []; |
||
39 | |||
40 | /** @var array */ |
||
41 | protected $playersToRemove = []; |
||
42 | /** |
||
43 | * @var LoggerInterface |
||
44 | */ |
||
45 | private $logger; |
||
46 | /** |
||
47 | * @var Console |
||
48 | */ |
||
49 | private $console; |
||
50 | |||
51 | /** |
||
52 | * PlayerDataProvider constructor. |
||
53 | * |
||
54 | * @param Factory $factory |
||
55 | * @param PlayerFactory $playerFactory |
||
56 | * @param LoggerInterface $logger |
||
57 | * @param Console $console |
||
58 | */ |
||
59 | 12 | public function __construct( |
|
60 | Factory $factory, |
||
61 | PlayerFactory $playerFactory, |
||
62 | LoggerInterface $logger, |
||
63 | Console $console |
||
64 | ) { |
||
65 | 12 | $this->factory = $factory; |
|
66 | 12 | $this->playerFactory = $playerFactory; |
|
67 | 12 | $this->logger = $logger; |
|
68 | 12 | $this->console = $console; |
|
69 | 12 | } |
|
70 | |||
71 | /** |
||
72 | * Get information about a player. |
||
73 | * |
||
74 | * @param string $login |
||
75 | * @param bool $forceNew |
||
76 | * |
||
77 | * @return Player |
||
78 | */ |
||
79 | 3 | public function getPlayerInfo($login, $forceNew = false) |
|
80 | { |
||
81 | 3 | if (!isset($this->online[$login]) || $forceNew) { |
|
82 | // to make sure even if an exception happens, player has login and basic nickname |
||
83 | 1 | $playerInformation = new PlayerInfo(); |
|
84 | 1 | $playerInformation->login = $login; |
|
85 | 1 | $playerDetails = new PlayerDetailedInfo(); |
|
86 | 1 | $playerDetails->nickName = $login; |
|
87 | |||
88 | try { |
||
89 | //fetch additional informations |
||
90 | 1 | $playerInformation = $this->factory->getConnection()->getPlayerInfo($login); |
|
91 | 1 | $playerDetails = $this->factory->getConnection()->getDetailedPlayerInfo($login); |
|
92 | } catch (InvalidArgumentException $e) { |
||
93 | $this->logger->error("Login unknown: $login", ["exception" => $e]); |
||
94 | $this->console->writeln('$f00Login Unknown: '.$login.' dedicated server said: $fff'.$e->getMessage()); |
||
95 | } catch (FaultException $ex) { |
||
96 | $this->logger->error("Login unknown: $login", ["exception" => $ex]); |
||
97 | $this->console->writeln('$f00Login Unknown: '.$login.' dedicated server said: $fff'.$ex->getMessage()); |
||
98 | } |
||
99 | 1 | return $this->playerFactory->createPlayer($playerInformation, $playerDetails); |
|
100 | } |
||
101 | |||
102 | 2 | return $this->online[$login]; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Fetch player data & store it when player connects. |
||
107 | * |
||
108 | * @inheritdoc |
||
109 | */ |
||
110 | 4 | public function onPlayerConnect(Player $playerData) |
|
111 | { |
||
112 | 4 | $login = $playerData->getLogin(); |
|
113 | |||
114 | 4 | $this->online[$login] = $playerData; |
|
115 | |||
116 | 4 | if ($playerData->isSpectator()) { |
|
117 | 2 | $this->spectators[$login] = $playerData; |
|
118 | } else { |
||
119 | 4 | $this->players[$login] = $playerData; |
|
120 | } |
||
121 | 4 | } |
|
122 | |||
123 | /** |
||
124 | * Remove player data when he disconnects. |
||
125 | * |
||
126 | * @inheritdoc |
||
127 | */ |
||
128 | 1 | public function onPlayerDisconnect(Player $playerData, $disconnectionReason) |
|
129 | { |
||
130 | 1 | $this->playersToRemove[] = $playerData->getLogin(); |
|
131 | 1 | } |
|
132 | |||
133 | /** |
||
134 | * Change the status of the players. |
||
135 | * |
||
136 | * @inheritdoc |
||
137 | */ |
||
138 | 1 | public function onPlayerInfoChanged(Player $oldPlayer, Player $player) |
|
139 | { |
||
140 | 1 | unset($this->players[$player->getLogin()]); |
|
141 | 1 | unset($this->spectators[$player->getLogin()]); |
|
142 | |||
143 | 1 | $this->onPlayerConnect($player); |
|
144 | 1 | } |
|
145 | |||
146 | /** |
||
147 | * Modify the player object. |
||
148 | * |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | 1 | public function onPlayerAlliesChanged(Player $oldPlayer, Player $player) |
|
152 | { |
||
153 | 1 | $this->onPlayerConnect($player); |
|
154 | 1 | } |
|
155 | |||
156 | /** |
||
157 | * @return Player[] |
||
158 | */ |
||
159 | 4 | public function getOnline() |
|
160 | { |
||
161 | 4 | return $this->online; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * @return Player[] |
||
166 | */ |
||
167 | 4 | public function getPlayers() |
|
168 | { |
||
169 | 4 | return $this->players; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return Player[] |
||
174 | */ |
||
175 | 1 | public function getSpectators() |
|
176 | { |
||
177 | 1 | return $this->spectators; |
|
178 | } |
||
179 | |||
180 | |||
181 | 1 | public function onPreLoop() |
|
182 | { |
||
183 | 1 | foreach ($this->playersToRemove as $login) { |
|
184 | 1 | unset($this->online[$login]); |
|
185 | 1 | unset($this->spectators[$login]); |
|
186 | 1 | unset($this->players[$login]); |
|
187 | } |
||
188 | |||
189 | 1 | $this->playersToRemove = []; |
|
190 | 1 | } |
|
191 | |||
192 | public function onPostLoop() |
||
196 | |||
197 | public function onEverySecond() |
||
201 | |||
202 | /** |
||
203 | * @return array |
||
204 | */ |
||
205 | 1 | public function getPlayersToRemove() |
|
206 | { |
||
207 | 1 | return $this->playersToRemove; |
|
208 | } |
||
209 | } |
||
210 |