This commit is contained in:
2025-12-08 12:24:45 +00:00
parent 2db041f919
commit e987f3de3b
6 changed files with 107 additions and 0 deletions
+65
View File
@@ -25,6 +25,32 @@ export const getAiMessageCount = (): number => {
return parseInt(localStorage.getItem(AI_MESSAGE_COUNT_KEY) || '0', 10);
};
// Game score achievement thresholds
export const GAME_SCORE_ACHIEVEMENTS = {
tetris: [
{ score: 1000, id: 'tetris_1000' },
{ score: 10000, id: 'tetris_10000' },
{ score: 50000, id: 'tetris_50000' },
],
pacman: [
{ score: 1000, id: 'pacman_1000' },
{ score: 5000, id: 'pacman_5000' },
{ score: 10000, id: 'pacman_10000' },
],
snake: [
{ score: 500, id: 'snake_500' },
{ score: 2000, id: 'snake_2000' },
{ score: 5000, id: 'snake_5000' },
],
breakout: [
{ score: 1000, id: 'breakout_1000' },
{ score: 5000, id: 'breakout_5000' },
{ score: 10000, id: 'breakout_10000' },
],
} as const;
export type GameType = keyof typeof GAME_SCORE_ACHIEVEMENTS;
interface AchievementsContextType {
achievements: Achievement[];
unlockAchievement: (id: string) => void;
@@ -32,6 +58,9 @@ interface AchievementsContextType {
getTotalCount: () => number;
timeOnSite: number;
checkAiAchievements: () => void;
checkGameScoreAchievements: (game: GameType, score: number) => void;
unlockMusicListener: () => void;
unlockMaxScore: () => void;
}
const defaultAchievements: Achievement[] = [
@@ -202,6 +231,19 @@ export const AchievementsProvider = ({ children }: { children: ReactNode }) => {
unlockAchievement('first_visit');
}, []);
// Check for music listener achievement (set by MusicContext)
useEffect(() => {
const checkMusicListener = () => {
if (localStorage.getItem('has-listened-to-music') === 'true') {
unlockAchievement('music_listener');
}
};
checkMusicListener();
// Also check periodically in case user starts playing music
const interval = setInterval(checkMusicListener, 2000);
return () => clearInterval(interval);
}, []);
const [notification, setNotification] = useState<Achievement | null>(null);
const unlockAchievement = useCallback((id: string) => {
@@ -236,6 +278,26 @@ export const AchievementsProvider = ({ children }: { children: ReactNode }) => {
if (count >= 20) unlockAchievement('ai_long_chat');
}, [unlockAchievement]);
// Check game score achievements
const checkGameScoreAchievements = useCallback((game: GameType, score: number) => {
const thresholds = GAME_SCORE_ACHIEVEMENTS[game];
for (const threshold of thresholds) {
if (score >= threshold.score) {
unlockAchievement(threshold.id);
}
}
}, [unlockAchievement]);
// Unlock music listener achievement
const unlockMusicListener = useCallback(() => {
unlockAchievement('music_listener');
}, [unlockAchievement]);
// Unlock max score achievement
const unlockMaxScore = useCallback(() => {
unlockAchievement('max_score');
}, [unlockAchievement]);
// Save achievements
useEffect(() => {
localStorage.setItem('achievements', JSON.stringify(achievements));
@@ -250,6 +312,9 @@ export const AchievementsProvider = ({ children }: { children: ReactNode }) => {
getTotalCount,
timeOnSite,
checkAiAchievements,
checkGameScoreAchievements,
unlockMusicListener,
unlockMaxScore,
}}
>
{children}