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) {
$category = $_GET['category'] ?? null;
$endpoint = 'achievements?select=*&order=sort_order.asc';
$endpoint = 'achievements?select=*&order=category.asc,tier.asc';
if ($category) {
$endpoint .= '&category=eq.' . $category;
}
$achRes = supabase_rest('GET', $endpoint, [], $token);
$achievements = $achRes['data'] ?? [];
$unlockedRes = supabase_rest('GET', 'user_achievements?select=achievement_id,unlocked_at', [], $token);
$unlocked = [];
if ($unlockedRes['status'] === 200 && is_array($unlockedRes['data'])) {
foreach ($unlockedRes['data'] as $row) {
$unlocked[$row['achievement_id']] = $row['unlocked_at'];
$achievements = (isset($achRes['data']) && is_array($achRes['data'])) ? $achRes['data'] : [];
if (!empty($achievements)) {
$unlockedRes = supabase_rest('GET', 'player_achievements?select=achievement_id,unlocked_at', [], $token);
$unlocked = [];
if ($unlockedRes['status'] === 200 && is_array($unlockedRes['data'])) {
foreach ($unlockedRes['data'] as $row) {
$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) {
$ach['unlocked'] = isset($unlocked[$ach['id']]);
$ach['unlocked_at'] = $unlocked[$ach['id']] ?? null;
$ach['progress'] = 0;
}
}
foreach ($achievements as &$ach) {
$ach['unlocked'] = isset($unlocked[$ach['id']]);
$ach['unlocked_at'] = $unlocked[$ach['id']] ?? null;
$ach['progress'] = $progressMap[$ach['id']] ?? 0;
}
echo json_encode(['achievements' => $achievements]);
......@@ -17,16 +17,17 @@ if ($method === 'GET') {
$category = $_GET['category'] ?? 'boards';
$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'] : [];
$ownedRes = supabase_rest('GET', 'user_items?select=item_id', [], $token);
$ownedIds = [];
if ($ownedRes['status'] === 200 && is_array($ownedRes['data'])) {
$ownedIds = array_column($ownedRes['data'], 'item_id');
}
foreach ($items as &$item) {
$item['owned'] = in_array($item['id'], $ownedIds);
if (!empty($items)) {
$ownedRes = supabase_rest('GET', 'user_items?select=item_id', [], $token);
$ownedIds = [];
if ($ownedRes['status'] === 200 && is_array($ownedRes['data'])) {
$ownedIds = array_column($ownedRes['data'], 'item_id');
}
foreach ($items as &$item) {
$item['owned'] = in_array($item['id'], $ownedIds);
}
}
echo json_encode(['items' => $items]);
......
......@@ -32,7 +32,7 @@ if ($method === 'GET') {
echo json_encode(['tournament' => $tournament, 'standings' => $standings]);
} 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'] ?? []]);
}
} elseif ($method === 'POST') {
......
......@@ -79,12 +79,12 @@ async function loadTournaments() {
const completed = [];
data.tournaments.forEach(t => {
const start = new Date(t.start_time);
const end = t.end_time ? new Date(t.end_time) : null;
const start = t.started_at ? new Date(t.started_at) : null;
const end = t.completed_at ? new Date(t.completed_at) : null;
if (t.status === 'completed' || (end && end < now)) {
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);
} else {
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