-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionOn.php
More file actions
132 lines (117 loc) · 4.43 KB
/
sessionOn.php
File metadata and controls
132 lines (117 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
class SessionOn {
private $conn;
public function __construct($conn) {
$this->conn = $conn;
}
public function findPlayers($player_uuid) {
// Look for a match with empty slots
$stmt = $this->conn->prepare('
SELECT * FROM matching
WHERE status = "waiting"
AND (player1_uuid IS NULL OR player2_uuid IS NULL OR player3_uuid IS NULL OR player4_uuid IS NULL)
ORDER BY id ASC
');
$stmt->execute();
$result = $stmt->get_result();
$matches = array();
while ($row = $result->fetch_assoc()) {
$matches[] = $row;
}
foreach ($matches as $match) {
if ($this->addPlayerToMatch($match['id'], $player_uuid)) {
return true;
}
}
// If no match is available, create a new match
$players = $this->getLowestPingPlayers(4);
if (count($players) === 4) {
$this->createMatch($players);
return true;
} else {
return false;
}
}
private function addPlayerToMatch($match_id, $player_uuid) {
$stmt = $this->conn->prepare('
UPDATE matching
SET player1_uuid = IF(player1_uuid IS NULL, ?, player1_uuid),
player2_uuid = IF(player2_uuid IS NULL, ?, player2_uuid),
player3_uuid = IF(player3_uuid IS NULL, ?, player3_uuid),
player4_uuid = IF(player4_uuid IS NULL, ?, player4_uuid)
WHERE id = ?
');
$stmt->bind_param('ssssi', $player_uuid, $player_uuid, $player_uuid, $player_uuid, $match_id);
$stmt->execute();
return $stmt->affected_rows > 0;
}
private function createMatch($players) {
$player_uuids = array_column($players, 'uuid');
sort($player_uuids);
$player_key = implode(',', $player_uuids);
$stmt = $this->conn->prepare('
SELECT id FROM matching
WHERE player1_uuid = ? AND player2_uuid = ? AND player3_uuid = ? AND player4_uuid = ?
AND status = "waiting"
');
$stmt->bind_param('ssss', ...$player_uuids);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
$host_uuid = $players[0]['uuid']; // The player with the lowest ping is the host
$player1_uuid = $players[1]['uuid'];
$player2_uuid = $players[2]['uuid'];
$player3_uuid = $players[3]['uuid'];
$stmt = $this->conn->prepare('
INSERT INTO matching (player1_uuid, player2_uuid, player3_uuid, player4_uuid, status)
VALUES (?, ?, ?, ?, "waiting")
');
$stmt->bind_param('ssss', $host_uuid, $player1_uuid, $player2_uuid, $player3_uuid);
$stmt->execute();
$this->updateWaitingList($players);
}
}
private function updateWaitingList($players) {
$uuids = array_column($players, 'uuid');
$stmt = $this->conn->prepare('
UPDATE waiting_list
SET status = "in_game"
WHERE uuid IN (' . implode(',', array_fill(0, count($uuids), '?')) . ')'
);
$stmt->bind_param(str_repeat('s', count($uuids)), ...$uuids);
$stmt->execute();
}
public function removeInactivePlayers() {
$stmt = $this->conn->prepare('
DELETE FROM waiting_list
WHERE last_activity < DATE_SUB(NOW(), INTERVAL 2 MINUTE)'
);
$stmt->execute();
}
public function removePlayersFromWaitingList($playerIds) {
$stmt = $this->conn->prepare('
DELETE FROM waiting_list
WHERE uuid IN (' . implode(',', array_fill(0, count($playerIds), '?')) . ')'
);
$stmt->bind_param(str_repeat('s', count($playerIds)), ...$playerIds);
$stmt->execute();
}
public function getLowestPingPlayers($numPlayers) {
$poolSize = $numPlayers * 2;
$stmt = $this->conn->prepare('
SELECT * FROM waiting_list
ORDER BY ping ASC
LIMIT ?'
);
$stmt->bind_param('i', $poolSize);
$stmt->execute();
$result = $stmt->get_result();
$players = array();
while ($row = $result->fetch_assoc()) {
$players[] = $row;
}
$selectedPlayers = array_slice($players, 0, $numPlayers);
return $selectedPlayers;
}
}
?>