Commit a43f91fe authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix: use matches table (not games) + fix analysis API table refs

Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 4f3da099
...@@ -21,7 +21,7 @@ if ($method === 'GET') { ...@@ -21,7 +21,7 @@ if ($method === 'GET') {
exit; exit;
} }
$res = supabase_rest('GET', "games?id=eq.{$gameId}&select=id,bot_id,player_color,pgn,fen,final_fen,result,reason,time_control,increment,rated,status,started_at,ended_at,game_state,clock_white,clock_black", [], $token); $res = supabase_rest('GET', "matches?id=eq.{$gameId}&select=*", [], $token);
if ($res['status'] >= 200 && $res['status'] < 300 && !empty($res['data'])) { if ($res['status'] >= 200 && $res['status'] < 300 && !empty($res['data'])) {
$game = $res['data'][0]; $game = $res['data'][0];
...@@ -83,7 +83,7 @@ switch ($action) { ...@@ -83,7 +83,7 @@ switch ($action) {
$response = curl_exec($ch); $response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); unset($ch);
if ($httpCode === 200) { if ($httpCode === 200) {
$data = json_decode($response, true); $data = json_decode($response, true);
...@@ -118,7 +118,7 @@ switch ($action) { ...@@ -118,7 +118,7 @@ switch ($action) {
]; ];
// Get existing game_state // Get existing game_state
$gameRes = supabase_rest('GET', "games?id=eq.{$gameId}&select=game_state", [], $token); $gameRes = supabase_rest('GET', "matches?id=eq.{$gameId}&select=game_state", [], $token);
$existingState = []; $existingState = [];
if (!empty($gameRes['data'][0]['game_state'])) { if (!empty($gameRes['data'][0]['game_state'])) {
$existingState = $gameRes['data'][0]['game_state']; $existingState = $gameRes['data'][0]['game_state'];
...@@ -128,7 +128,7 @@ switch ($action) { ...@@ -128,7 +128,7 @@ switch ($action) {
} }
$existingState['analysis'] = $analysisData; $existingState['analysis'] = $analysisData;
supabase_rest('PATCH', "games?id=eq.{$gameId}", [ supabase_rest('PATCH', "matches?id=eq.{$gameId}", [
'game_state' => json_encode($existingState) 'game_state' => json_encode($existingState)
], $token); ], $token);
...@@ -167,7 +167,7 @@ switch ($action) { ...@@ -167,7 +167,7 @@ switch ($action) {
$response = curl_exec($ch); $response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); unset($ch);
if ($httpCode === 200) { if ($httpCode === 200) {
$data = json_decode($response, true); $data = json_decode($response, true);
......
...@@ -16,8 +16,17 @@ $method = $_SERVER['REQUEST_METHOD']; ...@@ -16,8 +16,17 @@ $method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') { if ($method === 'GET') {
$action = $_GET['action'] ?? ''; $action = $_GET['action'] ?? '';
if ($action === 'recent') { if ($action === 'recent') {
$res = supabase_rest('GET', 'games?select=id,bot_id,result,reason,time_control,created_at&status=eq.completed&order=created_at.desc&limit=10', [], $token); $res = supabase_rest('GET', 'matches?select=id,bot_id,result,time_control,completed_at&status=eq.completed&order=completed_at.desc&limit=10', [], $token);
echo json_encode(['games' => $res['data'] ?? []]); echo json_encode(['games' => $res['data'] ?? []]);
} elseif ($action === 'get') {
$id = $_GET['id'] ?? '';
$res = supabase_rest('GET', "matches?id=eq.{$id}&select=*", [], $token);
if (!empty($res['data'])) {
echo json_encode(['game' => $res['data'][0]]);
} else {
http_response_code(404);
echo json_encode(['error' => 'not found']);
}
} else { } else {
echo json_encode(['error' => 'invalid action']); echo json_encode(['error' => 'invalid action']);
} }
...@@ -35,18 +44,34 @@ switch ($action) { ...@@ -35,18 +44,34 @@ switch ($action) {
$increment = $input['increment'] ?? 0; $increment = $input['increment'] ?? 0;
$rated = $input['rated'] ?? true; $rated = $input['rated'] ?? true;
$gameData = [ $userRes = supabase_auth('user', [], $token, 'GET');
'player_color' => $color, $userId = $userRes['data']['id'] ?? null;
$matchData = [
'match_type' => 'bot',
'bot_id' => $botId, 'bot_id' => $botId,
'time_control' => $timeControl, 'bot_difficulty' => $botId,
'increment' => $increment, 'status' => 'in_progress',
'rated' => $rated, 'time_control' => $timeControl <= 180 ? 'bullet' : ($timeControl <= 600 ? 'blitz' : 'rapid'),
'status' => 'active', 'initial_time_ms' => $timeControl * 1000,
'increment_ms' => $increment * 1000,
'white_time_remaining_ms' => $timeControl * 1000,
'black_time_remaining_ms' => $timeControl * 1000,
'is_rated' => $rated,
'starting_fen' => 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
'current_fen' => 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
'move_count' => 0,
'moves' => '[]',
'started_at' => date('c'), 'started_at' => date('c'),
'fen' => 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
]; ];
$res = supabase_rest('POST', 'games', $gameData, $token); if ($color === 'w') {
$matchData['white_player_id'] = $userId;
} else {
$matchData['black_player_id'] = $userId;
}
$res = supabase_rest('POST', 'matches', $matchData, $token);
if ($res['status'] >= 200 && $res['status'] < 300 && !empty($res['data'])) { if ($res['status'] >= 200 && $res['status'] < 300 && !empty($res['data'])) {
echo json_encode(['game_id' => $res['data'][0]['id'] ?? null, 'status' => 'started']); echo json_encode(['game_id' => $res['data'][0]['id'] ?? null, 'status' => 'started']);
...@@ -85,7 +110,7 @@ switch ($action) { ...@@ -85,7 +110,7 @@ switch ($action) {
if ($httpCode === 200) { if ($httpCode === 200) {
$data = json_decode($response, true); $data = json_decode($response, true);
echo json_encode(['move' => $data['best_move'] ?? null]); echo json_encode(['move' => $data['best_move'] ?? null, 'eval' => $data['evaluation'] ?? null]);
} else { } else {
http_response_code(502); http_response_code(502);
echo json_encode(['error' => 'bot_unavailable', 'status' => $httpCode]); echo json_encode(['error' => 'bot_unavailable', 'status' => $httpCode]);
...@@ -96,14 +121,19 @@ switch ($action) { ...@@ -96,14 +121,19 @@ switch ($action) {
$gameId = $input['game_id'] ?? ''; $gameId = $input['game_id'] ?? '';
$move = $input['move'] ?? ''; $move = $input['move'] ?? '';
$fen = $input['fen'] ?? ''; $fen = $input['fen'] ?? '';
$moves = $input['moves'] ?? null;
if ($gameId && !str_starts_with($gameId, 'game_')) { if ($gameId && !str_starts_with($gameId, 'game_')) {
supabase_rest('PATCH', "games?id=eq.{$gameId}", [ $update = [
'fen' => $fen, 'current_fen' => $fen,
'last_move' => $move, 'white_time_remaining_ms' => ($input['clock_white'] ?? 0) * 1000,
'clock_white' => $input['clock_white'] ?? null, 'black_time_remaining_ms' => ($input['clock_black'] ?? 0) * 1000,
'clock_black' => $input['clock_black'] ?? null ];
], $token); if ($moves !== null) {
$update['moves'] = json_encode($moves);
$update['move_count'] = count($moves);
}
supabase_rest('PATCH', "matches?id=eq.{$gameId}", $update, $token);
} }
echo json_encode(['ok' => true]); echo json_encode(['ok' => true]);
break; break;
...@@ -114,23 +144,26 @@ switch ($action) { ...@@ -114,23 +144,26 @@ switch ($action) {
$reason = $input['reason'] ?? ''; $reason = $input['reason'] ?? '';
$pgn = $input['pgn'] ?? ''; $pgn = $input['pgn'] ?? '';
$finalFen = $input['final_fen'] ?? ''; $finalFen = $input['final_fen'] ?? '';
$moves = $input['moves'] ?? null;
$resultMap = ['win' => 'white_win', 'loss' => 'black_win', 'draw' => 'draw'];
if ($gameId && !str_starts_with($gameId, 'game_')) { if ($gameId && !str_starts_with($gameId, 'game_')) {
supabase_rest('PATCH', "games?id=eq.{$gameId}", [ $update = [
'status' => 'completed', 'status' => 'completed',
'result' => $result, 'result' => $resultMap[$result] ?? $result,
'reason' => $reason,
'pgn' => $pgn, 'pgn' => $pgn,
'final_fen' => $finalFen, 'current_fen' => $finalFen,
'ended_at' => date('c') 'completed_at' => date('c'),
], $token); ];
} if ($moves !== null) {
$update['moves'] = json_encode($moves);
if ($result === 'win') { $update['move_count'] = count($moves);
supabase_rest('POST', 'rpc/add_coins', ['amount' => 50], $token); }
supabase_rest('PATCH', "matches?id=eq.{$gameId}", $update, $token);
} }
echo json_encode(['ok' => true, 'result' => $result]); echo json_encode(['ok' => true, 'result' => $result, 'game_id' => $gameId]);
break; break;
default: default:
......
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