|
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
|
|
|
public function __construct(Connection $connection, PlayerFactory $playerFactory) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->connection = $connection; |
|
40
|
|
|
$this->playerFactory = $playerFactory; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get information about a player. |
|
45
|
|
|
* |
|
46
|
|
|
* @param $login |
|
47
|
|
|
* |
|
48
|
|
|
* @return Player |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getPlayerInfo($login, $forceNew = false) |
|
51
|
|
|
{ |
|
52
|
|
|
if (!isset($this->online[$login]) || $forceNew) { |
|
53
|
|
|
$playerInformation = $this->connection->getPlayerInfo($login); |
|
54
|
|
|
$playerDetails = $this->connection->getDetailedPlayerInfo($login); |
|
55
|
|
|
|
|
56
|
|
|
return $this->playerFactory->createPlayer($playerInformation, $playerDetails); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->online[$login]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Fetch player data & store it when player connects. |
|
64
|
|
|
* |
|
65
|
|
|
* @inheritdoc |
|
66
|
|
|
*/ |
|
67
|
|
|
public function onPlayerConnect(Player $playerData) |
|
68
|
|
|
{ |
|
69
|
|
|
$login = $playerData->getLogin(); |
|
70
|
|
|
|
|
71
|
|
|
$this->online[$login] = $playerData; |
|
72
|
|
|
|
|
73
|
|
|
if ($playerData->isSpectator()) { |
|
74
|
|
|
$this->spectators[$login] = $playerData; |
|
75
|
|
|
} else { |
|
76
|
|
|
$this->players[$login] = $playerData; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Remove player data when he disconnects. |
|
82
|
|
|
* |
|
83
|
|
|
* @inheritdoc |
|
84
|
|
|
*/ |
|
85
|
|
|
public function onPlayerDisconnect(Player $playerData, $disconnectionReason) |
|
86
|
|
|
{ |
|
87
|
|
|
unset($this->online[$playerData->getLogin()]); |
|
88
|
|
|
unset($this->spectators[$playerData->getLogin()]); |
|
89
|
|
|
unset($this->players[$playerData->getLogin()]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Change the status of the players. |
|
94
|
|
|
* |
|
95
|
|
|
* @inheritdoc |
|
96
|
|
|
*/ |
|
97
|
|
|
public function onPlayerInfoChanged(Player $oldPlayer, Player $player) |
|
98
|
|
|
{ |
|
99
|
|
|
unset($this->players[$player->getLogin()]); |
|
100
|
|
|
unset($this->spectators[$player->getLogin()]); |
|
101
|
|
|
|
|
102
|
|
|
$this->onPlayerConnect($player); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Modify the player object. |
|
107
|
|
|
* |
|
108
|
|
|
* @inheritdoc |
|
109
|
|
|
*/ |
|
110
|
|
|
public function onPlayerAlliesChanged(Player $oldPlayer, Player $player) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->onPlayerConnect($player); |
|
113
|
|
|
} |
|
114
|
|
|
} |