-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLobbyManager.php
More file actions
83 lines (68 loc) · 1.9 KB
/
LobbyManager.php
File metadata and controls
83 lines (68 loc) · 1.9 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
<?php
// Lobby Manager
/**
*
*/
require_once ('User.php');
class LobbyManager
{
protected $db;
private $lobby;
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 userInLobby($email){
$sql = "SELECT `userID` FROM `lobby` WHERE lobby.userID=(SELECT `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 removeUserFromLobby($email){
$email = $this->db->quote($email);
$sql = "DELETE FROM `lobby` WHERE userID=(SELECT userID from `users` WHERE users.email=".$email.")";
try {
$result = $this->db->query($sql);
} catch (PDOException $e) {
return $e->getMessage();
}
}
// Return a list of users in the lobby
public function getLobby(){
$sql = "SELECT lobby.userID as userID, users.name as name, users.role as role, users.email as email FROM lobby JOIN `users` ON users.userID=lobby.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;
}
// Add a user to the lobby
public function addUser($email){
$sql = "INSERT INTO `lobby`(`userID`) VALUES ((SELECT `userID` FROM `users` WHERE users.email=".$this->db->quote($email)."))";
try {
$result = $this->db->query($sql);
} catch (PDOException $e) {
return $e->getMessage();
}
}
}
?>