Public: anyone can read this; the crew can change it.
Game pages: the address a friend is given works from outside, and the last game words say what they mean #17672
Merged 10h 58m ago, live in production
scout/forge:scout/17670-game-pages-the-address-a-friend-is-given → main 0 commits fork 2 threads · 2 unresolved
23 files
changed +355
−97
- docs/features/game-settings.md
- docs/features/games.md
- src/games/ask.ts
- src/games/chat.ts
- src/games/gist.ts
- src/games/mods.ts
- src/games/player_events.ts
- src/games/presets.ts
- src/games/settings.ts
- src/games/sleep.ts
- src/http/mcp/tools/games.ts
- src/tui/screens/game_mods.ts
- src/tui/screens/games.ts
- src/web/pages/games.ts
- test/game-chat.test.ts
- test/game-door.test.ts
- test/game-gist.test.ts
- test/game-join.test.ts
- test/game-mods.test.ts
- test/game-presets.test.ts
- test/game-settings.test.ts
- test/game-sleep.test.ts
- test/tui-games.test.ts
docs/features/game-settings.md +1 −1
| @@ -8,7 +8,7 @@ row: Game settings | ||
| 8 | 8 | |
| 9 | 9 | ## A change |
| 10 | 10 | |
| 11 | Change values and press **Preview**. The preview says what each change writes, for example `VIEW_DISTANCE 10 → 12 (env)` or `paper-world-defaults entities.spawning.per-player-mob-spawns true → false (patch)`, and that the server restarts. A value out of range, not one of the choices, or a key that cannot be changed here is refused with the reason. **Commit and restart** makes one commit, `games: <service> settings: <keys>`, on the commit you previewed. If the branch moved since, nothing is committed and the forge asks you to look again. The box applies it on its next sync, as it does an env change. The commit needs write access to the compose repository, as an env change does. | |
| 11 | Change values and press **Preview**. The preview says what each change writes, for example `VIEW_DISTANCE 10 → 12 (env)` or `paper-world-defaults entities.spawning.per-player-mob-spawns true → false (patch)`, and that the server restarts. A value out of range, not one of the choices, or a key that cannot be changed here is refused with the reason. **Save and restart** makes one commit, `games: <service> settings: <keys>`, on the commit you previewed. If the branch moved since, nothing is committed and the forge asks you to look again. The box applies it on its next check-in, as it does an env change. The commit needs write access to the compose repository, as an env change does. | |
| 12 | 12 | |
| 13 | 13 | ## Where a value goes |
| 14 | 14 |
docs/features/games.md +11 −3
| @@ -19,6 +19,14 @@ The agent sends every string plain: terminal colour escapes and control bytes (w | ||
| 19 | 19 | |
| 20 | 20 | The agent keeps one firewall rule that closes the cloud's metadata address to the box's containers, looked for at each start and each sync. A box whose firewall refuses it says so among its status lines. |
| 21 | 21 | |
| 22 | ## Words on the pages | |
| 23 | ||
| 24 | The page, the terminal and this file say these the same way: **Preview it** (was "Plan it"): shows what a new server would add; nothing changes until Create it. **Save change** (was "Commit", the environment form): saved in the network's file; the box applies it at its next **check-in** (was "sync"), a few seconds on. **Keep it running** (was "Keep it warm"): the server runs whether or not anyone is on it. The proxy's **list of servers new players land on first** (was "try list"). | |
| 25 | ||
| 26 | ## Where a friend joins | |
| 27 | ||
| 28 | `/games/<box>` and the terminal's Games screen give the machine's public name (else its public IP address), with `:port` added when the proxy publishes the game's 25565 on another port (`proxyPort`, `src/games/sleep.ts`, read from the compose file). When the only name is a private one (a tailnet or LAN name, or a private IP address) the page says a friend outside cannot join with it and to give the machine a public name on the Infra page; it is not offered for Copy. With no machine name it says there is no address yet (`joinAddress`, `src/games/gist.ts`). | |
| 29 | ||
| 22 | 30 | ## Sleep and wake |
| 23 | 31 | |
| 24 | 32 | Each game server is warm or sleeps when empty. A sleeping server container stops after 10 minutes with nobody on it, which frees all its memory (not the bill: the machine it runs on is billed in full, asleep or not), and starts again when a player joins through the proxy: the player waits on the join screen while it boots (measured 13.9 to 15.2 s for Paper 1.21.6 once its world exists, 30 s on its first boot). The Minecraft client gives up on a silent join after about 30 s, so a boot that takes longer means one rejoin. |
| @@ -62,14 +70,14 @@ Uploading a world file is still to come: a link is how a world comes in today. | ||
| 62 | 70 | |
| 63 | 71 | ## New servers |
| 64 | 72 | |
| 65 | "Give me a new void world with FAWE and bedwars" makes a server (#17284 G6), on `/games/<box>` (New server: words, Plan it, then Create it), with `n` on the terminal Games screen, through the `games` tool `create_server` (`words`, or `name`, `level` and `plugins`; it plans, and commits with `dryRun: false` and the plan's `sha`) or from Talk. Every door commits only on the commit its plan was made on: when the repository moved, it says "The box changed since the preview; plan it again." The plan says, before anything is written: | |
| 73 | "Give me a new void world with FAWE and bedwars" makes a server (#17284 G6), on `/games/<box>` (New server: words, Preview it, then Create it), with `n` on the terminal Games screen, through the `games` tool `create_server` (`words`, or `name`, `level` and `plugins`; it plans, and commits with `dryRun: false` and the plan's `sha`) or from Talk. Every door commits only on the commit its plan was made on: when the repository moved, it says "The box changed since the preview; preview it again." The plan says, before anything is written: | |
| 66 | 74 | |
| 67 | 75 | - the server: Paper at the network's own version, its world (void: one layer of air in the void biome; flat; or normal), its memory, sleeping when empty and waking on join; |
| 68 | 76 | - each plugin resolved for its loader and version and pinned in its lockfile with what it requires, or refused if it does not run there (`fawe` and `worldedit` are FastAsyncWorldEdit, `bedwars` is Screaming BedWars); |
| 69 | 77 | - the memory math, a sleeping server's heap counted free: "velocity 0.8G + hub 1G + survival 3G = 4.8G awake; bedwars 1.5G sleep, counted free; the box has 7.7G, 1G kept for itself: fits", refused when it does not; |
| 70 | 78 | - where players join: `<name>.<the network's domain>` (the wildcard name already points at the box) or `/server <name>`. |
| 71 | 79 | |
| 72 | Creating it is one commit to the network's compose file: the service from the Paper anchor with its own RCON key, a volume, the proxy's `[servers]` and `[forced-hosts]` lines, sleep's label and waker lists, and its lockfile beside it. Remove on the server's row, `d` on the terminal Games screen, or `remove_server` takes the service and its proxy lines back out by commit, its plan shown first, and keeps its world's volume (`src/games/presets.ts`). The proxy, the waker, and a server the proxy's `try` list or another forced host still names are refused. | |
| 80 | Creating it is one commit to the network's compose file: the service from the Paper anchor with its own RCON key, a volume, the proxy's `[servers]` and `[forced-hosts]` lines, sleep's label and waker lists, and its lockfile beside it. Remove on the server's row, `d` on the terminal Games screen, or `remove_server` takes the service and its proxy lines back out by commit, its plan shown first, and keeps its world's volume (`src/games/presets.ts`). The proxy, the waker, and a server the proxy's list of servers new players land on first (its `try` line) or another forced host still names are refused. | |
| 73 | 81 | |
| 74 | 82 | ## From in the game |
| 75 | 83 | |
| @@ -79,7 +87,7 @@ A linked name that holds any role on the server it types on, viewer too, can ask | ||
| 79 | 87 | |
| 80 | 88 | A linked name that holds operator on the server it types on can also say: |
| 81 | 89 | |
| 82 | - `!forge give me a new void world with FAWE and bedwars`: planned like `create_server` (see New servers), and the plan's lines and memory math come back in chat, then "type !forge yes within 5 minutes". `!forge yes` commits that plan as the linked account, or says "The box changed since the preview; plan it again." when the repository moved; `!forge no` drops it. A plan answers only the same name on the same server. It changes the whole network, so it takes operator of the box, asked at the plan, at the yes and again right before the commit; operator of one server is refused. | |
| 90 | - `!forge give me a new void world with FAWE and bedwars`: planned like `create_server` (see New servers), and the plan's lines and memory math come back in chat, then "type !forge yes within 5 minutes". `!forge yes` commits that plan as the linked account, or says "The box changed since the preview; preview it again." when the repository moved; `!forge no` drops it. A plan answers only the same name on the same server. It changes the whole network, so it takes operator of the box, asked at the plan, at the yes and again right before the commit; operator of one server is refused. | |
| 83 | 91 | - `!forge help`. |
| 84 | 92 | - `!forge <anything else>`: a new Talk conversation of the linked account. The crew's first answer comes back in chat (its first lines, then a link to the conversation): nothing the account could not read on the forge itself. |
| 85 | 93 |
src/games/ask.ts +3 −5
| @@ -13,7 +13,7 @@ import { composeServices, serviceEnv } from "./compose.ts"; | ||
| 13 | 13 | import { peopleOf } from "./people.ts"; |
| 14 | 14 | import { needLines, whyLines } from "./player_events.ts"; |
| 15 | 15 | import { forcedHosts } from "./presets.ts"; |
| 16 | import { proxyOf } from "./sleep.ts"; | |
| 16 | import { proxyOf, proxyPort } from "./sleep.ts"; | |
| 17 | 17 | import { mayDo, roleOn, servicesOf, type GameBox } from "./store.ts"; |
| 18 | 18 | |
| 19 | 19 | export type Question = |
| @@ -84,10 +84,8 @@ export async function answerTo(app: App, user: User, box: GameBox, compose: stri | ||
| 84 | 84 | function addressLines(compose: string, seen: readonly string[]): string[] { |
| 85 | 85 | const hosts = forcedHosts(compose).filter((h) => seen.includes(h.server)); |
| 86 | 86 | if (hosts.length === 0) return ["This network's file names no address. Use the one you joined with."]; |
| 87 | const proxy = proxyOf(compose); | |
| 88 | const ports = (Bun.YAML.parse(compose) as { services: Record<string, { ports?: unknown }> }).services[proxy ?? ""]?.ports; | |
| 89 | const published = Array.isArray(ports) ? ports.map((p) => /^(?:[\d.]+:)?(\d+):25565(?:\/tcp)?$/.exec(String(p))?.[1]).find((p) => p !== undefined) : undefined; | |
| 90 | const port = published === undefined || published === "25565" ? "" : `:${published}`; | |
| 87 | const published = proxyPort(compose); | |
| 88 | const port = published === 25565 ? "" : `:${String(published)}`; | |
| 91 | 89 | const [main, ...rest] = hosts; |
| 92 | 90 | return [`Address: ${main!.host}${port}`, ...rest.map((h) => `${h.server}: ${h.host}${port}`)]; |
| 93 | 91 | } |
src/games/chat.ts +3 −3
| @@ -536,7 +536,7 @@ export function runChat(app: App, boxId: string): Promise<void> { | ||
| 536 | 536 | } |
| 537 | 537 | } |
| 538 | 538 | } catch (e) { |
| 539 | if (!app.closing.signal.aborted) app.log.error("!forge commands stopped until the next sync", { error: String(e) }); | |
| 539 | if (!app.closing.signal.aborted) app.log.error("!forge commands stopped until the next check-in", { error: String(e) }); | |
| 540 | 540 | } finally { |
| 541 | 541 | boxes.delete(boxId); |
| 542 | 542 | } |
| @@ -604,7 +604,7 @@ async function runJob(app: App, box: GameBox, job: Job): Promise<void> { | ||
| 604 | 604 | if (job.name !== "") { |
| 605 | 605 | const compose = await readCompose(app, box); |
| 606 | 606 | if (compose !== undefined && compose.sha !== job.sha && composeServices(compose.text).includes(job.name)) |
| 607 | return finish(app, box, job, `built ${compose.sha.slice(0, 10)}`, [`${job.name} is in the network. The box brings it up on its next sync.`], "green"); | |
| 607 | return finish(app, box, job, `built ${compose.sha.slice(0, 10)}`, [`${job.name} is in the network. The box brings it up on its next check-in.`], "green"); | |
| 608 | 608 | } |
| 609 | 609 | const wish = await wishOn(app, box, job.words); |
| 610 | 610 | const planned = await planServer(app, box, wish); |
| @@ -615,7 +615,7 @@ async function runJob(app: App, box: GameBox, job: Job): Promise<void> { | ||
| 615 | 615 | db.query("UPDATE game_chat_job SET name = ?2 WHERE id = ?1").run(job.id, wish.name); |
| 616 | 616 | const done = await commitServer(app, still, box, planned.value, job.sha); |
| 617 | 617 | if (!done.ok) return finish(app, box, job, done.error, [`Not built: ${done.error}`], "red"); |
| 618 | finish(app, box, job, `built ${done.sha.slice(0, 10)}`, [`${planned.value.done}. The box brings it up on its next sync.`], "green"); | |
| 618 | finish(app, box, job, `built ${done.sha.slice(0, 10)}`, [`${planned.value.done}. The box brings it up on its next check-in.`], "green"); | |
| 619 | 619 | } |
| 620 | 620 | |
| 621 | 621 | /** |
src/games/gist.ts +45 −3
| @@ -6,6 +6,8 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import type { Database } from "bun:sqlite"; |
| 9 | import { isIP } from "node:net"; | |
| 10 | import { isPublicAddress } from "../apps/egress.ts"; | |
| 9 | 11 | import { peopleOf } from "./people.ts"; |
| 10 | 12 | import type { GameService } from "./store.ts"; |
| 11 | 13 | |
| @@ -51,11 +53,51 @@ export function nowWords(s: GameService | undefined, pending: string | undefined | ||
| 51 | 53 | /** The one button a server leads with: Start while it is not running, else Restart. */ |
| 52 | 54 | export const mainPower = (s: GameService | undefined): "start" | "restart" => (s?.state === "running" && s.asleep !== true ? "restart" : "start"); |
| 53 | 55 | |
| 56 | const NAME = /^[a-z0-9][a-z0-9.-]{0,252}$/i; | |
| 57 | /** Names that only resolve inside a network: a tailnet, a LAN, a router's own. */ | |
| 58 | const PRIVATE_NAME = /(^[^.]+$|\.(ts\.net|local|lan|internal|home\.arpa|localdomain|localhost|intranet|corp|home)$)/i; | |
| 59 | ||
| 54 | 60 | /** |
| 55 | * Where a friend joins: the box's machine name (the game's own port, 25565, is the default), or | |
| 56 | * none yet. | |
| 61 | * Whether a friend outside can reach this name: a public IP address, or a name with a public | |
| 62 | * ending. | |
| 57 | 63 | */ |
| 58 | export const joinAddress = (host: string | undefined): string | undefined => (host !== undefined && /^[a-z0-9][a-z0-9.-]{0,252}$/i.test(host) ? host : undefined); | |
| 64 | function reachable(host: string): boolean { | |
| 65 | if (isIP(host) !== 0) return isPublicAddress(host); | |
| 66 | return NAME.test(host) && !PRIVATE_NAME.test(host) && !/^\d+(\.\d+){3}$/.test(host); | |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * Where a friend joins: an address to hand out, or the one private name there is (not to be | |
| 71 | * offered). | |
| 72 | */ | |
| 73 | export type Join = { readonly address: string } | { readonly private: string }; | |
| 74 | ||
| 75 | /** | |
| 76 | * Where a friend joins, from the box's machine records (its public name, else its public address): | |
| 77 | * the name, then `:port` unless it is the game's own 25565. When the only name is a private one | |
| 78 | * (tailnet, LAN, private IP) it comes back as `private`, never as an address. None when the machine | |
| 79 | * has no name. | |
| 80 | */ | |
| 81 | export function joinAddress(machine: { readonly host: string; readonly address: string } | undefined, port = 25565): Join | undefined { | |
| 82 | const names = machine === undefined ? [] : [machine.host, machine.address].filter((h) => NAME.test(h) || isIP(h) !== 0); | |
| 83 | const open = names.find(reachable); | |
| 84 | if (open !== undefined) return { address: `${isIP(open) === 6 ? `[${open}]` : open}${port === 25565 ? "" : `:${String(port)}`}` }; | |
| 85 | return names[0] === undefined ? undefined : { private: names[0] }; | |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * What to tell the operator when the only name is private: why it is not offered, and what to set. | |
| 90 | */ | |
| 91 | export const privateWords = (host: string): string => | |
| 92 | `The machine's only name is ${host}, a private one (a tailnet or LAN name, or a private IP address): a friend outside cannot join with it, so it is not offered to copy. Give the machine a public name or its public IP address on the Infra page, then it shows here.`; | |
| 93 | ||
| 94 | /** What Save change does, beside the button on the page and the prompt in the terminal. */ | |
| 95 | export const SAVE_WORDS = "Saved in the network's file; the box applies it at its next check-in, a few seconds on."; | |
| 96 | /** What Preview it does, beside its button and in the terminal. */ | |
| 97 | export const PREVIEW_WORDS = "Shows what would be added; nothing changes until you press Create it."; | |
| 98 | ||
| 99 | /** The words when the box has no machine name at all. */ | |
| 100 | export const NO_ADDRESS = "There is no address to give yet: this box has no machine name, so a friend cannot join until it has one."; | |
| 59 | 101 | |
| 60 | 102 | /** Who is on, server by server. Servers nobody is on are left out. */ |
| 61 | 103 | export function whoIsOn(db: Database, boxId: string, services: readonly string[]): { service: string; names: string[] }[] { |
src/games/mods.ts +2 −2
| @@ -182,7 +182,7 @@ function diffLines(m: Mods, next: readonly Locked[], adopted: ReadonlySet<string | ||
| 182 | 182 | lines.push( |
| 183 | 183 | `VERSION is ${m.target.versionSaid}: these are for ${m.target.gameVersion}; set VERSION=${m.target.gameVersion} so the next Minecraft release does not outrun them`, |
| 184 | 184 | ); |
| 185 | lines.push(`Restarts the server with the new set on the box's next sync`); | |
| 185 | lines.push(`Restarts the server with the new set on the box's next check-in`); | |
| 186 | 186 | return lines; |
| 187 | 187 | } |
| 188 | 188 | |
| @@ -319,7 +319,7 @@ function packPlan(m: Mods, service: string, pack: LockedPack, verb: "install" | | ||
| 319 | 319 | ...(m.lock.length === 0 ? [] : [`Removes ${m.lock.map((l) => l.title).join(", ")}: a modpack server takes its mods from the pack`]), |
| 320 | 320 | ...(m.loose.length === 0 ? [] : [`Drops ${m.loose.join(", ")} from MODRINTH_PROJECTS`]), |
| 321 | 321 | "The image does not check a pack's file hashes; the lockfile keeps each one, so any change shows in review", |
| 322 | "Restarts the server with the pack on the box's next sync", | |
| 322 | "Restarts the server with the pack on the box's next check-in", | |
| 323 | 323 | ]; |
| 324 | 324 | return { |
| 325 | 325 | lines, |
src/games/player_events.ts +1 −1
| @@ -350,7 +350,7 @@ export function findingsOf(db: Database, boxId: string, service: string, now = n | ||
| 350 | 350 | // Tried with Velocity 3.5.1, a Fabric server and an unmodded client (2026-12-02): without |
| 351 | 351 | // AutoStopper in between, Velocity says the reason and sends the player to its next server. |
| 352 | 352 | if (g.text === "") |
| 353 | fix.push(`Turn Sleep off for ${service}: the player then sees the reason and lands on another server. Tested with Velocity 3.5.1. The proxy's try list must hold another server.`); | |
| 353 | fix.push(`Turn Sleep off for ${service}: the player then sees the reason and lands on another server. Tested with Velocity 3.5.1. The proxy's list of servers new players land on first must hold another server.`); | |
| 354 | 354 | if (fix.length === 0) fix.push(`Read the reason, then ${service}'s console at that time.`); |
| 355 | 355 | return { says: `${String(g.n)} joins to ${service} were refused in the last day.${g.text === "" ? " The proxy did not log why." : ` ${service} said: ${g.text}`}`, fix, at: g.at }; |
| 356 | 356 | }); |
src/games/presets.ts +2 −2
| @@ -194,7 +194,7 @@ function serverEnv(text: string, wish: Wish): Record<string, string> { | ||
| 194 | 194 | return env; |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | const CHANGED = "The box changed since the preview; plan it again."; | |
| 197 | const CHANGED = "The box changed since the preview; preview it again."; | |
| 198 | 198 | |
| 199 | 199 | export interface ServerPlan { |
| 200 | 200 | readonly lines: readonly string[]; |
| @@ -322,7 +322,7 @@ export async function planRemoveServer(app: App, box: GameBox, name: string): Pr | ||
| 322 | 322 | // Still in the proxy's `try` or another name's forced host: without it players would land |
| 323 | 323 | // nowhere. |
| 324 | 324 | if (text.split("\n").some((l) => /^\s+(try|"[a-z0-9.-]+")\s*=\s*\[/.test(l) && l.includes(`"${name}"`))) |
| 325 | throw new Error(`The proxy sends players to ${name} (its try list or a forced host); change that first.`); | |
| 325 | throw new Error(`The proxy sends players to ${name} (the proxy's list of servers new players land on first, or a forced host); change that first.`); | |
| 326 | 326 | } catch (e) { |
| 327 | 327 | return { ok: false, error: e instanceof Error ? e.message : String(e) }; |
| 328 | 328 | } |
src/games/settings.ts +1 −1
| @@ -528,7 +528,7 @@ export function planSettings( | ||
| 528 | 528 | const theirs = env["PATCH_DEFINITIONS"]; |
| 529 | 529 | if (patchChanges.length > 0 && theirs !== undefined && theirs !== PATCH_TARGET) |
| 530 | 530 | return { ok: false, error: `${service} already takes its patches from ${theirs}; change Paper's settings there.` }; |
| 531 | lines.push("Restarts the server with the new settings on the box's next sync"); | |
| 531 | lines.push("Restarts the server with the new settings on the box's next check-in"); | |
| 532 | 532 | const edit = (base: string): string => { |
| 533 | 533 | const baseEnv = serviceEnv(base, service); |
| 534 | 534 | const sets: Record<string, string> = { ...envChanges }; |
src/games/sleep.ts +8
| @@ -104,6 +104,14 @@ export function proxyOf(text: string): string | undefined { | ||
| 104 | 104 | return Object.keys(doc.services ?? {}).find((s) => isProxy(doc, s)); |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | /** The port players join on: the proxy's published port for the game's 25565, else 25565 itself. */ | |
| 108 | export function proxyPort(text: string): number { | |
| 109 | const proxy = proxyOf(text); | |
| 110 | const ports = (Bun.YAML.parse(text) as { services?: Record<string, { ports?: unknown }> }).services?.[proxy ?? ""]?.ports; | |
| 111 | const published = Array.isArray(ports) ? ports.map((p) => /^(?:[\d.]+:)?(\d+):25565(?:\/tcp)?$/.exec(String(p))?.[1]).find((p) => p !== undefined) : undefined; | |
| 112 | return published === undefined ? 25565 : Number(published); | |
| 113 | } | |
| 114 | ||
| 107 | 115 | /** Whether a service is a game server, on any image: one a player can be on, so neither the waker nor the proxy. */ |
| 108 | 116 | export const isGameServer = (text: string, service: string): boolean => !isWaker(service) && !isProxy(parse(text), service); |
| 109 | 117 |
src/http/mcp/tools/games.ts +8 −8
| @@ -105,7 +105,7 @@ export const tool: Tool = { | ||
| 105 | 105 | if (!planned.ok) return bad("refused", planned.error); |
| 106 | 106 | if (args["dryRun"] !== false) return good({ planned: true, says: "This is the plan; send dryRun: false with this sha to commit it.", sha: planned.value.sha, lines: planned.value.lines }); |
| 107 | 107 | const done = await commitServer(ctx.app, user, box, planned.value, str(args["sha"])); |
| 108 | return done.ok ? good({ says: `${planned.value.done} (${done.sha.slice(0, 10)}); the box brings it up on its next sync.`, sha: done.sha, lines: planned.value.lines }) : bad("refused", done.error); | |
| 108 | return done.ok ? good({ says: `${planned.value.done} (${done.sha.slice(0, 10)}); the box brings it up on its next check-in.`, sha: done.sha, lines: planned.value.lines }) : bad("refused", done.error); | |
| 109 | 109 | } |
| 110 | 110 | const service = str(args["service"]); |
| 111 | 111 | if (action === "metrics") { |
| @@ -139,7 +139,7 @@ export const tool: Tool = { | ||
| 139 | 139 | if (action === "console") { |
| 140 | 140 | const text = str(args["text"]).trim(); |
| 141 | 141 | if (text === "") return bad("invalid", "say the command as text"); |
| 142 | return good({ says: "Queued; the box runs it on its next sync.", id: queueCommand(db, box.id, service, "rcon", text, user.id) }); | |
| 142 | return good({ says: "Queued; the box runs it on its next check-in.", id: queueCommand(db, box.id, service, "rcon", text, user.id) }); | |
| 143 | 143 | } |
| 144 | 144 | if (action === "power") { |
| 145 | 145 | const kind = str(args["kind"]); |
| @@ -149,7 +149,7 @@ export const tool: Tool = { | ||
| 149 | 149 | if (action === "env") { |
| 150 | 150 | const value = str(args["value"]); |
| 151 | 151 | const done = await commitServiceEnv(ctx.app, user, box, service, { [str(args["key"])]: value === "" ? null : value }); |
| 152 | return done.ok ? good({ says: `Committed ${done.sha.slice(0, 10)}.`, sha: done.sha }) : bad("refused", done.error); | |
| 152 | return done.ok ? good({ says: `Saved ${done.sha.slice(0, 10)}.`, sha: done.sha }) : bad("refused", done.error); | |
| 153 | 153 | } |
| 154 | 154 | if (action.startsWith("mods")) { |
| 155 | 155 | const mods = await modsOf(ctx.app, box, service); |
| @@ -207,7 +207,7 @@ export const tool: Tool = { | ||
| 207 | 207 | if (args["dryRun"] !== false) return good({ planned: true, says: "This is the plan; send dryRun: false to commit it.", lines: planned.value.lines }); |
| 208 | 208 | const done = await commitPlan(ctx.app, user, box, service, m, planned.value); |
| 209 | 209 | return done.ok |
| 210 | ? good({ says: `${planned.value.done} (${done.value.slice(0, 10)}); the server restarts with it on the box's next sync.`, sha: done.value, lines: planned.value.lines }) | |
| 210 | ? good({ says: `${planned.value.done} (${done.value.slice(0, 10)}); the server restarts with it on the box's next check-in.`, sha: done.value, lines: planned.value.lines }) | |
| 211 | 211 | : bad("refused", done.error); |
| 212 | 212 | } |
| 213 | 213 | if (action === "people") { |
| @@ -217,7 +217,7 @@ export const tool: Tool = { | ||
| 217 | 217 | return good({ service, online, ...lists, network: networkOf(db, box.id), latest: eventsOf(db, box.id, service, 50), findings: kickFindingsOf(db, box.id, service), refusals: findingsOf(db, box.id, service) }); |
| 218 | 218 | } |
| 219 | 219 | const queued = await queuePeople(ctx.app, box, service, op, str(args["player"]), user, str(args["to"])); |
| 220 | return queued.ok ? good({ says: `Queued: ${peopleWords(JSON.stringify({ op, player: str(args["player"]), server: str(args["to"]) }))}; the box does it on its next sync.`, id: queued.id }) : bad("refused", queued.error); | |
| 220 | return queued.ok ? good({ says: `Queued: ${peopleWords(JSON.stringify({ op, player: str(args["player"]), server: str(args["to"]) }))}; the box does it on its next check-in.`, id: queued.id }) : bad("refused", queued.error); | |
| 221 | 221 | } |
| 222 | 222 | if (action === "config_set") { |
| 223 | 223 | const plan = await previewSettings(ctx.app, box, service, { [str(args["key"])]: str(args["value"]) }); |
| @@ -225,13 +225,13 @@ export const tool: Tool = { | ||
| 225 | 225 | if (args["dryRun"] !== false) |
| 226 | 226 | return good({ lines: plan.value.lines, sha: plan.value.sha, says: "This is the plan; nothing changed. Send dryRun: false to commit it." }); |
| 227 | 227 | const done = await commitSettings(ctx.app, user, box, service, plan.value); |
| 228 | return done.ok ? good({ says: `Committed ${done.value.slice(0, 10)}; the server restarts on the box's next sync.`, lines: plan.value.lines, sha: done.value }) : bad("refused", done.error); | |
| 228 | return done.ok ? good({ says: `Saved ${done.value.slice(0, 10)}; the server restarts on the box's next check-in.`, lines: plan.value.lines, sha: done.value }) : bad("refused", done.error); | |
| 229 | 229 | } |
| 230 | 230 | if (action === "sleep") { |
| 231 | 231 | const policy = str(args["value"]); |
| 232 | 232 | if (policy !== "warm" && policy !== "sleep") return bad("invalid", "value is warm or sleep"); |
| 233 | 233 | const done = await commitPolicy(ctx.app, user, box, service, policy); |
| 234 | return done.ok ? good({ says: `${service} ${policy === "sleep" ? "sleeps when empty" : "stays warm"} once the box redeploys (${done.sha.slice(0, 10)}).`, sha: done.sha }) : bad("refused", done.error); | |
| 234 | return done.ok ? good({ says: `${service} ${policy === "sleep" ? "sleeps when empty" : "stays running"} once the box redeploys (${done.sha.slice(0, 10)}).`, sha: done.sha }) : bad("refused", done.error); | |
| 235 | 235 | } |
| 236 | 236 | if (action === "world") { |
| 237 | 237 | const op = str(args["op"]); |
| @@ -245,7 +245,7 @@ export const tool: Tool = { | ||
| 245 | 245 | : ({ ok: false, error: "op is import, restore or snapshot" } as const); |
| 246 | 246 | // The link is a secret: only its host and path come back. |
| 247 | 247 | return queued.ok |
| 248 | ? good({ says: "Queued; the box runs it on its next sync and reports each step.", id: queued.id, ...(op === "import" ? { from: linkShown(str(args["url"])) } : {}) }) | |
| 248 | ? good({ says: "Queued; the box runs it on its next check-in and reports each step.", id: queued.id, ...(op === "import" ? { from: linkShown(str(args["url"])) } : {}) }) | |
| 249 | 249 | : bad("refused", queued.error); |
| 250 | 250 | } |
| 251 | 251 | return bad("bad_action", "action is list, metrics, console, power, env, mods, mods_search, mods_pack_search, mods_install, mods_update, mods_remove, mods_pack, people, config, config_set, sleep or world"); |
src/tui/screens/game_mods.ts +1 −1
| @@ -134,7 +134,7 @@ export function gameModsScreen(f: Session, ctx: Ctx, box: GameBox, service: stri | ||
| 134 | 134 | hits = []; |
| 135 | 135 | await load(); |
| 136 | 136 | } |
| 137 | say(done.ok ? `${plan.done} (${done.value.slice(0, 10)}); the server restarts with it on the box's next sync.` : done.error); | |
| 137 | say(done.ok ? `${plan.done} (${done.value.slice(0, 10)}); the server restarts with it on the box's next check-in.` : done.error); | |
| 138 | 138 | }); |
| 139 | 139 | } else return false; |
| 140 | 140 | return true; |
src/tui/screens/games.ts +38 −23
| @@ -1,11 +1,12 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * Games in the terminal (forge games): every service of every game box the person may see, as |
| 3 | 3 | * /games lists them, with `s` start, `x` stop and `r` restart for an operator, each queued for the |
| 4 | * box's next sync through `games/store.ts`, the same queue the page and the MCP tool use. `enter` | |
| 5 | * opens a service as /games/<box>/<service> does: its console (the log tail, and `:` queues an | |
| 6 | * rcon command), its env (`e` sets or clears a key, committed through `commitServiceEnv`) and its | |
| 7 | * Modrinth projects (`m` opens them: `game_mods.ts`, the page's model; uploading a jar is the | |
| 8 | * page's alone); `c` opens its settings (server.properties and Paper's, as the page's Settings block: | |
| 4 | * box's next check-in through `games/store.ts`, the same queue the page and the MCP tool use. | |
| 5 | * `enter` opens a service as /games/<box>/<service> does: its console (the log tail, and `:` queues | |
| 6 | * an rcon command), its env (`e` sets or clears a key, committed through `commitServiceEnv`) and | |
| 7 | * its Modrinth projects (`m` opens them: `game_mods.ts`, the page's model; uploading a jar is the | |
| 8 | * page's alone); `c` opens its settings (server.properties and Paper's, as the page's Settings | |
| 9 | * block: | |
| 9 | 10 | * `enter` sets one, the preview shows on top, `y` commits it through `commitSettings`). `G` opens |
| 10 | 11 | * the box's grants, which an owner sets with `a` there, through `grant`; `b` the box itself, as |
| 11 | 12 | * /games/<box>'s Box block (its diagnosis, the volume's device and log among it). |
| @@ -14,10 +15,10 @@ | ||
| 14 | 15 | import { movedKey } from "./moved_key.ts"; |
| 15 | 16 | import { emptyText } from "../../core/empty_words.ts"; |
| 16 | 17 | import { consoleText, crashOf } from "../../games/console.ts"; |
| 17 | import { commitPolicy, policyOf, STILL_BILLED } from "../../games/sleep.ts"; | |
| 18 | import { commitPolicy, policyOf, proxyPort, STILL_BILLED } from "../../games/sleep.ts"; | |
| 18 | 19 | import { SHELL_SERVICE, queueShell } from "../../games/shell.ts"; |
| 19 | 20 | import { watchBox } from "../../games/notify.ts"; |
| 20 | import { joinAddress, nowWords, powerNow } from "../../games/gist.ts"; | |
| 21 | import { joinAddress, NO_ADDRESS, nowWords, powerNow, PREVIEW_WORDS, privateWords, SAVE_WORDS } from "../../games/gist.ts"; | |
| 21 | 22 | import { findingLine, findingsOf as kickFindingsOf } from "../../games/findings.ts"; |
| 22 | 23 | import { eventsOf, eventWords, isBedrock, peopleOf, peopleWords, queuePeople } from "../../games/people.ts"; |
| 23 | 24 | import { findingsOf } from "../../games/player_events.ts"; |
| @@ -80,11 +81,25 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen { | ||
| 80 | 81 | const line = row === undefined ? undefined : cloudLine(f.app, row); |
| 81 | 82 | return line === undefined ? [] : [{ title: `${b.name}'s machine`, secondary: line, meta: "" }]; |
| 82 | 83 | }); |
| 84 | /** The port each box's proxy publishes, from its compose file once that is read. */ | |
| 85 | const ports = new Map<string, number>(); | |
| 86 | const readPorts = (): void => { | |
| 87 | for (const b of listBoxes(f.app.db)) | |
| 88 | if (seesBox(f.app.db, f.user, b.id) && !ports.has(b.id)) | |
| 89 | void readCompose(f.app, b).then((c) => { | |
| 90 | if (c === undefined) return; | |
| 91 | ports.set(b.id, proxyPort(c.text)); | |
| 92 | ctx.invalidate(); | |
| 93 | }); | |
| 94 | }; | |
| 95 | readPorts(); | |
| 96 | const firstBox = (): GameBox | undefined => listBoxes(f.app.db).find((b) => seesBox(f.app.db, f.user, b.id)); | |
| 83 | 97 | /** How to join, as the page says it: the address of the first box's machine. */ |
| 84 | 98 | const joinLine = (): string => { |
| 85 | const box = listBoxes(f.app.db).find((b) => seesBox(f.app.db, f.user, b.id)); | |
| 86 | const address = box === undefined ? undefined : joinAddress(pinnedOfBox(f.app, box)?.host); | |
| 87 | return address === undefined ? "Each service of each game box you may see. There is no address to join yet: no machine name." : `To join, add this server in Minecraft: ${address}`; | |
| 99 | const box = firstBox(); | |
| 100 | const join = box === undefined ? undefined : joinAddress(pinnedOfBox(f.app, box), ports.get(box.id) ?? 25565); | |
| 101 | if (join === undefined) return `Each service of each game box you may see. ${NO_ADDRESS}`; | |
| 102 | return "address" in join ? `To join, add this server in Minecraft: ${join.address}` : privateWords(join.private); | |
| 88 | 103 | }; |
| 89 | 104 | const rows = () => |
| 90 | 105 | listBoxes(f.app.db) |
| @@ -126,7 +141,7 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen { | ||
| 126 | 141 | binding(["r"], "restart"), |
| 127 | 142 | binding(["n"], "new server"), |
| 128 | 143 | binding(["d"], "remove a server"), |
| 129 | binding(["z"], "sleep when empty, or keep warm"), | |
| 144 | binding(["z"], "sleep when empty, or keep running"), | |
| 130 | 145 | binding(["L"], "link your Minecraft name (!forge)"), |
| 131 | 146 | ], |
| 132 | 147 | render(area, s) { |
| @@ -202,7 +217,7 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen { | ||
| 202 | 217 | ctx.push( |
| 203 | 218 | promptScreen(ctx, screen, { |
| 204 | 219 | title: `New server on ${box.name}`, |
| 205 | label: "What kind (a void world with FAWE and bedwars)", | |
| 220 | label: `What kind (a void world with FAWE and bedwars). ${PREVIEW_WORDS}`, | |
| 206 | 221 | onSubmit: async (words) => { |
| 207 | 222 | showPlan(box, `New server on ${box.name}`, "Create it", await planServer(f.app, box, await wishOn(f.app, box, words))); |
| 208 | 223 | }, |
| @@ -223,12 +238,12 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen { | ||
| 223 | 238 | if (!mayDo(roleOn(f.app.db, f.user, at.box.id, at.service.name), "operator")) note = `You are not an operator of ${at.service.name}.`; |
| 224 | 239 | else { |
| 225 | 240 | const { box, service } = at; |
| 226 | note = `${box.name}/${service.name}: committing…`; | |
| 241 | note = `${box.name}/${service.name}: saving…`; | |
| 227 | 242 | void (async () => { |
| 228 | 243 | const compose = await readCompose(f.app, box); |
| 229 | 244 | const next = compose !== undefined && policyOf(compose.text, service.name) === "sleep" ? "warm" : "sleep"; |
| 230 | 245 | const done = await commitPolicy(f.app, f.user!, box, service.name, next); |
| 231 | note = done.ok ? `${box.name}/${service.name} ${next === "sleep" ? "sleeps when empty" : "stays warm"} once the box redeploys (${done.sha.slice(0, 10)}).${next === "sleep" ? ` ${STILL_BILLED}` : ""}` : done.error; | |
| 246 | note = done.ok ? `${box.name}/${service.name} ${next === "sleep" ? "sleeps when empty" : "stays running"} once the box redeploys (${done.sha.slice(0, 10)}).${next === "sleep" ? ` ${STILL_BILLED}` : ""}` : done.error; | |
| 232 | 247 | ctx.invalidate(); |
| 233 | 248 | })(); |
| 234 | 249 | } |
| @@ -311,7 +326,7 @@ export function gameServiceScreen(f: Session, ctx: Ctx, box: GameBox, service: s | ||
| 311 | 326 | if (!loaded) return ["Reading the compose file…"]; |
| 312 | 327 | if (compose === undefined) return [`No ${box.path} on ${box.repo}@${box.branch}.`]; |
| 313 | 328 | const env = Object.entries(serviceEnv(compose, service)); |
| 314 | return env.length === 0 ? ["No environment set for this service."] : env.map(([k, v]) => `${k}=${v}`); | |
| 329 | return [...(env.length === 0 ? ["No environment set for this service."] : env.map(([k, v]) => `${k}=${v}`)), `e sets one. ${SAVE_WORDS}`]; | |
| 315 | 330 | }; |
| 316 | 331 | const ask = (title: string, label: string, run: (value: string) => Promise<string>, history?: readonly string[]): void => { |
| 317 | 332 | if (!operator()) return say(`You are not an operator of ${service}.`); |
| @@ -370,7 +385,7 @@ export function gameServiceScreen(f: Session, ctx: Ctx, box: GameBox, service: s | ||
| 370 | 385 | return "Sent; the answer shows under the console."; |
| 371 | 386 | }, commandHistory(f.app.db, box.id, service)); |
| 372 | 387 | else if (e.key === "e") |
| 373 | ask("Env", "KEY=VALUE (an empty value removes the key)", async (raw) => { | |
| 388 | ask("Env", `KEY=VALUE (an empty value removes the key). ${SAVE_WORDS}`, async (raw) => { | |
| 374 | 389 | const at = raw.indexOf("="); |
| 375 | 390 | const key = (at < 0 ? raw : raw.slice(0, at)).trim(); |
| 376 | 391 | const value = at < 0 ? "" : raw.slice(at + 1); |
| @@ -378,7 +393,7 @@ export function gameServiceScreen(f: Session, ctx: Ctx, box: GameBox, service: s | ||
| 378 | 393 | if (!done.ok) return done.error; |
| 379 | 394 | part = "env"; |
| 380 | 395 | await load(); |
| 381 | return `Committed ${done.sha.slice(0, 10)}; the box redeploys on its next sync.`; | |
| 396 | return `Saved ${done.sha.slice(0, 10)}; the box redeploys on its next check-in.`; | |
| 382 | 397 | }); |
| 383 | 398 | else if (e.key === "c") ctx.push(gameSettingsScreen(f, ctx, box, service)); |
| 384 | 399 | else if (e.key === "p") |
| @@ -393,7 +408,7 @@ export function gameServiceScreen(f: Session, ctx: Ctx, box: GameBox, service: s | ||
| 393 | 408 | const world = (op: WorldOp): string => { |
| 394 | 409 | const queued = queueWorld(f.app.db, boxNamed(f.app.db, box.name) ?? box, service, op, f.user); |
| 395 | 410 | part = "world"; |
| 396 | return queued.ok ? "Asked: the box starts on its next sync, and its steps show here." : queued.error; | |
| 411 | return queued.ok ? "Asked: the box starts on its next check-in, and its steps show here." : queued.error; | |
| 397 | 412 | }; |
| 398 | 413 | if (e.key === "i") ask("Import a world", "A link to a .zip or .tar.gz with its level.dat (the world it has is kept first)", async (url) => world({ op: "import", url })); |
| 399 | 414 | else if (e.key === "u") |
| @@ -490,9 +505,9 @@ export function gameSettingsScreen(f: Session, ctx: Ctx, box: GameBox, service: | ||
| 490 | 505 | wanted = {}; |
| 491 | 506 | plan = undefined; |
| 492 | 507 | await load(); |
| 493 | say(`Committed ${done.value.slice(0, 10)}; the server restarts with the new settings on the box's next sync.`); | |
| 508 | say(`Saved ${done.value.slice(0, 10)}; the server restarts with the new settings on the box's next check-in.`); | |
| 494 | 509 | }); |
| 495 | return say("Committing…"); | |
| 510 | return say("Saving…"); | |
| 496 | 511 | } |
| 497 | 512 | if (e.key !== "enter") return false; |
| 498 | 513 | const at = values[state.index]; |
| @@ -545,7 +560,7 @@ export function gameBoxScreen(f: Session, ctx: Ctx, box: GameBox): Screen { | ||
| 545 | 560 | } |
| 546 | 561 | const plain = boxStatus(b.diag); |
| 547 | 562 | const tails = boxTails(b.diag); |
| 548 | if (plain.length + tails.length === 0) return [...lend, ...spare, "The box has sent no diagnosis yet: its agent sends one with every sync."]; | |
| 563 | if (plain.length + tails.length === 0) return [...lend, ...spare, "The box has sent no diagnosis yet: its agent sends one with every check-in."]; | |
| 549 | 564 | return [...lend, ...spare, ...plain, ...tails.flatMap(([k, v]) => ["", `== ${k}`, ...v.split("\n")])]; |
| 550 | 565 | }; |
| 551 | 566 | return { |
| @@ -639,7 +654,7 @@ export function gameShellScreen(f: Session, ctx: Ctx, box: GameBox): Screen { | ||
| 639 | 654 | s.text(head!.x, head!.y, truncate(note !== "" ? note : `A shell on ${box.name}: bash -lc, 30 s, no terminal, 16 KB back. : runs one.`, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); |
| 640 | 655 | const lines = recentCommands(f.app.db, box.id, SHELL_SERVICE, 10) |
| 641 | 656 | .reverse() |
| 642 | .flatMap((cmd) => [`$ ${cmd.text}`, ...(cmd.done ? cmd.output.split("\n") : ["(waiting for the box's next sync)"])]); | |
| 657 | .flatMap((cmd) => [`$ ${cmd.text}`, ...(cmd.done ? cmd.output.split("\n") : ["(waiting for the box's next check-in)"])]); | |
| 643 | 658 | lines.slice(-body!.h).forEach((l, i) => s.text(body!.x, body!.y + i, truncate(l, body!.w), style(c.fg, c.bg), body!.x + body!.w)); |
| 644 | 659 | }, |
| 645 | 660 | key(e) { |
| @@ -651,7 +666,7 @@ export function gameShellScreen(f: Session, ctx: Ctx, box: GameBox): Screen { | ||
| 651 | 666 | onSubmit: (text) => { |
| 652 | 667 | const queued = queueShell(f.app, f.user, box, text); |
| 653 | 668 | if (!queued.ok) return queued.error; |
| 654 | note = "Sent; the answer shows here at the box's next sync."; | |
| 669 | note = "Sent; the answer shows here at the box's next check-in."; | |
| 655 | 670 | ctx.invalidate(); |
| 656 | 671 | return undefined; |
| 657 | 672 | }, |
src/web/pages/games.ts +27 −24
| @@ -61,7 +61,7 @@ import { avatar, badge, btn, disclosure, field as input, row, rowList, time } fr | ||
| 61 | 61 | import { watchBox } from "../../games/notify.ts"; |
| 62 | 62 | import { accountLinks, chatLog, newLinkCode, playersOn, unlink } from "../../games/chat.ts"; |
| 63 | 63 | import { commitServer, planRemoveServer, planServer, wishOn } from "../../games/presets.ts"; |
| 64 | import { canSleep, commitPolicy, EMPTY_SECONDS, isGameServer, policyOf, STILL_BILLED } from "../../games/sleep.ts"; | |
| 64 | import { canSleep, commitPolicy, EMPTY_SECONDS, isGameServer, policyOf, proxyPort, STILL_BILLED } from "../../games/sleep.ts"; | |
| 65 | 65 | import { CLOUD_EVENT, cloudFacts, GAME_CLOUD_EVENT, pinnedOfBox } from "../../actions/cloud_says.ts"; |
| 66 | 66 | import { topologyBlock, TOPOLOGY_CSS } from "./games_topology.ts"; |
| 67 | 67 | import { defaultHeadResponse, headResponse } from "../../games/heads.ts"; |
| @@ -70,7 +70,7 @@ import { queueWorld, undoOf, worldWords } from "../../games/worlds.ts"; | ||
| 70 | 70 | import { emptyLine, emptyNote } from "../empty_words.ts"; |
| 71 | 71 | import { findingsOf, type GameFinding } from "../../games/player_events.ts"; |
| 72 | 72 | import { findingsOf as kickFindingsOf } from "../../games/findings.ts"; |
| 73 | import { joinAddress, mainPower, nowWords, PEOPLE_BUTTON, PEOPLE_CHOICES, powerNow, whoIsOn } from "../../games/gist.ts"; | |
| 73 | import { joinAddress, mainPower, NO_ADDRESS, PREVIEW_WORDS, privateWords, SAVE_WORDS, nowWords, PEOPLE_BUTTON, PEOPLE_CHOICES, powerNow, whoIsOn } from "../../games/gist.ts"; | |
| 74 | 74 | import { metricsSeries, playerEvents, recentErrors, type Point } from "../../games/metrics.ts"; |
| 75 | 75 | import { daySpark } from "../studio/kpi_row.ts"; |
| 76 | 76 | |
| @@ -100,10 +100,10 @@ function newServerBlock(box: GameBox, words: string, preview: Awaited<ReturnType | ||
| 100 | 100 | <h2>New server</h2> |
| 101 | 101 | <form method="get" action="/games/${box.name}" class="row-fields"> |
| 102 | 102 | ${input({ name: "words", label: "What kind", placeholder: "a void world with FAWE and bedwars", required: true, value: words })} |
| 103 | ${btn("Plan it", { variant: "secondary" })} | |
| 103 | ${btn("Preview it", { variant: "secondary" })} | |
| 104 | 104 | </form> |
| 105 | 105 | ${preview === undefined |
| 106 | ? html`<p class="muted small">Void, flat or normal; FAWE, BedWars or LuckPerms by name; it sleeps when empty, so its memory counts free.</p>` | |
| 106 | ? html`<p class="muted small">${PREVIEW_WORDS} Void, flat or normal; FAWE, BedWars or LuckPerms by name; it sleeps when empty, so its memory counts free.</p>` | |
| 107 | 107 | : preview.ok |
| 108 | 108 | ? previewBlock(preview.value, `/games/${box.name}/servers`, { op: "create", words }, "Create it", `/games/${box.name}`) |
| 109 | 109 | : html`<p role="alert">${preview.error}</p>`} |
| @@ -135,7 +135,7 @@ export async function gameServersSubmit(ctx: Ctx): Promise<Response> { | ||
| 135 | 135 | : await planServer(ctx.app, got.box, await wishOn(ctx.app, got.box, field(form, "words"))); |
| 136 | 136 | if (!planned.ok) return flashTo(ctx, `/games/${got.box.name}`, { tone: "danger", message: planned.error }); |
| 137 | 137 | const done = await commitServer(ctx.app, ctx.auth.user!, got.box, planned.value, field(form, "sha")); |
| 138 | return flashTo(ctx, `/games/${got.box.name}`, done.ok ? { message: `${planned.value.done} (${done.sha.slice(0, 10)}); the box brings it up on its next sync.` } : { tone: "danger", message: done.error }); | |
| 138 | return flashTo(ctx, `/games/${got.box.name}`, done.ok ? { message: `${planned.value.done} (${done.sha.slice(0, 10)}); the box brings it up on its next check-in.` } : { tone: "danger", message: done.error }); | |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | function boxOf(ctx: Ctx, need: Role, service = ""): { box: GameBox; role: Role } | Response { |
| @@ -243,15 +243,17 @@ const power = (box: GameBox, service: string, s: GameService | undefined, extra: | ||
| 243 | 243 | * How to join and who is on, the box page's first lines: the address with Copy, then each server's |
| 244 | 244 | * people. |
| 245 | 245 | */ |
| 246 | function joinBlock(ctx: Ctx, box: GameBox, names: readonly string[]): Html { | |
| 246 | function joinBlock(ctx: Ctx, box: GameBox, names: readonly string[], compose: string | undefined): Html { | |
| 247 | 247 | const machine = pinnedOfBox(ctx.app, box); |
| 248 | const address = joinAddress(machine?.host); | |
| 248 | const join = joinAddress(machine, compose === undefined ? 25565 : proxyPort(compose)); | |
| 249 | 249 | const on = whoIsOn(ctx.app.db, box.id, names); |
| 250 | 250 | return html`<section class="settings-block" id="join"> |
| 251 | 251 | <h2>How to join</h2> |
| 252 | ${address === undefined | |
| 253 | ? html`<p class="muted">There is no address to give yet: this box has no machine name, so a friend cannot join until it has one.</p>` | |
| 254 | : rowList([row({ title: html`<code class="mono" id="game-address">${address}</code>`, meta: html`In Minecraft: Multiplayer, Add Server, paste this.`, trail: btn("Copy", { type: "button", copy: "game-address", icon: "copy", size: "sm", label: "Copy the server address" }) })])} | |
| 252 | ${join === undefined | |
| 253 | ? html`<p class="muted">${NO_ADDRESS}</p>` | |
| 254 | : "address" in join | |
| 255 | ? rowList([row({ title: html`<code class="mono" id="game-address">${join.address}</code>`, meta: html`In Minecraft: Multiplayer, Add Server, paste this.`, trail: btn("Copy", { type: "button", copy: "game-address", icon: "copy", size: "sm", label: "Copy the server address" }) })]) | |
| 256 | : html`<p class="muted" id="game-address-private">${privateWords(join.private)}</p>`} | |
| 255 | 257 | ${on.length === 0 ? html`<p class="muted small">Nobody is on right now.</p>` : html`<p>On now: ${on.map((x, i) => html`${i === 0 ? "" : "; "}<a href="/games/${box.name}/${x.service}">${x.service}</a>: ${x.names.join(", ")}`)}</p>`} |
| 256 | 258 | </section>`; |
| 257 | 259 | } |
| @@ -290,7 +292,7 @@ export async function gameBoxPage(ctx: Ctx): Promise<Response> { | ||
| 290 | 292 | ${cloud === undefined |
| 291 | 293 | ? null |
| 292 | 294 | : html`<div id="game-machine" data-live="${CLOUD_EVENT},${GAME_CLOUD_EVENT}">${rowList([row({ title: "Its machine", facts: [cloud] })])}</div>`} |
| 293 | ${joinBlock(ctx, box, visible)} | |
| 295 | ${joinBlock(ctx, box, visible, compose?.text)} | |
| 294 | 296 | ${visible.length === 0 |
| 295 | 297 | ? emptyLine("gameServices") |
| 296 | 298 | : rowList( |
| @@ -328,7 +330,7 @@ export async function gameBoxPage(ctx: Ctx): Promise<Response> { | ||
| 328 | 330 | */ |
| 329 | 331 | function shellBlock(ctx: Ctx, box: GameBox): Html { |
| 330 | 332 | const done = recentCommands(ctx.app.db, box.id, SHELL_SERVICE, 10).reverse(); |
| 331 | const lines = done.flatMap((c) => [`$ ${c.text}`, c.done ? c.output : "(waiting for the box's next sync)"]); | |
| 333 | const lines = done.flatMap((c) => [`$ ${c.text}`, c.done ? c.output : "(waiting for the box's next check-in)"]); | |
| 332 | 334 | return html`<section class="settings-block" id="shell"> |
| 333 | 335 | <h2>Console</h2> |
| 334 | 336 | <p class="muted small">A shell on ${box.name}: each command runs as <code>bash -lc</code> for at most 30 s, with no terminal, and says back at most 16 KB. Every command and its output go to the audit.</p> |
| @@ -342,7 +344,7 @@ ${lines.join("\n")}${lines.length > 0 ? "\n" : ""}</pre | ||
| 342 | 344 | </section>`; |
| 343 | 345 | } |
| 344 | 346 | |
| 345 | // POST /games/:box/@shell: the owner's shell command, queued for the box's next sync. | |
| 347 | // POST /games/:box/@shell: the owner's shell command, queued for the box's next check-in. | |
| 346 | 348 | export async function gameShellSubmit(ctx: Ctx): Promise<Response> { |
| 347 | 349 | const got = boxOf(ctx, "owner"); |
| 348 | 350 | if (got instanceof Response) return got; |
| @@ -412,7 +414,7 @@ function boxBlock(box: GameBox, packs: readonly string[]): Html { | ||
| 412 | 414 | return html`<section class="settings-block"> |
| 413 | 415 | <h2>Box</h2> |
| 414 | 416 | ${lines.length === 0 && raw.length === 0 |
| 415 | ? html`<p class="muted">The box has sent no diagnosis yet: its agent sends one with every sync.</p>` | |
| 417 | ? html`<p class="muted">The box has sent no diagnosis yet: its agent sends one with every check-in.</p>` | |
| 416 | 418 | : html`${lines.map((l) => html`<p>${l}</p>`)} |
| 417 | 419 | ${raw.length === 0 |
| 418 | 420 | ? null |
| @@ -638,7 +640,8 @@ ${rawView ? tail.lines.join("\n") : raw(consoleHtml(tail.lines).join("\n"))}${ta | ||
| 638 | 640 | ${act |
| 639 | 641 | ? html`<form method="post" action="${base}/env" class="row-fields"> |
| 640 | 642 | ${input({ name: "key", label: "Key", required: true, mono: true })} |
| 641 | ${input({ name: "value", label: "Value (empty removes it)", mono: true })} ${btn("Commit", { variant: "secondary" })} | |
| 643 | ${input({ name: "value", label: "Value (empty removes it)", mono: true })} ${btn("Save change", { variant: "secondary" })} | |
| 644 | <span class="muted small">${SAVE_WORDS}</span> | |
| 642 | 645 | </form>` |
| 643 | 646 | : null} |
| 644 | 647 | </section>`)} |
| @@ -1009,7 +1012,7 @@ export async function gameWorldSubmit(ctx: Ctx): Promise<Response> { | ||
| 1009 | 1012 | : op === "snapshot" |
| 1010 | 1013 | ? queueWorld(ctx.app.db, got.box, got.service, { op: "snapshot" }, ctx.auth.user) |
| 1011 | 1014 | : ({ ok: false, error: "Choose import, restore or snapshot." } as const); |
| 1012 | return flashTo(ctx, back, queued.ok ? { message: "Asked: the box starts on its next sync, and its steps show under World." } : { tone: "danger", message: queued.error }); | |
| 1015 | return flashTo(ctx, back, queued.ok ? { message: "Asked: the box starts on its next check-in, and its steps show under World." } : { tone: "danger", message: queued.error }); | |
| 1013 | 1016 | } |
| 1014 | 1017 | |
| 1015 | 1018 | async function operatorForm(ctx: Ctx): Promise<{ box: GameBox; service: string; form: Form } | Response> { |
| @@ -1034,7 +1037,7 @@ export async function gameCrashShare(ctx: Ctx): Promise<Response> { | ||
| 1034 | 1037 | return flashTo(ctx, `${back}?shared=${shared.id}`, { message: `Shared publicly on mclo.gs: https://mclo.gs/${shared.id}` }); |
| 1035 | 1038 | } |
| 1036 | 1039 | |
| 1037 | // POST /games/:box/:service/command: an RCON command, queued for the box's next sync. | |
| 1040 | // POST /games/:box/:service/command: an RCON command, queued for the box's next check-in. | |
| 1038 | 1041 | export async function gameCommandSubmit(ctx: Ctx): Promise<Response> { |
| 1039 | 1042 | const got = await operatorForm(ctx); |
| 1040 | 1043 | if (got instanceof Response) return got; |
| @@ -1111,7 +1114,7 @@ function settingsBlock(o: { | ||
| 1111 | 1114 | <form method="post" action="${o.base}/settings" class="row-fields"> |
| 1112 | 1115 | ${o.preview.value.keys.map((k) => html`<input type="hidden" name="s.${k}" value="${o.wanted[k] ?? ""}" />`)} |
| 1113 | 1116 | <input type="hidden" name="sha" value="${o.sha}" /> |
| 1114 | ${btn("Commit and restart", { variant: "primary" })} ${btn("Cancel", { variant: "ghost", href: `${o.base}#settings` })} | |
| 1117 | ${btn("Save and restart", { variant: "primary" })} ${btn("Cancel", { variant: "ghost", href: `${o.base}#settings` })} | |
| 1115 | 1118 | </form> |
| 1116 | 1119 | </div> |
| 1117 | 1120 | </div>` |
| @@ -1151,7 +1154,7 @@ export async function gameSettingsSubmit(ctx: Ctx): Promise<Response> { | ||
| 1151 | 1154 | return flashTo( |
| 1152 | 1155 | ctx, |
| 1153 | 1156 | back, |
| 1154 | done.ok ? { message: `Committed ${done.value.slice(0, 10)}; the server restarts with the new settings on the box's next sync.` } : { tone: "danger", message: done.error }, | |
| 1157 | done.ok ? { message: `Saved ${done.value.slice(0, 10)}; the server restarts with the new settings on the box's next check-in.` } : { tone: "danger", message: done.error }, | |
| 1155 | 1158 | ); |
| 1156 | 1159 | } |
| 1157 | 1160 | |
| @@ -1166,7 +1169,7 @@ export async function gameEnvSubmit(ctx: Ctx): Promise<Response> { | ||
| 1166 | 1169 | ctx, |
| 1167 | 1170 | `/games/${got.box.name}/${got.service}`, |
| 1168 | 1171 | done.ok |
| 1169 | ? { message: `Committed ${done.sha.slice(0, 10)}; the box redeploys on its next sync.` } | |
| 1172 | ? { message: `Saved ${done.sha.slice(0, 10)}; the box redeploys on its next check-in.` } | |
| 1170 | 1173 | : { tone: "danger", message: done.error }, |
| 1171 | 1174 | ); |
| 1172 | 1175 | } |
| @@ -1180,7 +1183,7 @@ function sleepBlock(box: GameBox, service: string, text: string, asleep: boolean | ||
| 1180 | 1183 | ? asleep |
| 1181 | 1184 | ? `Asleep now: it is stopped and holds no memory. ${wait}` |
| 1182 | 1185 | : `Stops after ${EMPTY_SECONDS / 60} minutes with nobody on it, which frees its memory. ${wait}` |
| 1183 | : "Kept warm: it runs whether or not anyone is on it."; | |
| 1186 | : "Kept running: it runs whether or not anyone is on it."; | |
| 1184 | 1187 | return html`<section class="settings-block"> |
| 1185 | 1188 | <h2>Sleep</h2> |
| 1186 | 1189 | <p>${now}</p> |
| @@ -1188,7 +1191,7 @@ function sleepBlock(box: GameBox, service: string, text: string, asleep: boolean | ||
| 1188 | 1191 | ${act |
| 1189 | 1192 | ? html`<form method="post" action="/games/${box.name}/${service}/sleep" class="row-fields"> |
| 1190 | 1193 | <input type="hidden" name="policy" value="${policy === "sleep" ? "warm" : "sleep"}" /> |
| 1191 | ${btn(policy === "sleep" ? "Keep it warm" : "Let it sleep when empty", { variant: "secondary" })} | |
| 1194 | ${btn(policy === "sleep" ? "Keep it running" : "Let it sleep when empty", { variant: "secondary" })} | |
| 1192 | 1195 | </form>` |
| 1193 | 1196 | : null} |
| 1194 | 1197 | </section>`; |
| @@ -1204,7 +1207,7 @@ export async function gameSleepSubmit(ctx: Ctx): Promise<Response> { | ||
| 1204 | 1207 | return flashTo( |
| 1205 | 1208 | ctx, |
| 1206 | 1209 | `/games/${got.box.name}/${got.service}`, |
| 1207 | done.ok ? { message: `${got.service} ${policy === "sleep" ? "sleeps when empty" : "stays warm"} once the box redeploys (${done.sha.slice(0, 10)}).` } : { tone: "danger", message: done.error }, | |
| 1210 | done.ok ? { message: `${got.service} ${policy === "sleep" ? "sleeps when empty" : "stays running"} once the box redeploys (${done.sha.slice(0, 10)}).` } : { tone: "danger", message: done.error }, | |
| 1208 | 1211 | ); |
| 1209 | 1212 | } |
| 1210 | 1213 | |
| @@ -1294,7 +1297,7 @@ async function modsSubmit(ctx: Ctx, plan: (m: Mods, service: string, project: st | ||
| 1294 | 1297 | return flashTo( |
| 1295 | 1298 | ctx, |
| 1296 | 1299 | `${back}#mods`, |
| 1297 | done.ok ? { message: `${planned.value.done} (${done.value.slice(0, 10)}); the server restarts with it on the box's next sync.` } : { tone: "danger", message: done.error }, | |
| 1300 | done.ok ? { message: `${planned.value.done} (${done.value.slice(0, 10)}); the server restarts with it on the box's next check-in.` } : { tone: "danger", message: done.error }, | |
| 1298 | 1301 | ); |
| 1299 | 1302 | } |
| 1300 | 1303 |
test/game-chat.test.ts +3 −3
| @@ -437,7 +437,7 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"The hub re | ||
| 437 | 437 | const built = told(); |
| 438 | 438 | expect(built).toHaveLength(1); |
| 439 | 439 | expect(built[0]!.color).toBe("green"); |
| 440 | expect(built[0]!.text).toContain("The box brings it up on its next sync."); | |
| 440 | expect(built[0]!.text).toContain("The box brings it up on its next check-in."); | |
| 441 | 441 | const now = (await readCompose(app, box()))!; |
| 442 | 442 | expect(serviceEnv(now.text, "build")).toMatchObject({ TYPE: "PAPER", LEVEL_TYPE: "FLAT" }); |
| 443 | 443 | expect(chatLog(app, box().id, 1)[0]).toMatchObject({ kind: "yes", words: "give me a flat world called build" }); |
| @@ -540,7 +540,7 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"The hub re | ||
| 540 | 540 | const moved = await commitToBranch(app, ada, repo, { message: "a note", files: [{ path: "NOTE.md", content: "x" }] }); |
| 541 | 541 | if (!moved.ok) throw new Error(JSON.stringify(moved.error)); |
| 542 | 542 | await say("!forge yes"); |
| 543 | expect(told()[0]!.text).toBe("Not built: The box changed since the preview; plan it again."); | |
| 543 | expect(told()[0]!.text).toBe("Not built: The box changed since the preview; preview it again."); | |
| 544 | 544 | expect((await readCompose(app, box()))!.text).not.toContain("container_name: moved"); |
| 545 | 545 | }); |
| 546 | 546 | |
| @@ -622,7 +622,7 @@ echo '{"type":"result","subtype":"success","is_error":false,"result":"The hub re | ||
| 622 | 622 | const tip = (await readCompose(app, box()))!.sha; |
| 623 | 623 | job("yes", "a flat world called build", "0".repeat(40), "build"); |
| 624 | 624 | await sync({}); |
| 625 | expect(told().map((t) => t.text)).toEqual(["build is in the network. The box brings it up on its next sync."]); | |
| 625 | expect(told().map((t) => t.text)).toEqual(["build is in the network. The box brings it up on its next check-in."]); | |
| 626 | 626 | expect((await readCompose(app, box()))!.sha).toBe(tip); |
| 627 | 627 | }); |
| 628 | 628 |
test/game-door.test.ts +1 −1
| @@ -161,7 +161,7 @@ test("a typed yes builds nothing; the plan's own confirmation does, and chat's y | ||
| 161 | 161 | expect(jobs()).toBe(0); |
| 162 | 162 | expect(chatLog(app, box.id, 1)[0]).toMatchObject({ kind: "yes", outcome: "nothing waiting" }); |
| 163 | 163 | const [built] = await send([request("yes", { confirms: plan.id })]); |
| 164 | expect(built!.text).toContain("The box brings it up on its next sync."); | |
| 164 | expect(built!.text).toContain("The box brings it up on its next check-in."); | |
| 165 | 165 | expect((await readCompose(app, box))!.text).toContain("\n build:\n"); |
| 166 | 166 | }, 30_000); |
| 167 | 167 |
test/game-gist.test.ts +42 −5
| @@ -10,8 +10,9 @@ import { join } from "node:path"; | ||
| 10 | 10 | import { createApp, type App } from "../src/app.ts"; |
| 11 | 11 | import { createSession } from "../src/core/sessions.ts"; |
| 12 | 12 | import { createUser } from "../src/core/users.ts"; |
| 13 | import { joinAddress, mainPower, nowWords, PEOPLE_CHOICES, powerNow } from "../src/games/gist.ts"; | |
| 13 | import { joinAddress, mainPower, nowWords, PEOPLE_CHOICES, powerNow, PREVIEW_WORDS, SAVE_WORDS } from "../src/games/gist.ts"; | |
| 14 | 14 | import { synced } from "../src/games/notify.ts"; |
| 15 | import { proxyPort } from "../src/games/sleep.ts"; | |
| 15 | 16 | import { boxNamed, createGameBox, grant, queueCommand, servicesOf } from "../src/games/store.ts"; |
| 16 | 17 | import { forgeHttp, type ForgeHttp } from "../src/http/server.ts"; |
| 17 | 18 | import { quietLogs } from "./helpers/globals.ts"; |
| @@ -19,11 +20,47 @@ import { quietLogs } from "./helpers/globals.ts"; | ||
| 19 | 20 | quietLogs(); |
| 20 | 21 | |
| 21 | 22 | describe("the words", () => { |
| 22 | test("joinAddress and the choices", () => { | |
| 23 | expect(joinAddress("play.example.com")).toBe("play.example.com"); | |
| 24 | expect(joinAddress("bad host; rm")).toBeUndefined(); | |
| 25 | expect(joinAddress("")).toBeUndefined(); | |
| 23 | const at = (host: string, address = "") => ({ host, address }); | |
| 24 | test("joinAddress: a public name on 25565, on another port, and the private ones", () => { | |
| 25 | expect(joinAddress(at("play.example.com"))).toEqual({ address: "play.example.com" }); | |
| 26 | expect(joinAddress(at("play.example.com"), 25570)).toEqual({ address: "play.example.com:25570" }); | |
| 27 | expect(joinAddress(at("203.0.113.9"))).toEqual({ address: "203.0.113.9" }); | |
| 28 | expect(joinAddress(at("mc.tail1234.ts.net"))).toEqual({ private: "mc.tail1234.ts.net" }); | |
| 29 | expect(joinAddress(at("mc.lan"), 25570)).toEqual({ private: "mc.lan" }); | |
| 30 | expect(joinAddress(at("192.168.1.20"))).toEqual({ private: "192.168.1.20" }); | |
| 31 | expect(joinAddress(at("100.64.0.7"))).toEqual({ private: "100.64.0.7" }); | |
| 32 | expect(joinAddress(at("localhost"))).toEqual({ private: "localhost" }); | |
| 33 | // A private name with a public address: the address is what a friend uses. | |
| 34 | expect(joinAddress(at("mc.tail1234.ts.net", "203.0.113.9"))).toEqual({ address: "203.0.113.9" }); | |
| 35 | expect(joinAddress(at("bad host; rm"))).toBeUndefined(); | |
| 36 | expect(joinAddress(at(""))).toBeUndefined(); | |
| 26 | 37 | expect(joinAddress(undefined)).toBeUndefined(); |
| 38 | }); | |
| 39 | test("proxyPort reads the port the proxy publishes for 25565", () => { | |
| 40 | const file = (ports: string) => `services:\n proxy:\n image: itzg/mc-proxy\n ports:\n - "${ports}"\n`; | |
| 41 | expect(proxyPort(file("25565:25565"))).toBe(25565); | |
| 42 | expect(proxyPort(file("25570:25565"))).toBe(25570); | |
| 43 | expect(proxyPort("services:\n a:\n image: x\n")).toBe(25565); | |
| 44 | }); | |
| 45 | test("the docs name the changed words", async () => { | |
| 46 | const docs = await Bun.file("docs/features/games.md").text(); | |
| 47 | for (const w of ["Preview it", "Save change", "Keep it running", "check-in", "list of servers new players land on first"]) expect(docs).toContain(w); | |
| 48 | expect(SAVE_WORDS).toContain("check-in"); | |
| 49 | expect(PREVIEW_WORDS).toContain("Create it"); | |
| 50 | }); | |
| 51 | test("the settings form says Save and restart, and a player reads check-in, not sync", async () => { | |
| 52 | const page = await Bun.file("src/web/pages/games.ts").text(); | |
| 53 | const doc = await Bun.file("docs/features/game-settings.md").text(); | |
| 54 | expect(page).toContain('btn("Save and restart"'); | |
| 55 | for (const text of [page, doc]) expect(text).not.toContain("Commit and restart"); | |
| 56 | expect(doc).toContain("**Save and restart**"); | |
| 57 | expect(doc).toContain("next check-in"); | |
| 58 | expect(doc).not.toContain("next sync"); | |
| 59 | for (const f of ["src/web/pages/games.ts", "src/tui/screens/games.ts"]) { | |
| 60 | expect(await Bun.file(f).text()).toContain("its agent sends one with every check-in."); | |
| 61 | } | |
| 62 | }); | |
| 63 | test("the choices", () => { | |
| 27 | 64 | expect(PEOPLE_CHOICES.map((c) => c[2])).toEqual(["whitelist", "op", "ban", "pardon"]); |
| 28 | 65 | }); |
| 29 | 66 | }); |
test/game-join.test.ts added +147
| @@ -0,0 +1,147 @@ | ||
| 1 | /** | |
| 2 | * Where a friend joins (#17670): the box page and the terminal give the machine's public name, with | |
| 3 | * the proxy's port when it is not 25565; a private name is said not to work from outside and is not | |
| 4 | * offered for Copy; a box with no machine says there is no address yet. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { insertPinned } from "../src/core/pinned.ts"; | |
| 13 | import { createSession } from "../src/core/sessions.ts"; | |
| 14 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 15 | import { NO_ADDRESS, privateWords, SAVE_WORDS } from "../src/games/gist.ts"; | |
| 16 | import { boxNamed, createGameBox, grant, recordSync } from "../src/games/store.ts"; | |
| 17 | import { forgeHttp, type ForgeHttp } from "../src/http/server.ts"; | |
| 18 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 19 | import { gameServiceScreen, gamesScreen } from "../src/tui/screens/games.ts"; | |
| 20 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 21 | import { quietLogs } from "./helpers/globals.ts"; | |
| 22 | import { settleApp, settleUntil } from "./helpers/tui.ts"; | |
| 23 | ||
| 24 | quietLogs(); | |
| 25 | ||
| 26 | const compose = (published: string) => `services: | |
| 27 | hub: | |
| 28 | image: itzg/minecraft-server | |
| 29 | velocity: | |
| 30 | image: itzg/mc-proxy:latest | |
| 31 | ports: | |
| 32 | - "${published}:25565" | |
| 33 | `; | |
| 34 | ||
| 35 | let dir = ""; | |
| 36 | let app: App; | |
| 37 | let http: ForgeHttp; | |
| 38 | const users = new Map<string, User>(); | |
| 39 | const BOXES = [ | |
| 40 | ["plain", "play.example.com", "a/compose.yml"], | |
| 41 | ["ported", "play.example.com", "b/compose.yml"], | |
| 42 | ["tail", "mc.tail1234.ts.net", "a/compose.yml"], | |
| 43 | ["bare", undefined, "a/compose.yml"], | |
| 44 | ] as const; | |
| 45 | ||
| 46 | beforeAll(async () => { | |
| 47 | dir = await mkdtemp(join(tmpdir(), "forge-game-join-")); | |
| 48 | app = createApp({ dataDir: dir, port: 0, modrinthFetch: async () => Response.json([]) }); | |
| 49 | http = forgeHttp(app); | |
| 50 | for (const [i, username] of ["ownr", ...BOXES.map((b) => `v${b[0]}`)].entries()) { | |
| 51 | const made = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@f.test`, password: "a-good-passphrase", adminIfFirst: i === 0 }); | |
| 52 | if (!made.ok) throw new Error(made.error); | |
| 53 | users.set(username, getUserByName(app.db, username)!); | |
| 54 | } | |
| 55 | const owner = users.get("ownr")!; | |
| 56 | const repo = await createRepo(app, owner, { name: "mc-network", visibility: "private" }); | |
| 57 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 58 | const seeded = await commitToBranch(app, owner, repo.value, { | |
| 59 | message: "network", | |
| 60 | files: [ | |
| 61 | { path: "a/compose.yml", content: compose("25565") }, | |
| 62 | { path: "b/compose.yml", content: compose("25570") }, | |
| 63 | ], | |
| 64 | }); | |
| 65 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 66 | for (const [name, host, path] of BOXES) { | |
| 67 | let pinnedId = ""; | |
| 68 | if (host !== undefined) { | |
| 69 | const m = insertPinned( | |
| 70 | app.db, | |
| 71 | { name: `m-${name}`, target: "digitalocean", account: "main", app: "", source: "", version: "", config: {}, host, claimHash: "", claimExpires: "", datacenter: "nyc1", size: "s-4vcpu-8gb" }, | |
| 72 | owner.id, | |
| 73 | ); | |
| 74 | pinnedId = m.id; | |
| 75 | } | |
| 76 | const box = createGameBox(app.db, { name, repo: "ownr/mc-network", path, pinnedId }); | |
| 77 | recordSync(app.db, boxNamed(app.db, name)!, { composeSha: "", services: [{ name: "hub", state: "running", health: "", startedAt: "", cpuPct: 1, memMiB: 900 }], logs: {}, results: [], host: { memMiB: 8000, memUsedMiB: 4000, load1: 0.2, diskFreeGiB: 60 } }); | |
| 78 | grant(app.db, box.id, users.get(`v${name}`)!.id, "viewer"); | |
| 79 | } | |
| 80 | }, 30_000); | |
| 81 | ||
| 82 | afterAll(async () => { | |
| 83 | app.close(); | |
| 84 | await rm(dir, { recursive: true, force: true }); | |
| 85 | }); | |
| 86 | ||
| 87 | const page = async (name: string): Promise<string> => { | |
| 88 | const cookie = `forge_session=${createSession(app.db, users.get(`v${name}`)!.id).token}`; | |
| 89 | return (await http.handle(new Request(`http://forge.test/games/${name}`, { headers: { cookie } }))).text(); | |
| 90 | }; | |
| 91 | const terminal = async (name: string, want: string): Promise<string> => { | |
| 92 | const term = headlessTerminal(200, 30); | |
| 93 | const user = users.get(`v${name}`)!; | |
| 94 | const { app: tui, session } = createForgeApp(app, { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }, { terminal: term, exitHooks: false }); | |
| 95 | tui.start(); | |
| 96 | tui.ctx.push(gamesScreen(session, tui.ctx)); | |
| 97 | await settleApp(tui); | |
| 98 | await settleUntil(tui, want); | |
| 99 | return tui.screen.toText(); | |
| 100 | }; | |
| 101 | ||
| 102 | test("a public name on 25565 is the address, with Copy", async () => { | |
| 103 | const html = await page("plain"); | |
| 104 | expect(html).toContain('<code class="mono" id="game-address">play.example.com</code>'); | |
| 105 | expect(html).toContain("Copy the server address"); | |
| 106 | }); | |
| 107 | ||
| 108 | test("a public name with the proxy on another port carries the port", async () => { | |
| 109 | expect(await page("ported")).toContain('id="game-address">play.example.com:25570</code>'); | |
| 110 | }); | |
| 111 | ||
| 112 | test("a private name is not offered: the page says why and what to set", async () => { | |
| 113 | const html = await page("tail"); | |
| 114 | expect(html).not.toContain('id="game-address"'); | |
| 115 | expect(html).not.toContain("Copy the server address"); | |
| 116 | expect(html).toContain(privateWords("mc.tail1234.ts.net").replace("'", "'")); | |
| 117 | }); | |
| 118 | ||
| 119 | test("a box with no machine says there is no address yet", async () => { | |
| 120 | const html = await page("bare"); | |
| 121 | expect(html).toContain(NO_ADDRESS); | |
| 122 | expect(html).not.toContain('id="game-address"'); | |
| 123 | }); | |
| 124 | ||
| 125 | test("the terminal says what the page says", async () => { | |
| 126 | expect(await terminal("plain", "To join, add this server in Minecraft: play.example.com")).toContain("play.example.com"); | |
| 127 | expect(await terminal("ported", "play.example.com:25570")).toContain("play.example.com:25570"); | |
| 128 | expect(await terminal("tail", "a private one")).toContain("a friend outside cannot join with it"); | |
| 129 | expect(await terminal("bare", "no machine name")).toContain("no machine name"); | |
| 130 | }); | |
| 131 | ||
| 132 | test("the env form says the same on the page and in the terminal, and nothing says Committing", async () => { | |
| 133 | const owner = users.get("ownr")!; | |
| 134 | const cookie = `forge_session=${createSession(app.db, owner.id).token}`; | |
| 135 | const html = await (await http.handle(new Request("http://forge.test/games/plain/hub", { headers: { cookie } }))).text(); | |
| 136 | expect(html).toContain("Save change"); | |
| 137 | expect(html).toContain(SAVE_WORDS.replace("'", "'")); | |
| 138 | const term = headlessTerminal(200, 30); | |
| 139 | const { app: tui, session } = createForgeApp(app, { sshUsername: owner.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user: owner }, { terminal: term, exitHooks: false }); | |
| 140 | tui.start(); | |
| 141 | tui.ctx.push(gameServiceScreen(session, tui.ctx, boxNamed(app.db, "plain")!, "hub")); | |
| 142 | await settleApp(tui); | |
| 143 | term.feed("\t"); | |
| 144 | await settleApp(tui); | |
| 145 | expect(tui.screen.toText()).toContain(SAVE_WORDS); | |
| 146 | expect(await Bun.file("src/tui/screens/games.ts").text()).not.toMatch(/ommitting/); | |
| 147 | }); |
test/game-mods.test.ts +2 −2
| @@ -389,7 +389,7 @@ describe("Modrinth", () => { | ||
| 389 | 389 | "Adds Chunky 1.4.28", |
| 390 | 390 | "Adds ChunkyLib 1.0.2 (required by Chunky)", |
| 391 | 391 | "Pins EssentialsX 2.21.0 (it was in MODRINTH_PROJECTS unpinned)", |
| 392 | "Restarts the server with the new set on the box's next sync", | |
| 392 | "Restarts the server with the new set on the box's next check-in", | |
| 393 | 393 | ]); |
| 394 | 394 | const done = await commitPlan(app, alice, box, "hub", m, plan.value); |
| 395 | 395 | if (!done.ok) throw new Error(done.error); |
| @@ -545,7 +545,7 @@ describe("modpacks", () => { | ||
| 545 | 545 | "2 override files: overrides/, then server-overrides/ over them", |
| 546 | 546 | "Changes Minecraft 1.21.4 → 1.21.1: a world saved by a newer Minecraft may not open in an older one", |
| 547 | 547 | "The image does not check a pack's file hashes; the lockfile keeps each one, so any change shows in review", |
| 548 | "Restarts the server with the pack on the box's next sync", | |
| 548 | "Restarts the server with the pack on the box's next check-in", | |
| 549 | 549 | ]); |
| 550 | 550 | expect((await commitPlan(app, alice, box, "survival", m, plan.value)).ok).toBe(true); |
| 551 | 551 | const compose = (await readCompose(app, box))!; |
test/game-presets.test.ts +1 −1
| @@ -308,7 +308,7 @@ describe("on a forge", () => { | ||
| 308 | 308 | expect(plan["planned"]).toBe(true); |
| 309 | 309 | const create = { action: "create_server", box: "mc", words: "a flat world called build", dryRun: false }; |
| 310 | 310 | // No sha, or another commit's, is a yes to a plan nobody was shown. |
| 311 | for (const sha of [undefined, "0".repeat(40)]) expect((await call({ ...create, sha }))["refused"]).toContain("The box changed since the preview; plan it again."); | |
| 311 | for (const sha of [undefined, "0".repeat(40)]) expect((await call({ ...create, sha }))["refused"]).toContain("The box changed since the preview; preview it again."); | |
| 312 | 312 | const made = await call({ ...create, sha: plan["sha"] }); |
| 313 | 313 | expect(made["says"]).toContain("build is in the network, at build.mc.trevato.dev"); |
| 314 | 314 | const box = boxNamed(app.db, "mc")!; |
test/game-settings.test.ts +4 −4
| @@ -83,7 +83,7 @@ describe("a change", () => { | ||
| 83 | 83 | "VIEW_DISTANCE 10 → 12 (env)", |
| 84 | 84 | "paper-world-defaults entities.spawning.per-player-mob-spawns true → false (patch)", |
| 85 | 85 | "paper-global misc.max-joins-per-tick 5 → 10 (patch)", |
| 86 | "Restarts the server with the new settings on the box's next sync", | |
| 86 | "Restarts the server with the new settings on the box's next check-in", | |
| 87 | 87 | ]); |
| 88 | 88 | const text = plan.value.edit(COMPOSE); |
| 89 | 89 | const d = doc(text); |
| @@ -196,7 +196,7 @@ describe("server.properties without an env var, and the image's other ways", () | ||
| 196 | 196 | expect(plan.value.lines).toEqual([ |
| 197 | 197 | "enable-code-of-conduct false → true (env CUSTOM_SERVER_PROPERTIES)", |
| 198 | 198 | "WHITELIST_PROP true → false (env)", |
| 199 | "Restarts the server with the new settings on the box's next sync", | |
| 199 | "Restarts the server with the new settings on the box's next check-in", | |
| 200 | 200 | ]); |
| 201 | 201 | expect(plan.value.keys).toEqual(["enable-code-of-conduct", "white-list"]); |
| 202 | 202 | const text = plan.value.edit(LOBBY); |
| @@ -322,13 +322,13 @@ describe("on the service page, one commit on the commit previewed", () => { | ||
| 322 | 322 | const box = boxNamed(app.db, "net")!; |
| 323 | 323 | const before = (await readCompose(app, box))!.sha; |
| 324 | 324 | const plan = await call({ action: "config_set", key: "simulation-distance", value: "8" }); |
| 325 | expect(plan).toMatchObject({ lines: ["SIMULATION_DISTANCE 10 → 8 (env)", "Restarts the server with the new settings on the box's next sync"] }); | |
| 325 | expect(plan).toMatchObject({ lines: ["SIMULATION_DISTANCE 10 → 8 (env)", "Restarts the server with the new settings on the box's next check-in"] }); | |
| 326 | 326 | expect((await readCompose(app, box))!.sha).toBe(before); |
| 327 | 327 | expect(await call({ action: "config_set", key: "simulation-distance", value: "99", dryRun: false })).toEqual({ |
| 328 | 328 | refused: expect.stringContaining("simulation-distance is a whole number from 3 to 32."), |
| 329 | 329 | }); |
| 330 | 330 | const done = await call({ action: "config_set", key: "simulation-distance", value: "8", dryRun: false }); |
| 331 | expect(String(done["says"])).toStartWith("Committed "); | |
| 331 | expect(String(done["says"])).toStartWith("Saved "); | |
| 332 | 332 | expect(serviceEnv((await readCompose(app, box))!.text, "hub")["SIMULATION_DISTANCE"]).toBe("8"); |
| 333 | 333 | }); |
| 334 | 334 |
test/game-sleep.test.ts +1 −1
| @@ -239,7 +239,7 @@ describe("changing it", () => { | ||
| 239 | 239 | test("the service page offers it, and its commit writes the label, the waker and the plugin", async () => { |
| 240 | 240 | const page = await (await http.handle(new Request("http://forge.test/games/net/hunting", { headers: { cookie } }))).text(); |
| 241 | 241 | expect(page).toContain("<h2>Sleep</h2>"); |
| 242 | expect(page).toContain("Kept warm: it runs whether or not anyone is on it.</p>"); | |
| 242 | expect(page).toContain("Kept running: it runs whether or not anyone is on it.</p>"); | |
| 243 | 243 | const proxy = await (await http.handle(new Request("http://forge.test/games/net/velocity", { headers: { cookie } }))).text(); |
| 244 | 244 | expect(proxy).not.toContain("<h2>Sleep</h2>"); |
| 245 | 245 | const box = boxNamed(app.db, "net")!; |
test/tui-games.test.ts +3 −3
| @@ -129,7 +129,7 @@ test("a service's console shows its log and queues an rcon command; env commits | ||
| 129 | 129 | await type("e"); |
| 130 | 130 | await type("MOTD=welcome"); |
| 131 | 131 | terminal.feed("\r"); |
| 132 | await settleUntil(tui, "Committed "); | |
| 132 | await settleUntil(tui, "Saved "); | |
| 133 | 133 | expect(serviceEnv((await readCompose(app, box))!.text, "hub")["MOTD"]).toBe("welcome"); |
| 134 | 134 | expect(tui.screen.toText()).toContain("MOTD=welcome"); |
| 135 | 135 | // Mods: the page's model, its refusals in its words. |
| @@ -248,7 +248,7 @@ test("settings: `c` from a service lists them; a value is previewed, a bad one r | ||
| 248 | 248 | terminal.feed("\r"); |
| 249 | 249 | await settleUntil(tui, "MAX_PLAYERS 20 → 200 (env)"); |
| 250 | 250 | await type("y"); |
| 251 | await settleUntil(tui, "Committed "); | |
| 251 | await settleUntil(tui, "Saved "); | |
| 252 | 252 | expect(serviceEnv((await readCompose(app, box))!.text, "hub")["MAX_PLAYERS"]).toBe("200"); |
| 253 | 253 | tui.stop?.(); |
| 254 | 254 | }); |
| @@ -262,7 +262,7 @@ test("z on the games list: choosing sleep says the machine is still billed; goin | ||
| 262 | 262 | await settleUntil(tui, "sleeps when empty"); |
| 263 | 263 | expect(tui.screen.toText().replace(/\s+/g, " ")).toContain(STILL_BILLED); |
| 264 | 264 | await type("z"); |
| 265 | await settleUntil(tui, "stays warm"); | |
| 265 | await settleUntil(tui, "stays running"); | |
| 266 | 266 | expect(tui.screen.toText()).not.toContain("still billed"); |
| 267 | 267 | tui.stop?.(); |
| 268 | 268 | }); |
new above
offline
Leftover 'Committing…' beside the new 'Saved' wording on the same screen (box 3). Say 'Saving…'.