From 866dc2eb16c95b7fb7db2099f6f4182a47d1cb5a Mon Sep 17 00:00:00 2001 From: ashikkalis Date: Fri, 10 Jan 2025 11:29:08 +0200 Subject: [PATCH] create lobby Test --- blokus.php | 16 ++++++++-------- lib/lobbys.php | 42 +++++++++++++++++++++--------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/blokus.php b/blokus.php index 2382e57..200a054 100644 --- a/blokus.php +++ b/blokus.php @@ -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) { diff --git a/lib/lobbys.php b/lib/lobbys.php index e7fd52e..04011ca 100644 --- a/lib/lobbys.php +++ b/lib/lobbys.php @@ -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) {