Public: anyone can read this; the crew can change it.

Games: a new server's words are searched on Modrinth, not matched against five names #18041

opened this

Merged 12h 55m ago, live in production

sold/server-themes → main 0 commits

  • Merged. sold/server-themes was merged into main as 31cdee4.

Review

Sign in to review this change.
3 files changed +60 −5
docs/features/games.md +1 −1
@@ -80,7 +80,7 @@ Uploading a world file is still to come: a link is how a world comes in today.
8080 - 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;
8181 - where players join: `<name>.<the network's domain>` (the wildcard name already points at the box) or `/server <name>`.
8282
83 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.
83 Words beyond the few it knows are themes: "zombie survival with custom guns and world gen" becomes zombie survival, custom guns and worldgen, and each is searched on Modrinth for the most relevant plugin that runs on the server's Paper version and has at least 1,000 downloads (`src/games/presets.ts`). The preview names each pick and its downloads, and says when a theme found none, before anything is written. 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.
8484
8585 ## From in the game
8686
src/games/presets.ts +36 −4
@@ -13,7 +13,7 @@ import type { App } from "../app.ts";
1313 import type { User } from "../core/users.ts";
1414 import { commitCompose, composeServices, readCompose, serviceEnv, setTopEntry } from "./compose.ts";
1515 import { lockPath, lockText, planInstall, type Mods } from "./mods.ts";
16 import { targetOf, type Locked, type Result } from "./modrinth.ts";
16 import { search, targetOf, type Locked, type Result } from "./modrinth.ts";
1717 import { isGameServer, policyOf, proxyOf, setPolicy } from "./sleep.ts";
1818 import { isWaker, SERVICE_NAME, servicesOf, WAKER, type GameBox } from "./store.ts";
1919
@@ -34,9 +34,16 @@ export interface Wish {
3434 readonly level: Level;
3535 readonly plugins: readonly string[];
3636 readonly memory: string;
37 /** What else the words ask for ("custom guns", "zombie survival"), each searched on Modrinth. */
38 readonly themes?: readonly string[];
3739 }
3840
41 /** Words that say nothing about what a server is: left out of a theme. */
42 const FILLER = /\b(?:give|me|make|build|create|i|want|need|a|an|the|new|server|world|map|with|and|plus|some|please|latest|greatest|best|out|of|box|cool|awesome|great|good|really|very|that|has|have|lots|full|for|to|in|on|it|action|fun)\b/g;
43
3944 const MEMORY = "1536M";
45 /** A plugin found by a theme is one people use: fewer downloads than this is not picked. */
46 const MIN_DOWNLOADS = 1_000;
4047 /** What the box keeps for itself and Docker, out of its memory. */
4148 const KEPT_MIB = 1024;
4249
@@ -59,11 +66,24 @@ export function parseWish(words: string, existing: readonly string[] = []): Wish
5966 ),
6067 ];
6168 const called = /\b(?:called|named)\s+([a-z0-9][a-z0-9-]{0,30})/.exec(w)?.[1];
69 // The rest, split where a sentence lists things, each part without the words above: a theme.
70 const rest = Object.keys(ALIASES).reduce((s, k) => s.replace(new RegExp(`\\b${k}\\b`, "g"), " "), w)
71 .replace(/\b(?:called|named)\s+[a-z0-9-]+/, " ")
72 .replace(/\b(?:void|empty|skyblock|flat|superflat|normal)\b/g, " ")
73 .replace(/\bworld ?gen(?:eration)?\b/g, "worldgen");
74 const themes = [
75 ...new Set(
76 rest
77 .split(/[,;.]|\bwith\b|\band\b|\bplus\b/)
78 .map((part) => part.replace(/[^a-z0-9 ]/g, " ").replace(FILLER, " ").replace(/\s+/g, " ").trim())
79 .filter((part) => part.length >= 3),
80 ),
81 ].slice(0, 4);
6282 const base =
6383 called ?? (plugins.includes("screamingbedwars") ? "bedwars" : level === "void" ? "void" : level === "flat" ? "creative" : "world");
6484 let name = base;
6585 for (let i = 2; existing.includes(name); i++) name = `${base}-${i}`;
66 return { name, level, plugins, memory: MEMORY };
86 return { name, level, plugins, memory: MEMORY, themes };
6787 }
6888
6989 /**
@@ -226,8 +246,20 @@ export async function planServer(app: App, box: GameBox, wish: Wish): Promise<Re
226246 if (!target.ok) return target;
227247 let m: Mods = { target: target.value, lock: [], loose: [], urls: [], held: [], sha: compose.sha, env, pack: undefined };
228248 const pluginLines: string[] = [];
249 // Each theme: the most fitting plugin Modrinth finds for it that runs here and is well used.
250 const chosen = [...wish.plugins];
251 for (const theme of wish.themes ?? []) {
252 const hits = await search(app, target.value, theme);
253 const hit = hits.ok ? hits.value.find((h) => h.fits && h.downloads >= MIN_DOWNLOADS && !chosen.includes(h.slug)) : undefined;
254 if (hit === undefined) {
255 pluginLines.push(`For "${theme}": nothing on Modrinth runs on ${target.value.label} with ${String(MIN_DOWNLOADS)} downloads or more.`);
256 continue;
257 }
258 chosen.push(hit.slug);
259 pluginLines.push(`For "${theme}": ${hit.title} (${hit.downloads.toLocaleString("en-US")} downloads), found on Modrinth.`);
260 }
229261 let pinned: readonly Locked[] = [];
230 for (const slug of wish.plugins) {
262 for (const slug of chosen) {
231263 const planned = await planInstall(app, m, wish.name, slug);
232264 if (!planned.ok) return { ok: false, error: `${slug}: ${planned.error}` };
233265 pinned = planned.value.lock;
@@ -277,7 +309,7 @@ export async function planServer(app: App, box: GameBox, wish: Wish): Promise<Re
277309 ],
278310 text,
279311 lock: pinned.length === 0 ? undefined : { path: lockPath(box, wish.name), content: lockText(m.target, pinned, undefined) },
280 message: `games: new server ${wish.name} (${levelWords}${wish.plugins.length === 0 ? "" : ` with ${wish.plugins.join(", ")}`})`,
312 message: `games: new server ${wish.name} (${levelWords}${chosen.length === 0 ? "" : ` with ${chosen.join(", ")}`})`,
281313 sha: compose.sha,
282314 done: `${wish.name} is in the network${host === undefined ? "" : `, at ${host}`}`,
283315 },
test/game-presets.test.ts +23
@@ -101,6 +101,7 @@ const PROJECTS = [
101101 { id: "FAid0001", slug: "fastasyncworldedit", title: "FastAsyncWorldEdit", games: ["1.21.6"] },
102102 { id: "SBid0001", slug: "screamingbedwars", title: "Screaming BedWars", games: ["1.21.6"] },
103103 { id: "OLid0001", slug: "oldthing", title: "Old Thing", games: ["1.20.1"] },
104 { id: "GNid0001", slug: "gunplay", title: "Gun Play", games: ["1.21.6"] },
104105 ];
105106 const wire = (p: (typeof PROJECTS)[number]) => ({
106107 id: `${p.id.slice(0, 2)}v00001`,
@@ -137,6 +138,12 @@ async function modrinth(url: string): Promise<Response> {
137138 ),
138139 );
139140 }
141 // Search: "guns" finds Gun Play (well used) and a fresh one nobody uses; anything else finds nothing.
142 if (p === "/v2/search") {
143 const q = u.searchParams.get("query") ?? "";
144 const hit = (slug: string, id: string, downloads: number) => ({ project_id: id, slug, title: slug === "gunplay" ? "Gun Play" : slug, author: "a", description: "", downloads, versions: ["1.21.6"], categories: ["paper"], server_side: "required" });
145 return Response.json({ hits: /guns/.test(q) ? [hit("brandnew", "BNid0001", 3), hit("gunplay", "GNid0001", 52_000)] : [] });
146 }
140147 const one = /^\/v2\/version\/([^/]+)$/.exec(p);
141148 if (one) {
142149 const x = PROJECTS.find((y) => wire(y).id === one[1]);
@@ -152,7 +159,14 @@ describe("the pieces", () => {
152159 level: "void",
153160 plugins: ["fastasyncworldedit", "screamingbedwars"],
154161 memory: "1536M",
162 themes: [],
155163 });
164 // Words beyond the five it knows become themes, each searched on Modrinth.
165 expect(parseWish("the latest and greatest zombie survival with custom guns and world gen. out of the box action").themes).toEqual([
166 "zombie survival",
167 "custom guns",
168 "worldgen",
169 ]);
156170 expect(parseWish("a flat world called build")).toMatchObject({ name: "build", level: "flat", plugins: [] });
157171 expect(parseWish("bedwars", ["bedwars"]).name).toBe("bedwars-2");
158172 expect([heapMiB("1G"), heapMiB("1536M"), heapMiB("768m"), heapMiB(undefined)]).toEqual([1024, 1536, 768, 1024]);
@@ -267,6 +281,15 @@ describe("on a forge", () => {
267281 expect((await wishOn(app, box, "a flat world called forge-waker")).name).toBe(`${WAKER}-2`);
268282 });
269283
284 test("themes are searched on Modrinth: a well used plugin that runs here is picked, and a theme with none says so", async () => {
285 const planned = await planServer(app, boxNamed(app.db, "mc")!, parseWish("zombie survival with custom guns called zs"));
286 if (!planned.ok) throw new Error(planned.error);
287 expect(planned.value.lines).toContain('For "custom guns": Gun Play (52,000 downloads), found on Modrinth.');
288 expect(planned.value.lines.some((l) => l.startsWith('For "zombie survival": nothing on Modrinth runs on'))).toBe(true);
289 expect(planned.value.lock?.content).toContain("gunplay");
290 expect(planned.value.lock?.content).not.toContain("brandnew");
291 });
292
270293 test("a plugin that does not run there, a name taken, or too little memory is refused before anything is written", async () => {
271294 const box = boxNamed(app.db, "mc")!;
272295 const old = await planServer(app, box, { name: "relic", level: "normal", plugins: ["oldthing"], memory: "1G" });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs