Commit 43b1e2a0 authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix: multiplayer table routing, backgammon state merge

Phase 2.4: multiplayer.php now routes to correct table for all games
(chess→matches, domino→domino_matches, backgammon→backgammon_matches,
ludo→ludo_matches). Validates game_key against allowed list.

Phase 4.3: handleServerState in backgammon only copies specific fields
(state, dice, turn, gameOver) instead of Object.assign which clobbered
local methods and properties.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent bc27e3c6
......@@ -14,10 +14,22 @@ $input = getInput();
$action = $input['action'] ?? '';
$gameKey = $input['game_key'] ?? '';
$validGames = ['chess', 'ludo', 'domino', 'backgammon'];
if ($gameKey && !in_array($gameKey, $validGames)) { jsonError('Invalid game_key', 400); }
function resolveTable(string $gameKey): string {
return match($gameKey) {
'chess' => 'matches',
'domino' => 'domino_matches',
'backgammon' => 'backgammon_matches',
default => 'ludo_matches',
};
}
switch ($action) {
case 'create_room':
$db = supabase($token);
$table = $gameKey === 'domino' ? 'domino_matches' : 'ludo_matches';
$table = resolveTable($gameKey);
$result = null;
$code = '';
for ($attempt = 0; $attempt < 5; $attempt++) {
......@@ -40,7 +52,7 @@ switch ($action) {
$roomCode = $input['room_code'] ?? '';
if (!$roomCode) jsonError('room_code required');
$sdb = supabaseService();
$table = $gameKey === 'domino' ? 'domino_matches' : 'ludo_matches';
$table = resolveTable($gameKey);
$match = $sdb->get($table, ['room_code' => 'eq.' . $roomCode, 'status' => 'eq.waiting', 'select' => 'id,players,player_count', 'limit' => 1]);
if (!is_array($match) || empty($match) || isset($match['error'])) jsonError('Room not found', 404);
$match = $match[0];
......
......@@ -633,7 +633,11 @@ function handleServerState(data) {
if (!data || data.error) return;
matchSession.markOpponentActive();
if (data.game_state) {
Object.assign(game, data.game_state);
const gs = data.game_state;
if (gs.state) game.state = gs.state;
if (gs.dice) game.dice = gs.dice;
if (gs.turn !== undefined) game.turn = gs.turn;
if (gs.gameOver !== undefined) game.gameOver = gs.gameOver;
if (data.match_state) match.scores = data.match_state.scores || match.scores;
if (data.cube) match.cube = data.cube;
updateUI();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment