A campaign with unlocks, synthesised sound, puzzles and the puzzle of the week
- Campaign projects start with NAND only; challenges open along a
dependency chain, each solution becomes a usable part, and basic parts
unlock on the way. Locked parts and challenges say what opens them.
- Sound effects made with Web Audio: place, connect, switches, buttons,
delete, clock ticks (opt-in), solve, fail, unlock, record. Mute and
volume in the top bar and Display settings.
- Publish any circuit as a puzzle link; only its behaviour is shared.
The server re-runs the checks on submitted solutions, counts their
transistors and keeps each player's best; the board shows a histogram
and your place. A generated puzzle of the week for everyone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 17:54:57 +00:00
|
|
|
// Published puzzles and their scores, as JSON files under data/puzzles:
|
|
|
|
|
// specs:<id> holds the puzzle, scores:<id> the best score of each player.
|
|
|
|
|
// The puzzle of the week is generated, so only its scores are stored.
|
|
|
|
|
|
|
|
|
|
import type { H3Event } from 'h3'
|
|
|
|
|
import { cleanPuzzle, isWeekKey, weekKey, weeklyPuzzle } from '../../app/lib/logic/puzzles'
|
|
|
|
|
import type { PuzzleSpec } from '../../app/lib/logic/types'
|
|
|
|
|
|
|
|
|
|
export interface PuzzleRecord {
|
|
|
|
|
spec: PuzzleSpec
|
|
|
|
|
created: number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Entry {
|
|
|
|
|
t: number
|
|
|
|
|
d: number | null
|
|
|
|
|
at: number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type ScoreBoard = Record<string, Entry>
|
|
|
|
|
|
|
|
|
|
export const puzzleStorage = () => useStorage('puzzles')
|
|
|
|
|
|
|
|
|
|
export const isPlayer = (v: unknown): v is string => typeof v === 'string' && /^[A-Za-z0-9_-]{8,40}$/.test(v)
|
|
|
|
|
|
|
|
|
|
/** The puzzle behind an API id: a link id, or "week-2026-43" for a weekly one up to this week. */
|
|
|
|
|
export async function findPuzzle(id: string | undefined): Promise<PuzzleSpec | null> {
|
|
|
|
|
if (!id) return null
|
|
|
|
|
const week = /^week-(\d{4}-\d{2})$/.exec(id)?.[1]
|
|
|
|
|
if (week) return isWeekKey(week) && week <= weekKey() ? weeklyPuzzle(week) : null
|
|
|
|
|
if (!isShareId(id)) return null
|
|
|
|
|
const rec = await puzzleStorage().getItem<PuzzleRecord>(`specs:${id}`)
|
|
|
|
|
return rec ? cleanPuzzle(rec.spec, `pz:${id}`) : null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** One write at a time per puzzle, so two scores arriving together both count. */
|
|
|
|
|
const queues = new Map<string, Promise<unknown>>()
|
|
|
|
|
export function locked<T>(key: string, fn: () => Promise<T>): Promise<T> {
|
|
|
|
|
const prev = queues.get(key) ?? Promise.resolve()
|
|
|
|
|
const next = prev.then(fn, fn)
|
|
|
|
|
queues.set(
|
|
|
|
|
key,
|
|
|
|
|
next.catch(() => {}),
|
|
|
|
|
)
|
|
|
|
|
return next
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Summary {
|
|
|
|
|
players: number
|
|
|
|
|
/** Transistor counts and how many players have each, fewest first. */
|
|
|
|
|
histogram: { t: number; n: number }[]
|
|
|
|
|
/** This player's best and place (1 = best), when they have a score. */
|
|
|
|
|
you?: { t: number; d: number | null; rank: number }
|
A main menu: free play, campaign, challenges and leaderboard over a circuit that builds itself
Logigate opens on a title screen. Continue, Free play (your own projects),
Campaign (progress and what is next), Challenges (every challenge and the
puzzle of the week in one Challenges project), Leaderboard (the best ten of
this and earlier weeks, your place, your records), Settings. Arrow keys,
Enter and Esc work through it; links still open straight into the editor.
Players can name themselves on the leaderboards. The logo in the editor
leads back to the menu.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-22 10:22:35 +00:00
|
|
|
/** The best ten: fewest transistors, then shortest delay, then first to get there. */
|
|
|
|
|
top: { name: string; t: number; d: number | null; you?: true }[]
|
A campaign with unlocks, synthesised sound, puzzles and the puzzle of the week
- Campaign projects start with NAND only; challenges open along a
dependency chain, each solution becomes a usable part, and basic parts
unlock on the way. Locked parts and challenges say what opens them.
- Sound effects made with Web Audio: place, connect, switches, buttons,
delete, clock ticks (opt-in), solve, fail, unlock, record. Mute and
volume in the top bar and Display settings.
- Publish any circuit as a puzzle link; only its behaviour is shared.
The server re-runs the checks on submitted solutions, counts their
transistors and keeps each player's best; the board shows a histogram
and your place. A generated puzzle of the week for everyone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 17:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
A main menu: free play, campaign, challenges and leaderboard over a circuit that builds itself
Logigate opens on a title screen. Continue, Free play (your own projects),
Campaign (progress and what is next), Challenges (every challenge and the
puzzle of the week in one Challenges project), Leaderboard (the best ten of
this and earlier weeks, your place, your records), Settings. Arrow keys,
Enter and Esc work through it; links still open straight into the editor.
Players can name themselves on the leaderboards. The logo in the editor
leads back to the menu.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-22 10:22:35 +00:00
|
|
|
/** Names players chose for leaderboards, by player id. */
|
|
|
|
|
export async function playerNames(): Promise<Record<string, string>> {
|
|
|
|
|
return (await puzzleStorage().getItem<Record<string, string>>('players')) ?? {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** A leaderboard name: letters, digits, spaces and a little punctuation, at most 24 characters. */
|
|
|
|
|
export function cleanName(v: unknown): string {
|
|
|
|
|
if (typeof v !== 'string') return ''
|
|
|
|
|
return v
|
|
|
|
|
.normalize('NFC')
|
|
|
|
|
.replace(/[^\p{L}\p{N} ._\-'!?&+#]/gu, '')
|
|
|
|
|
.replace(/\s+/g, ' ')
|
|
|
|
|
.trim()
|
|
|
|
|
.slice(0, 24)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function summarize(board: ScoreBoard, player?: string, names: Record<string, string> = {}): Summary {
|
A campaign with unlocks, synthesised sound, puzzles and the puzzle of the week
- Campaign projects start with NAND only; challenges open along a
dependency chain, each solution becomes a usable part, and basic parts
unlock on the way. Locked parts and challenges say what opens them.
- Sound effects made with Web Audio: place, connect, switches, buttons,
delete, clock ticks (opt-in), solve, fail, unlock, record. Mute and
volume in the top bar and Display settings.
- Publish any circuit as a puzzle link; only its behaviour is shared.
The server re-runs the checks on submitted solutions, counts their
transistors and keeps each player's best; the board shows a histogram
and your place. A generated puzzle of the week for everyone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 17:54:57 +00:00
|
|
|
const entries = Object.values(board)
|
|
|
|
|
const counts = new Map<number, number>()
|
|
|
|
|
for (const e of entries) counts.set(e.t, (counts.get(e.t) ?? 0) + 1)
|
|
|
|
|
const histogram = [...counts.entries()].sort((a, b) => a[0] - b[0]).map(([t, n]) => ({ t, n }))
|
|
|
|
|
const mine = player ? board[player] : undefined
|
A main menu: free play, campaign, challenges and leaderboard over a circuit that builds itself
Logigate opens on a title screen. Continue, Free play (your own projects),
Campaign (progress and what is next), Challenges (every challenge and the
puzzle of the week in one Challenges project), Leaderboard (the best ten of
this and earlier weeks, your place, your records), Settings. Arrow keys,
Enter and Esc work through it; links still open straight into the editor.
Players can name themselves on the leaderboards. The logo in the editor
leads back to the menu.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-22 10:22:35 +00:00
|
|
|
const top = Object.entries(board)
|
|
|
|
|
.sort(([, a], [, b]) => a.t - b.t || (a.d ?? Infinity) - (b.d ?? Infinity) || a.at - b.at)
|
|
|
|
|
.slice(0, 10)
|
|
|
|
|
.map(([id, e]) => ({ name: names[id] ?? '', t: e.t, d: e.d, ...(id === player ? { you: true as const } : {}) }))
|
|
|
|
|
const summary: Summary = { players: entries.length, histogram, top }
|
A campaign with unlocks, synthesised sound, puzzles and the puzzle of the week
- Campaign projects start with NAND only; challenges open along a
dependency chain, each solution becomes a usable part, and basic parts
unlock on the way. Locked parts and challenges say what opens them.
- Sound effects made with Web Audio: place, connect, switches, buttons,
delete, clock ticks (opt-in), solve, fail, unlock, record. Mute and
volume in the top bar and Display settings.
- Publish any circuit as a puzzle link; only its behaviour is shared.
The server re-runs the checks on submitted solutions, counts their
transistors and keeps each player's best; the board shows a histogram
and your place. A generated puzzle of the week for everyone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 17:54:57 +00:00
|
|
|
if (mine) summary.you = { t: mine.t, d: mine.d, rank: 1 + entries.filter(e => e.t < mine.t).length }
|
|
|
|
|
return summary
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function board(id: string): Promise<ScoreBoard> {
|
|
|
|
|
return (await puzzleStorage().getItem<ScoreBoard>(`scores:${id}`)) ?? {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function playerParam(event: H3Event): string | undefined {
|
|
|
|
|
const p = getQuery(event).player
|
|
|
|
|
return isPlayer(p) ? p : undefined
|
|
|
|
|
}
|