Sega-Channel/server/rotation.mjs
orca 065647af6d Server version, lineups and pixel-art screens; deployed on the OptiPlex
Commits the in-progress work that was sitting in the tree: the Node server
(server/), Dockerfile, lineups page, new pixel-art components and themes,
with the old library/ROM loaders and render.yaml removed. The README now
points at cyberdeck/optiplex/segachannel/deploy.py (the home server moved
from the Mac mini to an OptiPlex).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 20:02:16 -05:00

93 lines
3.6 KiB
JavaScript

// The channel's rotating lineup. Like the later Sega Channel, the lineup changes twice a month (the 1st and
// the 15th) and the rooms stay fixed while games move through them.
//
// Each game's air dates are worked out from its id and the window number alone, so every device sees the same
// lineup, nothing has to be stored, and adding a ROM doesn't reshuffle the others: a game is on air when
// hash(id, window) < size / library size. With ~50 of 150 games on air, about two thirds of the lineup is new
// each window (the real channel replaced about 75% a month).
//
// When the calendar lines up with one of the eight lineups archived from sega.com (1996-97), the games you
// have from that real lineup are always on air too, tagged "classic".
import { createHash } from 'node:crypto'
/** Window number: two per month, counted from year 0. */
export function windowIndex(date) {
return (date.getFullYear() * 12 + date.getMonth()) * 2 + (date.getDate() >= 15 ? 1 : 0)
}
export function windowBounds(n) {
const year = Math.floor(n / 24)
const month = Math.floor(n / 2) % 12
const second = n % 2 === 1
const last = new Date(year, month + 1, 0).getDate()
return {
start: new Date(year, month, second ? 15 : 1),
end: new Date(year, month, second ? last : 14),
}
}
const iso = d => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
function hash(id, n) {
return createHash('sha1').update(`${id}:${n}`).digest().readUInt32BE(0) / 2 ** 32
}
// Archived period index (see data/sega-channel-lineup.json) -> [month, half] slots it covers, by where most of
// its days fall. The first two were posted as whole-month schedules.
const ARCHIVE_SLOTS = {
0: [[11, 0], [11, 1]], // December 1996
1: [[1, 0], [1, 1]], // February 1997
2: [[1, 1]], // February 14 - 27
3: [[2, 1]], // March 14 - 27
4: [[3, 0]], // March 28 - April 10
5: [[4, 0]], // April 25 - May 8
6: [[4, 1]], // May 23 - June 5
7: [[5, 1]], // June 20 - July 3
}
/** Archive periods that fall in this window's slot of the year. */
function archivePeriods(n) {
const month = Math.floor(n / 2) % 12
const half = n % 2
return Object.entries(ARCHIVE_SLOTS)
.filter(([, slots]) => slots.some(([m, h]) => m === month && h === half))
.map(([p]) => Number(p))
}
/**
* Picks the window's lineup. `games` need id, room and lineup ({ p } or null).
* Returns { onAir: Set<id>, classic: Set<id>, periods: number[] }.
*/
export function pickLineup(games, n, { size = 50, minPerRoom = 3 } = {}) {
const p = Math.min(1, size / Math.max(1, games.length))
const scored = games.map(g => ({ g, h: hash(g.id, n) }))
const onAir = new Set(scored.filter(s => s.h < p).map(s => s.g.id))
// Every room gets a few games, even when the dice leave it nearly empty.
const byRoom = new Map()
for (const s of scored)
byRoom.set(s.g.room, [...(byRoom.get(s.g.room) ?? []), s])
for (const list of byRoom.values()) {
let have = list.filter(s => onAir.has(s.g.id)).length
for (const s of list.sort((a, b) => a.h - b.h)) {
if (have >= minPerRoom)
break
if (!onAir.has(s.g.id)) {
onAir.add(s.g.id)
have++
}
}
}
const periods = archivePeriods(n)
const classic = new Set(games.filter(g => g.lineup?.p.some(i => periods.includes(i))).map(g => g.id))
for (const id of classic)
onAir.add(id)
return { onAir, classic, periods }
}
export function describeWindow(n) {
const { start, end } = windowBounds(n)
const next = windowBounds(n + 1).start
return { n, start: iso(start), end: iso(end), next: iso(next) }
}