logigate/app/components/editor/MenuLeaderboard.vue

124 lines
4.9 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
// The leaderboard page of the title menu: the best ten of each week's
// puzzle, your place, and the name shown next to your scores.
import { CHALLENGES } from '~/lib/logic/challenges'
import { weekKey } from '~/lib/logic/puzzles'
const ed = useEditor()
const DAY = 86_400_000
const weeks = Array.from({ length: 5 }, (_, i) => {
const key = weekKey(new Date(Date.now() - i * 7 * DAY))
return { id: i ? `week-${key}` : 'weekly', label: i === 0 ? 'This week' : i === 1 ? 'Last week' : `Week ${Number(key.split('-')[1])}` }
})
const at = ref(0)
type Board = Awaited<ReturnType<typeof ed.fetchBoard>>
const boards = reactive<Record<string, Board | 'loading'>>({})
const board = computed(() => boards[weeks[at.value]!.id])
watch(
at,
async i => {
const id = weeks[i]!.id
if (boards[id] && boards[id] !== null) return
boards[id] = 'loading'
boards[id] = await ed.fetchBoard(id)
},
{ immediate: true },
)
const name = ref(ed.playerName.value)
const saving = ref(false)
const nameError = ref('')
async function saveName() {
saving.value = true
nameError.value = ''
try {
await ed.setPlayerName(name.value)
name.value = ed.playerName.value
// Your name shows on the boards already loaded once they are fetched again.
for (const k of Object.keys(boards)) delete boards[k]
const id = weeks[at.value]!.id
boards[id] = await ed.fetchBoard(id)
} catch (e) {
nameError.value = (e as Error).message
} finally {
saving.value = false
}
}
/** Your best per challenge, over the campaign and the challenges project. */
const records = (() => {
const best: Record<string, number> = {}
for (const p of [ed.projectOf('campaign'), ed.projectOf('challenges')])
for (const [id, t] of Object.entries(p?.best ?? {})) best[id] = Math.min(best[id] ?? Infinity, t)
return CHALLENGES.filter(c => best[c.id] !== undefined).map(c => ({ name: c.name, t: best[c.id]! }))
})()
</script>
<template>
<div class="menu-board">
<form class="name-row" @submit.prevent="saveName">
<label class="field grow">
<span>Your name on the leaderboards</span>
<input v-model="name" maxlength="24" placeholder="Anonymous" autocomplete="nickname" />
</label>
<button class="btn" :disabled="saving || name.trim() === ed.playerName.value">{{ saving ? 'Saving…' : 'Save' }}</button>
</form>
<p v-if="nameError" class="hint text-(--danger)">{{ nameError }}</p>
<div class="tabs board-tabs" role="tablist">
<button v-for="(w, i) in weeks" :key="w.id" role="tab" :aria-selected="at === i" :class="{ active: at === i }" @click="at = i">{{ w.label }}</button>
</div>
<p v-if="board === 'loading' || board === undefined" class="hint mt-3">Loading…</p>
<p v-else-if="board === null" class="hint mt-3">Could not load this board. Are you online?</p>
<template v-else>
<div class="mt-3 flex items-start gap-3">
<div class="grow">
<div class="font-semibold">{{ board.spec.name }}</div>
<p class="hint">{{ board.spec.goal }}</p>
</div>
<button v-if="at === 0" class="btn btn-primary shrink-0" @click="ed.playWeekly()"><Icon name="lucide:play" class="size-4" /> Play</button>
</div>
<p v-if="!board.summary.players" class="hint mt-3">Nobody has solved it yet. Be the first.</p>
<template v-else>
<table class="board">
<thead>
<tr>
<th>#</th>
<th>Player</th>
<th class="num">Transistors</th>
<th class="num" title="Gates a signal passes through on the longest path">Delay</th>
</tr>
</thead>
<tbody>
<tr v-for="(e, i) in board.summary.top" :key="i" :class="{ you: e.you }">
<td class="rank">{{ i + 1 }}</td>
<td>{{ e.name || 'Anonymous' }}<span v-if="e.you" class="you-tag">you</span></td>
<td class="num">{{ e.t.toLocaleString() }}</td>
<td class="num">{{ e.d ?? '–' }}</td>
</tr>
<tr v-if="board.summary.you && !board.summary.top?.some(e => e.you)" class="you gap">
<td class="rank">{{ board.summary.you.rank }}</td>
<td>{{ ed.playerName.value || 'Anonymous' }}<span class="you-tag">you</span></td>
<td class="num">{{ board.summary.you.t.toLocaleString() }}</td>
<td class="num">{{ board.summary.you.d ?? '–' }}</td>
</tr>
</tbody>
</table>
<EditorScoreChart :summary="board.summary" />
</template>
</template>
<h3 class="mt-6">Your challenge records</h3>
<p v-if="!records.length" class="hint">Solve challenges in the campaign or on the Challenges page; your fewest transistors show up here.</p>
<ul v-else class="records">
<li v-for="r in records" :key="r.name">
<span>{{ r.name }}</span>
<span class="tabular-nums text-(--muted)">{{ r.t.toLocaleString() }} transistors</span>
</li>
</ul>
</div>
</template>