94 lines
2.9 KiB
TypeScript
94 lines
2.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { redis } from '@/lib/redis'
|
|
|
|
const ACHIEVEMENTS = [
|
|
{ id: 'first_vote', name: 'First Vote', description: 'Cast your first vote', icon: '🗳️', requirement: 1 },
|
|
{ id: 'voter_10', name: 'Active Voter', description: 'Vote on 10 polls', icon: '📊', requirement: 10 },
|
|
{ id: 'voter_50', name: 'Poll Expert', description: 'Vote on 50 polls', icon: '🎯', requirement: 50 },
|
|
{ id: 'first_win', name: 'First Win', description: 'Win your first poll', icon: '🏆', requirement: 1 },
|
|
{ id: 'winner_10', name: 'Lucky Streak', description: 'Win 10 polls', icon: '🍀', requirement: 10 },
|
|
{ id: 'streak_3', name: 'Hot Streak', description: 'Win 3 in a row', icon: '🔥', requirement: 3 },
|
|
{ id: 'streak_5', name: 'Unstoppable', description: 'Win 5 in a row', icon: '⚡', requirement: 5 },
|
|
{ id: 'earner_10', name: 'Money Maker', description: 'Earn 10 cUSD', icon: '💰', requirement: 10 },
|
|
{ id: 'earner_100', name: 'Big Winner', description: 'Earn 100 cUSD', icon: '💎', requirement: 100 }
|
|
]
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(req.url)
|
|
const fid = searchParams.get('fid')
|
|
|
|
if (!fid) return NextResponse.json({ achievements: [] })
|
|
|
|
const votes = await redis.lrange(`votes:${fid}`, 0, -1)
|
|
|
|
let totalVotes = 0
|
|
let totalWins = 0
|
|
let totalEarnings = 0
|
|
let currentStreak = 0
|
|
let maxStreak = 0
|
|
|
|
for (const voteStr of votes) {
|
|
const vote = JSON.parse(voteStr)
|
|
totalVotes++
|
|
|
|
if (vote.won) {
|
|
totalWins++
|
|
totalEarnings += vote.winnings || 0
|
|
currentStreak++
|
|
if (currentStreak > maxStreak) maxStreak = currentStreak
|
|
} else {
|
|
currentStreak = 0
|
|
}
|
|
}
|
|
|
|
const progress = {
|
|
totalVotes,
|
|
totalWins,
|
|
totalEarnings,
|
|
maxStreak
|
|
}
|
|
|
|
const achievements = ACHIEVEMENTS.map(ach => {
|
|
let current = 0
|
|
let unlocked = false
|
|
|
|
switch (ach.id) {
|
|
case 'first_vote':
|
|
case 'voter_10':
|
|
case 'voter_50':
|
|
current = totalVotes
|
|
unlocked = totalVotes >= ach.requirement
|
|
break
|
|
case 'first_win':
|
|
case 'winner_10':
|
|
current = totalWins
|
|
unlocked = totalWins >= ach.requirement
|
|
break
|
|
case 'streak_3':
|
|
case 'streak_5':
|
|
current = maxStreak
|
|
unlocked = maxStreak >= ach.requirement
|
|
break
|
|
case 'earner_10':
|
|
case 'earner_100':
|
|
current = totalEarnings
|
|
unlocked = totalEarnings >= ach.requirement
|
|
break
|
|
}
|
|
|
|
return {
|
|
...ach,
|
|
current,
|
|
unlocked,
|
|
progress: Math.min((current / ach.requirement) * 100, 100)
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({ achievements })
|
|
} catch (error) {
|
|
console.error('Achievements error:', error)
|
|
return NextResponse.json({ achievements: [] })
|
|
}
|
|
}
|