BBS/src/doors/starlane/screens/sysop.ts

70 lines
3.2 KiB
TypeScript

import type { Session } from '../../../session/Session';
import { withTx } from '../repo/q';
import { allPilots, getPilot, savePilot } from '../repo/players';
import { bigBang, worldStats, getUniverse } from '../repo/world';
import { addNews } from '../repo/news';
import { runTick } from '../ticker';
import { BIG_BANG } from '../art/scenes';
import { header, field, commas, esc, readNumber, pickFromList, art, confirm } from './ui';
export async function sysopMenu(s: Session): Promise<void> {
for (;;) {
const st = await worldStats();
const u = getUniverse();
await s.println();
await s.println(header('STARLANE Sysop', `|07Epoch |15${u.epoch}`));
await s.println(field('Universe', `|15${commas(st.sectors)}|07 sectors, ${commas(st.ports)} ports, seed ${u.seed}, ${u.turns_per_day} turns/day`));
await s.println(field('Population', `|15${st.pilots}|07 pilots, ${st.planets} planets, ${st.reavers} Reavers, ${st.syndicates} syndicates`));
const k = await s.promptChoice(
'|08 [|15E|08]|07dit pilot |08[|15T|08]|07ick now |08[|15P|08]|07ost news |08[|15B|08]|07ig Bang |08[|15Q|08]|07uit|08: |07',
'ETPBQ\r',
);
await s.println();
if (k === 'Q' || k === '\r') return;
if (k === 'T') {
await runTick();
await s.println(' |10World tick complete.');
}
if (k === 'P') {
await s.print('|03 Headline: ');
const t = (await s.readLine({ maxLen: 70 })).trim();
if (t) await addNews(`|14SYSOP:|07 ${esc(t)}`);
}
if (k === 'E') await editPilot(s);
if (k === 'B') await doBigBang(s);
}
}
async function editPilot(s: Session): Promise<void> {
const pilots = await allPilots();
const i = await pickFromList(s, 'Pilot', pilots.slice(0, 18).map((p) => `|11${esc(p.handle)} |08${commas(p.credits)} cr, ${p.turns_left} turns, sector ${p.sector}`));
if (i === null) return;
const uid = pilots[i].user_id;
await s.print('|03 Set credits |08(Enter=keep)|03: ');
const cr = await readNumber(s, 0, 2_000_000_000, null, 11);
await s.print('|03 Set turns left |08(Enter=keep)|03: ');
const tu = await readNumber(s, 0, 100000, null, 6);
await withTx(async (c) => {
const p = (await getPilot(c, uid, true))!;
if (cr !== null) p.credits = cr;
if (tu !== null) p.turns_left = tu;
await savePilot(c, p);
});
await s.println(' |10Saved.');
}
async function doBigBang(s: Session): Promise<void> {
await s.println(' |12This destroys every pilot, planet, syndicate and port and builds a new universe.');
await s.print(`|03 Sectors |08(100-5000, Enter=${getUniverse().size})|03: `);
const size = (await readNumber(s, 100, 5000, getUniverse().size, 5))!;
await s.print(`|03 Turns per day |08(10-5000, Enter=${getUniverse().turns_per_day})|03: `);
const turns = (await readNumber(s, 10, 5000, getUniverse().turns_per_day, 5))!;
if (!(await confirm(s, ' Are you absolutely sure?', false))) return;
await s.print('|12 Type BIGBANG to confirm: ');
if ((await s.readLine({ maxLen: 10 })).trim() !== 'BIGBANG') return;
await s.println(' |07Collapsing the old universe...');
await bigBang(size, turns);
await art(s, BIG_BANG);
await s.println(' |10A new frontier is born. All pilots start fresh.');
}