mirror of
https://github.com/pupperpowell/bibdle.git
synced 2026-02-04 10:54:44 -05:00
redirect to bibdle.dev and components
This commit is contained in:
43
src/lib/utils/game.ts
Normal file
43
src/lib/utils/game.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { bibleBooks, type BibleBook } from '$lib/types/bible';
|
||||
|
||||
export function getBookById(id: string): BibleBook | undefined {
|
||||
return bibleBooks.find((b) => b.id === id);
|
||||
}
|
||||
|
||||
export function isAdjacent(id1: string, id2: string): boolean {
|
||||
const b1 = getBookById(id1);
|
||||
const b2 = getBookById(id2);
|
||||
return !!(b1 && b2 && Math.abs(b1.order - b2.order) === 1);
|
||||
}
|
||||
|
||||
export function getGrade(numGuesses: number, popularity: number): string {
|
||||
const difficulty = 14 - popularity;
|
||||
const performanceScore = Math.max(0, 10 - numGuesses);
|
||||
const totalScore = performanceScore + difficulty * 0.8;
|
||||
if (totalScore >= 14) return "🟢 S";
|
||||
if (totalScore >= 11) return "🟢 A";
|
||||
if (totalScore >= 8) return "🟡 B";
|
||||
if (totalScore >= 5) return "🟠 C";
|
||||
return "🔴 C-";
|
||||
}
|
||||
|
||||
export function toOrdinal(n: number): string {
|
||||
if (n >= 11 && n <= 13) {
|
||||
return `${n}th`;
|
||||
}
|
||||
const mod = n % 10;
|
||||
const suffix = mod === 1 ? "st" : mod === 2 ? "nd" : mod === 3 ? "rd" : "th";
|
||||
return `${n}${suffix}`;
|
||||
}
|
||||
|
||||
export function generateUUID(): string {
|
||||
if (typeof window !== 'undefined' && typeof (window as any).crypto?.randomUUID === "function") {
|
||||
return (window as any).crypto.randomUUID();
|
||||
}
|
||||
// Fallback
|
||||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
||||
const r = ((window as any).crypto.getRandomValues(new Uint8Array(1))[0] % 16) | 0;
|
||||
const v = c === "x" ? r : (r & 0x3) | 0x8;
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user