logigate/app/components/editor/ChallengesPanel.vue

99 lines
4.7 KiB
Vue

<script setup lang="ts">
// Challenges, puzzles and the puzzle of the week, opened from the top bar.
// Starting one opens its circuit; the Test tab then shows what to build.
import { CHALLENGES } from '~/lib/logic/challenges'
import { campaignProgress, challengeName, challengeOpen, isCampaign, NEEDS } from '~/lib/logic/progress'
import { weekKey, weeklyId } from '~/lib/logic/puzzles'
const ed = useEditor()
const emit = defineEmits<{ close: [] }>()
const solved = computed(() => new Set(ed.project.value.solved))
const best = computed(() => ed.project.value.best ?? {})
const solvedCount = computed(() => CHALLENGES.filter(c => solved.value.has(c.id)).length)
const campaign = computed(() => isCampaign(ed.project.value))
const progress = computed(() => campaignProgress(ed.project.value))
const open = (id: string) => challengeOpen(ed.project.value, id)
const needsText = (id: string) => `Solve ${(NEEDS[id] ?? []).filter(n => !solved.value.has(n)).map(challengeName).join(' and ')} first`
const puzzles = computed(() => (ed.project.value.puzzles ?? []).filter(p => !p.id.startsWith('week:') || p.id === weeklyId(weekKey())))
const weeklyStarted = computed(() => ed.project.value.components.some(d => d.challenge === weeklyId(weekKey())))
const started = computed(() => new Map(ed.project.value.components.filter(d => d.challenge).map(d => [d.challenge!, d])))
function openWeekly() {
const spec = ed.project.value.puzzles?.find(p => p.id === weeklyId(weekKey()))
if (spec) ed.beginPuzzle(spec)
else void ed.openPuzzle('weekly')
emit('close')
}
defineExpose({ solvedCount })
</script>
<template>
<div class="challenges-panel" @click="($event.target as HTMLElement).closest('.btn-sm:not(:disabled)') && emit('close')">
<div v-if="campaign" class="campaign">
<div class="flex items-center justify-between">
<span class="font-semibold">Campaign</span>
<span class="tabular-nums text-(--muted)">{{ solvedCount }} / {{ CHALLENGES.length }}</span>
</div>
<div class="bar"><span :style="{ width: `${progress * 100}%` }" /></div>
<p>Every part you solve becomes yours to use. New parts and challenges open as you go.</p>
</div>
<p v-else class="intro">
Build each part from NAND and the parts you already made. Start a challenge, wire it up, then press
<strong>Check</strong> in the side panel.
</p>
<div class="weekly">
<div class="flex items-center gap-2">
<Icon name="lucide:calendar-days" class="size-4 shrink-0 text-(--accent)" />
<span class="grow font-medium">Puzzle of the week</span>
<button class="btn-sm" @click="openWeekly">{{ weeklyStarted ? 'Open' : 'Play' }}</button>
</div>
<p>A new logic puzzle every Monday, the same for everyone. Fewest transistors wins.</p>
</div>
<template v-if="puzzles.length">
<h3>Puzzles</h3>
<ul class="challenges">
<li v-for="p in puzzles" :key="p.id" :class="{ solved: solved.has(p.id) }">
<div class="flex items-center gap-2">
<Icon :name="solved.has(p.id) ? 'lucide:circle-check' : 'lucide:puzzle'" class="size-4 shrink-0" :class="solved.has(p.id) ? 'text-(--on)' : 'text-(--muted)'" />
<span class="grow font-medium">{{ p.name }}</span>
<button class="btn-sm" @click="ed.beginPuzzle(p)">Open</button>
</div>
<p class="pins">
{{ p.inputs.join(', ') }} → {{ p.outputs.join(', ') }}
<span v-if="best[p.id] !== undefined" class="best"> · best {{ best[p.id]!.toLocaleString() }} transistors</span>
</p>
</li>
</ul>
</template>
<h3>Challenges</h3>
<ul class="challenges">
<li v-for="c in CHALLENGES" :key="c.id" :class="{ solved: solved.has(c.id), locked: !open(c.id) }">
<div class="flex items-center gap-2">
<Icon
:name="solved.has(c.id) ? 'lucide:circle-check' : open(c.id) ? 'lucide:circle' : 'lucide:lock'"
class="size-4 shrink-0"
:class="solved.has(c.id) ? 'text-(--on)' : 'text-(--muted)'"
/>
<span class="grow font-medium">{{ c.name }}</span>
<button class="btn-sm" :disabled="!open(c.id) && !started.has(c.id)" :title="open(c.id) ? '' : needsText(c.id)" @click="ed.beginChallenge(c)">
{{ started.has(c.id) ? 'Open' : 'Start' }}
</button>
</div>
<p v-if="!open(c.id)" class="needs">{{ needsText(c.id) }}</p>
<p>{{ c.goal }}</p>
<p class="pins">
{{ c.inputs.join(', ') }} → {{ c.outputs.join(', ') }}
<span v-if="best[c.id] !== undefined" class="best" title="Fewest transistors in a passing solution so far">
· best {{ best[c.id]!.toLocaleString() }} transistors
</span>
</p>
</li>
</ul>
</div>
</template>