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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
<?
session_start();
//For testing purposes send a fake 404.
// if (rand(1,3) == 2) {
// header("HTTP/1.0 404 Not Found");
// exit;
// }
//header_remove();
require_once('../includes/sqlEmbedded.php');
$r = '';
if (is_int($_GET['matchID'])) {
$matchID = $_GET['matchID'];
}
$matchID = 1;
// Client sends isReady
if (isset($_REQUEST['isReady'])) {
if ($_REQUEST['isReady'] == "true") {
$r['isReady'] = true;
} else {
$r['isReady'] = false;
}
setReady($userID, $matchID, $r['isReady']);
}
//echo "2";
// Client sends StartGame
if (isset($_REQUEST['startGame'])) {
// Can he start the game?
// Generate map
// Set map expires
// Return true
$r['startGameSuccess'] = true;
}
//echo "3";
// Client sends updateRequest
if (isset($_REQUEST['requestUpdate'])) {
//Return data on match
$r['gameStarted'] = true;
$r['gameStarted'] = true;
$r['secondsTillGameStarts'] = 8.6342;
$r['secondsTillGameExpires'] = 122.6342;
$r['mapID'] = 534;
$r['mapReady'] = true;
$r['players'] = getPlayersForMatch($matchID);
echo json_encode($r);
}
//echo "4";
// Functions
function setReady($userID, $matchID, $readyStatus) {
global $mysqli;
//Turn this to 0 to fill the bit
$intReadyStatus = intval($readyStatus);
$stmt = $mysqli->prepare("UPDATE `matchUsers`
SET `isReady` = ?
WHERE `userID` = ?
AND `matchID` = ?");
$stmt->bind_param('iii', $intReadyStatus, $userID, $matchID);
$stmt->execute();
return;
}
// !!
function getPlayersForMatch($matchID) {
global $mysqli;
if (!is_numeric($matchID)) {
return false;
}
$res = $mysqli->query("
SELECT
matchUsers.userID,
TIME_TO_SEC(TIMEDIFF(NOW(), matchUsers.dateLastChecked)) as secFromLastChecked,
matchUsers.isReady,
users.displayName as 'display',
users.displayColor,
users.wallColor,
users.wallEmblem,
users.wallOrientation,
(matches.creatorUserID = users.ID) as `isCreator`
FROM `matches`
LEFT JOIN `matchUsers`
ON matchUsers.matchID = matches.ID
LEFT JOIN `users`
ON matchUsers.userID = users.ID
");
$array = array();
if ($res->num_rows == 0) {
$res->close();
return false;
}
while ($response = $res->fetch_assoc()) {
$array['users'][] = $response;
}
$res->close();
$mysqli->close();
if (count($array) < 1) return false;
return $array;
}
function getMatchStatus($matchID) {
global $mysqli;
if (!is_numeric($matchID)) return false;
//Not sure why this happens; but just return nothing..
if (!is_object($mysqli)) die("mysqli is not an object");
if ($res = $mysqli->query("
SELECT
matches.creatorUserID,
matches.mapID,
matches.isComplete,
matches.isStarted,
matches.dateCreated,
matches.dateExpires,
matches.dateStarted,
matches.requiredPlayers,
matches.secondsGiven,
matches.useSmartTime,
users.displayName,
users.displayColor,
users.wallColor,
users.wallEmblem,
users.wallOrientation
FROM `matches`
LEFT JOIN `users`
ON matches.userID = users.ID
WHERE matches.ID = '$matchID'
")) {
$array = array();
if ($res->num_rows == 0) {
$res->close();
return false;
}
while ($response = $res->fetch_assoc()) {
$array[] = $response;
}
$res->close();
if (count($array) < 1) return false;
return $array;
} else {
printf("DError: %s\n", $mysqli->error);
return false;
}
}
//Returns the matchID
function createMatch($creatorID, $requiredPlayers, $secondsGiven, $useSmartTime = false) {
global $mysqli;
$useSmartTime = intval($useSmartTime);
$stmt = $mysqli->prepare("INSERT INTO `matches`
(`creatorUserID`, `requiredPlayers`, `secondsGiven`, `useSmartTime`)
VALUES (?, ?)");
$stmt->bind_param('iiii', $creatorID, $requiredPlayers, $secondsGiven, $useSmartTime);
$stmt->execute();
$ID = $stmt->insert_id;
$stmt->close();
return $ID;
}
?>
|