logigate/app/components/editor/ScoreChart.vue

127 lines
3.4 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">
// How everyone did on a puzzle: players per transistor count, fewest on the
// left. Wide ranges are grouped into at most 16 bars. Your own bar stands
// out and is named in the text, so it never relies on colour alone.
import type { PuzzleSummary } from '~/composables/useEditor'
const props = defineProps<{ summary: PuzzleSummary; mine?: number }>()
const MAX_BARS = 16
const bars = computed(() => {
const h = props.summary.histogram
if (!h.length) return []
const lo = h[0]!.t
const hi = h[h.length - 1]!.t
// Transistor counts come in steps of 4 (one NAND each).
const steps = (hi - lo) / 4 + 1
const per = Math.max(1, Math.ceil(steps / MAX_BARS))
const out: { from: number; to: number; n: number }[] = []
for (let i = 0; i < Math.ceil(steps / per); i++) out.push({ from: lo + i * per * 4, to: lo + ((i + 1) * per - 1) * 4, n: 0 })
for (const { t, n } of h) out[Math.min(out.length - 1, Math.floor((t - lo) / 4 / per))]!.n += n
return out
})
const top = computed(() => Math.max(1, ...bars.value.map(b => b.n)))
const you = computed(() => props.summary.you?.t ?? props.mine)
const isMine = (b: { from: number; to: number }) => you.value !== undefined && you.value >= b.from && you.value <= b.to
const range = (b: { from: number; to: number }) => (b.from === b.to ? `${b.from}` : `${b.from}–${b.to}`)
const hover = ref<number | null>(null)
const readout = computed(() => {
const b = hover.value !== null ? bars.value[hover.value] : undefined
if (b) return `${b.n} player${b.n === 1 ? '' : 's'} with ${range(b)} transistors`
const y = props.summary.you
if (y) return `You: ${y.t} transistors, place ${y.rank} of ${props.summary.players}`
return `${props.summary.players} player${props.summary.players === 1 ? '' : 's'} solved it`
})
</script>
<template>
<figure class="score-chart">
<figcaption>Everyone's transistors</figcaption>
<div class="plot" @pointerleave="hover = null">
<div
v-for="(b, i) in bars"
:key="i"
class="col"
:class="{ mine: isMine(b), hot: hover === i }"
:aria-label="`${range(b)} transistors: ${b.n} players`"
@pointerenter="hover = i"
>
<span class="bar" :style="{ height: b.n ? `${Math.max(6, (b.n / top) * 100)}%` : '0' }" />
</div>
</div>
<div class="axis">
<span>{{ bars[0]?.from }}</span>
<span>fewer is better</span>
<span>{{ bars.at(-1)?.to }}</span>
</div>
<p class="readout">{{ readout }}</p>
</figure>
</template>
<style scoped>
.score-chart {
margin-top: 10px;
padding: 10px;
border: 1px solid var(--border);
border-radius: 8px;
background: var(--panel-2);
}
figcaption {
font-size: 11px;
font-weight: 600;
color: var(--muted);
margin-bottom: 6px;
}
.plot {
display: flex;
align-items: stretch;
gap: 2px;
height: 64px;
border-bottom: 1px solid var(--border);
}
.col {
flex: 1;
display: flex;
align-items: flex-end;
justify-content: center;
cursor: default;
}
.bar {
width: 100%;
max-width: 22px;
border-radius: 4px 4px 0 0;
background: #4f6278;
transition: height 0.4s ease;
}
.col.hot .bar {
background: #7d93ad;
}
.col.mine .bar {
background: var(--on);
}
.axis {
display: flex;
justify-content: space-between;
margin-top: 3px;
font-family: var(--font-mono);
font-size: 10px;
color: var(--muted);
}
.readout {
margin-top: 6px;
font-size: 12px;
color: var(--text);
}
</style>