Commit f96c69db authored by Mahmoud Aglan's avatar Mahmoud Aglan

feat: challenges + battlepass now read from DB tables (admin-configurable)

challenges.php:
- Reads challenge_templates from DB instead of hardcoded array
- Admin can add/remove/edit challenge types via management panel
- Fallback to 3 basic challenges if DB is empty

battlepass.php:
- Reads active season from 'seasons' table instead of hardcoded config
- Admin can create new seasons, set dates, tiers, XP requirements
- Fallback to Season 1 defaults if no active season

Both APIs now fully driven by database:
- Management panel writes to tables → Player app reads automatically
- No more hardcoded game config in PHP files
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 211a94f5
......@@ -27,7 +27,22 @@ if ($method === 'POST') {
}
function getBattlePass($db, string $userId): void {
// Current season config
// Load active season from DB (admin-configurable)
$sdb = supabaseService();
$seasons = $sdb->get('seasons', ['is_active' => 'eq.true', 'select' => '*', 'limit' => 1]);
if (!empty($seasons) && !isset($seasons['error']) && !empty($seasons[0])) {
$s = $seasons[0];
$season = [
'id' => $s['id'],
'name' => $s['name_ar'] ?? $s['name'],
'name_en' => $s['name'],
'starts_at' => $s['starts_at'],
'ends_at' => $s['ends_at'],
'total_tiers' => intval($s['total_tiers'] ?? 30),
'xp_per_tier' => intval($s['xp_per_tier'] ?? 100)
];
} else {
// Fallback hardcoded
$season = [
'id' => 'season_1',
'name' => 'الموسم الأول',
......@@ -37,6 +52,7 @@ function getBattlePass($db, string $userId): void {
'total_tiers' => 30,
'xp_per_tier' => 100
];
}
// Get player XP
$profiles = $db->get('profiles', ['id' => 'eq.' . $userId, 'select' => 'xp,level', 'limit' => 1]);
......
......@@ -34,24 +34,27 @@ function getDailyChallenges($db, string $userId): void {
$profile = is_array($profiles) && !empty($profiles) ? $profiles[0] : ['level' => 1];
$level = $profile['level'] ?? 1;
// Generate 3 daily challenges based on the day (deterministic per day + user)
$seed = crc32($today . $userId);
srand($seed);
// Load challenge templates from DB (admin-configurable)
$sdb = supabaseService();
$templates = $sdb->get('challenge_templates', ['is_active' => 'eq.true', 'select' => 'id,title,title_ar,type,target,reward_coins,reward_xp,icon']);
$allChallenges = [];
if (is_array($templates) && !isset($templates['error'])) {
foreach ($templates as $t) {
$allChallenges[] = ['id' => $t['id'], 'title' => $t['title_ar'] ?? $t['title'], 'title_en' => $t['title'], 'target' => intval($t['target']), 'type' => $t['type'], 'reward_coins' => intval($t['reward_coins']), 'reward_xp' => intval($t['reward_xp']), 'icon' => $t['icon'] ?? '🎮'];
}
}
if (empty($allChallenges)) {
// Fallback if DB is empty
$allChallenges = [
['id' => 'play_3', 'title' => 'العب 3 مباريات', 'title_en' => 'Play 3 games', 'target' => 3, 'type' => 'games_played', 'reward_coins' => 50, 'reward_xp' => 30, 'icon' => '🎮'],
['id' => 'win_2', 'title' => 'اربح مباراتين', 'title_en' => 'Win 2 games', 'target' => 2, 'type' => 'wins', 'reward_coins' => 80, 'reward_xp' => 50, 'icon' => '🏆'],
['id' => 'play_chess', 'title' => 'العب شطرنج', 'title_en' => 'Play a chess game', 'target' => 1, 'type' => 'chess_played', 'reward_coins' => 30, 'reward_xp' => 20, 'icon' => '♟'],
['id' => 'play_ludo', 'title' => 'العب لودو', 'title_en' => 'Play a Ludo game', 'target' => 1, 'type' => 'ludo_played', 'reward_coins' => 30, 'reward_xp' => 20, 'icon' => '🎲'],
['id' => 'play_domino', 'title' => 'العب دومينو', 'title_en' => 'Play a Domino game', 'target' => 1, 'type' => 'domino_played', 'reward_coins' => 30, 'reward_xp' => 20, 'icon' => '⬚'],
['id' => 'win_streak_3', 'title' => 'اربح 3 متتالية', 'title_en' => 'Win 3 in a row', 'target' => 3, 'type' => 'win_streak', 'reward_coins' => 150, 'reward_xp' => 80, 'icon' => '🔥'],
['id' => 'puzzle_5', 'title' => 'حل 5 أحجيات', 'title_en' => 'Solve 5 puzzles', 'target' => 5, 'type' => 'puzzles_solved', 'reward_coins' => 60, 'reward_xp' => 40, 'icon' => '🧩'],
['id' => 'play_5', 'title' => 'العب 5 مباريات', 'title_en' => 'Play 5 games', 'target' => 5, 'type' => 'games_played', 'reward_coins' => 100, 'reward_xp' => 60, 'icon' => '⚡'],
['id' => 'beat_bot', 'title' => 'اهزم بوت', 'title_en' => 'Beat a bot', 'target' => 1, 'type' => 'bot_wins', 'reward_coins' => 40, 'reward_xp' => 25, 'icon' => '🤖'],
['id' => 'claim_daily', 'title' => 'اجمع المكافأة اليومية', 'title_en' => 'Claim daily reward', 'target' => 1, 'type' => 'daily_claimed', 'reward_coins' => 20, 'reward_xp' => 10, 'icon' => '🎁'],
];
}
// Pick 3 different challenges for today
// Pick 3 deterministic per day+user
$seed = crc32($today . $userId);
srand($seed);
$shuffled = $allChallenges;
shuffle($shuffled);
$dailyChallenges = array_slice($shuffled, 0, 3);
......
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