From 823698a34ebac0bf24b8b2cc461b364968248863 Mon Sep 17 00:00:00 2001 From: ashikkalis Date: Mon, 23 Dec 2024 14:30:06 +0200 Subject: [PATCH] Test5 'leaveLobby' function --- blokus.php | 21 +++++----- lib/lobbys.php | 112 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 86 insertions(+), 47 deletions(-) diff --git a/blokus.php b/blokus.php index 5937236..f5edc4b 100644 --- a/blokus.php +++ b/blokus.php @@ -118,18 +118,19 @@ public function routeRequest($input) { } }); -$router->add('POST', 'lobbys/leave', function() { - leaveLobby(); // No parameters needed, as `lobbyId` is hardcoded in the function -}); +// //works +// $router->add('POST', 'lobbys/leave', function() { +// leaveLobby(); +// }); -// $router->add('POST', 'lobbys/leave', function($input) { -// if (isset($input['userId']) && isset($input['lobbyId'])) { -// leaveLobby((int)$input['userId'], (int)$input['lobbyId']); -// } else { -// echo json_encode(['error' => 'Missing userId or lobbyId parameters']); -// } -// }); +$router->add('POST', 'lobbys/leave', function($input) { + if (isset($input['lobbyId'])) { + leaveLobby((int)$input['lobbyId']); + } else { + echo json_encode(['error' => 'Missing lobbyId parameter']); + } +}); // Handle the request $input = json_decode(file_get_contents('php://input'), true); diff --git a/lib/lobbys.php b/lib/lobbys.php index d66a769..9fc2cc9 100644 --- a/lib/lobbys.php +++ b/lib/lobbys.php @@ -56,11 +56,28 @@ function joinLobby($userId, $lobbyId) { } } -function leaveLobby() { +//works +// function leaveLobby() { +// $pdo = getDatabaseConnection(); +// $lobbyId = 2; + +// try { +// $sql = "DELETE FROM game_lobbies WHERE id = ?"; +// $stmt = $pdo->prepare($sql); +// $stmt->execute([$lobbyId]); + +// if ($stmt->rowCount() > 0) { +// echo json_encode(['success' => true, 'message' => "Lobby with ID $lobbyId deleted successfully"]); +// } else { +// echo json_encode(['success' => false, 'message' => "No lobby found with ID $lobbyId"]); +// } +// } catch (PDOException $e) { +// echo json_encode(['error' => 'Database error: ' . $e->getMessage()]); +// } +// } + +function leaveLobby($lobby) { $pdo = getDatabaseConnection(); - - // Static lobby ID for testing - $lobbyId = 2; try { $sql = "DELETE FROM game_lobbies WHERE id = ?"; @@ -77,39 +94,60 @@ function leaveLobby() { } } -// function leaveLobby() { -// $pdo = getDatabaseConnection(); - -// try { -// $sql = "SELECT * FROM game_lobbies WHERE id = ? AND player1_id = ?"; -// $stmt = $pdo->prepare($sql); -// $stmt->execute([$lobbyId, $userId]); -// $lobby = $stmt->fetch(PDO::FETCH_ASSOC); - -// if ($lobby) { -// // If the user is the lobby owner, delete the lobby -// $sql = "DELETE FROM game_lobbies WHERE id = ?"; -// $stmt = $pdo->prepare($sql); -// $stmt->execute([$lobbyId]); - -// echo json_encode(['success' => true, 'message' => 'Lobby deleted successfully']); -// return; -// } else { -// // If the user is not the owner, remove them from the lobby -// $sql = "UPDATE game_lobbies SET player2_id = NULL WHERE id = ? AND player2_id = ?"; -// $stmt = $pdo->prepare($sql); -// $stmt->execute([$lobbyId, $userId]); - -// if ($stmt->rowCount() > 0) { -// echo json_encode(['success' => true, 'message' => 'You have left the lobby']); -// return; -// } -// } +// Handle all API requests +function handleRequest() { + // Check if the action parameter is set + if (isset($_GET['action'])) { + $action = $_GET['action']; + + // Handle based on the action + switch ($action) { + case 'getLobbies': + echo json_encode(getLobbies()); + break; + + case 'createLobby': + if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['userId'])) { // Use $_POST for POST data + $userId = (int) $_POST['userId']; + echo json_encode(createLobby($userId)); + } else { + echo json_encode(['error' => 'User ID is required']); + } + break; + + case 'joinLobby': + if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['userId'], $_POST['lobbyId'])) { + echo json_encode(joinLobby($_POST['userId'], $_POST['lobbyId'])); + } else { + echo json_encode(['error' => 'User ID and Lobby ID are required']); + } + break; + + case 'leaveLobby': + if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['userId'], $_POST['lobbyId'])) { + echo json_encode(leaveLobby($_POST['userId'], $_POST['lobbyId'])); + } else { + echo json_encode(['error' => 'User ID and Lobby ID are required']); + } + break; + + case 'getLobbyDetails': + if (isset($_GET['lobbyId'])) { + echo json_encode(getLobbyDetails($_GET['lobbyId'])); + } else { + echo json_encode(['error' => 'Lobby ID is required']); + } + break; + + default: + echo json_encode(['error' => 'Invalid action']); + break; + } + } else { + echo json_encode(['error' => 'Action parameter is missing']); + } +} -// echo json_encode(['error' => 'You are not part of this lobby']); -// } catch (PDOException $e) { -// echo json_encode(['error' => 'Database error: ' . $e->getMessage()]); -// } -// } +handleRequest(); ?> \ No newline at end of file