'use client'; import { useState} from 'react'; import { ENTRY_FEE_DINO, MAX_ATTEMPTS } from '../shared'; interface LeaderboardEntry { owner: string; score: string; } interface LeaderboardHistoryEntry { owner: string; highest_score: number; } interface LeaderboardHistoryData { leaderboard_id: number; metadata: string; total_players: number; highest_score: number; lowest_score: number; average_score: number; players: LeaderboardHistoryEntry[]; } interface LeaderboardHistoryResponse { success: boolean; total_leaderboards: number; data: LeaderboardHistoryData[]; } interface LeaderboardProps { leaderboard: LeaderboardEntry[]; loading: boolean; error: string | null; attemptsCount: number; isRefreshing?: boolean; // New prop to track background refreshes refreshCounter?: number; // New prop to track refresh cycles } export default function Leaderboard({ leaderboard, loading, error, attemptsCount, isRefreshing = false, refreshCounter = 0 }: LeaderboardProps) { const [showHistory, setShowHistory] = useState(false); const [historyData, setHistoryData] = useState([]); const [historyLoading, setHistoryLoading] = useState(false); const [historyError, setHistoryError] = useState(null); const [currentLeaderboardIndex, setCurrentLeaderboardIndex] = useState(0); const fetchHistoryData = async () => { try { setHistoryLoading(true); setHistoryError(null); const response = await fetch('https://vps.playpoolstudios.com/dino/api/get_leaderboard_history.php'); if (!response.ok) { throw new Error('Failed to fetch leaderboard history'); } const data: LeaderboardHistoryResponse = await response.json(); if (data.success && data.data.length > 0) { setHistoryData(data.data); setCurrentLeaderboardIndex(0); // Reset to first leaderboard } else { throw new Error('No history data available'); } } catch (err) { console.error('Error fetching leaderboard history:', err); setHistoryError(err instanceof Error ? err.message : 'Failed to fetch leaderboard history'); } finally { setHistoryLoading(false); } }; const openHistoryModal = () => { setShowHistory(true); fetchHistoryData(); }; const nextLeaderboard = () => { setCurrentLeaderboardIndex((prev) => prev === historyData.length - 1 ? 0 : prev + 1 ); }; const prevLeaderboard = () => { setCurrentLeaderboardIndex((prev) => prev === 0 ? historyData.length - 1 : prev - 1 ); }; const currentLeaderboard = historyData[currentLeaderboardIndex]; // Only show loading state if we have no data yet if (loading && leaderboard.length === 0) { return (

🏆 Leaderboard

); } if (error) { return (

🏆 Leaderboard

Failed to load leaderboard

); } // Sort leaderboard by score (highest first) const sortedLeaderboard = [...leaderboard].sort((a, b) => parseInt(b.score) - parseInt(a.score) ); return ( <>

🏆 Leaderboard

{/* Update indicator */} {isRefreshing && (
Updating...
)}
{/* Combined Prize and Attempts Information */}

💰 Prize ({MAX_ATTEMPTS * ENTRY_FEE_DINO} DINO Total)

🎯 Attempts Remaining:
{MAX_ATTEMPTS - attemptsCount}
🥇 1st Place: {(MAX_ATTEMPTS * ENTRY_FEE_DINO * 0.8).toFixed(1)} DINO (80%)
🥈 2nd Place: {(MAX_ATTEMPTS * ENTRY_FEE_DINO * 0.1).toFixed(1)} DINO (10%)
🥉 3rd Place: {(MAX_ATTEMPTS * ENTRY_FEE_DINO * 0.05).toFixed(1)} DINO (5%)

Leaderboard resets after {MAX_ATTEMPTS} attempts

{sortedLeaderboard.length === 0 ? (

No scores yet. Be the first to play!

) : (
{sortedLeaderboard.map((entry, index) => (
{index + 1}

{entry.owner.length > 20 ? `${entry.owner.slice(0, 8)}...${entry.owner.slice(-8)}` : entry.owner }

{/* Prize indicators for top 3 */} {index < 3 && (
{index === 0 ? `🥇 ${(MAX_ATTEMPTS * ENTRY_FEE_DINO * 0.8).toFixed(1)} DINO Prize` : index === 1 ? `🥈 ${(MAX_ATTEMPTS * ENTRY_FEE_DINO * 0.1).toFixed(1)} DINO Prize` : `🥉 ${(MAX_ATTEMPTS * ENTRY_FEE_DINO * 0.05).toFixed(1)} DINO Prize` }
)}

{entry.score}

points

))}
)}

{sortedLeaderboard.length} player{sortedLeaderboard.length !== 1 ? 's' : ''}

{/* History Modal */} {showHistory && (
{/* Modal Header */}

📊 Leaderboard History

{/* Modal Content */}
{historyLoading ? (
) : historyError ? (

{historyError}

) : historyData.length > 0 ? (
{/* Leaderboard Carousel Navigation */}

Leaderboard #{currentLeaderboard.leaderboard_id}

{currentLeaderboardIndex + 1} of {historyData.length}

{/* Statistics Summary */}
{currentLeaderboard.total_players}
Total Players
{currentLeaderboard.highest_score}
Highest Score
{currentLeaderboard.lowest_score}
Lowest Score
{currentLeaderboard.average_score.toFixed(1)}
Average Score
{/* History Entries */}

All Time Entries

{currentLeaderboard.players.map((entry, index) => (
{index + 1}

{entry.owner.length > 20 ? `${entry.owner.slice(0, 8)}...${entry.owner.slice(-8)}` : entry.owner }

{entry.highest_score}

points

))}
) : null}
{/* Modal Footer */}
)} ); }