- add /cool, /cool?vote=1, /cool/stage and /cool/city on the existing Nuxt/Onyx stack - wire real route/shade metrics and OSM geometry from the route bundle; city-gap data for the city view - show shade % and direct-sun minutes together; recommend Accessible for Ruth with the 15% ramp trade-off - add in-memory time/scene/round state and one anonymous vote per session, polled every 2 s - keep the retail module linked but out of the Cool City story
This commit is contained in:
1 parent
33063afbdf
commit
2bc52a314d
28 files changed
+8633
-45
No files matched your search
@@ -0,0 +1,17 @@
|
||||
import { isCoolRound } from '../../../shared/cool'
|
||||
import { getCoolVoteTally, resetCoolRound } from '../../utils/cool-votes'
|
||||
|
||||
interface ResetBody {
|
||||
round?: unknown
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
setHeader(event, 'cache-control', 'no-store')
|
||||
const body = await readBody<ResetBody>(event)
|
||||
const round = body?.round ?? 'preselection'
|
||||
if (!isCoolRound(round)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid round. Expected preselection | final.' })
|
||||
}
|
||||
const state = resetCoolRound(round)
|
||||
return { state, tally: getCoolVoteTally(round) }
|
||||
})
|
||||
@@ -0,0 +1,12 @@
|
||||
import { getCoolState } from '../../utils/cool-votes'
|
||||
import { isCoolRound } from '../../../shared/cool'
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
setHeader(event, 'cache-control', 'no-store')
|
||||
const query = getQuery(event)
|
||||
const round = query.round === undefined ? 'preselection' : query.round
|
||||
if (!isCoolRound(round)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid round. Expected preselection | final.' })
|
||||
}
|
||||
return getCoolState(round)
|
||||
})
|
||||
@@ -0,0 +1,38 @@
|
||||
import { isCoolRouteId, isCoolRound, isCoolScene, isCoolTime } from '../../../shared/cool'
|
||||
import { updateCoolState } from '../../utils/cool-votes'
|
||||
|
||||
interface StateBody {
|
||||
round?: unknown
|
||||
time?: unknown
|
||||
selectedRoute?: unknown
|
||||
scene?: unknown
|
||||
voteOpen?: unknown
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
setHeader(event, 'cache-control', 'no-store')
|
||||
const body = await readBody<StateBody>(event)
|
||||
const round = body?.round ?? 'preselection'
|
||||
if (!isCoolRound(round)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid round. Expected preselection | final.' })
|
||||
}
|
||||
if (body.time !== undefined && !isCoolTime(body.time)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid time. Expected 10:00 | 12:00 | 14:00 | 16:00.' })
|
||||
}
|
||||
if (body.selectedRoute !== undefined && !isCoolRouteId(body.selectedRoute)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid route.' })
|
||||
}
|
||||
if (body.scene !== undefined && !isCoolScene(body.scene)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid scene.' })
|
||||
}
|
||||
if (body.voteOpen !== undefined && typeof body.voteOpen !== 'boolean') {
|
||||
throw createError({ statusCode: 400, statusMessage: 'voteOpen must be a boolean.' })
|
||||
}
|
||||
|
||||
return updateCoolState(round, {
|
||||
...(isCoolTime(body.time) ? { time: body.time } : {}),
|
||||
...(isCoolRouteId(body.selectedRoute) ? { selectedRoute: body.selectedRoute } : {}),
|
||||
...(isCoolScene(body.scene) ? { scene: body.scene } : {}),
|
||||
...(typeof body.voteOpen === 'boolean' ? { voteOpen: body.voteOpen } : {}),
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,12 @@
|
||||
import { isCoolRound } from '../../../shared/cool'
|
||||
import { getCoolVoteTally } from '../../utils/cool-votes'
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
setHeader(event, 'cache-control', 'no-store')
|
||||
const query = getQuery(event)
|
||||
const round = query.round === undefined ? 'preselection' : query.round
|
||||
if (!isCoolRound(round)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid round. Expected preselection | final.' })
|
||||
}
|
||||
return getCoolVoteTally(round)
|
||||
})
|
||||
@@ -0,0 +1,41 @@
|
||||
import { isCoolRouteId, isCoolRound } from '../../../shared/cool'
|
||||
import { getCoolState, isValidCoolSessionId, recordCoolVote } from '../../utils/cool-votes'
|
||||
|
||||
interface VoteBody {
|
||||
route?: unknown
|
||||
sessionId?: unknown
|
||||
round?: unknown
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
setHeader(event, 'cache-control', 'no-store')
|
||||
const body = await readBody<VoteBody>(event)
|
||||
const round = body?.round ?? 'preselection'
|
||||
|
||||
if (!isCoolRound(round)) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid round. Expected preselection | final.' })
|
||||
}
|
||||
if (!isCoolRouteId(body?.route)) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid route. Expected shortest | shaded | accessible.',
|
||||
})
|
||||
}
|
||||
if (!isValidCoolSessionId(body?.sessionId)) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid session. Expected an anonymous 8–80 character ID.',
|
||||
})
|
||||
}
|
||||
if (!getCoolState(round).voteOpen) {
|
||||
throw createError({ statusCode: 409, statusMessage: 'Voting is not open.' })
|
||||
}
|
||||
|
||||
const result = recordCoolVote(round, body.route, body.sessionId)
|
||||
setResponseStatus(event, result.duplicate ? 200 : 201)
|
||||
return {
|
||||
yourRoute: result.route,
|
||||
duplicate: result.duplicate,
|
||||
tally: result.tally,
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,121 @@
|
||||
import { isCoolRouteId, isCoolScene, isCoolTime } from '../../shared/cool'
|
||||
import type {
|
||||
CoolDemoState,
|
||||
CoolRouteId,
|
||||
CoolRound,
|
||||
CoolVoteCounts,
|
||||
CoolVoteTally,
|
||||
} from '../../shared/cool'
|
||||
|
||||
type StatePatch = Partial<Pick<CoolDemoState, 'time' | 'selectedRoute' | 'scene' | 'voteOpen'>>
|
||||
|
||||
const states = new Map<CoolRound, CoolDemoState>()
|
||||
const votes = new Map<CoolRound, Map<string, CoolRouteId>>()
|
||||
const counts = new Map<CoolRound, CoolVoteCounts>()
|
||||
let updatedAt = new Map<CoolRound, string | null>()
|
||||
|
||||
function blankState(round: CoolRound): CoolDemoState {
|
||||
return {
|
||||
round,
|
||||
time: '10:00',
|
||||
selectedRoute: 'shortest',
|
||||
scene: 'ruth',
|
||||
voteOpen: false,
|
||||
updatedAt: new Date().toISOString(),
|
||||
}
|
||||
}
|
||||
|
||||
function ensureVotes(round: CoolRound): Map<string, CoolRouteId> {
|
||||
let map = votes.get(round)
|
||||
if (!map) {
|
||||
map = new Map()
|
||||
votes.set(round, map)
|
||||
}
|
||||
return map
|
||||
}
|
||||
|
||||
function ensureCounts(round: CoolRound): CoolVoteCounts {
|
||||
let value = counts.get(round)
|
||||
if (!value) {
|
||||
value = { shortest: 0, shaded: 0, accessible: 0 }
|
||||
counts.set(round, value)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
export function getCoolState(round: CoolRound): CoolDemoState {
|
||||
let state = states.get(round)
|
||||
if (!state) {
|
||||
state = blankState(round)
|
||||
states.set(round, state)
|
||||
}
|
||||
return { ...state }
|
||||
}
|
||||
|
||||
export function updateCoolState(round: CoolRound, patch: StatePatch): CoolDemoState {
|
||||
const current = getCoolState(round)
|
||||
const next: CoolDemoState = {
|
||||
...current,
|
||||
...(isCoolTime(patch.time) ? { time: patch.time } : {}),
|
||||
...(isCoolRouteId(patch.selectedRoute) ? { selectedRoute: patch.selectedRoute } : {}),
|
||||
...(isCoolScene(patch.scene) ? { scene: patch.scene } : {}),
|
||||
...(typeof patch.voteOpen === 'boolean' ? { voteOpen: patch.voteOpen } : {}),
|
||||
updatedAt: new Date().toISOString(),
|
||||
}
|
||||
states.set(round, next)
|
||||
return { ...next }
|
||||
}
|
||||
|
||||
export function resetCoolRound(round: CoolRound): CoolDemoState {
|
||||
const next = blankState(round)
|
||||
states.set(round, next)
|
||||
votes.set(round, new Map())
|
||||
counts.set(round, { shortest: 0, shaded: 0, accessible: 0 })
|
||||
updatedAt = new Map([...updatedAt].filter(([key]) => key !== round))
|
||||
return { ...next }
|
||||
}
|
||||
|
||||
export function getCoolVoteTally(round: CoolRound): CoolVoteTally {
|
||||
return {
|
||||
round,
|
||||
counts: { ...ensureCounts(round) },
|
||||
total: ensureVotes(round).size,
|
||||
updatedAt: updatedAt.get(round) ?? null,
|
||||
}
|
||||
}
|
||||
|
||||
export function recordCoolVote(
|
||||
round: CoolRound,
|
||||
route: CoolRouteId,
|
||||
sessionId: string,
|
||||
): { duplicate: boolean; route: CoolRouteId; tally: CoolVoteTally } {
|
||||
const state = getCoolState(round)
|
||||
if (!state.voteOpen) {
|
||||
throw new Error('Voting is not open')
|
||||
}
|
||||
|
||||
const roundVotes = ensureVotes(round)
|
||||
const existing = roundVotes.get(sessionId)
|
||||
if (existing) {
|
||||
return { duplicate: true, route: existing, tally: getCoolVoteTally(round) }
|
||||
}
|
||||
|
||||
if (!isCoolRouteId(route)) throw new Error(`Invalid route: ${route}`)
|
||||
roundVotes.set(sessionId, route)
|
||||
const current = ensureCounts(round)
|
||||
counts.set(round, { ...current, [route]: current[route] + 1 })
|
||||
updatedAt.set(round, new Date().toISOString())
|
||||
return { duplicate: false, route, tally: getCoolVoteTally(round) }
|
||||
}
|
||||
|
||||
export function isValidCoolSessionId(value: unknown): value is string {
|
||||
return typeof value === 'string' && /^[A-Za-z0-9_-]{8,80}$/.test(value)
|
||||
}
|
||||
|
||||
/** Test helper only. */
|
||||
export function resetCoolDemoStore(): void {
|
||||
states.clear()
|
||||
votes.clear()
|
||||
counts.clear()
|
||||
updatedAt = new Map()
|
||||
}
|
||||
Reference in new issue
Block a user