-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlobby.php
More file actions
77 lines (66 loc) · 2.19 KB
/
Copy pathlobby.php
File metadata and controls
77 lines (66 loc) · 2.19 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
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$room = $_POST['room'] ?? '';
$name = trim($_POST['name'] ?? '');
$path = "rooms/$room.json";
if (!file_exists($path)) {
die("Room not found.");
}
$data = json_decode(file_get_contents($path), true);
// Set the host as the first player to join
if (!isset($data['host'])) {
$data['host'] = $name;
}
if (!in_array($name, $data['players'])) {
$data['players'][] = $name;
file_put_contents($path, json_encode($data));
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Lobby - Room <?= htmlspecialchars($room) ?></title>
</head>
<body>
<h2>Room Code: <?= htmlspecialchars($room) ?></h2>
<h3>Welcome, <?= htmlspecialchars($name) ?>!</h3>
<p>Waiting for players to join...</p>
<ul id="player-list"></ul>
<?php if ($data['host'] === $name): ?>
<form action="start_game.php" method="POST">
<input type="hidden" name="room" value="<?= htmlspecialchars($room) ?>">
<button type="submit">Start Game</button>
</form>
<?php endif; ?>
<script>
function fetchPlayers() {
fetch('get_players.php?room=<?= $room ?>')
.then(res => res.json())
.then(data => {
const list = document.getElementById("player-list");
list.innerHTML = "";
data.players.forEach(player => {
const li = document.createElement("li");
li.textContent = player;
list.appendChild(li);
});
});
}
function checkGameStart() {
fetch('get_status.php?room=<?= $room ?>')
.then(res => res.json())
.then(data => {
if (data.started) {
window.location.href = "game.php?room=<?= $room ?>&name=<?= urlencode($name) ?>";
}
});
}
setInterval(fetchPlayers, 1000);
setInterval(checkGameStart, 1000);
fetchPlayers();
</script>
</body>
</html>