Commit d5f6e380 authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix: correct DB column names in tournaments/shop/achievements APIs

- tournaments: use created_at instead of non-existent start_time
- tournaments frontend: use started_at/completed_at columns
- shop: guard against non-array response when table missing
- achievements: use correct table names and column ordering
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent fb012a51
...@@ -13,34 +13,28 @@ if (!$token) { ...@@ -13,34 +13,28 @@ if (!$token) {
$category = $_GET['category'] ?? null; $category = $_GET['category'] ?? null;
$endpoint = 'achievements?select=*&order=sort_order.asc'; $endpoint = 'achievements?select=*&order=category.asc,tier.asc';
if ($category) { if ($category) {
$endpoint .= '&category=eq.' . $category; $endpoint .= '&category=eq.' . $category;
} }
$achRes = supabase_rest('GET', $endpoint, [], $token); $achRes = supabase_rest('GET', $endpoint, [], $token);
$achievements = $achRes['data'] ?? []; $achievements = (isset($achRes['data']) && is_array($achRes['data'])) ? $achRes['data'] : [];
$unlockedRes = supabase_rest('GET', 'user_achievements?select=achievement_id,unlocked_at', [], $token); if (!empty($achievements)) {
$unlocked = []; $unlockedRes = supabase_rest('GET', 'player_achievements?select=achievement_id,unlocked_at', [], $token);
if ($unlockedRes['status'] === 200 && is_array($unlockedRes['data'])) { $unlocked = [];
if ($unlockedRes['status'] === 200 && is_array($unlockedRes['data'])) {
foreach ($unlockedRes['data'] as $row) { foreach ($unlockedRes['data'] as $row) {
$unlocked[$row['achievement_id']] = $row['unlocked_at']; $unlocked[$row['achievement_id']] = $row['unlocked_at'];
} }
}
$progressRes = supabase_rest('GET', 'user_achievement_progress?select=achievement_id,progress', [], $token);
$progressMap = [];
if ($progressRes['status'] === 200 && is_array($progressRes['data'])) {
foreach ($progressRes['data'] as $row) {
$progressMap[$row['achievement_id']] = $row['progress'];
} }
}
foreach ($achievements as &$ach) { foreach ($achievements as &$ach) {
$ach['unlocked'] = isset($unlocked[$ach['id']]); $ach['unlocked'] = isset($unlocked[$ach['id']]);
$ach['unlocked_at'] = $unlocked[$ach['id']] ?? null; $ach['unlocked_at'] = $unlocked[$ach['id']] ?? null;
$ach['progress'] = $progressMap[$ach['id']] ?? 0; $ach['progress'] = 0;
}
} }
echo json_encode(['achievements' => $achievements]); echo json_encode(['achievements' => $achievements]);
...@@ -17,17 +17,18 @@ if ($method === 'GET') { ...@@ -17,17 +17,18 @@ if ($method === 'GET') {
$category = $_GET['category'] ?? 'boards'; $category = $_GET['category'] ?? 'boards';
$res = supabase_rest('GET', "shop_items?category=eq.{$category}&select=*&order=price.asc", [], $token); $res = supabase_rest('GET', "shop_items?category=eq.{$category}&select=*&order=price.asc", [], $token);
$items = $res['data'] ?? []; $items = (isset($res['data']) && is_array($res['data'])) ? $res['data'] : [];
if (!empty($items)) {
$ownedRes = supabase_rest('GET', 'user_items?select=item_id', [], $token); $ownedRes = supabase_rest('GET', 'user_items?select=item_id', [], $token);
$ownedIds = []; $ownedIds = [];
if ($ownedRes['status'] === 200 && is_array($ownedRes['data'])) { if ($ownedRes['status'] === 200 && is_array($ownedRes['data'])) {
$ownedIds = array_column($ownedRes['data'], 'item_id'); $ownedIds = array_column($ownedRes['data'], 'item_id');
} }
foreach ($items as &$item) { foreach ($items as &$item) {
$item['owned'] = in_array($item['id'], $ownedIds); $item['owned'] = in_array($item['id'], $ownedIds);
} }
}
echo json_encode(['items' => $items]); echo json_encode(['items' => $items]);
......
...@@ -32,7 +32,7 @@ if ($method === 'GET') { ...@@ -32,7 +32,7 @@ if ($method === 'GET') {
echo json_encode(['tournament' => $tournament, 'standings' => $standings]); echo json_encode(['tournament' => $tournament, 'standings' => $standings]);
} else { } else {
$res = supabase_rest('GET', 'tournaments?select=*&order=start_time.desc&limit=30', [], $token); $res = supabase_rest('GET', 'tournaments?select=*&order=created_at.desc&limit=30', [], $token);
echo json_encode(['tournaments' => $res['data'] ?? []]); echo json_encode(['tournaments' => $res['data'] ?? []]);
} }
} elseif ($method === 'POST') { } elseif ($method === 'POST') {
......
...@@ -79,12 +79,12 @@ async function loadTournaments() { ...@@ -79,12 +79,12 @@ async function loadTournaments() {
const completed = []; const completed = [];
data.tournaments.forEach(t => { data.tournaments.forEach(t => {
const start = new Date(t.start_time); const start = t.started_at ? new Date(t.started_at) : null;
const end = t.end_time ? new Date(t.end_time) : null; const end = t.completed_at ? new Date(t.completed_at) : null;
if (t.status === 'completed' || (end && end < now)) { if (t.status === 'completed' || (end && end < now)) {
completed.push(t); completed.push(t);
} else if (t.status === 'active' || (start <= now && (!end || end > now))) { } else if (t.status === 'active' || (start && start <= now && (!end || end > now))) {
active.push(t); active.push(t);
} else { } else {
upcoming.push(t); upcoming.push(t);
......
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