Skip to content

Commit

Permalink
Test8 'leaveLobby' function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashikkalis committed Dec 23, 2024
1 parent 0504d5e commit 2094de8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
22 changes: 11 additions & 11 deletions blokus.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,19 @@ public function routeRequest($input) {
});

// //works
// $router->add('POST', 'lobbys/leave', function() {
// leaveLobby();
// });


$router->add('POST', 'lobbys/leave', function($input) {
if (isset($input['id'])) {
leaveLobby((int)$input['id']);
} else {
echo json_encode(['error' => 'Missing lobbyId parameter']);
}
$router->add('POST', 'lobbys/leave', function() {
leaveLobby();
});

//Issue with parameters
// $router->add('POST', 'lobbys/leave', function($input) {
// if (isset($input['id'])) {
// leaveLobby((int)$input['id']);
// } else {
// echo json_encode(['error' => 'Missing lobbyId parameter']);
// }
// });

// Handle the request
$input = json_decode(file_get_contents('php://input'), true);
$router->routeRequest($input);
Expand Down
43 changes: 22 additions & 21 deletions lib/lobbys.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,9 @@ function joinLobby($userId, $lobbyId) {
}

//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($lobbyId) {
function leaveLobby() {
$pdo = getDatabaseConnection();
$lobbyId = "SELECT id FROM game_lobbies";
$lobbyId = 2;

try {
$sql = "DELETE FROM game_lobbies WHERE id = ?";
Expand All @@ -95,6 +76,26 @@ function leaveLobby($lobbyId) {
}
}

//Issue with parameters
// function leaveLobby($lobbyId) {
// $pdo = getDatabaseConnection();
// $lobbyId = "SELECT id FROM game_lobbies";

// 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()]);
// }
// }

// Handle all API requests
function handleRequest() {
// Check if the action parameter is set
Expand Down

0 comments on commit 2094de8

Please sign in to comment.