From 282ca9eddf3e825485078f95df119ec294abe737 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Fri, 21 Aug 2026 00:25:33 -0400 Subject: [PATCH] feat(xp): animate sidebar gains --- src/app/(frontend)/flappy/actions.ts | 6 +++ src/app/(frontend)/styles.css | 19 +++++++- src/components/frontend/blocks/XPDisplay.tsx | 43 ++++++++++++++++++- src/components/frontend/flappy/FlappyBird.tsx | 11 ++++- 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/app/(frontend)/flappy/actions.ts b/src/app/(frontend)/flappy/actions.ts index 082a8c3..330a5bc 100644 --- a/src/app/(frontend)/flappy/actions.ts +++ b/src/app/(frontend)/flappy/actions.ts @@ -4,6 +4,7 @@ import config from "@payload-config"; import { getPayload } from "payload"; import { emitGameEvent } from "@/utils/event-log/emit"; import { EventTypes } from "@/utils/event-log/eventTypes"; +import { broadcastToUser } from "@/lib/realtime/bus"; interface ActionResult { success: boolean; @@ -100,6 +101,11 @@ export async function submitFlappyScore(input: { overrideAccess: true, depth: 0, }); + broadcastToUser("profile-update", Number(user.id), { + reason: "xp", + xpGained: runXp, + profileId: profile.id, + }); if (passes > 0) { await emitGameEvent(payload, { diff --git a/src/app/(frontend)/styles.css b/src/app/(frontend)/styles.css index 8c9bea0..23719ef 100644 --- a/src/app/(frontend)/styles.css +++ b/src/app/(frontend)/styles.css @@ -298,6 +298,23 @@ } } +@keyframes xp-gain { + 0% { + opacity: 0; + transform: translateX(0.35rem); + } + 15% { + opacity: 1; + } + 75% { + opacity: 1; + } + 100% { + opacity: 0; + transform: translateX(-1.1rem); + } +} + .locker-search-highlight { animation: locker-search-pulse 1.1s ease-in-out infinite; box-shadow: 0 0 0 3px rgb(251 191 36 / 0.9); @@ -331,4 +348,4 @@ *::-webkit-scrollbar-corner { background: transparent; -} \ No newline at end of file +} diff --git a/src/components/frontend/blocks/XPDisplay.tsx b/src/components/frontend/blocks/XPDisplay.tsx index 192f4db..4b16fd4 100644 --- a/src/components/frontend/blocks/XPDisplay.tsx +++ b/src/components/frontend/blocks/XPDisplay.tsx @@ -1,12 +1,42 @@ "use client"; +import { useEffect, useRef, useState } from "react"; import { Item, ItemContent, ItemHeader, ItemTitle } from "@/components/ui/item"; import { Progress } from "@/components/ui/progress"; import { SparkleIcon } from "lucide-react"; import type { ResolvedLevel } from "@/utils/xp/resolveLevel"; +import { PROFILE_UPDATE_EVENT, type ProfileUpdateDetail } from "@/hooks/useGameTick"; const XPDisplay = ({ xpLevel }: { xpLevel: ResolvedLevel }) => { const { levelName, progress, xp, nextLevelXP } = xpLevel; + const [xpGain, setXpGain] = useState(null); + const [xpGainId, setXpGainId] = useState(0); + const lastGainRef = useRef<{ amount: number; at: number } | null>(null); + + useEffect(() => { + let clearTimer: ReturnType | null = null; + + const handleProfileUpdate = (event: Event) => { + const detail = (event as CustomEvent).detail; + if (!detail || typeof detail.xpGained !== "number" || detail.xpGained <= 0) return; + const now = Date.now(); + if (lastGainRef.current?.amount === detail.xpGained && now - lastGainRef.current.at < 500) { + return; + } + lastGainRef.current = { amount: detail.xpGained, at: now }; + + setXpGain(detail.xpGained); + setXpGainId((id) => id + 1); + if (clearTimer) clearTimeout(clearTimer); + clearTimer = setTimeout(() => setXpGain(null), 1500); + }; + + window.addEventListener(PROFILE_UPDATE_EVENT, handleProfileUpdate); + return () => { + if (clearTimer) clearTimeout(clearTimer); + window.removeEventListener(PROFILE_UPDATE_EVENT, handleProfileUpdate); + }; + }, []); return ( @@ -15,7 +45,18 @@ const XPDisplay = ({ xpLevel }: { xpLevel: ResolvedLevel }) => {
{levelName} - {xp.toLocaleString()} + + {xp.toLocaleString()} + {xpGain !== null && ( + + +{xpGain.toLocaleString()} XP + + )} +
diff --git a/src/components/frontend/flappy/FlappyBird.tsx b/src/components/frontend/flappy/FlappyBird.tsx index 762cac6..9252340 100644 --- a/src/components/frontend/flappy/FlappyBird.tsx +++ b/src/components/frontend/flappy/FlappyBird.tsx @@ -1,8 +1,10 @@ "use client"; import { useCallback, useEffect, useRef, useState } from "react"; +import { useRouter } from "next/navigation"; import { type FlappyStats, submitFlappyScore } from "@/app/(frontend)/flappy/actions"; +import { PROFILE_UPDATE_EVENT } from "@/hooks/useGameTick"; import { playFlap, playHit, playXp } from "./sounds"; const CANVAS_W = 420; @@ -330,6 +332,7 @@ export function FlappyBird({ onGameRecorded?: () => void; onPhaseChange?: (phase: Phase) => void; }) { + const router = useRouter(); const canvasRef = useRef(null); const wrapperRef = useRef(null); const phaseRef = useRef("ready"); @@ -399,6 +402,12 @@ export function FlappyBird({ onGameRecorded?.(); if (res.data.xpEarned > 0) { playXp(); + window.dispatchEvent( + new CustomEvent(PROFILE_UPDATE_EVENT, { + detail: { reason: "xp", xpGained: res.data.xpEarned }, + }), + ); + window.setTimeout(() => router.refresh(), 1700); } setLastRun({ score: finalScore, @@ -414,7 +423,7 @@ export function FlappyBird({ }); } }, - [onGameRecorded], + [onGameRecorded, router], ); const handleGameOverRef = useRef(handleGameOver);