-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvote.php
More file actions
72 lines (58 loc) · 1.71 KB
/
Copy pathvote.php
File metadata and controls
72 lines (58 loc) · 1.71 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
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$room = $_POST['room'] ?? '';
$name = $_POST['name'] ?? '';
$vote = $_POST['vote'] ?? '';
$path = "rooms/$room.json";
if (!file_exists($path)) {
die("Room not found.");
}
$data = json_decode(file_get_contents($path), true);
// Record the vote
if (!isset($data['votes'][$vote])) {
$data['votes'][$vote] = 0;
}
$data['votes'][$vote]++;
// Mark the voter
if (!in_array($name, $data['voted'])) {
$data['voted'][] = $name;
}
// If all players have voted, mark results as ready and end the round
if (count($data['voted']) >= count($data['players'])) {
$data['results_ready'] = true;
$data['round_started'] = false;
}
file_put_contents($path, json_encode($data));
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Waiting for Others</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h2>✅ Your vote has been recorded!</h2>
<p><em>Waiting for the rest of the players to vote...</em></p>
<div class="loader" style="margin: 20px auto; border: 5px solid #ccc; border-top: 5px solid #ffce00; border-radius: 50%; width: 40px; height: 40px; animation: spin 1s linear infinite;"></div>
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
<script>
function checkVotes() {
fetch('check_votes.php?room=<?= $room ?>')
.then(res => res.json())
.then(data => {
if (data.results_ready) {
window.location.href = "results.php?room=<?= $room ?>&name=<?= urlencode($name) ?>";
}
});
}
setInterval(checkVotes, 1000);
</script>
</body>
</html>