-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameStatus.php
More file actions
118 lines (103 loc) · 3.11 KB
/
gameStatus.php
File metadata and controls
118 lines (103 loc) · 3.11 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
<?php
require_once 'debugSettings.php';
require_once 'Game.php';
require_once 'gameManager.php';
require_once '../dbinfo/dbcred.php';
require_once 'DBSessionHandler.php';
require_once 'LobbyManager.php';
$errors = array();
try{
$handler = new DBSessionHandler();
session_set_save_handler($handler);
session_start();
} catch (Exception $e){
$error = $e->getMessage();
}
header('Content-Type: application/json');
if ( !isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true ) {
$status = "Unauthenticated access attempt.";
$response = array('status' => $status);
echo json_encode($response);
exit;
}
$gameManager = new GameManager();
$lobbyManager = new LobbyManager();
$lobby = $lobbyManager->getLobby();
if(!$lobbyManager->userInLobby($_SESSION['email']) && !$gameManager->userInGame($_SESSION['email'])){
try {
$lobbyManager->addUser($_SESSION['email']);
} catch (Exception $e){
array_push($errors, $e->getMessage());
}
}
try {
$game = new Game();
// get the game status
$game->setGame($gameManager->getCurrentGame());
} catch (Exception $e){
$game = false;
array_push($errors, $e->getMessage());
}
$active = false;
if(is_object($game)){
if(isset($game->active)){
$active = true;
}
}
if($_SESSION['role'] == 'overlord' || $_SESSION['role'] == 'manager'){
if(!$active && isset($_POST['START']) && isset($_SESSION['role'])){
$game = new Game();
$gameManager->new($game);
$game->setGame($gameManager->getCurrentGame());
// empty lobby and put users in activeGamePlayers
$gameManager->moveUsersFromLobbyToGame();
}
if($active && isset($_POST['NEXT']) && isset($_SESSION['role'])){
$game->callNextNumber();
$gameManager->update($game);
}
if($active && isset($_POST['CHANGE_TYPE']) && isset($_POST['type'])){
$type = htmlspecialchars($_POST['type']);
$game->setType($type);
$gameManager->update($game);
}
if($active && isset($_POST['REMOVE_PLAYER'])){
$player = htmlspecialchars($_POST['REMOVE_PLAYER']);
$gameManager->removePlayerFromGame($player);
}
if($active && isset($_POST['END']) && isset($_SESSION['role'])){
$gameManager->end($game);
// empty activeGamePlayers and put users in lobby
$gameManager->moveUsersFromGameToLobby();
}
}
if(!$active){
$status = "The game has not started.";
$response = array('status' => $status, 'lobby' => $lobby, 'active' => $active);
} else {
require_once 'CardManager.php';
$status = "A game is in progress. Game type: " . $game->types[0];
$cardManager = new CardManager();
try {
$players = $gameManager->getActiveGamePlayers();
} catch (Exception $e){
$players = [];
array_push($errors, $e->getMessage());
}
try {
if($players){
$bingos = $game->checkForBingos($players, $cardManager);
} else {
$bingos = [];
}
} catch (Exception $e){
$bingos = [];
array_push($errors, $e->getMessage());
}
$response = array('game' => $game, 'status' => $status, 'lobby' => $lobby, 'players' => $players, 'bingos' => $bingos, 'active' => $active);
}
if(count($errors) > 0){
$response['error'] = $errors;
}
echo json_encode($response);
?>