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