-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincrease.php
55 lines (47 loc) · 1.41 KB
/
increase.php
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
<?php
declare(strict_types=1);
if (!isset($_SERVER["HTTPS"])) { // We should not get these
header("HTTP/1.0 404 Not Found");
exit();
} elseif ($_SERVER["REQUEST_METHOD"] !== "POST") { // allow only POST
header("HTTP/1.0 405 Method Not Allowed");
exit();
}
$connection = new mysqli($_SERVER["MYSQL_HOST"],
$_SERVER["MYSQL_USER"],
$_SERVER["MYSQL_PASSWD"],
"button");
// Check connection
if ($connection->connect_error) {
// TODO: Some logging would be nice
header("HTTP/1.0 500 We fucked up");
exit();
}
// Queries in nice format
$q1 = "LOCK TABLES counter WRITE";
$q2 = "UPDATE counter SET value = value + 1";
$q3 = "SELECT value FROM counter";
$q4 = "UNLOCK TABLES";
if ( $connection->query($q1) === TRUE
&& $connection->query($q2) === TRUE) {
$counter = $connection->query($q3)->fetch_assoc()["value"];
$connection->query($q4);
$data = array("points"=>0);
// Vheck if user wins
if ($counter % 10 === 0) {
$data["points"] = 5;
}
if ($counter % 100 === 0) {
$data["points"] = 40;
}
if ($counter % 500 === 0) {
$data["points"] = 250;
}
header("content-type application/json");
echo json_encode($data);
} else {
header("HTTP/1.0 500 We fucked up");
// TODO: Some logging would be nice
}
$connection->close();
?>