staging-tester / ocean-landing-serve / src/ lib/ error-scenes.ts

// Each error page draws its status as solid numerals standing on a dock, every 0 a life ring, with one prop
// beside it that tells what happened. Shapes are filled from the same density ramp as the ocean, so the scene
// reads as dense as the hero water. The last row sits on the ocean strip's wave line.

const H = 20;
const W = 96;
const DECK = H - 4;

const hash = (x: number, y: number) => {
	const n = Math.sin(x * 12.9898 + y * 78.233) * 43758.5453;
	return n - Math.floor(n);
};

// 7x11 bitmaps, each pixel two columns wide so it draws square. In the 0, `o` marks the life ring's light bands.
const digits: Record<string, string[]> = {
	0: ['..ooo..', '.#ooo#.', '##...##', '##...##', 'oo...oo', 'oo...oo', 'oo...oo', '##...##', '##...##', '.#ooo#.', '..ooo..'],
	1: ['...##..', '..###..', '.####..', '...##..', '...##..', '...##..', '...##..', '...##..', '...##..', '...##..', '.######'],
	2: ['.#####.', '##...##', '.....##', '.....##', '....##.', '...##..', '..##...', '.##....', '##.....', '##.....', '#######'],
	3: ['.#####.', '##...##', '.....##', '.....##', '..####.', '.....##', '.....##', '.....##', '.....##', '##...##', '.#####.'],
	4: ['....##.', '...###.', '..####.', '.##.##.', '##..##.', '##..##.', '#######', '....##.', '....##.', '....##.', '....##.'],
	5: ['#######', '##.....', '##.....', '##.....', '######.', '.....##', '.....##', '.....##', '.....##', '##...##', '.#####.'],
	6: ['..####.', '.##....', '##.....', '##.....', '######.', '##...##', '##...##', '##...##', '##...##', '##...##', '.#####.'],
	7: ['#######', '.....##', '.....##', '....##.', '....##.', '...##..', '...##..', '..##...', '..##...', '..##...', '..##...'],
	8: ['.#####.', '##...##', '##...##', '##...##', '.#####.', '##...##', '##...##', '##...##', '##...##', '##...##', '.#####.'],
	9: ['.#####.', '##...##', '##...##', '##...##', '##...##', '.######', '.....##', '.....##', '.....##', '....##.', '.####..']
};

// In prop art, `@` is solid fill, `+` is light fill (sails, glass, stripes) and every other character is drawn as is.
type Prop = { art: string[]; on: 'water' | 'deck'; beam?: number };

const props = {
	// The boat that should be in this slip, already drifting off.
	gone: {
		on: 'water',
		art: [
			'       |\\',
			'       |+\\',
			'       |++\\',
			'       |+++\\',
			'       |++++\\',
			'       |+++++\\',
			'       |++++++\\',
			'       |+++++++\\',
			' ______|________\\___',
			' \\@@@@@@@@@@@@@@@@@/',
			'  \\@@@@@@@@@@@@@@@/'
		]
	},
	// A striped lighthouse on its rock, door shut: only keepers climb.
	keepers: {
		on: 'water',
		beam: 1,
		art: [
			'     _/\\_',
			'    |++++|',
			'   _|____|_',
			'    |@@@@|',
			'    |++++|',
			'    |@@@@|',
			'    |++++|',
			'    |@@@@|',
			'    |@[]@|',
			'  __|@@@@|__',
			' /@@@@@@@@@@\\',
			'/@@@@@@@@@@@@\\'
		]
	},
	// A chained gangway with a sign hung from it.
	chained: {
		on: 'deck',
		art: [
			' _                  _',
			'(@)                (@)',
			'|@|o=o=o=o=o=o=o=o=|@|',
			'|@|     |    |     |@|',
			'|@|  ___|____|___  |@|',
			'|@| |  private   | |@|',
			'|@| |____________| |@|',
			'|@|                |@|'
		]
	},
	// Boats waiting their turn at the dock.
	queue: {
		on: 'water',
		art: [
			'   |\\         |\\         |\\',
			'   |+\\        |+\\        |+\\',
			'   |++\\       |++\\       |++\\',
			'   |+++\\      |+++\\      |+++\\',
			' __|____\\__ __|____\\__ __|____\\__',
			' \\@@@@@@@@/ \\@@@@@@@@/ \\@@@@@@@@/'
		]
	},
	// Capsized, with two sailors standing on the hull and waving.
	capsized: {
		on: 'water',
		art: [
			'          o/    \\o',
			'         /|      |\\',
			'         / \\    / \\',
			'   ______________|_______',
			'  /@@@@@@@@@@@@@@@@@@@@@@\\',
			' /@@@@@@@@@@@@@@@@@@@@@@@@\\',
			'|++++++++++++++++++++++++++|'
		]
	},
	// Hauled out on stands for repairs.
	hauled: {
		on: 'deck',
		art: [
			'        |\\',
			'        |+\\',
			'        |++\\',
			'        |+++\\',
			'        |++++\\',
			'  ______|_____\\____',
			'  \\@@@@@@@@@@@@@@@/',
			'   \\@@@@@@@@@@@@@/',
			'    /\\         /\\',
			'   /  \\       /  \\'
		]
	},
	// A message in a bottle, for everything else.
	bottle: {
		on: 'water',
		art: [
			'  .-""""""""""""""-.',
			' /++++++++++++++++++\\___',
			'|++ ~~ ~~~ ~~ ~ +++++|_@@|',
			' \\++++++++++++++++++/',
			"  '-..............-'"
		]
	}
} satisfies Record<string, Prop>;

const clouds = [
	['      .--.', '   .-(::::)-.', '  (::::::::::)', "   `--------'"],
	['    .-.', ' .-(:::)--.', '(:::::::::)', " `-------'"]
];

const propFor = (status: number): Prop =>
	status === 404 ? props.gone
	: status === 401 ? props.keepers
	: status === 403 ? props.chained
	: status === 429 ? props.queue
	: status === 503 ? props.hauled
	: status >= 500 ? props.capsized
	: props.bottle;

const SOLID = 1;
const LIGHT = 2;
const SHADE = 3;

const solid = '=+*#%@';
const light = '.:-=+';

export const scene = (status: number) => {
	const g = Array.from({ length: H }, () => Array<string>(W).fill(' '));
	const fill = Array.from({ length: H }, () => Array<number>(W).fill(0));
	const set = (r: number, c: number, ch: string, kind = 0) => {
		if (g[r]?.[c] === undefined) return;
		g[r][c] = ch;
		fill[r]![c] = kind;
	};
	const empty = (r: number, c: number) => g[r]?.[c] === ' ';

	// Numerals, standing on the deck with a one-cell extrusion down and to the right.
	let x = 4;
	const stems: number[] = [];
	for (const d of String(status)) {
		const glyph = digits[d] ?? digits[0]!;
		const top = DECK - glyph.length;
		glyph.forEach((line, i) =>
			[...line].forEach((p, j) => {
				if (p === '.') return;
				for (const k of [0, 1]) set(top + i, x + j * 2 + k, '#', p === 'o' ? LIGHT : SOLID);
			})
		);
		glyph.forEach((line, i) =>
			[...line].forEach((p, j) => {
				if (p === '.') return;
				for (const k of [1, 2]) if (top + i + 1 < DECK && empty(top + i + 1, x + j * 2 + k)) set(top + i + 1, x + j * 2 + k, ':', SHADE);
			})
		);
		// A numeral that ends in a stem (the 4) carries it down through the deck as a piling.
		const foot = glyph[glyph.length - 1]!;
		if (foot.replaceAll('.', '') === '##') stems.push(x + foot.indexOf('#') * 2);
		x += 14 + 4;
	}

	const prop = propFor(status);
	const width = Math.max(...prop.art.map((l) => l.length));
	let right = x - 3;
	const px = prop.on === 'deck' ? right + 3 : Math.min(right + 8, W - width - 1);
	if (prop.on === 'deck') right = px + width + 1;

	// The dock: a board row, a solid beam, shade beneath it and pilings into the water.
	const left = 2;
	const isStem = (c: number) => stems.some((s) => c >= s && c <= s + 3);
	const pile = (c: number) => isStem(c) || (!stems.some((s) => c >= s - 3 && c <= s + 6) && ((c - left) % 9 < 2 || c >= right - 1));
	for (let c = left; c <= right; c++) {
		set(DECK, c, '=');
		set(DECK + 1, c, '#', SOLID);
		set(DECK + 2, c, pile(c) ? '#' : ':', pile(c) ? SOLID : SHADE);
		if (pile(c)) set(DECK + 3, c, '#', SOLID);
	}

	const bottom = prop.on === 'deck' ? DECK - 1 : H - 1;
	prop.art.forEach((line, i) => {
		const r = bottom - prop.art.length + 1 + i;
		[...line].forEach((ch, c) => {
			if (ch === ' ') return;
			set(r, px + c, ch === '@' || ch === '+' ? '#' : ch, ch === '@' ? SOLID : ch === '+' ? LIGHT : 0);
		});
	});
	if (prop.beam !== undefined) {
		const r = bottom - prop.art.length + 1 + prop.beam;
		for (let c = right + 3; c < px + 3; c++) if (empty(r, c)) set(r, c, c % 2 ? '-' : '=');
	}

	for (const [i, [r, c]] of [
		[0, 8],
		[1, 44]
	].entries()) {
		clouds[i]!.forEach((line, j) =>
			[...line].forEach((ch, k) => {
				if (ch !== ' ' && empty(r + j, c + k)) set(r + j, c + k, ch, ch === ':' ? LIGHT : 0);
			})
		);
	}

	// Gulls, wherever the sky is clear around them.
	const clear = (r: number, c: number) => [-1, 0, 1].every((dr) => [-2, -1, 0, 1, 2].every((dc) => (g[r + dr]?.[c + dc] ?? ' ') === ' '));
	for (const [r, c] of [
		[1, 30],
		[0, 34],
		[2, 64],
		[1, 70],
		[3, 76]
	]) {
		if (clear(r!, c!)) set(r!, c!, 'v');
	}

	// Shade every filled cell from the ramp: lit along top and left edges, darkest on the right, with grain.
	const filled = (r: number, c: number, kind: number) => fill[r]?.[c] === kind;
	for (let r = 0; r < H; r++)
		for (let c = 0; c < W; c++) {
			const kind = fill[r]![c]!;
			if (kind === 0) continue;
			const grain = (hash(c, r + status) - 0.5) * 0.22;
			if (kind === SHADE) {
				g[r]![c] = hash(c, r) > 0.35 ? ':' : '.';
				continue;
			}
			const edge = (filled(r - 1, c, kind) ? 0 : -0.3) + (filled(r, c - 1, kind) ? 0 : -0.12) + (filled(r, c + 1, kind) ? 0 : 0.18) + (filled(r + 1, c, kind) ? 0 : 0.08);
			const ramp = kind === SOLID ? solid : light;
			const v = (kind === SOLID ? 0.72 : 0.55) + edge + grain;
			g[r]![c] = ramp[Math.max(0, Math.min(ramp.length - 1, Math.floor(v * ramp.length)))]!;
		}

	return g
		.map((row) => row.join('').trimEnd())
		.join('\n')
		.replace(/^\n+/, '');
};