-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some functions in games.php file but they are not working yet
- Loading branch information
1 parent
3a2b604
commit e272213
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
require_once 'dbconnect.php'; | ||
require_once 'helper.php'; | ||
|
||
header('Content-Type: application/json'); | ||
|
||
function getNextPlayerTurn($gameId) { | ||
global $pdo; | ||
|
||
$sql = "SELECT current_turn FROM games WHERE game_id = ?"; | ||
$stmt = $pdo->prepare($sql); | ||
$stmt->execute([$gameId]); | ||
$currentTurn = $stmt->fetchColumn(); | ||
|
||
if ($currentTurn !== false) { | ||
$sql = "SELECT user_id FROM game_players WHERE game_id = ? ORDER BY player_order"; | ||
$stmt = $pdo->prepare($sql); | ||
$stmt->execute([$gameId]); | ||
$players = $stmt->fetchAll(PDO::FETCH_COLUMN); | ||
|
||
$currentIndex = array_search ($currentTurn, $players); | ||
$nextIndex = ($currentIndex + 1) % count($players); | ||
|
||
return $players[$nextIndex]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function getAvailableMoves($gameId, $playerId) { | ||
global $pdo; | ||
|
||
$sql = "SELECT board_state FROM games WHERE game_id = ?"; | ||
$stmt = $pdo->prepare($sql); | ||
$stmt->execute([$gameId]); | ||
$boardState = $stmt->fetchColumn(); | ||
|
||
$sql = "SELECT piece_id FROM player_pieces WHERE game_id = ? AND player_id = ? AND is_used = 0"; | ||
$stmt = $pdo->prepare($sql); | ||
$stmt->execute([$gameId, $playerId]); | ||
$availablePieces = $stmt->fetchAll(PDO::FETCH_COLUMN); | ||
|
||
$availableMoves = [ | ||
'placeholder' => 'Implement game rules to calculate moves' | ||
]; | ||
|
||
return $availableMoves; | ||
} | ||
|
||
function getPlayerPieces($gameId, $playerId) { | ||
global $pdo; | ||
|
||
$sql = "SELECT piece_id FROM player_pieces WHERE game_id = ? AND player_id = ? AND is_used = 0"; | ||
$stmt = $pdo->prepare($sql); | ||
$stmt->execute([$gameId, $playerId]); | ||
|
||
return $stmt->fetchAll(PDO::FETCH_COLUMN); | ||
} | ||
|
||
function updatePlayerMove($gameId, $playerId, $move) { | ||
global $pdo; | ||
|
||
$pdo->beginTransaction(); | ||
|
||
try { | ||
$sql = "UPDATE games SET board_state = ?, current_turn = ? WHERE game_id = ?"; | ||
$stmt = $pdo->prepare($sql); | ||
$newBoardState = json_encode($move['boardState']); | ||
$stmt->execute([$newBoardState, $move['nextPlayer'], $gameId]); | ||
|
||
$sql = "UPDATE player_pieces SET is_used = 1 WHERE game_id = ? AND player_id = ? AND piece_id = ?"; | ||
$stmt = $pdo->prepare($sql); | ||
$stmt->execute([$gameId, $playerId, $move['pieceId']]); | ||
|
||
$pdo->commit(); | ||
|
||
return true; | ||
} catch (Exception $e) { | ||
$pdo->rollBack(); | ||
return false; | ||
} | ||
} | ||
?> |