|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Asylamba\Classes\Daemon; |
|
4
|
|
|
|
|
5
|
|
|
use Asylamba\Classes\Daemon\Client; |
|
6
|
|
|
|
|
7
|
|
|
use Asylamba\Classes\Redis\RedisManager; |
|
8
|
|
|
use Asylamba\Classes\Library\Session\SessionWrapper; |
|
9
|
|
|
|
|
10
|
|
|
use Asylamba\Classes\Library\Http\Request; |
|
11
|
|
|
|
|
12
|
|
|
class ClientManager |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var array **/ |
|
15
|
|
|
protected $clients = []; |
|
16
|
|
|
/** @var RedisManager **/ |
|
17
|
|
|
protected $redisManager; |
|
18
|
|
|
/** @var SessionWrapper **/ |
|
19
|
|
|
protected $sessionWrapper; |
|
20
|
|
|
/** @var int **/ |
|
21
|
|
|
protected $sessionLifetime; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param RedisManager $redisManager |
|
25
|
|
|
* @param SessionWrapper $sessionWrapper |
|
26
|
|
|
* @param int $sessionLifetime |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(RedisManager $redisManager, SessionWrapper $sessionWrapper, $sessionLifetime) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->redisManager = $redisManager; |
|
31
|
|
|
$this->sessionWrapper = $sessionWrapper; |
|
32
|
|
|
$this->sessionLifetime = $sessionLifetime; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getClients() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->clients; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Request $request |
|
45
|
|
|
* @return Client |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getClient(Request $request) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$request->cookies->exist('session_id')) { |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
$sessionId = $request->cookies->get('session_id'); |
|
53
|
|
|
if (!isset($this->clients[$sessionId])) { |
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
|
|
$client = $this->clients[$sessionId]; |
|
57
|
|
|
$client->setIsFirstConnection(false); |
|
58
|
|
|
$client->setLastConnectedAt(new \DateTime()); |
|
59
|
|
|
if (($session = $this->sessionWrapper->fetchSession($sessionId)) === null) { |
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
$this->sessionWrapper->setCurrentSession($session); |
|
63
|
|
|
return $client; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param Request $request |
|
68
|
|
|
* @return Client |
|
69
|
|
|
*/ |
|
70
|
|
|
public function createClient(Request $request) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$client = |
|
73
|
|
|
(new Client()) |
|
74
|
|
|
->setId($this->generateClientId()) |
|
75
|
|
|
->setIsFirstConnection(true) |
|
76
|
|
|
->setLastConnectedAt(new \DateTime()) |
|
77
|
|
|
; |
|
78
|
|
|
$this->clients[$client->getId()] = $client; |
|
79
|
|
|
$this->sessionWrapper->setCurrentSession($this->sessionWrapper->createSession($client->getId())); |
|
80
|
|
|
return $client; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $sessionId |
|
85
|
|
|
* @param int $playerId |
|
86
|
|
|
*/ |
|
87
|
|
|
public function bindPlayerId($sessionId, $playerId) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->clients[$sessionId]->setPlayerId($playerId); |
|
90
|
|
|
|
|
91
|
|
|
$this->redisManager->getConnection()->set('player:' . $playerId, $sessionId); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param int $playerId |
|
96
|
|
|
* @return Session |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getSessionByPlayerId($playerId) |
|
99
|
|
|
{ |
|
100
|
|
|
if (($sessionId = $this->redisManager->getConnection()->get('player:' . $playerId)) === false) { |
|
101
|
|
|
return null; |
|
102
|
|
|
} |
|
103
|
|
|
return $this->sessionWrapper->fetchSession($sessionId); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function generateClientId() |
|
110
|
|
|
{ |
|
111
|
|
|
do { |
|
112
|
|
|
$id = uniqid(); |
|
113
|
|
|
} while (isset($this->clients[$id])); |
|
114
|
|
|
|
|
115
|
|
|
return $id; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param int $clientId |
|
120
|
|
|
* @return boolean |
|
121
|
|
|
*/ |
|
122
|
|
|
public function removeClient($clientId) |
|
123
|
|
|
{ |
|
124
|
|
|
if (!isset($this->clients[$clientId])) { |
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
$this->redisManager->getConnection()->delete('player:' . $this->clients[$clientId]->getPlayerId()); |
|
128
|
|
|
unset($this->clients[$clientId]); |
|
129
|
|
|
return true; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function clear() |
|
133
|
|
|
{ |
|
134
|
|
|
$limitDatetime = (new \DateTime("-{$this->sessionLifetime} seconds")); |
|
135
|
|
|
|
|
136
|
|
|
foreach ($this->clients as $client) |
|
137
|
|
|
{ |
|
138
|
|
|
if ($client->getLastConnectedAt() < $limitDatetime) { |
|
139
|
|
|
$this->removeClient($client->getId()); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.