mirror of
https://github.com/pupperpowell/bibdle.git
synced 2026-02-04 10:54:44 -05:00
Revamped middle statline (ranking instead of arbitrary percentage)
This commit is contained in:
@@ -1,222 +1,233 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { fade } from "svelte/transition";
|
import { fade } from "svelte/transition";
|
||||||
import { getBookById, toOrdinal, getNextGradeMessage } from "$lib/utils/game";
|
import {
|
||||||
import { onMount } from "svelte";
|
getBookById,
|
||||||
import Container from "./Container.svelte";
|
toOrdinal,
|
||||||
import CountdownTimer from "./CountdownTimer.svelte";
|
getNextGradeMessage,
|
||||||
import ChapterGuess from "./ChapterGuess.svelte";
|
} from "$lib/utils/game";
|
||||||
|
import { onMount } from "svelte";
|
||||||
|
import Container from "./Container.svelte";
|
||||||
|
import CountdownTimer from "./CountdownTimer.svelte";
|
||||||
|
import ChapterGuess from "./ChapterGuess.svelte";
|
||||||
|
|
||||||
interface StatsData {
|
interface StatsData {
|
||||||
solveRank: number;
|
solveRank: number;
|
||||||
guessRank: number;
|
guessRank: number;
|
||||||
totalSolves: number;
|
totalSolves: number;
|
||||||
averageGuesses: number;
|
averageGuesses: number;
|
||||||
}
|
tiedCount: number;
|
||||||
|
}
|
||||||
|
|
||||||
interface WeightedMessage {
|
interface WeightedMessage {
|
||||||
text: string;
|
text: string;
|
||||||
weight: number;
|
weight: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
let {
|
let {
|
||||||
grade,
|
grade,
|
||||||
statsData,
|
statsData,
|
||||||
correctBookId,
|
correctBookId,
|
||||||
handleShare,
|
handleShare,
|
||||||
copyToClipboard,
|
copyToClipboard,
|
||||||
copied = $bindable(false),
|
copied = $bindable(false),
|
||||||
statsSubmitted,
|
statsSubmitted,
|
||||||
guessCount,
|
guessCount,
|
||||||
reference,
|
reference,
|
||||||
onChapterGuessCompleted,
|
onChapterGuessCompleted,
|
||||||
} = $props();
|
} = $props();
|
||||||
|
|
||||||
let bookName = $derived(getBookById(correctBookId)?.name ?? "");
|
let bookName = $derived(getBookById(correctBookId)?.name ?? "");
|
||||||
let hasWebShare = $derived(
|
let hasWebShare = $derived(
|
||||||
typeof navigator !== "undefined" && "share" in navigator
|
typeof navigator !== "undefined" && "share" in navigator,
|
||||||
);
|
);
|
||||||
let copySuccess = $state(false);
|
let copySuccess = $state(false);
|
||||||
|
|
||||||
// List of congratulations messages with weights
|
// List of congratulations messages with weights
|
||||||
const congratulationsMessages: WeightedMessage[] = [
|
const congratulationsMessages: WeightedMessage[] = [
|
||||||
{ text: "Congratulations!", weight: 10 },
|
{ text: "Congratulations!", weight: 10 },
|
||||||
{ text: "You got it!", weight: 1000 },
|
{ text: "You got it!", weight: 1000 },
|
||||||
{ text: "Yup.", weight: 100 },
|
{ text: "Yup.", weight: 100 },
|
||||||
{ text: "Very nice!", weight: 1 },
|
{ text: "Very nice!", weight: 1 },
|
||||||
];
|
];
|
||||||
|
|
||||||
// Function to select a random message based on weights
|
// Function to select a random message based on weights
|
||||||
function getRandomCongratulationsMessage(): string {
|
function getRandomCongratulationsMessage(): string {
|
||||||
// Special case for first try success
|
// Special case for first try success
|
||||||
if (guessCount === 1) {
|
if (guessCount === 1) {
|
||||||
const n = Math.random();
|
const n = Math.random();
|
||||||
if (n < 0.99) {
|
if (n < 0.99) {
|
||||||
return "🌟 First try! 🌟";
|
return "🌟 First try! 🌟";
|
||||||
} else {
|
} else {
|
||||||
return "🗣️ Axios! 🗣️";
|
return "🗣️ Axios! 🗣️";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalWeight = congratulationsMessages.reduce(
|
const totalWeight = congratulationsMessages.reduce(
|
||||||
(sum, msg) => sum + msg.weight,
|
(sum, msg) => sum + msg.weight,
|
||||||
0
|
0,
|
||||||
);
|
);
|
||||||
let random = Math.random() * totalWeight;
|
let random = Math.random() * totalWeight;
|
||||||
|
|
||||||
for (const message of congratulationsMessages) {
|
for (const message of congratulationsMessages) {
|
||||||
random -= message.weight;
|
random -= message.weight;
|
||||||
if (random <= 0) {
|
if (random <= 0) {
|
||||||
return message.text;
|
return message.text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to first message if something goes wrong
|
// Fallback to first message if something goes wrong
|
||||||
return congratulationsMessages[0].text;
|
return congratulationsMessages[0].text;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate the congratulations message
|
// Generate the congratulations message
|
||||||
let congratulationsMessage = $derived(getRandomCongratulationsMessage());
|
let congratulationsMessage = $derived(getRandomCongratulationsMessage());
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex flex-col gap-6">
|
<div class="flex flex-col gap-6">
|
||||||
<Container
|
<Container
|
||||||
class="w-full p-8 sm:p-12 bg-linear-to-r from-green-400/10 to-green-600/30 text-gray-800 shadow-2xl text-center fade-in"
|
class="w-full p-8 sm:p-12 bg-linear-to-r from-green-400/10 to-green-600/30 text-gray-800 shadow-2xl text-center fade-in"
|
||||||
>
|
>
|
||||||
<p class="text-2xl sm:text-3xl md:text-4xl leading-relaxed">
|
<p class="text-2xl sm:text-3xl md:text-4xl leading-relaxed">
|
||||||
{congratulationsMessage} The verse is from
|
{congratulationsMessage} The verse is from
|
||||||
<span class="font-black text-3xl md:text-4xl">{bookName}</span>.
|
<span class="font-black text-3xl md:text-4xl">{bookName}</span>.
|
||||||
</p>
|
</p>
|
||||||
<p class="text-lg sm:text-xl md:text-2xl mt-4">
|
<p class="text-lg sm:text-xl md:text-2xl mt-4">
|
||||||
You guessed correctly after {guessCount}
|
You guessed correctly after {guessCount}
|
||||||
{guessCount === 1 ? "guess" : "guesses"}.
|
{guessCount === 1 ? "guess" : "guesses"}.
|
||||||
<span class="font-bold bg-white/40 rounded px-1.5 py-0.75">{grade}</span>
|
<span class="font-bold bg-white/40 rounded px-1.5 py-0.75"
|
||||||
</p>
|
>{grade}</span
|
||||||
|
>
|
||||||
|
</p>
|
||||||
|
|
||||||
<div class="flex justify-center mt-6">
|
<div class="flex justify-center mt-6">
|
||||||
{#if hasWebShare}
|
{#if hasWebShare}
|
||||||
<!-- mobile and arc in production -->
|
<!-- mobile and arc in production -->
|
||||||
<button
|
<button
|
||||||
onclick={handleShare}
|
onclick={handleShare}
|
||||||
data-umami-event="Share"
|
data-umami-event="Share"
|
||||||
class="text-2xl font-bold p-4 bg-white/70 hover:bg-white/80 rounded-xl inline-block transition-all shadow-lg mx-2 cursor-pointer border-none appearance-none"
|
class="text-2xl font-bold p-4 bg-white/70 hover:bg-white/80 rounded-xl inline-block transition-all shadow-lg mx-2 cursor-pointer border-none appearance-none"
|
||||||
>
|
>
|
||||||
📤 Share
|
📤 Share
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onclick={() => {
|
onclick={() => {
|
||||||
copyToClipboard();
|
copyToClipboard();
|
||||||
copySuccess = true;
|
copySuccess = true;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
copySuccess = false;
|
copySuccess = false;
|
||||||
}, 3000);
|
}, 3000);
|
||||||
}}
|
}}
|
||||||
data-umami-event="Copy to Clipboard"
|
data-umami-event="Copy to Clipboard"
|
||||||
class={`text-2xl font-bold p-4 rounded-lg inline-block transition-all shadow-lg mx-2 cursor-pointer border-none appearance-none ${
|
class={`text-2xl font-bold p-4 rounded-lg inline-block transition-all shadow-lg mx-2 cursor-pointer border-none appearance-none ${
|
||||||
copySuccess ? "bg-white/30" : "bg-white/70 hover:bg-white/80"
|
copySuccess
|
||||||
}`}
|
? "bg-white/30"
|
||||||
>
|
: "bg-white/70 hover:bg-white/80"
|
||||||
{copySuccess ? "✅ Copied!" : "📋 Copy"}
|
}`}
|
||||||
</button>
|
>
|
||||||
{:else}
|
{copySuccess ? "✅ Copied!" : "📋 Copy"}
|
||||||
<!-- dev mode and desktop browsers -->
|
</button>
|
||||||
<button
|
{:else}
|
||||||
onclick={handleShare}
|
<!-- dev mode and desktop browsers -->
|
||||||
data-umami-event="Copy to Clipboard"
|
<button
|
||||||
class={`text-2xl font-bold p-4 ${
|
onclick={handleShare}
|
||||||
copied ? "bg-white/30" : "bg-white/70 hover:bg-white/80"
|
data-umami-event="Copy to Clipboard"
|
||||||
} rounded-xl inline-block transition-all shadow-lg mx-2 cursor-pointer border-none appearance-none`}
|
class={`text-2xl font-bold p-4 ${
|
||||||
>
|
copied ? "bg-white/30" : "bg-white/70 hover:bg-white/80"
|
||||||
{copied ? "✅ Copied!" : "📋 Share"}
|
} rounded-xl inline-block transition-all shadow-lg mx-2 cursor-pointer border-none appearance-none`}
|
||||||
</button>
|
>
|
||||||
{/if}
|
{copied ? "✅ Copied!" : "📋 Share"}
|
||||||
</div>
|
</button>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
{#if guessCount !== 1}
|
{#if guessCount !== 1}
|
||||||
<p class="pt-6 big-text text-gray-700!">
|
<p class="pt-6 big-text text-gray-700!">
|
||||||
{getNextGradeMessage(guessCount)}
|
{getNextGradeMessage(guessCount)}
|
||||||
</p>
|
</p>
|
||||||
{/if}
|
{/if}
|
||||||
</Container>
|
</Container>
|
||||||
|
|
||||||
<!-- S++ Bonus Challenge for first try -->
|
<!-- S++ Bonus Challenge for first try -->
|
||||||
{#if guessCount === 1}
|
{#if guessCount === 1}
|
||||||
<ChapterGuess
|
<ChapterGuess
|
||||||
{reference}
|
{reference}
|
||||||
bookId={correctBookId}
|
bookId={correctBookId}
|
||||||
onCompleted={onChapterGuessCompleted}
|
onCompleted={onChapterGuessCompleted}
|
||||||
/>
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<CountdownTimer />
|
<CountdownTimer />
|
||||||
|
|
||||||
<!-- Statistics Display -->
|
<!-- Statistics Display -->
|
||||||
{#if statsData}
|
{#if statsData}
|
||||||
<Container
|
<Container
|
||||||
class="w-full p-4 bg-white/50 backdrop-blur-sm text-gray-800 shadow-lg text-center"
|
class="w-full p-4 bg-white/50 backdrop-blur-sm text-gray-800 shadow-lg text-center"
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
class="grid grid-cols-3 gap-4 gap-x-8 text-center"
|
class="grid grid-cols-3 gap-4 gap-x-8 text-center"
|
||||||
in:fade={{ delay: 800 }}
|
in:fade={{ delay: 800 }}
|
||||||
>
|
>
|
||||||
<!-- Solve Rank Column -->
|
<!-- Solve Rank Column -->
|
||||||
<div class="flex flex-col">
|
<div class="flex flex-col">
|
||||||
<div class="text-3xl sm:text-4xl font-black">
|
<div class="text-3xl sm:text-4xl font-black">
|
||||||
#{statsData.solveRank}
|
#{statsData.solveRank}
|
||||||
</div>
|
</div>
|
||||||
<div class="text-sm sm:text-sm opacity-90 mt-1">
|
<div class="text-sm sm:text-sm opacity-90 mt-1">
|
||||||
You were the {toOrdinal(statsData.solveRank)} person to solve today
|
You were the {toOrdinal(statsData.solveRank)} person to solve
|
||||||
</div>
|
today
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Guess Rank Column -->
|
<!-- Guess Rank Column -->
|
||||||
<div class="flex flex-col">
|
<div class="flex flex-col">
|
||||||
<div class="text-3xl sm:text-4xl font-black">
|
<div class="text-3xl sm:text-4xl font-black">
|
||||||
{Math.round(
|
{toOrdinal(statsData.guessRank)}
|
||||||
((statsData.totalSolves - statsData.guessRank + 1) /
|
</div>
|
||||||
statsData.totalSolves) *
|
<div class="text-sm sm:text-sm opacity-90 mt-1">
|
||||||
100
|
You ranked {toOrdinal(statsData.guessRank)} of {statsData.totalSolves}
|
||||||
)}%
|
{statsData.totalSolves === 1
|
||||||
</div>
|
? "solve"
|
||||||
<div class="text-sm sm:text-sm opacity-90 mt-1">
|
: "solves"}{statsData.tiedCount > 0
|
||||||
You ranked {toOrdinal(statsData.guessRank)} of {statsData.totalSolves}
|
? `, tied with ${statsData.tiedCount} ${statsData.tiedCount === 1 ? "other" : "others"}`
|
||||||
total solves
|
: ""}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Average Column -->
|
<!-- Average Column -->
|
||||||
<div class="flex flex-col">
|
<div class="flex flex-col">
|
||||||
<div class="text-3xl sm:text-4xl font-black">
|
<div class="text-3xl sm:text-4xl font-black">
|
||||||
{statsData.averageGuesses}
|
{statsData.averageGuesses}
|
||||||
</div>
|
</div>
|
||||||
<div class="text-sm sm:text-sm opacity-90 mt-1">
|
<div class="text-sm sm:text-sm opacity-90 mt-1">
|
||||||
People guessed correctly after {statsData.averageGuesses}
|
People guessed correctly after {statsData.averageGuesses}
|
||||||
{statsData.averageGuesses === 1 ? "guess" : "guesses"} on average
|
{statsData.averageGuesses === 1 ? "guess" : "guesses"} on
|
||||||
</div>
|
average
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Container>
|
</div>
|
||||||
{:else if !statsSubmitted}
|
</Container>
|
||||||
<Container
|
{:else if !statsSubmitted}
|
||||||
class="w-full p-6 bg-white/50 backdrop-blur-sm text-gray-800 shadow-lg text-center"
|
<Container
|
||||||
>
|
class="w-full p-6 bg-white/50 backdrop-blur-sm text-gray-800 shadow-lg text-center"
|
||||||
<div class="text-sm opacity-80">Submitting stats...</div>
|
>
|
||||||
</Container>
|
<div class="text-sm opacity-80">Submitting stats...</div>
|
||||||
{/if}
|
</Container>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
@keyframes fadeIn {
|
@keyframes fadeIn {
|
||||||
from {
|
from {
|
||||||
opacity: 0;
|
opacity: 0;
|
||||||
transform: translateY(-10px);
|
transform: translateY(-10px);
|
||||||
}
|
}
|
||||||
to {
|
to {
|
||||||
opacity: 1;
|
opacity: 1;
|
||||||
transform: translateY(0);
|
transform: translateY(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.fade-in {
|
.fade-in {
|
||||||
animation: fadeIn 0.5s ease-out;
|
animation: fadeIn 0.5s ease-out;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -1,494 +1,510 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { bibleBooks, type BibleBook } from "$lib/types/bible";
|
import { bibleBooks, type BibleBook } from "$lib/types/bible";
|
||||||
|
|
||||||
import type { PageProps } from "./$types";
|
import type { PageProps } from "./$types";
|
||||||
import { browser } from "$app/environment";
|
import { browser } from "$app/environment";
|
||||||
|
|
||||||
import VerseDisplay from "$lib/components/VerseDisplay.svelte";
|
import VerseDisplay from "$lib/components/VerseDisplay.svelte";
|
||||||
import SearchInput from "$lib/components/SearchInput.svelte";
|
import SearchInput from "$lib/components/SearchInput.svelte";
|
||||||
import GuessesTable from "$lib/components/GuessesTable.svelte";
|
import GuessesTable from "$lib/components/GuessesTable.svelte";
|
||||||
import WinScreen from "$lib/components/WinScreen.svelte";
|
import WinScreen from "$lib/components/WinScreen.svelte";
|
||||||
import Feedback from "$lib/components/Feedback.svelte";
|
import Feedback from "$lib/components/Feedback.svelte";
|
||||||
import TitleAnimation from "$lib/components/TitleAnimation.svelte";
|
import TitleAnimation from "$lib/components/TitleAnimation.svelte";
|
||||||
import { getGrade } from "$lib/utils/game";
|
import { getGrade } from "$lib/utils/game";
|
||||||
|
|
||||||
interface Guess {
|
interface Guess {
|
||||||
book: BibleBook;
|
book: BibleBook;
|
||||||
testamentMatch: boolean;
|
testamentMatch: boolean;
|
||||||
sectionMatch: boolean;
|
sectionMatch: boolean;
|
||||||
adjacent: boolean;
|
adjacent: boolean;
|
||||||
firstLetterMatch: boolean;
|
firstLetterMatch: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { data }: PageProps = $props();
|
let { data }: PageProps = $props();
|
||||||
|
|
||||||
let dailyVerse = $derived(data.dailyVerse);
|
let dailyVerse = $derived(data.dailyVerse);
|
||||||
let correctBookId = $derived(data.correctBookId);
|
let correctBookId = $derived(data.correctBookId);
|
||||||
|
|
||||||
let guesses = $state<Guess[]>([]);
|
let guesses = $state<Guess[]>([]);
|
||||||
|
|
||||||
let searchQuery = $state("");
|
let searchQuery = $state("");
|
||||||
|
|
||||||
let copied = $state(false);
|
let copied = $state(false);
|
||||||
let isDev = $state(false);
|
let isDev = $state(false);
|
||||||
let chapterGuessCompleted = $state(false);
|
let chapterGuessCompleted = $state(false);
|
||||||
let chapterCorrect = $state(false);
|
let chapterCorrect = $state(false);
|
||||||
|
|
||||||
let anonymousId = $state("");
|
let anonymousId = $state("");
|
||||||
let statsSubmitted = $state(false);
|
let statsSubmitted = $state(false);
|
||||||
let statsData = $state<{
|
let statsData = $state<{
|
||||||
solveRank: number;
|
solveRank: number;
|
||||||
guessRank: number;
|
guessRank: number;
|
||||||
totalSolves: number;
|
totalSolves: number;
|
||||||
averageGuesses: number;
|
averageGuesses: number;
|
||||||
} | null>(null);
|
tiedCount: number;
|
||||||
|
} | null>(null);
|
||||||
|
|
||||||
let guessedIds = $derived(new Set(guesses.map((g) => g.book.id)));
|
let guessedIds = $derived(new Set(guesses.map((g) => g.book.id)));
|
||||||
|
|
||||||
const currentDate = $derived(
|
const currentDate = $derived(
|
||||||
new Date().toLocaleDateString("en-US", {
|
new Date().toLocaleDateString("en-US", {
|
||||||
weekday: "long",
|
weekday: "long",
|
||||||
year: "numeric",
|
year: "numeric",
|
||||||
month: "long",
|
month: "long",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
})
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
let isWon = $derived(guesses.some((g) => g.book.id === correctBookId));
|
let isWon = $derived(guesses.some((g) => g.book.id === correctBookId));
|
||||||
let grade = $derived(
|
let grade = $derived(
|
||||||
isWon
|
isWon
|
||||||
? guesses.length === 1 && chapterCorrect
|
? guesses.length === 1 && chapterCorrect
|
||||||
? "S++"
|
? "S++"
|
||||||
: getGrade(guesses.length, getBookById(correctBookId)?.popularity ?? 0)
|
: getGrade(
|
||||||
: ""
|
guesses.length,
|
||||||
);
|
getBookById(correctBookId)?.popularity ?? 0,
|
||||||
let blurChapter = $derived(
|
)
|
||||||
isWon && guesses.length === 1 && !chapterGuessCompleted
|
: "",
|
||||||
);
|
);
|
||||||
|
let blurChapter = $derived(
|
||||||
|
isWon && guesses.length === 1 && !chapterGuessCompleted,
|
||||||
|
);
|
||||||
|
|
||||||
function getBookById(id: string): BibleBook | undefined {
|
function getBookById(id: string): BibleBook | undefined {
|
||||||
return bibleBooks.find((b) => b.id === id);
|
return bibleBooks.find((b) => b.id === id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isAdjacent(id1: string, id2: string): boolean {
|
function isAdjacent(id1: string, id2: string): boolean {
|
||||||
const b1 = getBookById(id1);
|
const b1 = getBookById(id1);
|
||||||
const b2 = getBookById(id2);
|
const b2 = getBookById(id2);
|
||||||
return !!(b1 && b2 && Math.abs(b1.order - b2.order) === 1);
|
return !!(b1 && b2 && Math.abs(b1.order - b2.order) === 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function submitGuess(bookId: string) {
|
function submitGuess(bookId: string) {
|
||||||
if (guesses.some((g) => g.book.id === bookId)) return;
|
if (guesses.some((g) => g.book.id === bookId)) return;
|
||||||
|
|
||||||
const book = getBookById(bookId);
|
const book = getBookById(bookId);
|
||||||
if (!book) return;
|
if (!book) return;
|
||||||
|
|
||||||
const correctBook = getBookById(correctBookId);
|
const correctBook = getBookById(correctBookId);
|
||||||
if (!correctBook) return;
|
if (!correctBook) return;
|
||||||
|
|
||||||
const testamentMatch = book.testament === correctBook.testament;
|
const testamentMatch = book.testament === correctBook.testament;
|
||||||
const sectionMatch = book.section === correctBook.section;
|
const sectionMatch = book.section === correctBook.section;
|
||||||
const adjacent = isAdjacent(bookId, correctBookId);
|
const adjacent = isAdjacent(bookId, correctBookId);
|
||||||
|
|
||||||
// Special case: if correct book is Epistles + starts with "1",
|
|
||||||
// any guess starting with "1" counts as first letter match
|
|
||||||
const correctIsEpistlesWithNumber = correctBook.section === "Epistles" && correctBook.name[0] === "1";
|
|
||||||
const guessStartsWithNumber = book.name[0] === "1";
|
|
||||||
|
|
||||||
const firstLetterMatch = correctIsEpistlesWithNumber && guessStartsWithNumber
|
|
||||||
? true
|
|
||||||
: book.name[0].toUpperCase() === correctBook.name[0].toUpperCase();
|
|
||||||
|
|
||||||
console.log(
|
// Special case: if correct book is Epistles + starts with "1",
|
||||||
`Guess: ${book.name} (order ${book.order}), Correct: ${correctBook.name} (order ${correctBook.order}), Adjacent: ${adjacent}`
|
// any guess starting with "1" counts as first letter match
|
||||||
);
|
const correctIsEpistlesWithNumber =
|
||||||
|
correctBook.section === "Epistles" && correctBook.name[0] === "1";
|
||||||
|
const guessStartsWithNumber = book.name[0] === "1";
|
||||||
|
|
||||||
if (guesses.length === 0) {
|
const firstLetterMatch =
|
||||||
const key = `bibdle-first-guess-${dailyVerse.date}`;
|
correctIsEpistlesWithNumber && guessStartsWithNumber
|
||||||
if (
|
? true
|
||||||
localStorage.getItem(key) !== "true" &&
|
: book.name[0].toUpperCase() ===
|
||||||
browser &&
|
correctBook.name[0].toUpperCase();
|
||||||
(window as any).umami
|
|
||||||
) {
|
|
||||||
(window as any).umami.track("First guess");
|
|
||||||
localStorage.setItem(key, "true");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
guesses = [
|
console.log(
|
||||||
{
|
`Guess: ${book.name} (order ${book.order}), Correct: ${correctBook.name} (order ${correctBook.order}), Adjacent: ${adjacent}`,
|
||||||
book,
|
);
|
||||||
testamentMatch,
|
|
||||||
sectionMatch,
|
|
||||||
adjacent,
|
|
||||||
firstLetterMatch,
|
|
||||||
},
|
|
||||||
...guesses,
|
|
||||||
];
|
|
||||||
|
|
||||||
searchQuery = "";
|
if (guesses.length === 0) {
|
||||||
}
|
const key = `bibdle-first-guess-${dailyVerse.date}`;
|
||||||
|
if (
|
||||||
|
localStorage.getItem(key) !== "true" &&
|
||||||
|
browser &&
|
||||||
|
(window as any).umami
|
||||||
|
) {
|
||||||
|
(window as any).umami.track("First guess");
|
||||||
|
localStorage.setItem(key, "true");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function generateUUID(): string {
|
guesses = [
|
||||||
// Try native randomUUID if available
|
{
|
||||||
if (typeof window.crypto.randomUUID === "function") {
|
book,
|
||||||
return window.crypto.randomUUID();
|
testamentMatch,
|
||||||
}
|
sectionMatch,
|
||||||
|
adjacent,
|
||||||
|
firstLetterMatch,
|
||||||
|
},
|
||||||
|
...guesses,
|
||||||
|
];
|
||||||
|
|
||||||
// Fallback UUID v4 generator for older browsers
|
searchQuery = "";
|
||||||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
}
|
||||||
const r = window.crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0;
|
|
||||||
const v = c === "x" ? r : (r & 0x3) | 0x8;
|
|
||||||
return v.toString(16);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getOrCreateAnonymousId(): string {
|
function generateUUID(): string {
|
||||||
if (!browser) return "";
|
// Try native randomUUID if available
|
||||||
const key = "bibdle-anonymous-id";
|
if (typeof window.crypto.randomUUID === "function") {
|
||||||
let id = localStorage.getItem(key);
|
return window.crypto.randomUUID();
|
||||||
if (!id) {
|
}
|
||||||
id = generateUUID();
|
|
||||||
localStorage.setItem(key, id);
|
|
||||||
}
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize anonymous ID
|
// Fallback UUID v4 generator for older browsers
|
||||||
$effect(() => {
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
||||||
if (!browser) return;
|
const r =
|
||||||
anonymousId = getOrCreateAnonymousId();
|
window.crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0;
|
||||||
const statsKey = `bibdle-stats-submitted-${dailyVerse.date}`;
|
const v = c === "x" ? r : (r & 0x3) | 0x8;
|
||||||
statsSubmitted = localStorage.getItem(statsKey) === "true";
|
return v.toString(16);
|
||||||
const chapterGuessKey = `bibdle-chapter-guess-${dailyVerse.reference}`;
|
});
|
||||||
chapterGuessCompleted = localStorage.getItem(chapterGuessKey) !== null;
|
}
|
||||||
if (chapterGuessCompleted) {
|
|
||||||
const saved = localStorage.getItem(chapterGuessKey);
|
|
||||||
if (saved) {
|
|
||||||
const data = JSON.parse(saved);
|
|
||||||
const match = dailyVerse.reference.match(/\s(\d+):/);
|
|
||||||
const correctChapter = match ? parseInt(match[1], 10) : 1;
|
|
||||||
chapterCorrect = data.selectedChapter === correctChapter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$effect(() => {
|
function getOrCreateAnonymousId(): string {
|
||||||
if (!browser) return;
|
if (!browser) return "";
|
||||||
isDev = window.location.host === "localhost:5173";
|
const key = "bibdle-anonymous-id";
|
||||||
});
|
let id = localStorage.getItem(key);
|
||||||
|
if (!id) {
|
||||||
|
id = generateUUID();
|
||||||
|
localStorage.setItem(key, id);
|
||||||
|
}
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
// Load saved guesses
|
// Initialize anonymous ID
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (!browser) return;
|
if (!browser) return;
|
||||||
|
anonymousId = getOrCreateAnonymousId();
|
||||||
|
const statsKey = `bibdle-stats-submitted-${dailyVerse.date}`;
|
||||||
|
statsSubmitted = localStorage.getItem(statsKey) === "true";
|
||||||
|
const chapterGuessKey = `bibdle-chapter-guess-${dailyVerse.reference}`;
|
||||||
|
chapterGuessCompleted = localStorage.getItem(chapterGuessKey) !== null;
|
||||||
|
if (chapterGuessCompleted) {
|
||||||
|
const saved = localStorage.getItem(chapterGuessKey);
|
||||||
|
if (saved) {
|
||||||
|
const data = JSON.parse(saved);
|
||||||
|
const match = dailyVerse.reference.match(/\s(\d+):/);
|
||||||
|
const correctChapter = match ? parseInt(match[1], 10) : 1;
|
||||||
|
chapterCorrect = data.selectedChapter === correctChapter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const key = `bibdle-guesses-${dailyVerse.date}`;
|
$effect(() => {
|
||||||
const saved = localStorage.getItem(key);
|
if (!browser) return;
|
||||||
if (saved) {
|
isDev = window.location.host === "localhost:5173";
|
||||||
let savedIds: string[] = JSON.parse(saved);
|
});
|
||||||
savedIds = Array.from(new Set(savedIds));
|
|
||||||
guesses = savedIds.map((bookId: string) => {
|
|
||||||
const book = getBookById(bookId)!;
|
|
||||||
const correctBook = getBookById(correctBookId)!;
|
|
||||||
const testamentMatch = book.testament === correctBook.testament;
|
|
||||||
const sectionMatch = book.section === correctBook.section;
|
|
||||||
const adjacent = isAdjacent(bookId, correctBookId);
|
|
||||||
|
|
||||||
// Apply same first letter logic as in submitGuess
|
|
||||||
const correctIsEpistlesWithNumber = correctBook.section === "Epistles" && correctBook.name[0] === "1";
|
|
||||||
const guessStartsWithNumber = book.name[0] === "1";
|
|
||||||
|
|
||||||
const firstLetterMatch = correctIsEpistlesWithNumber && guessStartsWithNumber
|
|
||||||
? true
|
|
||||||
: book.name[0].toUpperCase() === correctBook.name[0].toUpperCase();
|
|
||||||
|
|
||||||
return {
|
|
||||||
book,
|
|
||||||
testamentMatch,
|
|
||||||
sectionMatch,
|
|
||||||
adjacent,
|
|
||||||
firstLetterMatch,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$effect(() => {
|
// Load saved guesses
|
||||||
if (!browser) return;
|
$effect(() => {
|
||||||
localStorage.setItem(
|
if (!browser) return;
|
||||||
`bibdle-guesses-${dailyVerse.date}`,
|
|
||||||
JSON.stringify(guesses.map((g) => g.book.id))
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Auto-submit stats when user wins
|
const key = `bibdle-guesses-${dailyVerse.date}`;
|
||||||
$effect(() => {
|
const saved = localStorage.getItem(key);
|
||||||
console.log("Stats effect triggered:", {
|
if (saved) {
|
||||||
browser,
|
let savedIds: string[] = JSON.parse(saved);
|
||||||
isWon,
|
savedIds = Array.from(new Set(savedIds));
|
||||||
anonymousId,
|
guesses = savedIds.map((bookId: string) => {
|
||||||
statsSubmitted,
|
const book = getBookById(bookId)!;
|
||||||
statsData,
|
const correctBook = getBookById(correctBookId)!;
|
||||||
});
|
const testamentMatch = book.testament === correctBook.testament;
|
||||||
|
const sectionMatch = book.section === correctBook.section;
|
||||||
|
const adjacent = isAdjacent(bookId, correctBookId);
|
||||||
|
|
||||||
if (!browser || !isWon || !anonymousId) {
|
// Apply same first letter logic as in submitGuess
|
||||||
console.log("Basic conditions not met");
|
const correctIsEpistlesWithNumber =
|
||||||
return;
|
correctBook.section === "Epistles" &&
|
||||||
}
|
correctBook.name[0] === "1";
|
||||||
|
const guessStartsWithNumber = book.name[0] === "1";
|
||||||
|
|
||||||
if (statsSubmitted && !statsData) {
|
const firstLetterMatch =
|
||||||
console.log("Fetching existing stats...");
|
correctIsEpistlesWithNumber && guessStartsWithNumber
|
||||||
|
? true
|
||||||
|
: book.name[0].toUpperCase() ===
|
||||||
|
correctBook.name[0].toUpperCase();
|
||||||
|
|
||||||
(async () => {
|
return {
|
||||||
try {
|
book,
|
||||||
const response = await fetch(
|
testamentMatch,
|
||||||
`/api/submit-completion?anonymousId=${anonymousId}&date=${dailyVerse.date}`
|
sectionMatch,
|
||||||
);
|
adjacent,
|
||||||
const result = await response.json();
|
firstLetterMatch,
|
||||||
console.log("Stats response:", result);
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (result.success && result.stats) {
|
$effect(() => {
|
||||||
console.log("Setting stats data:", result.stats);
|
if (!browser) return;
|
||||||
statsData = result.stats;
|
localStorage.setItem(
|
||||||
localStorage.setItem(
|
`bibdle-guesses-${dailyVerse.date}`,
|
||||||
`bibdle-stats-submitted-${dailyVerse.date}`,
|
JSON.stringify(guesses.map((g) => g.book.id)),
|
||||||
"true"
|
);
|
||||||
);
|
});
|
||||||
} else if (result.error) {
|
|
||||||
console.error("Server error:", result.error);
|
|
||||||
} else {
|
|
||||||
console.error("Unexpected response format:", result);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Stats fetch failed:", err);
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
return;
|
// Auto-submit stats when user wins
|
||||||
}
|
$effect(() => {
|
||||||
|
console.log("Stats effect triggered:", {
|
||||||
|
browser,
|
||||||
|
isWon,
|
||||||
|
anonymousId,
|
||||||
|
statsSubmitted,
|
||||||
|
statsData,
|
||||||
|
});
|
||||||
|
|
||||||
console.log("Submitting stats...");
|
if (!browser || !isWon || !anonymousId) {
|
||||||
|
console.log("Basic conditions not met");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
async function submitStats() {
|
if (statsSubmitted && !statsData) {
|
||||||
try {
|
console.log("Fetching existing stats...");
|
||||||
const payload = {
|
|
||||||
anonymousId,
|
|
||||||
date: dailyVerse.date,
|
|
||||||
guessCount: guesses.length,
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log("Sending POST request with:", payload);
|
(async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch(
|
||||||
|
`/api/submit-completion?anonymousId=${anonymousId}&date=${dailyVerse.date}`,
|
||||||
|
);
|
||||||
|
const result = await response.json();
|
||||||
|
console.log("Stats response:", result);
|
||||||
|
|
||||||
const response = await fetch("/api/submit-completion", {
|
if (result.success && result.stats) {
|
||||||
method: "POST",
|
console.log("Setting stats data:", result.stats);
|
||||||
headers: {
|
statsData = result.stats;
|
||||||
"Content-Type": "application/json",
|
localStorage.setItem(
|
||||||
},
|
`bibdle-stats-submitted-${dailyVerse.date}`,
|
||||||
body: JSON.stringify(payload),
|
"true",
|
||||||
});
|
);
|
||||||
|
} else if (result.error) {
|
||||||
|
console.error("Server error:", result.error);
|
||||||
|
} else {
|
||||||
|
console.error("Unexpected response format:", result);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Stats fetch failed:", err);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
const result = await response.json();
|
return;
|
||||||
console.log("Stats response:", result);
|
}
|
||||||
|
|
||||||
if (result.success && result.stats) {
|
console.log("Submitting stats...");
|
||||||
console.log("Setting stats data:", result.stats);
|
|
||||||
statsData = result.stats;
|
|
||||||
statsSubmitted = true;
|
|
||||||
localStorage.setItem(
|
|
||||||
`bibdle-stats-submitted-${dailyVerse.date}`,
|
|
||||||
"true"
|
|
||||||
);
|
|
||||||
} else if (result.error) {
|
|
||||||
console.error("Server error:", result.error);
|
|
||||||
} else {
|
|
||||||
console.error("Unexpected response format:", result);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Stats submission failed:", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
submitStats();
|
async function submitStats() {
|
||||||
});
|
try {
|
||||||
|
const payload = {
|
||||||
|
anonymousId,
|
||||||
|
date: dailyVerse.date,
|
||||||
|
guessCount: guesses.length,
|
||||||
|
};
|
||||||
|
|
||||||
$effect(() => {
|
console.log("Sending POST request with:", payload);
|
||||||
if (!browser || !isWon) return;
|
|
||||||
const key = `bibdle-win-tracked-${dailyVerse.date}`;
|
|
||||||
if (localStorage.getItem(key) === "true") return;
|
|
||||||
if ((window as any).umami) {
|
|
||||||
(window as any).umami.track("Guessed correctly", {
|
|
||||||
totalGuesses: guesses.length,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
localStorage.setItem(key, "true");
|
|
||||||
});
|
|
||||||
|
|
||||||
function generateShareText(): string {
|
const response = await fetch("/api/submit-completion", {
|
||||||
const emojis = guesses
|
method: "POST",
|
||||||
.slice()
|
headers: {
|
||||||
.reverse()
|
"Content-Type": "application/json",
|
||||||
.map((guess) => {
|
},
|
||||||
if (guess.book.id === correctBookId) return "✅";
|
body: JSON.stringify(payload),
|
||||||
if (guess.adjacent) return "‼️";
|
});
|
||||||
if (guess.sectionMatch) return "🟩";
|
|
||||||
if (guess.testamentMatch) return "🟧";
|
|
||||||
return "🟥";
|
|
||||||
})
|
|
||||||
.join("");
|
|
||||||
|
|
||||||
const dateFormatter = new Intl.DateTimeFormat("en-US", {
|
const result = await response.json();
|
||||||
month: "short",
|
console.log("Stats response:", result);
|
||||||
day: "numeric",
|
|
||||||
year: "numeric",
|
|
||||||
});
|
|
||||||
const formattedDate = dateFormatter.format(
|
|
||||||
new Date(`${dailyVerse.date}T00:00:00`)
|
|
||||||
);
|
|
||||||
const siteUrl = window.location.origin;
|
|
||||||
return [
|
|
||||||
`📖 Bibdle | ${formattedDate} 📖`,
|
|
||||||
`${grade} (${guesses.length} ${guesses.length === 1 ? "guess" : "guesses"})`,
|
|
||||||
`${emojis}${guesses.length === 1 && chapterCorrect ? " ⭐" : ""}`,
|
|
||||||
siteUrl,
|
|
||||||
].join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
async function share() {
|
if (result.success && result.stats) {
|
||||||
if (!browser) return;
|
console.log("Setting stats data:", result.stats);
|
||||||
|
statsData = result.stats;
|
||||||
|
statsSubmitted = true;
|
||||||
|
localStorage.setItem(
|
||||||
|
`bibdle-stats-submitted-${dailyVerse.date}`,
|
||||||
|
"true",
|
||||||
|
);
|
||||||
|
} else if (result.error) {
|
||||||
|
console.error("Server error:", result.error);
|
||||||
|
} else {
|
||||||
|
console.error("Unexpected response format:", result);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Stats submission failed:", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const shareText = generateShareText();
|
submitStats();
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
$effect(() => {
|
||||||
if ("share" in navigator) {
|
if (!browser || !isWon) return;
|
||||||
await (navigator as any).share({ text: shareText });
|
const key = `bibdle-win-tracked-${dailyVerse.date}`;
|
||||||
} else {
|
if (localStorage.getItem(key) === "true") return;
|
||||||
await (navigator as any).clipboard.writeText(shareText);
|
if ((window as any).umami) {
|
||||||
}
|
(window as any).umami.track("Guessed correctly", {
|
||||||
} catch (err) {
|
totalGuesses: guesses.length,
|
||||||
console.error("Share failed:", err);
|
});
|
||||||
throw err;
|
}
|
||||||
}
|
localStorage.setItem(key, "true");
|
||||||
}
|
});
|
||||||
|
|
||||||
async function copyToClipboard() {
|
function generateShareText(): string {
|
||||||
if (!browser) return;
|
const emojis = guesses
|
||||||
|
.slice()
|
||||||
|
.reverse()
|
||||||
|
.map((guess) => {
|
||||||
|
if (guess.book.id === correctBookId) return "✅";
|
||||||
|
if (guess.adjacent) return "‼️";
|
||||||
|
if (guess.sectionMatch) return "🟩";
|
||||||
|
if (guess.testamentMatch) return "🟧";
|
||||||
|
return "🟥";
|
||||||
|
})
|
||||||
|
.join("");
|
||||||
|
|
||||||
const shareText = generateShareText();
|
const dateFormatter = new Intl.DateTimeFormat("en-US", {
|
||||||
|
month: "short",
|
||||||
|
day: "numeric",
|
||||||
|
year: "numeric",
|
||||||
|
});
|
||||||
|
const formattedDate = dateFormatter.format(
|
||||||
|
new Date(`${dailyVerse.date}T00:00:00`),
|
||||||
|
);
|
||||||
|
const siteUrl = window.location.origin;
|
||||||
|
return [
|
||||||
|
`📖 Bibdle | ${formattedDate} 📖`,
|
||||||
|
`${grade} (${guesses.length} ${guesses.length === 1 ? "guess" : "guesses"})`,
|
||||||
|
`${emojis}${guesses.length === 1 && chapterCorrect ? " ⭐" : ""}`,
|
||||||
|
siteUrl,
|
||||||
|
].join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
async function share() {
|
||||||
await (navigator as any).clipboard.writeText(shareText);
|
if (!browser) return;
|
||||||
copied = true;
|
|
||||||
setTimeout(() => {
|
|
||||||
copied = false;
|
|
||||||
}, 5000);
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Copy to clipboard failed:", err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleShare() {
|
const shareText = generateShareText();
|
||||||
if (copied || !browser) return;
|
|
||||||
const useClipboard = !("share" in navigator);
|
|
||||||
if (useClipboard) {
|
|
||||||
copied = true;
|
|
||||||
}
|
|
||||||
share()
|
|
||||||
.then(() => {
|
|
||||||
if (useClipboard) {
|
|
||||||
setTimeout(() => {
|
|
||||||
copied = false;
|
|
||||||
}, 5000);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
if (useClipboard) {
|
|
||||||
copied = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function clearLocalStorage() {
|
try {
|
||||||
if (!browser) return;
|
if ("share" in navigator) {
|
||||||
// Clear all bibdle-related localStorage items
|
await (navigator as any).share({ text: shareText });
|
||||||
const keysToRemove: string[] = [];
|
} else {
|
||||||
for (let i = 0; i < localStorage.length; i++) {
|
await (navigator as any).clipboard.writeText(shareText);
|
||||||
const key = localStorage.key(i);
|
}
|
||||||
if (key && key.startsWith("bibdle-")) {
|
} catch (err) {
|
||||||
keysToRemove.push(key);
|
console.error("Share failed:", err);
|
||||||
}
|
throw err;
|
||||||
}
|
}
|
||||||
keysToRemove.forEach((key) => localStorage.removeItem(key));
|
}
|
||||||
// Reload the page to reset state
|
|
||||||
window.location.reload();
|
async function copyToClipboard() {
|
||||||
}
|
if (!browser) return;
|
||||||
|
|
||||||
|
const shareText = generateShareText();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await (navigator as any).clipboard.writeText(shareText);
|
||||||
|
copied = true;
|
||||||
|
setTimeout(() => {
|
||||||
|
copied = false;
|
||||||
|
}, 5000);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Copy to clipboard failed:", err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleShare() {
|
||||||
|
if (copied || !browser) return;
|
||||||
|
const useClipboard = !("share" in navigator);
|
||||||
|
if (useClipboard) {
|
||||||
|
copied = true;
|
||||||
|
}
|
||||||
|
share()
|
||||||
|
.then(() => {
|
||||||
|
if (useClipboard) {
|
||||||
|
setTimeout(() => {
|
||||||
|
copied = false;
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
if (useClipboard) {
|
||||||
|
copied = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearLocalStorage() {
|
||||||
|
if (!browser) return;
|
||||||
|
// Clear all bibdle-related localStorage items
|
||||||
|
const keysToRemove: string[] = [];
|
||||||
|
for (let i = 0; i < localStorage.length; i++) {
|
||||||
|
const key = localStorage.key(i);
|
||||||
|
if (key && key.startsWith("bibdle-")) {
|
||||||
|
keysToRemove.push(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
keysToRemove.forEach((key) => localStorage.removeItem(key));
|
||||||
|
// Reload the page to reset state
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
<!-- <title>Bibdle — A daily bible game{isDev ? " (dev)" : ""}</title> -->
|
<!-- <title>Bibdle — A daily bible game{isDev ? " (dev)" : ""}</title> -->
|
||||||
<title>A daily bible game{isDev ? " (dev)" : ""}</title>
|
<title>A daily bible game{isDev ? " (dev)" : ""}</title>
|
||||||
<!-- <meta
|
<!-- <meta
|
||||||
name="description"
|
name="description"
|
||||||
content="Guess which book of the Bible a verse comes from."
|
content="Guess which book of the Bible a verse comes from."
|
||||||
/> -->
|
/> -->
|
||||||
</svelte:head>
|
</svelte:head>
|
||||||
|
|
||||||
<div class="min-h-dvh md:bg-linear-to-br md:from-blue-50 md:to-indigo-200 py-8">
|
<div class="min-h-dvh md:bg-linear-to-br md:from-blue-50 md:to-indigo-200 py-8">
|
||||||
<div class="w-full max-w-3xl mx-auto px-4">
|
<div class="w-full max-w-3xl mx-auto px-4">
|
||||||
<h1
|
<h1
|
||||||
class="text-3xl md:text-4xl font-bold text-center uppercase text-gray-600 drop-shadow-2xl tracking-widest p-4"
|
class="text-3xl md:text-4xl font-bold text-center uppercase text-gray-600 drop-shadow-2xl tracking-widest p-4"
|
||||||
>
|
>
|
||||||
<TitleAnimation />
|
<TitleAnimation />
|
||||||
<div class="font-normal"></div>
|
<div class="font-normal"></div>
|
||||||
</h1>
|
</h1>
|
||||||
<div class="text-center mb-8">
|
<div class="text-center mb-8">
|
||||||
<span class="big-text"
|
<span class="big-text"
|
||||||
>{isDev ? "Dev Edition | " : ""}{currentDate}</span
|
>{isDev ? "Dev Edition | " : ""}{currentDate}</span
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex flex-col gap-6">
|
<div class="flex flex-col gap-6">
|
||||||
<VerseDisplay {data} {isWon} {blurChapter} />
|
<VerseDisplay {data} {isWon} {blurChapter} />
|
||||||
|
|
||||||
{#if !isWon}
|
{#if !isWon}
|
||||||
<SearchInput bind:searchQuery {guessedIds} {submitGuess} />
|
<SearchInput bind:searchQuery {guessedIds} {submitGuess} />
|
||||||
{:else}
|
{:else}
|
||||||
<WinScreen
|
<WinScreen
|
||||||
{grade}
|
{grade}
|
||||||
{statsData}
|
{statsData}
|
||||||
{correctBookId}
|
{correctBookId}
|
||||||
{handleShare}
|
{handleShare}
|
||||||
{copyToClipboard}
|
{copyToClipboard}
|
||||||
bind:copied
|
bind:copied
|
||||||
{statsSubmitted}
|
{statsSubmitted}
|
||||||
guessCount={guesses.length}
|
guessCount={guesses.length}
|
||||||
reference={dailyVerse.reference}
|
reference={dailyVerse.reference}
|
||||||
onChapterGuessCompleted={() => {
|
onChapterGuessCompleted={() => {
|
||||||
chapterGuessCompleted = true;
|
chapterGuessCompleted = true;
|
||||||
const key = `bibdle-chapter-guess-${dailyVerse.reference}`;
|
const key = `bibdle-chapter-guess-${dailyVerse.reference}`;
|
||||||
const saved = localStorage.getItem(key);
|
const saved = localStorage.getItem(key);
|
||||||
if (saved) {
|
if (saved) {
|
||||||
const data = JSON.parse(saved);
|
const data = JSON.parse(saved);
|
||||||
const match = dailyVerse.reference.match(/\s(\d+):/);
|
const match =
|
||||||
const correctChapter = match ? parseInt(match[1], 10) : 1;
|
dailyVerse.reference.match(/\s(\d+):/);
|
||||||
chapterCorrect = data.selectedChapter === correctChapter;
|
const correctChapter = match
|
||||||
}
|
? parseInt(match[1], 10)
|
||||||
}}
|
: 1;
|
||||||
/>
|
chapterCorrect =
|
||||||
{/if}
|
data.selectedChapter === correctChapter;
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<GuessesTable {guesses} {correctBookId} />
|
<GuessesTable {guesses} {correctBookId} />
|
||||||
|
|
||||||
{#if isWon}
|
{#if isWon}
|
||||||
<Feedback />
|
<Feedback />
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{#if isDev}
|
{#if isDev}
|
||||||
<button
|
<button
|
||||||
onclick={clearLocalStorage}
|
onclick={clearLocalStorage}
|
||||||
class="mt-4 px-4 py-2 bg-red-500 hover:bg-red-600 text-white rounded-lg text-sm font-bold transition-colors"
|
class="mt-4 px-4 py-2 bg-red-500 hover:bg-red-600 text-white rounded-lg text-sm font-bold transition-colors"
|
||||||
>
|
>
|
||||||
Clear LocalStorage
|
Clear LocalStorage
|
||||||
</button>
|
</button>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -48,13 +48,16 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
const betterGuesses = allCompletions.filter(c => c.guessCount < guessCount).length;
|
const betterGuesses = allCompletions.filter(c => c.guessCount < guessCount).length;
|
||||||
const guessRank = betterGuesses + 1;
|
const guessRank = betterGuesses + 1;
|
||||||
|
|
||||||
|
// Count ties: how many have the SAME guessCount (excluding self)
|
||||||
|
const tiedCount = allCompletions.filter(c => c.guessCount === guessCount && c.anonymousId !== anonymousId).length;
|
||||||
|
|
||||||
// Average guesses
|
// Average guesses
|
||||||
const totalGuesses = allCompletions.reduce((sum, c) => sum + c.guessCount, 0);
|
const totalGuesses = allCompletions.reduce((sum, c) => sum + c.guessCount, 0);
|
||||||
const averageGuesses = Math.round((totalGuesses / totalSolves) * 10) / 10;
|
const averageGuesses = Math.round((totalGuesses / totalSolves) * 10) / 10;
|
||||||
|
|
||||||
return json({
|
return json({
|
||||||
success: true,
|
success: true,
|
||||||
stats: { solveRank, guessRank, totalSolves, averageGuesses }
|
stats: { solveRank, guessRank, totalSolves, averageGuesses, tiedCount }
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error submitting completion:', err);
|
console.error('Error submitting completion:', err);
|
||||||
@@ -105,13 +108,16 @@ export const GET: RequestHandler = async ({ url }) => {
|
|||||||
const betterGuesses = allCompletions.filter(c => c.guessCount < guessCount).length;
|
const betterGuesses = allCompletions.filter(c => c.guessCount < guessCount).length;
|
||||||
const guessRank = betterGuesses + 1;
|
const guessRank = betterGuesses + 1;
|
||||||
|
|
||||||
|
// Count ties: how many have the SAME guessCount (excluding self)
|
||||||
|
const tiedCount = allCompletions.filter(c => c.guessCount === guessCount && c.anonymousId !== anonymousId).length;
|
||||||
|
|
||||||
// Average guesses
|
// Average guesses
|
||||||
const totalGuesses = allCompletions.reduce((sum, c) => sum + c.guessCount, 0);
|
const totalGuesses = allCompletions.reduce((sum, c) => sum + c.guessCount, 0);
|
||||||
const averageGuesses = Math.round((totalGuesses / totalSolves) * 10) / 10;
|
const averageGuesses = Math.round((totalGuesses / totalSolves) * 10) / 10;
|
||||||
|
|
||||||
return json({
|
return json({
|
||||||
success: true,
|
success: true,
|
||||||
stats: { solveRank, guessRank, totalSolves, averageGuesses }
|
stats: { solveRank, guessRank, totalSolves, averageGuesses, tiedCount }
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error fetching stats:', err);
|
console.error('Error fetching stats:', err);
|
||||||
|
|||||||
Reference in New Issue
Block a user