Skip to content

Commit

Permalink
Test 'leaveLobby' function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashikkalis committed Dec 23, 2024
1 parent 0e789e4 commit ddeae9c
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions lib/lobbys.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,43 @@ function joinLobby($userId, $lobbyId) {

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

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

echo json_encode(['success' => true, 'message' => 'Lobby deleted successfully']);

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

?>

0 comments on commit ddeae9c

Please sign in to comment.