-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.php
503 lines (389 loc) Β· 12.4 KB
/
script.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<?php
require 'settings.php';
require 'steamauth/steamauth.php';
// MYSQLI CREATE
$conn = mysqli_connect($mysqli_server, $mysqli_user, $mysqli_key, $mysqli_db);
if (!$conn) {
die("Database Connection failed: " . mysqli_connect_error());
}
mysqli_set_charset($conn, $mysqli_charset);
// Set timezone
date_default_timezone_set($timezone);
// Configure current page
if (isset($_GET['page'])) {
switch ($_GET['page']) {
case "secure":
$page = "secure";
break;
default:
$page = "index";
}
} else {
$page = "index";
}
// FUNCTIONS
function redirect($location = null)
{
if (isset($location)) {
header("Location: " . $location);
} else {
header("Location: " . $_SERVER['REQUEST_URI']);
}
exit();
}
function check_steamgroup_xml($id) // Retrieves user's Steam page to check if they are currently in the Steam group
{
$profilelink = "http://steamcommunity.com/profiles/" . $_SESSION['steamid'] . "?xml=1";
//$profilelink = "http://steamcommunity.com/profiles/76561197968657644?xml=1"; // demo user who is NOT part of the steam group
$profile = simplexml_load_file($profilelink);
foreach ($profile->groups->group as $groups) {
$groupIDs[] = floatval($groups->groupID64);
}
$steamgroupvalid = false;
foreach ($groupIDs as $groupID) {
if ($groupID == $id) {
$steamgroupvalid = true;
}
}
return $steamgroupvalid;
}
function check_registered() // Checks if user has a DB entry
{
global $conn;
$query = 'SELECT 1
FROM user
WHERE steamID64 = "' . $_SESSION['steamid'] . '"';
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
$user_registered = true;
} else {
$user_registered = false;
}
return $user_registered;
}
function check_is_in_group() // Checks 'is_in_group' attribute
{
global $conn;
$query = 'SELECT is_in_group
FROM user
WHERE steamID64 = "' . $_SESSION['steamid'] . '"';
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
$user_is_in_group = mysqli_fetch_row($result);
$user_is_in_group = $user_is_in_group[0];
} else {
return "Error: User profile corrupt or user does not exist!";
}
return $user_is_in_group;
}
function check_is_active() // Checks 'is_active' attribute
{
global $conn;
$query = 'SELECT is_active
FROM user
WHERE steamID64 = "' . $_SESSION['steamid'] . '"';
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
$user_is_active = mysqli_fetch_row($result);
$user_is_active = $user_is_active[0];
} else {
return "Error: User profile corrupt or user does not exist!";
}
return $user_is_active;
}
function get_dates() // Retrieve dates from DB
{
global $conn;
$query = 'SELECT attribute, value
FROM var
WHERE attribute LIKE "date.%"';
$result = mysqli_query($conn, $query);
if (!mysqli_num_rows($result) > 0) {
return "Error: Dates are not setup correctly.";
} else {
while ($row = mysqli_fetch_assoc($result)) {
$dates[$row["attribute"]] = $row["value"];
}
}
return $dates;
}
function display_date($dates, $type) // Returns correctly formatted date for display (format can be set in settings.php)
{
global $dateformat;
if (!($type == "register" || $type == "gifts" || $type == "end")) {
return "Invalid date type.";
} else {
$time = strtotime($dates["date." . $type]);
$display_date = date($dateformat, $time);
return $display_date;
}
}
function get_gamestate($dates) // Get current gamestate (Registration open = 0; Preparing gifts: 1; Game ended: 2)
{
// end date
$checked_date = new DateTime($dates["date.end"]);
$current_date = new DateTime();
if ($checked_date < $current_date) {
return 3; // end date has passed
}
// gifts date
$checked_date = new DateTime($dates["date.gifts"]);
$current_date = new DateTime();
if ($checked_date < $current_date) {
return 2; // gift preparation date has passed
}
// register date
$checked_date = new DateTime($dates["date.register"]);
$current_date = new DateTime();
if ($checked_date < $current_date) {
return 1; // registration date has passed
}
return 0; // registration is still open
}
function user_set_active($new_value, $steamID64)
{
global $conn;
if ($new_value) {
$new_value = "1";
} else {
$new_value = "0";
}
$query = 'UPDATE user
SET is_active=' . $new_value . '
WHERE steamID64 = "' . $steamID64 . '"';
$result = mysqli_query($conn, $query);
if (!$result) {
echo "Could not make user participate.";
exit;
}
}
function get_participants()
{
global $conn;
$query = 'SELECT steamID64, name, avatar
FROM user
WHERE is_active = 1
ORDER BY name ASC';
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$participants[] = array_map("htmlspecialchars", $row);
}
} else {
$participants = false;
}
return $participants;
}
function get_players_matched()
{
global $conn;
$query = 'SELECT value
FROM var
WHERE attribute = "players.matched"';
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
$players_matched = mysqli_fetch_row($result);
$players_matched = $players_matched[0];
} else {
return "Error: Can't retrieve player matching status.";
}
return $players_matched;
}
function match_players($allow_pairs = true, $max_pair_chance)
{
global $conn;
// GET ACTIVE PLAYERS
$query = 'SELECT steamID64
FROM user
WHERE is_active = 1';
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_row($result)) {
$row = array_map("htmlspecialchars", $row);
$players[] = $row[0];
}
$playercount = count($players);
// MATCH PLAYERS
if (!$allow_pairs) { // Don't generate pairs (creates "circle" of players)
shuffle($players);
$players_origin = $players; // player
$players_match = $players; // player's match
array_push($players_match, array_shift($players_match)); // array_shift removes the first value. array_push adds it to the end of the array
} else { // Generate Pairs
$pair_count = random_int(0, $playercount); // random number between 0 - amount of players
$pair_count = $pair_count * 0.5 * $max_pair_chance; // 0.5 to get pairs, instead of individuals
$pair_count = floor($pair_count);
shuffle($players);
for ($i = 1; $i <= $pair_count; $i++) { // creates pairs in different arrays
$pairs_origin[] = $players[0];
$pairs_origin[] = $players[1];
$pairs_match[] = $players[1];
$pairs_match[] = $players[0];
array_shift($players);
array_shift($players);
}
$players_origin = $players;
$players_match = $players;
if (!empty($players)) { // only shift array, if it isn't empty (happens when only pairs exist)
array_push($players_match, array_shift($players_match)); // array_shift removes the first value. array_push adds it to the end of the array
}
if (isset($pairs_origin)) { // only merge with pairs array, if at least one pair was created
$players_origin = array_merge($pairs_origin, $players_origin);
$players_match = array_merge($pairs_match, $players_match);
}
}
// DELETE OLD TABLE ROWS
$query = 'TRUNCATE TABLE matches';
$result = mysqli_query($conn, $query);
if (mysqli_error($conn)) {
echo "Could not truncate matches table.";
exit;
}
// INSERT MATCHES INTO DB
$query = 'INSERT INTO matches (steamID64_origin, steamID64_match)
VALUES ';
for ($i = 0; $i <= $playercount - 1; $i++) {
$query .= '("' . $players_origin[$i] . '", "' . $players_match[$i] . '"), ';
}
$query = substr($query, 0, -2);
$query .= ';';
$result = mysqli_query($conn, $query);
$error = mysqli_error($conn);
if ($error) {
echo "Could not add matched users to database: " . $error;
exit;
}
// SET players.matched
$query = 'UPDATE var
SET value = "1"
WHERE attribute = "players.matched"';
$result = mysqli_query($conn, $query);
if (mysqli_error($conn)) {
echo "Could not update players.matched variable.";
exit;
}
}
function get_match()
{
global $conn;
$query = 'SELECT user.steamID64, user.name, user.avatar
FROM matches
INNER JOIN user ON matches.steamID64_match=user.steamID64
WHERE matches.steamID64_origin = ' . $_SESSION['steamid'];
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
$match = mysqli_fetch_row($result);
} else {
return "Error: User profile corrupt or user does not exist!";
}
return $match;
}
function update_player_steamdata($steamID64)
{
global $conn, $steamapikey;
unset($_SESSION['steam_uptodate']);
include 'steamauth/userInfo.php';
$query = 'UPDATE user
SET name="' . $_SESSION['steam_personaname'] . '", avatar="' . $_SESSION['steam_avatar'] . '"
WHERE steamID64 = "' . $steamID64 . '"';
$result = mysqli_query($conn, $query);
if (mysqli_error($conn)) {
echo "Could not update user's details.";
exit;
}
}
function check_steam_update_timeout()
{
global $steam_updatetimeout;
if (isset($_SESSION['last_steam_update']) && (time() - $_SESSION['last_steam_update'] < $steam_updatetimeout)) {
// Still in Timeout
return true;
} else {
// Has been long enough
return false;
}
}
// INDEX PAGE
if ($page == "index") { // index/landing page is open
$dates = get_dates();
$gamestate = get_gamestate($dates);
}
// SECURE PAGE
if ($page == "secure") { // secure page is open
if (isset($_SESSION['steamid'])) { // user is signed into Steam
include 'steamauth/userInfo.php';
// Check if user is in the database. If not, register them.
if (!check_registered()) {
$query = 'INSERT INTO user (steamID64, name, avatar, is_active, is_in_group)
VALUES (' . $_SESSION['steamid'] . ', "' . $steamprofile['personaname'] . '", "' . $steamprofile['avatar'] . '", 0, 0)';
$result = mysqli_query($conn, $query);
if (!$result) {
echo "Could not register user.";
exit;
}
}
if (check_is_in_group()) {
$secure_level = 2; // User is signed into Steam and part of the correct Steam Group
} elseif (check_steamgroup_xml($steamgroupid)) { // check if user is in group via XML
$query = 'UPDATE user
SET is_in_group=1
WHERE steamID64 = "' . $_SESSION['steamid'] . '"';
$result = mysqli_query($conn, $query);
if (!$result) {
echo "Could not update user group.";
exit;
}
$secure_level = 2;
} else {
$secure_level = 1; // User is signed into Steam but not in the correct Steam Group
session_unset(); // Automatically sign out user if they cannot participate
session_destroy();
}
} else {
$secure_level = 0; // User is not signed into Steam
}
if ($secure_level == 2) {
$dates = get_dates();
$gamestate = get_gamestate($dates); // Valid Gamestates: 0 - Registration; 1 - Prepare Gifts; 2 - Distribute Gifts; 3 - Game Ended
if (isset($_GET['update_steamdata'])) { // Handle steam profile update link
if (!check_steam_update_timeout()) {
update_player_steamdata($_SESSION['steamid']);
$_SESSION['last_steam_update'] = time();
redirect("./?page=secure");
}
}
$participants = get_participants(); // get participants AFTER updating local player
$is_active = check_is_active();
if (isset($_POST['participate'])) { // Handle participation button
if ($gamestate > 0) {
echo "You can no longer participate.";
exit();
}
if ($is_active) {
user_set_active(false, $_SESSION['steamid']);
} else {
user_set_active(true, $_SESSION['steamid']);
}
redirect();
}
switch ($gamestate) {
case 0: // Registration
# ...
break;
case 1: // Prepare Gifts
if (!get_players_matched()) {
match_players($allow_pairs, $max_pair_chance);
}
$match = get_match();
break;
case 2: // Distribute Gifts
if (!get_players_matched()) {
match_players($allow_pairs, $max_pair_chance);
}
$match = get_match();
break;
case 3: // Game Ended
# ...
break;
}
}
}