-
Notifications
You must be signed in to change notification settings - Fork 0
/
getUpdate.php
77 lines (70 loc) · 1.85 KB
/
getUpdate.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
header('Content-Type: application/json; charset=utf-8');
session_start(['cookie_lifetime' => 86400]);
// Exit if no key.
if (! isset($_GET['key']))
{
$output['error'] = true;
$output['status'] = "err-bad-params";
echo json_encode($output);
exit;
}
// Check if key is valid.
if (strlen($_GET['key']) == 6 && ctype_alnum($_GET['key']))
{
$keyCode = strtolower($_GET['key']);
}
else
{
$output['error'] = true;
$output['status'] = "err-bad-key";
echo json_encode($output);
exit;
}
$dbPath = "db/" . $keyCode . ".db";
// Check database exists.
if(! file_exists($dbPath))
{
$output['error'] = true;
$output['status'] = "err-bad-key";
echo json_encode($output);
exit;
}
if ($db = new SQLite3($dbPath, SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE))
{
// Check if the player is in the match (using session_id).
$stmt = $db->prepare("SELECT * FROM Players WHERE SessID=:sessID");
$stmt->bindValue(":sessID", session_id());
$player = $stmt->execute()->fetchArray();
if (! $player)
{
$output['error'] = true;
$output['status'] = "err-bad-auth";
echo json_encode($output);
exit;
}
// Send only relevant data to the player.
$stmt = $db->prepare("SELECT Name,First FROM Players");
$res = $stmt->execute();
while ($r = $res->fetchArray())
{
$players[] = $r;
}
$stmt = $db->prepare("SELECT Location,Playing,EndTime,Paused FROM Match LIMIT 1");
$match = $stmt->execute()->fetchArray();
$match['TimeLeft'] = $match['EndTime'] - time();
$output['error'] = false;
$output['player'] = $player;
$output['players'] = $players;
$output['match'] = $match;
echo json_encode($output);
exit;
}
else
{
$output['error'] = true;
$output['status'] = "err-database-unknown";
echo json_encode($output);
exit;
}
?>