diff --git a/blokus.php b/blokus.php index c49ebd4..162681e 100644 --- a/blokus.php +++ b/blokus.php @@ -110,7 +110,7 @@ public function routeRequest($input) { } }); -$router->add('POST', 'lobbys/join', function($input) { +$router->add('POST', 'lobbys/join', function($input) { //Figure out how we can test this one too if (isset($input['userId']) && isset($input['lobbyId'])) { joinLobby((int)$input['userId'], (int)$input['lobbyId']); } else { @@ -118,6 +118,14 @@ public function routeRequest($input) { } }); +$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']); + } +}); + // Handle the request $input = json_decode(file_get_contents('php://input'), true); $router->routeRequest($input); diff --git a/lib/lobbys.php b/lib/lobbys.php index 0f2f6e5..7896c74 100644 --- a/lib/lobbys.php +++ b/lib/lobbys.php @@ -36,14 +36,12 @@ function joinLobby($userId, $lobbyId) { $pdo = getDatabaseConnection(); try { - // Check if the lobby exists and is in 'waiting' status $sql = "SELECT * FROM game_lobbies WHERE id = ? AND status = 'waiting'"; $stmt = $pdo->prepare($sql); $stmt->execute([$lobbyId]); $lobby = $stmt->fetch(PDO::FETCH_ASSOC); if ($lobby) { - // Update the lobby to add the player and change the status to 'full' or similar $sql = "UPDATE game_lobbies SET player1_id = ?, status = 'full' WHERE id = ?"; $stmt = $pdo->prepare($sql); $stmt->execute([$userId, $lobbyId]); @@ -52,10 +50,43 @@ function joinLobby($userId, $lobbyId) { return; } - // If the lobby is not available, send an error message echo json_encode(['error' => 'Lobby is not available or already full']); } catch (PDOException $e) { - // Handle database errors + echo json_encode(['error' => 'Database error: ' . $e->getMessage()]); + } +} + +function leaveLobby($userId, $lobbyId) { + $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; + } + } + + echo json_encode(['error' => 'You are not part of this lobby']); + } catch (PDOException $e) { echo json_encode(['error' => 'Database error: ' . $e->getMessage()]); } }