-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameManager.php
More file actions
179 lines (153 loc) · 4.59 KB
/
gameManager.php
File metadata and controls
179 lines (153 loc) · 4.59 KB
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
<?php
require_once 'debugSettings.php';
require_once '../dbinfo/dbcred.php';
require_once 'Game.php';
require_once 'User.php';
/* Game Manager
*
*
*
*/
/**
* Game Manger Class
*/
class GameManager {
protected $db;
private $game;
function __construct()
{
$this->db = $this->dbConnect();
}
private function dbConnect() {
try {
$db = new PDO('mysql:host=localhost;dbname='.DB_NAME, DB_USERNAME, DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
} catch (PDOException $e) {
//echo $e->getMessage();
return $e->getMessage();
}
}
public function getCurrentGame(){
//SELECT hash from `users` WHERE email=$email
$sql = "SELECT * FROM `games` WHERE `active`=1";
//$sql = "SELECT * FROM users";
$result = $this->db->query($sql);
$data = $result->fetch(PDO::FETCH_ASSOC);
if ($data) {
if($data['gameID']){
return $data;
}
}
return false;
}
public function userInGame($email){
$sql = "SELECT `userID` FROM `activeGamePlayers` WHERE activeGamePlayers.userID=(SELECT users.userID FROM users WHERE users.email=". $this->db->quote($email) .")";
try {
$result = $this->db->query($sql);
$data = $result->fetch(PDO::FETCH_ASSOC);
if($data) {
return true;
}
return false;
} catch (PDOException $e) {
return $e->getMessage();
}
}
public function new($game){
$types = json_encode($game->types);
$called_numbers = json_encode($game->calledNumbers);
$sql = "INSERT INTO `games` (`gameID`, `start_time`, `end_time`, `game_types`, `called_numbers`, `active`) VALUES (NULL, CURRENT_TIMESTAMP, NULL, ". $this->db->quote($types) .", ". $this->db->quote($called_numbers) .", '1')";
try {
return $this->db->query($sql);
} catch (PDOException $e) {
return $e->getMessage();
}
}
public function moveUsersFromLobbyToGame(){
//INSERT `activeGamePlayers` (SELECT * FROM `lobby`)
$sql = "INSERT `activeGamePlayers` (SELECT * FROM `lobby`); DELETE FROM `lobby` WHERE 1=1;";
try {
return $this->db->query($sql);
} catch (PDOException $e) {
echo $e->getMessage();
return $e->getMessage();
}
}
public function moveUsersFromGameToLobby(){
$sql = "INSERT `lobby` (SELECT * FROM `activeGamePlayers`); DELETE FROM `activeGamePlayers` WHERE 1=1;";
try {
return $this->db->query($sql);
} catch (PDOException $e) {
echo $e->getMessage();
return $e->getMessage();
}
}
// Return a list of users in the active game
public function getActiveGamePlayers(){
$sql = "SELECT activeGamePlayers.userID as userID, users.name as name, users.role as role, users.email as email FROM activeGamePlayers JOIN `users` ON users.userID=activeGamePlayers.userID";
//$sql = "SELECT * FROM `lobby`";
$result = $this->db->query($sql);
//preDump($result);
//$data = $result->fetch(PDO::FETCH_ASSOC);
$data = $result->fetchAll(PDO::FETCH_CLASS, "User");
if ($data) {
return $data;
}
return false;
}
public function removePlayerFromGame($email){
$email = $this->db->quote($email);
$sql = "DELETE FROM `activeGamePlayers` WHERE userID=(SELECT userID from `users` WHERE users.email=".$email.")";
try {
$result = $this->db->query($sql);
} catch (PDOException $e) {
return $e->getMessage();
}
}
public function addPlayerToGame($email){
$email = $this->db->quote($email);
$sql = "INSERT INTO `activeGamePlayers` (`userID`) VALUES ((SELECT userID from `users` WHERE users.email=".$email."))";
try {
$result = $this->db->query($sql);
return $result;
} catch (PDOException $e) {
return $e->getMessage();
}
}
// update a game
public function update($game){
///echo "<br> Updating game: " . $game->gameID .". <br>";
$types = json_encode($game->types);
$called_numbers = json_encode($game->calledNumbers);
$sql = "UPDATE `games` SET `game_types`=".$this->db->quote($types).",`called_numbers`=".$this->db->quote($called_numbers)." WHERE `gameID`=" . $this->db->quote($game->gameID);
// echo "<br>";
// echo $sql;
// echo "<br>";
try {
return $this->db->query($sql);
} catch (PDOException $e) {
echo $e->getMessage();
return $e->getMessage();
}
}
// end a game
public function end($game){
$sql = "UPDATE `games` SET `end_time`=CURRENT_TIMESTAMP, `active`=0 WHERE `gameID`=" . $this->db->quote($game->gameID);
try {
return $this->db->query($sql);
} catch (PDOException $e) {
return $e->getMessage();
}
}
// delete a game entry
public function delete($game){
$sql = "DELETE FROM `games` WHERE `gameID`=" . $this->db->quote($game->gameID);
try {
return $this->db->query($sql);
} catch (PDOException $e) {
return $e->getMessage();
}
}
}
?>