Skip to content

Commit

Permalink
Test 'createLobby' function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashikkalis committed Dec 23, 2024
1 parent 2094de8 commit b0b8161
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
17 changes: 11 additions & 6 deletions blokus.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,19 @@ public function routeRequest($input) {

//lobby functions
$router->add('GET', 'lobbys', 'getLobbies');
$router->add('GET', 'lobbys/uid', function($input) { //Figure out how we can test this one
if (isset($input['player1_id']) && is_numeric($input['player1_id'])) {
createLobby((int)$input['player1_id']);
} else {
echo json_encode(['error' => 'Invalid or missing userid parameter.']);
}

$router->add('POST', 'lobbys/create', function() {
createLobby(); // No parameters needed as the userId is hardcoded
});

// $router->add('GET', 'lobbys/create', function($input) { //Figure out how we can test this one
// if (isset($input['player1_id']) && is_numeric($input['player1_id'])) {
// createLobby((int)$input['player1_id']);
// } else {
// echo json_encode(['error' => 'Invalid or missing userid parameter.']);
// }
// });

$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']);
Expand Down
22 changes: 21 additions & 1 deletion lib/lobbys.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ function getLobbies() {
}
}

function createLobby($userId) {
function createLobby() {
$pdo = getDatabaseConnection();

// Hardcoded userId for testing
$userId = 1; // Replace with any valid user ID from your database

try {
$sql = "INSERT INTO game_lobbies (player1_id) VALUES (:userId)";
$stmt = $pdo->prepare($sql);
Expand All @@ -32,6 +36,22 @@ function createLobby($userId) {
}
}

// function createLobby($userId) {
// $pdo = getDatabaseConnection();
// try {
// $sql = "INSERT INTO game_lobbies (player1_id) VALUES (:userId)";
// $stmt = $pdo->prepare($sql);
// $stmt->bindParam(':userId', $userId, PDO::PARAM_INT);
// $stmt->execute();

// $lobbyId = $pdo->lastInsertId();

// echo json_encode(['lobby_id' => (int)$lobbyId]);
// } catch (PDOException $e) {
// echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
// }
// }

function joinLobby($userId, $lobbyId) {
$pdo = getDatabaseConnection();

Expand Down

0 comments on commit b0b8161

Please sign in to comment.