Skip to content

Commit

Permalink
Test4 'leaveLobby' function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashikkalis committed Dec 23, 2024
1 parent 6675ee9 commit bea74c2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
9 changes: 3 additions & 6 deletions blokus.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,11 @@ public function routeRequest($input) {
}
});

$router->add('POST', 'lobbys/leave', function($input) {
if (isset($input['lobbyId'])) {
leaveLobby((int)$input['lobbyId']);
} else {
echo json_encode(['error' => 'Missing lobbyId parameter']);
}
$router->add('POST', 'lobbys/leave', function() {
leaveLobby(); // No parameters needed, as `lobbyId` is hardcoded in the function
});


// $router->add('POST', 'lobbys/leave', function($input) {
// if (isset($input['userId']) && isset($input['lobbyId'])) {
// leaveLobby((int)$input['userId'], (int)$input['lobbyId']);
Expand Down
12 changes: 10 additions & 2 deletions lib/lobbys.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,22 @@ function joinLobby($userId, $lobbyId) {
}
}

function leaveLobby($lobbyId) {
function leaveLobby() {
$pdo = getDatabaseConnection();

// Static lobby ID for testing
$lobbyId = 2;

try {
$sql = "DELETE FROM game_lobbies WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$lobbyId]);

echo json_encode(['success' => true, 'message' => 'Lobby deleted successfully']);
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()]);
}
Expand Down

0 comments on commit bea74c2

Please sign in to comment.