-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetGames.php
118 lines (107 loc) · 3.4 KB
/
getGames.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
<?php
require("config.php");
if(empty($_SESSION['user']))
{
header("Location: index.php");
die("Redirecting to index.php");
}
$username = htmlentities($_SESSION['user']['email'], ENT_QUOTES, 'UTF-8');
if($_POST['param'] == "active"){
$stmt = $db->prepare('SELECT * FROM users WHERE email = :username');
$stmt->execute(array(':username' => $username));
foreach ($stmt as $row) {
$data['gameQueue'] = $row['gameQueue'];
}
$gamesQ = json_decode($data['gameQueue'], true);
$ids = array();
$status = array();
if($gamesQ != ""){
foreach($gamesQ as $game){
$ids[] = $game['gameID'];
$status[] = $game['status'];
}
$inQuery = implode(',', array_fill(0, count($ids), '?'));
$stmt = $db->prepare(
'SELECT *
FROM games
WHERE gameID IN(' . $inQuery . ') AND status!="ended"'
);
foreach ($ids as $k => $id){
$stmt->bindValue(($k+1), $id);
}
$stmt->execute();
$games = array('gameID' => array(), 'created' => array());
foreach ($stmt as $row) {
$games['gameID'][] = $row['gameID'];
$games['game_name'][] = $row['game_name'];
$games['created'][] = $row['created'];
$games['mapProperties'][] = json_decode($row['mapProperties']);
$games['gameStatus'][] = $row['status'];
}
if(count($games>0)){
for($i=0;$i<count($ids);$i++){
for($j=0;$j<count($games['gameID']);$j++){
if($ids[$i]==$games['gameID'][$j]){
$games['playerStatus'][$j]=$status[$i];
}
}
}
for ($i=0; $i<count($games['created']); $i++){
$epoch = $games['created'][$i];
$dt = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
$games['created'][$i] = $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
}
echo JSON_encode($games);
}
}else{
echo JSON_encode("None");
}
}
if($_POST['param'] == "public"){
$stmt = $db->prepare('SELECT * FROM games WHERE status = "invites"');
$stmt->execute();
$games = array();
foreach ($stmt as $row) {
$games['gameID'][] = $row['gameID'];
$games['game_name'][] = $row['game_name'];
$games['created'][] = $row['created'];
}
if(count($games)>0){
$stmt = $db->prepare('SELECT * FROM users WHERE email = :username');
$stmt->execute(array(':username' => $username));
foreach ($stmt as $row) {
$data['gameQueue'] = $row['gameQueue'];
}
$gamesQ = json_decode($data['gameQueue'], true);
$ids = array();
$status = array();
$publicGames = array();
if($gamesQ == ""){
$gamesQ = array();
}
for($i=0;$i<count($gamesQ);$i++){
$ids[] = $gamesQ[$i]['gameID'];
$status[] = $gamesQ[$i]['status'];
}
for($i=0;$i<count($games['gameID']);$i++){
$trig = false;
for($j=0;$j<count($ids);$j++){
if($games['gameID'][$i]==$ids[$j]){
$trig = true;
}
}
if($trig == false){
$publicGames[] = array("gameID"=>$games['gameID'][$i],"game_name"=>$games['game_name'][$i],"created"=>$games['created'][$i]);
}
}
for ($i=0; $i<count($publicGames); $i++){
$epoch = $publicGames[$i]['created'];
$dt = new DateTime("@$epoch"); // convert UNIX timestamp to PHP DateTime
$publicGames[$i]['created'] = $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
}
echo JSON_encode($publicGames);
}else{
echo JSON_encode("None");
}
}
?>