-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_score.php
More file actions
167 lines (136 loc) · 6.92 KB
/
update_score.php
File metadata and controls
167 lines (136 loc) · 6.92 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
header('Content-Type: application/json');
$servername = "localhost";
$DBUsername = "root";
$DBPassword = "root";
$dbname = "new_unity";
// Get JSON data from request body
$json = file_get_contents('php://input');
$data = json_decode($json, true);
// Check if JSON was decoded successfully
if ($data === null) {
die(json_encode(array("success" => false, "error" => 10, "message" => "Invalid JSON data")));
}
// Extract score data
$username = $data["username"];
$completionTime = $data["completionTime"];
$difficulty = $data["difficulty"];
// Connect to database
$conn = new mysqli($servername, $DBUsername, $DBPassword, $dbname);
if($conn->connect_error) {
die(json_encode(array("success" => false, "error" => 1, "message" => "Connection to database failed")));
}
try {
// First, get the user ID from the username
$userIdStmt = $conn->prepare("SELECT user_id FROM users WHERE username = ?");
if (!$userIdStmt) {
die(json_encode(array("success" => false, "error" => 2, "message" => "Failed to prepare user lookup statement: " . $conn->error)));
}
$userIdStmt->bind_param("s", $username);
if (!$userIdStmt->execute()) {
die(json_encode(array("success" => false, "error" => 3, "message" => "User ID lookup failed: " . $userIdStmt->error)));
}
$userResult = $userIdStmt->get_result();
if ($userResult->num_rows == 0) {
$userIdStmt->close();
die(json_encode(array("success" => false, "error" => 4, "message" => "User not found")));
}
$userRow = $userResult->fetch_assoc();
$userId = $userRow["user_id"];
$userIdStmt->close();
// Check if the user already has a score for this difficulty
$checkScoreStmt = $conn->prepare("SELECT id, completion_time FROM scores WHERE user_id = ? AND difficulty = ? ORDER BY completion_time ASC LIMIT 1");
if (!$checkScoreStmt) {
die(json_encode(array("success" => false, "error" => 5, "message" => "Failed to prepare score check statement: " . $conn->error)));
}
$checkScoreStmt->bind_param("is", $userId, $difficulty);
if (!$checkScoreStmt->execute()) {
die(json_encode(array("success" => false, "error" => 6, "message" => "Score check failed: " . $checkScoreStmt->error)));
}
$scoreResult = $checkScoreStmt->get_result();
$isNewBest = false;
$existingScoreId = null;
$existingTime = null;
if ($scoreResult->num_rows > 0) {
// User already has a score for this difficulty
$scoreRow = $scoreResult->fetch_assoc();
$existingScoreId = $scoreRow["id"];
$existingTime = $scoreRow["completion_time"];
// Check if new time is better (lower) than existing time
if ($completionTime < $existingTime) {
$isNewBest = true;
} else {
// Current score is not better, return early with message
$checkScoreStmt->close();
echo json_encode(array(
"success" => true,
"message" => "Score not saved. Your previous best time of " . number_format($existingTime, 3) . " seconds is better.",
"data" => array(
"username" => $username,
"newTime" => $completionTime,
"bestTime" => $existingTime,
"difficulty" => $difficulty,
"improved" => false
)
));
$conn->close();
exit;
}
} else {
// No existing score, this is the first one
$isNewBest = true;
}
$checkScoreStmt->close();
if ($isNewBest) {
if ($existingScoreId !== null) {
// Update existing record with new best time
$updateScoreStmt = $conn->prepare("UPDATE scores SET completion_time = ?, timestamp = NOW() WHERE id = ?");
if (!$updateScoreStmt) {
die(json_encode(array("success" => false, "error" => 7, "message" => "Failed to prepare score update statement: " . $conn->error)));
}
$updateScoreStmt->bind_param("di", $completionTime, $existingScoreId);
if (!$updateScoreStmt->execute()) {
die(json_encode(array("success" => false, "error" => 8, "message" => "Score update failed: " . $updateScoreStmt->error)));
}
$updateScoreStmt->close();
echo json_encode(array(
"success" => true,
"message" => "New best time saved! You improved by " . number_format($existingTime - $completionTime, 3) . " seconds.",
"data" => array(
"username" => $username,
"completionTime" => $completionTime,
"previousBest" => $existingTime,
"difficulty" => $difficulty,
"improved" => true,
"improvement" => $existingTime - $completionTime
)
));
} else {
// Insert new score
$insertScoreStmt = $conn->prepare("INSERT INTO scores (user_id, completion_time, difficulty, timestamp) VALUES (?, ?, ?, NOW())");
if (!$insertScoreStmt) {
die(json_encode(array("success" => false, "error" => 9, "message" => "Failed to prepare score insert statement: " . $conn->error)));
}
$insertScoreStmt->bind_param("ids", $userId, $completionTime, $difficulty);
if (!$insertScoreStmt->execute()) {
die(json_encode(array("success" => false, "error" => 10, "message" => "Score insertion failed: " . $insertScoreStmt->error)));
}
$insertScoreStmt->close();
echo json_encode(array(
"success" => true,
"message" => "First completion time saved!",
"data" => array(
"username" => $username,
"completionTime" => $completionTime,
"difficulty" => $difficulty,
"improved" => true,
"firstTime" => true
)
));
}
}
} catch(Exception $e) {
die(json_encode(array("success" => false, "error" => 30, "message" => "Database error: " . $e->getMessage())));
}
$conn->close();
?>