import { useGameStore } from '@/store'; import { ACHIEVEMENT_DEFINITIONS } from '@token-empire/game-engine'; import { formatNumber } from '@token-empire/shared'; import { Trophy, Lock, Server, Brain, Rocket, DollarSign, Sprout, Users, Globe, Sparkles, TrendingUp, Building2, Atom, Cpu, FlaskConical, GitBranch, Zap, } from 'lucide-react'; import type { AchievementCondition } from '@token-empire/shared'; const ICON_MAP: Record> = { Trophy, Server, Brain, Rocket, DollarSign, Sprout, Users, Globe, Sparkles, TrendingUp, Building2, Atom, Cpu, FlaskConical, GitBranch, Zap, }; function resolveField(state: Record, path: string): number { const parts = path.split('.'); let current: unknown = state; for (const part of parts) { if (current == null || typeof current !== 'object') return 0; current = (current as Record)[part]; } return typeof current === 'number' ? current : 0; } function getProgress(state: Record, condition: AchievementCondition): { current: number; target: number; pct: number } | null { if (condition.field.startsWith('meta._')) return null; if (condition.operator === 'gt' && condition.value === 0) { const current = resolveField(state, condition.field); return { current, target: 1, pct: current > 0 ? 100 : 0 }; } if (condition.operator === 'gte' || condition.operator === 'eq') { const current = resolveField(state, condition.field); const pct = condition.value > 0 ? Math.min(100, (current / condition.value) * 100) : (current > 0 ? 100 : 0); return { current, target: condition.value, pct }; } return null; } export function AchievementsPage() { const unlocked = useGameStore((s) => s.achievements.unlocked); const unlockedIds = new Set(unlocked.map(a => a.id)); const state = useGameStore.getState() as unknown as Record; return (

Achievements

{unlocked.length} / {ACHIEVEMENT_DEFINITIONS.length} unlocked
{ACHIEVEMENT_DEFINITIONS.map(def => { const isUnlocked = unlockedIds.has(def.id); const IconComponent = ICON_MAP[def.icon] ?? Trophy; const progress = !isUnlocked ? getProgress(state, def.condition) : null; return (
{isUnlocked ? : }

{def.name}

{def.description}

{isUnlocked && (

Unlocked

)} {!isUnlocked && progress && progress.target > 0 && (
{formatNumber(progress.current)} / {formatNumber(progress.target)} {Math.floor(progress.pct)}%
)}
); })}
); }