Skip to content

Commit

Permalink
Create 'leaveLobby' function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashikkalis committed Dec 23, 2024
1 parent 33523cf commit 0e789e4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
10 changes: 9 additions & 1 deletion blokus.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,22 @@ 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 {
echo json_encode(['error' => 'Missing userId or lobbyId parameters']);
}
});

$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);
Expand Down
39 changes: 35 additions & 4 deletions lib/lobbys.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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()]);
}
}
Expand Down

0 comments on commit 0e789e4

Please sign in to comment.