Skip to content

Commit

Permalink
create lobby Test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashikkalis committed Jan 10, 2025
1 parent 3d7133a commit 866dc2e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
16 changes: 8 additions & 8 deletions blokus.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@
$router->add('GET', 'lobbys', 'getLobbies');
$router->add('GET', 'lobbys/{id}', 'getLobby');

// $router->add('POST', 'lobbys/create', function($input) {
// if (!isset($input['hostId'])) {
// echo json_encode(['error' => 'Missing required parameters: hostId']);
// return;
// }
// $hostId = (int)$input['hostId'];
$router->add('POST', 'lobbys/create', function($input) {
if (!isset($input['hostId'])) {
echo json_encode(['error' => 'Missing required parameters: hostId']);
return;
}
$hostId = (int)$input['hostId'];

// createGame($hostId);
// });
createGame($hostId);
});


$router->add('POST', 'lobbys/leave', function($input) {
Expand Down
42 changes: 21 additions & 21 deletions lib/lobbys.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,27 @@ function getLobby($lobby_id) {
}

//===============IN PROGGRESS===============
// function createGame($host_id) {
// $pdo = getDatabaseConnection();
// try {
// $sql = "CALL CreateGame(:host_id, @game_id);"; // Call the stored procedure
// $stmt = $pdo->prepare($sql);
// $stmt->bindParam(':host_id', $host_id, PDO::PARAM_INT);
// $stmt->execute();

// // Retrieve the output parameter (game_id)
// $result = $pdo->query("SELECT @game_id AS game_id;");
// $game = $result->fetch(PDO::FETCH_ASSOC);

// if ($game && isset($game['game_id'])) {
// echo json_encode(['game_id' => $game['game_id']], JSON_PRETTY_PRINT);
// } else {
// echo json_encode(['error' => 'Failed to create game'], JSON_PRETTY_PRINT);
// }
// } catch (PDOException $e) {
// echo json_encode(['error' => 'Error in createGame(): ' . $e->getMessage()]);
// }
// }
function createGame($host_id) {
$pdo = getDatabaseConnection();
try {
$sql = "CALL CreateGame(:host_id, @game_id);";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':host_id', $host_id, PDO::PARAM_INT);
$stmt->execute();

// Retrieve the output parameter (game_id)
$result = $pdo->query("SELECT @game_id AS game_id;");
$game = $result->fetch(PDO::FETCH_ASSOC);

if ($game && isset($game['game_id'])) {
echo json_encode(['game_id' => $game['game_id']], JSON_PRETTY_PRINT);
} else {
echo json_encode(['error' => 'Failed to create game'], JSON_PRETTY_PRINT);
}
} catch (PDOException $e) {
echo json_encode(['error' => 'Error in createGame(): ' . $e->getMessage()]);
}
}

//===============DONE===============
function joinLobby($userId, $gameId) {
Expand Down

0 comments on commit 866dc2e

Please sign in to comment.