-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgames.php
More file actions
241 lines (209 loc) · 8.3 KB
/
Copy pathgames.php
File metadata and controls
241 lines (209 loc) · 8.3 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
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
<?php
// games.php
require_once 'auth_check.php'; // e.g. ensureRole('Jogador')
require_once 'db_connection.php'; // getDBConnection()
session_start();
// Only a Jogador can access
requireRole('Jogador');
// 1) Connect to local DB using session credentials
$pdo = getDBConnection();
// 2) Set @myEmail from session
$loggedEmail = $_SESSION['dbUser'];
$pdo->exec("SET @myEmail = '" . addslashes($loggedEmail) . "'");
// 3) Identify the action
$action = $_GET['action'] ?? null;
$selectedIDJogo = $_GET['selected_jogo'] ?? null;
// 4) “Criar” action => minimal creation
if ($action === 'criar') {
try {
$Descricao = "Jogo " . date('Y-m-d H:i:s');
// Call Criar_Jogo(pDescricao)
$stmtCriar = $pdo->prepare("CALL Labirinto.Criar_Jogo(:Descricao)");
$stmtCriar->execute([':Descricao' => $Descricao]);
$stmtCriar->closeCursor();
// Now retrieve LAST_INSERT_ID() from the same connection
$stmtId = $pdo->query("SELECT LAST_INSERT_ID() AS newIDJogo");
$rowId = $stmtId->fetch(PDO::FETCH_ASSOC);
$stmtId->closeCursor();
$newIDJogo = (int)$rowId['newIDJogo'];
// Redirect to the edit form
header("Location: game_edit.php?id=$newIDJogo");
exit;
} catch (PDOException $ex) {
die("Error calling Criar_Jogo: " . $ex->getMessage());
}
}
// 5) “Iniciar” => your existing logic to set the game to A Decorrer, import corridors, etc.
elseif ($action === 'iniciar') {
if (!$selectedIDJogo) {
// no game selected
header("Location: games.php?err=NoGameSelected");
exit;
}
try {
// (A) 1) Fetch main config from remote "setupmaze"
$pdoRemote = new PDO(
'mysql:host=194.210.86.10;port=3306;dbname=maze;charset=utf8mb4',
'aluno',
'aluno',
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
$stmtRem = $pdoRemote->query("
SELECT numbermarsamis, numberrooms, normalnoise,
noisevartoleration, timemarsamilive
FROM setupmaze
WHERE ID = 1
LIMIT 1
");
$cfg = $stmtRem->fetch(PDO::FETCH_ASSOC);
$stmtRem->closeCursor();
// If needed, also fetch corridor data now or later
$stmtCorr = $pdoRemote->query("
SELECT Rooma, Roomb, Distance
FROM corridor
");
$corridors = $stmtCorr->fetchAll(PDO::FETCH_ASSOC);
$stmtCorr->closeCursor();
$pdoRemote = null; // done with remote
// (A) 2) Map the config
$NumeroMarsamis = (int)$cfg['numbermarsamis'];
$NumeroSalas = (int)$cfg['numberrooms'];
$RuidoNormal = (float)$cfg['normalnoise'];
$ToleranciaVariacaoRuido = (float)$cfg['noisevartoleration'];
$TempoAteMarsamisPararem = (int)$cfg['timemarsamilive'];
// (B) Call Iniciar_Jogo with an OUT param
$stmtIni = $pdo->prepare("CALL Labirinto.Iniciar_Jogo(
:idJogo, :NumeroMarsamis, :NumeroSalas, :RuidoNormal, :ToleranciaVariacaoRuido, :TempoAteMarsamisPararem, @outValid
)");
$stmtIni->execute([
':idJogo' => (int)$selectedIDJogo,
':NumeroMarsamis' => $NumeroMarsamis,
':NumeroSalas' => $NumeroSalas,
':RuidoNormal' => $RuidoNormal,
':ToleranciaVariacaoRuido' => $ToleranciaVariacaoRuido,
':TempoAteMarsamisPararem'=> $TempoAteMarsamisPararem
]);
$stmtIni->closeCursor();
// (C) Read the OUT param
$stmtVal = $pdo->query("SELECT @outValid AS Valid");
$rowVal = $stmtVal->fetch(PDO::FETCH_ASSOC);
$stmtVal->closeCursor();
if (!$rowVal || $rowVal['Valid'] != 1) {
// fail => game not started
header("Location: games.php?err=CannotStart");
exit;
}
// (D) If we get here => the game is started successfully => import corridor
// For each corridor row from the remote DB, call InicializarTopologiaLabirinto
$stmtTopo = $pdo->prepare("
CALL Labirinto.InicializarTopologiaLabirinto(
:IDJogo, :SalaOrigem, :SalaDestino
)
");
foreach ($corridors as $corridor) {
$stmtTopo->execute([
':IDJogo' => (int)$selectedIDJogo,
':SalaOrigem' => (int)$corridor['Rooma'],
':SalaDestino'=> (int)$corridor['Roomb']
]);
$stmtTopo->closeCursor();
}
// (E) Now initialize occupant rows => all 0 occupant
// using SP: InicializarOcupacaoSalas(IDJogo, Sala)
for ($sala = 0; $sala <= $NumeroSalas; $sala++) {
$stmtOcc = $pdo->prepare("
CALL Labirinto.InicializarOcupacaoSalas(:IDJogo, :IDSala)
");
$stmtOcc->execute([
':IDJogo' => (int)$selectedIDJogo,
':IDSala' => $sala
]);
$stmtOcc->closeCursor();
}
// At this point, Sala 0 also has 0 occupant if you want them all 0.
// (F) Insert Marsamis by looping in PHP => call the SP for each
try {
for ($i = 1; $i <= $NumeroMarsamis; $i++) {
$stmtMars = $pdo->prepare("CALL Labirinto.InicializarPosicaoMarsami(:IDJogo, :Numero)");
$stmtMars->execute([
':IDJogo' => (int)$selectedIDJogo,
':Numero' => $i
]);
// Make sure we consume any leftover result sets
$stmtMars->closeCursor();
}
} catch (PDOException $ex) {
die("Error calling InicializarPosicaoMarsami loop: " . $ex->getMessage());
}
// (G) Launch a Python script if needed (fire-and-forget)
$pythonCmd = "python"; // Must be in PATH (check with 'python --version')
$scriptPath = "C:\\xampp\\tools\\mazerun\\mazerun.py";
// We use 'start /B' so it runs in background.
// The double quotes "" after /B is an empty window title param required by Windows.
$command = sprintf(
'start /B "" %s "%s"',
$pythonCmd,
$scriptPath
);
// pclose(popen(..., "r")) => fire-and-forget, doesn't block PHP
pclose(popen($command, "r"));
// Done => redirect
header("Location: games.php?msg=GameStartedWithTopology");
exit;
} catch (PDOException $ex) {
die("Error in Iniciar_Jogo or importing corridors: " . $ex->getMessage());
}
}
// 6) “Terminar” => new code to set game to Concluído
elseif ($action === 'terminar') {
if (!$selectedIDJogo) {
header("Location: games.php?err=NoGameSelected");
exit;
}
try {
// We call Terminar_Jogo with an OUT param @outValid
$stmtTerm = $pdo->prepare("
CALL Labirinto.Terminar_Jogo(:IDJogo, @outValid)
");
$stmtTerm->execute([':IDJogo' => (int)$selectedIDJogo]);
$stmtTerm->closeCursor();
// Now read @outValid
$stmtVal = $pdo->query("SELECT @outValid AS Valid");
$rowVal = $stmtVal->fetch(PDO::FETCH_ASSOC);
$stmtVal->closeCursor();
if (!$rowVal || $rowVal['Valid'] != 1) {
// fail => game not in state=2
header("Location: games.php?err=CannotTerminate");
} else {
// success
header("Location: games.php?msg=GameTerminated");
}
exit;
} catch (PDOException $ex) {
die("Error in Terminar_Jogo: " . $ex->getMessage());
}
}
// 7) “Visualizar” => new code to view game details
elseif ($action === 'visualizar') {
if (!$selectedIDJogo) {
header("Location: games.php?err=NoGameSelected");
exit;
}
// Just go to a new page with ?id= that game
header("Location: game_view.php?id=$selectedIDJogo");
exit;
}
else {
// 8) No action specified => show the list of games
// (A) Call ViewListaJogos() to get all games
$stmt = $pdo->query("CALL Labirinto.ViewListaJogos()");
$games = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
}
// 9) Fetch all messages from the user’s current game
$stmtM = $pdo->query("CALL Labirinto.ViewMensagens()");
$messages = $stmtM->fetchAll(PDO::FETCH_ASSOC);
$stmtM->closeCursor();
$pageTitle = "Games";
$contentFile = "games_content.php";
include('layout.php');