added streak container

This commit is contained in:
George Powell
2026-02-26 00:51:48 -05:00
parent f9f3f3de12
commit a5cf248e29
9 changed files with 167 additions and 56 deletions

View File

@@ -1,5 +1,43 @@
import type { SHA512_256 } from '@oslojs/crypto/sha2';
import type { Guess } from './game';
export function getVerseSnippet(verseText: string): string {
const words = verseText.trim().split(/\s+/);
const slice = words.slice(0, 25);
const text = slice.join(' ');
// Returns character index immediately after the Nth word (1-indexed)
function posAfterWord(n: number): number {
let pos = 0;
for (let w = 0; w < Math.min(n, slice.length); w++) {
if (w > 0) pos++; // space between words
pos += slice[w].length;
}
return pos;
}
const start = posAfterWord(10);
const end = posAfterWord(25);
// Find first punctuation mark between words 10 and 25
const range = text.substring(start, end);
const match = range.match(/[,;:.!?—–-]/);
function withClosedQuotes(snippet: string): string {
const opens = (snippet.match(/\u201C/g) ?? []).length;
const closes = (snippet.match(/\u201D/g) ?? []).length;
const closeQuote = opens > closes ? '\u201D' : '';
return `\u201C${snippet}...${closeQuote}\u201D`;
}
if (match && match.index !== undefined) {
const cutPos = start + match.index;
return withClosedQuotes(text.substring(0, cutPos).trimEnd());
}
return withClosedQuotes(text);
}
export function generateShareText(params: {
guesses: Guess[];
correctBookId: string;
@@ -8,8 +46,9 @@ export function generateShareText(params: {
isLoggedIn: boolean;
streak?: number;
origin: string;
verseText: string;
}): string {
const { guesses, correctBookId, dailyVerseDate, chapterCorrect, isLoggedIn, streak, origin } = params;
const { guesses, correctBookId, dailyVerseDate, chapterCorrect, isLoggedIn, streak, origin, verseText } = params;
const emojis = guesses
.slice()
@@ -35,17 +74,15 @@ export function generateShareText(params: {
const bookEmoji = isLoggedIn ? "📜" : "📖";
const guessWord = guesses.length === 1 ? "guess" : "guesses";
const streakPart = streak !== undefined && streak > 1 ? `, ${streak} days 🔥` : "";
const streakPart = streak !== undefined && streak > 1 ? ` (${streak} days🔥)` : "";
const chapterStar = guesses.length === 1 && chapterCorrect ? " ⭐" : "";
const lines = [
`${bookEmoji} Bibdle | ${formattedDate} ${bookEmoji}`,
`${guesses.length} ${guessWord}${streakPart}`,
];
lines.push(
`${emojis}${guesses.length === 1 && chapterCorrect ? " ⭐" : ""}`,
getVerseSnippet(verseText),
`${emojis}${chapterStar} ${guesses.length} ${guessWord}${streakPart}`,
origin,
);
];
return lines.join("\n");
}