From d67723f3c54df64cd215d9625891785c1c490842 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Wed, 19 Nov 2025 13:34:31 +0800 Subject: [PATCH] [+] Stats --- src/routes/song/[slug]/+page.svelte | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/routes/song/[slug]/+page.svelte b/src/routes/song/[slug]/+page.svelte index b4463d5..3f5e133 100644 --- a/src/routes/song/[slug]/+page.svelte +++ b/src/routes/song/[slug]/+page.svelte @@ -40,12 +40,21 @@ return 'typing' } + // For computing stats + let startTime = $state(null) + let now = $state(Date.now()) + onMount(() => { hiddenInput.focus() + const interval = setInterval(() => { + if (startTime) now = Date.now() + }) + return () => clearInterval(interval) }) // On input changed: Convert to hiragana, compare with current position, update states function inputChanged(input: string, isComposed: boolean) { + if (!startTime && input) startTime = Date.now() console.log(`input changed: ${input}`) // Convert to hiragana inp = toHiragana(inp, { IMEMode: true }) @@ -78,7 +87,10 @@ $effect(() => inputChanged(inp, false)) - let progress = $derived(Math.min(100, Math.floor((states.flat().filter(s => s !== 'unseen').length / states.flat().length) * 100))) + let flat = $derived(states.flat()) + let progress = $derived(Math.min(100, Math.floor((flat.filter(s => s !== 'unseen').length / flat.length) * 100))) + let totalTyped = $derived(flat.filter(s => s !== 'unseen').length) + let totalRight = $derived(flat.filter(s => s === 'right' || s === 'fuzzy').length) a.name).join(", ") + " - " + data.brief.album} right={[ @@ -93,15 +105,15 @@
-
速度: 10cpm
-
正確率: 90%
+
速度: {startTime ? Math.round(totalTyped / (Math.max(1, (now - startTime)) / 60000)) : '-'} cpm
+
正確率: {totalTyped === 0 ? 100 : Math.round((totalRight / totalTyped) * 100)}%
-
正确:{states.flat().filter(s => s === 'right').length}
-
模糊:{states.flat().filter(s => s === 'fuzzy').length}
-
错误:{states.flat().filter(s => s === 'wrong').length}
-
剩余:{states.flat().filter(s => s === 'unseen').length}
+
正确:{flat.filter(s => s === 'right').length}
+
模糊:{flat.filter(s => s === 'fuzzy').length}
+
错误:{flat.filter(s => s === 'wrong').length}
+
剩余:{flat.filter(s => s === 'unseen').length}