86 lines
3.5 KiB
Vue
86 lines
3.5 KiB
Vue
<script setup lang="ts">
|
|
import { chipPins } from '~/lib/logic/geometry'
|
|
import { describeCircuit } from '~/lib/logic/puzzles'
|
|
|
|
const ed = useEditor()
|
|
const close = () => (ed.dialog.value = null)
|
|
|
|
const name = ref(ed.def.value.name)
|
|
const goal = ref(ed.def.value.description ?? '')
|
|
const link = ref('')
|
|
const error = ref('')
|
|
const busy = ref(false)
|
|
|
|
/** What players will get, or why this circuit can't be a puzzle yet. */
|
|
const preview = computed(() => {
|
|
try {
|
|
const d = describeCircuit(ed.getDef.value, ed.def.value.id)
|
|
return { ok: true as const, ...d }
|
|
} catch (e) {
|
|
return { ok: false as const, error: (e as Error).message }
|
|
}
|
|
})
|
|
const pins = computed(() => chipPins(ed.def.value))
|
|
|
|
async function publish() {
|
|
busy.value = true
|
|
error.value = ''
|
|
try {
|
|
link.value = await ed.publishPuzzle(name.value, goal.value)
|
|
ed.play('unlock')
|
|
} catch (e) {
|
|
error.value = (e as Error).message
|
|
} finally {
|
|
busy.value = false
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="modal-backdrop" @click.self="close">
|
|
<div class="modal" role="dialog" aria-labelledby="publish-title">
|
|
<div class="flex items-start justify-between gap-4">
|
|
<h2 id="publish-title">Publish as puzzle</h2>
|
|
<button class="tb-btn" title="Close" @click="close"><Icon name="lucide:x" class="size-4" /></button>
|
|
</div>
|
|
<p class="hint mt-1">
|
|
Others get the pins of "{{ ed.def.value.name }}" and have to build something that behaves the same. Your circuit stays private: only its behaviour is
|
|
published. Everyone's transistor count goes on the puzzle's board, next to yours ({{ ed.transistors(ed.def.value.id).toLocaleString() }}).
|
|
</p>
|
|
|
|
<template v-if="!link">
|
|
<label class="field mt-4">
|
|
<span>Name</span>
|
|
<input v-model="name" maxlength="60" />
|
|
</label>
|
|
<label class="field">
|
|
<span>Goal, shown to players</span>
|
|
<textarea v-model="goal" rows="3" maxlength="400" placeholder="What should it do? Leave empty to just show the target." />
|
|
</label>
|
|
<p v-if="preview.ok" class="result pass">
|
|
<Icon name="lucide:circle-check" class="size-4 shrink-0" />
|
|
<span>
|
|
{{ pins.inputs.map(p => p.label).join(', ') }} → {{ pins.outputs.map(p => p.label).join(', ') }}.
|
|
<template v-if="preview.sequential">It stores state, so players get {{ preview.steps.length }} steps to match from power-on.</template>
|
|
<template v-else>Players get the full truth table, {{ preview.steps.length }} rows.</template>
|
|
</span>
|
|
</p>
|
|
<p v-else class="result fail"><Icon name="lucide:circle-x" class="size-4 shrink-0" /> {{ preview.error }}</p>
|
|
<p v-if="error" class="result fail"><Icon name="lucide:circle-x" class="size-4 shrink-0" /> {{ error }}</p>
|
|
<button class="btn btn-primary mt-3" :disabled="!preview.ok || busy" @click="publish">
|
|
<Icon name="lucide:puzzle" class="size-4" /> {{ busy ? 'Publishing…' : 'Publish' }}
|
|
</button>
|
|
</template>
|
|
|
|
<template v-else>
|
|
<h3>Puzzle link</h3>
|
|
<div class="link-row">
|
|
<input :value="link" readonly @focus="($event.target as HTMLInputElement).select()" />
|
|
<button class="btn btn-primary" @click="ed.copyText(link, 'Puzzle link copied')"><Icon name="lucide:copy" class="size-4" /> Copy</button>
|
|
</div>
|
|
<p class="hint mt-2">Anyone with the link gets the puzzle in their own project. Changing your circuit later doesn't change the puzzle.</p>
|
|
</template>
|
|
</div>
|
|
</div>
|
|
</template>
|