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

One refusal that Velocity and AutoStopper both log is one event #17744

opened this

Merged 16h 56m ago, live in production

sold/one-refusal → main 0 commits

  • Merged. sold/one-refusal was merged into main as 5cd3596.

Review

Sign in to review this change.
2 files changed +109 −9
src/games/player_events.ts +66 −9
@@ -57,7 +57,32 @@ const ADDRESS = new RegExp(`\\[?(?:(?<![\\w:])(?:${HEXES})?::(?:(?:[0-9a-f]{1,4}
5757 const noAddress = (text: string): string => text.replace(ADDRESS, "(address)");
5858 const clockOf = (head: string): string => /\d{2}:\d{2}:\d{2}/.exec(head)?.[0] ?? "";
5959
60 /** What the proxy's `lines` say, oldest first; `before` are the lines it wrote just before them. */
60 /** How far apart Velocity's line and AutoStopper's for one refusal may be. */
61 const PAIR_S = 5;
62 const DAY_S = 86_400;
63 /** The seconds between two clocks, over midnight too; NaN when either is none. */
64 const apart = (a: string, b: string): number => {
65 const at = (clock: string): number => clock.split(":").reduce((sum, part) => sum * 60 + Number(part), 0);
66 const gap = a === "" || b === "" ? Number.NaN : Math.abs(at(a) - at(b));
67 return Math.min(gap, DAY_S - gap);
68 };
69 /** One refusal: the clock of the line that said it first, and of each line that has said it. */
70 interface Refusal {
71 readonly server: string;
72 player: string;
73 readonly clock: string;
74 velocity?: string;
75 autostopper?: string;
76 /** Where its fact is among this call's; none for one from the lines before. */
77 at?: number;
78 }
79
80 /**
81 * What the proxy's `lines` say, oldest first; `before` are the lines it wrote just before them.
82 * The forge's AutoStopper leaves a refusal to Velocity, so each writes a line for it, in either
83 * order: one of each kind, for one player and server and a few seconds apart, is one refusal. Two
84 * of one kind are two tries, however close.
85 */
6186 export function proxyFacts(lines: readonly string[], before: readonly string[] = []): ProxyFact[] {
6287 const out: ProxyFact[] = [];
6388 // Who was last dropped by each server: AutoStopper's line names no player.
@@ -66,7 +91,37 @@ export function proxyFacts(lines: readonly string[], before: readonly string[] =
6691 const m = CLOSED.exec(text);
6792 if (m !== null) closed.set(m[4]!, m[3]!);
6893 };
69 for (const line of before) close(plain(line).trimEnd());
94 const refusals: Refusal[] = [];
95 const refuse = (by: "velocity" | "autostopper", server: string, player: string, text: string, clock: string, old: boolean): void => {
96 const same = refusals.filter((r) => r.server === server && (r.player === player || r.player === "" || player === ""));
97 // The second of a pair, sent again: the pair is known. The first goes the way it did, and is kept once.
98 if (same.some((r) => r[by] === clock && r.velocity !== undefined && r.autostopper !== undefined && r.clock !== clock)) return;
99 const other = same.find((r) => r[by] === undefined && apart(r.clock, clock) <= PAIR_S);
100 if (other === undefined) {
101 refusals.push({ server, player, clock, [by]: clock, ...(old ? {} : { at: out.length }) });
102 if (!old) out.push({ kind: "refused", server, player, text, clock });
103 return;
104 }
105 other[by] = clock;
106 if (other.player === "") other.player = player;
107 if (by === "autostopper" || old) return;
108 const fact: ProxyFact = { kind: "refused", server, player, text, clock: other.clock };
109 if (other.at !== undefined) out[other.at] = fact;
110 else if (text !== "") out.push(fact);
111 };
112 const waitedOut = (m: RegExpExecArray, old: boolean): void => {
113 const server = m[4]!;
114 if (m[5] === "SERVER_DISCONNECTED" || m[5] === "CONNECTION_FAILED") refuse("autostopper", server, closed.get(server) ?? "", "", clockOf(m[1]!), old);
115 closed.delete(server);
116 };
117 for (const line of before) {
118 const text = plain(line).trimEnd();
119 close(text);
120 const v = REFUSED.exec(text);
121 if (v !== null) refuse("velocity", v[4]!, v[3]!, "", clockOf(v[1]!), true);
122 const w = LIFECYCLE.exec(text);
123 if (w?.[3] === "CONNECTION_WAIT") waitedOut(w, true);
124 }
70125 const texts = lines.map((l) => plain(l).trimEnd());
71126 for (const [i, text] of texts.entries()) {
72127 close(text);
@@ -78,17 +133,15 @@ export function proxyFacts(lines: readonly string[], before: readonly string[] =
78133 under.push(next.trim());
79134 }
80135 const reason = [m[5]!, ...under].filter((l) => l !== "").join(" ");
81 out.push({ kind: "refused", server: m[4]!, player: m[3]!, text: reason === NO_REASON ? "" : noAddress(reason).slice(0, REASON_MAX), clock: clockOf(m[1]!) });
136 refuse("velocity", m[4]!, m[3]!, reason === NO_REASON ? "" : noAddress(reason).slice(0, REASON_MAX), clockOf(m[1]!), false);
82137 continue;
83138 }
84139 m = LIFECYCLE.exec(text);
85140 if (m === null) continue;
86141 const [op, server, outcome, took] = [m[3]!, m[4]!, m[5]!, `${String(Math.round(Number(m[6]!) / 1000))} s`];
87142 const clock = clockOf(m[1]!);
88 if (op === "CONNECTION_WAIT") {
89 if (outcome === "SERVER_DISCONNECTED" || outcome === "CONNECTION_FAILED") out.push({ kind: "refused", server, player: closed.get(server) ?? "", text: "", clock });
90 closed.delete(server);
91 } else if (outcome === "READY") out.push({ kind: "wake", server, player: "", text: took, clock });
143 if (op === "CONNECTION_WAIT") waitedOut(m, false);
144 else if (outcome === "READY") out.push({ kind: "wake", server, player: "", text: took, clock });
92145 else if (outcome !== "PROXY_SHUTDOWN" && outcome !== "CANCELLED") out.push({ kind: "stuck", server, player: "", text: `${outcome} after ${took}`, clock });
93146 }
94147 return out;
@@ -118,9 +171,13 @@ export function recordProxyEvents(db: Database, box: Pick<GameBox, "id">, logs:
118171 // A server the proxy calls by a name no service has stays on the proxy's own page.
119172 const service = SERVICE_NAME.test(f.server) && known.has(f.server) ? f.server : proxy;
120173 const again = db
121 .query("SELECT 1 FROM game_player_event WHERE box_id = ?1 AND service = ?2 AND kind = ?3 AND clock = ?4 AND at > ?5 AND (player = ?6 OR ?6 = '' OR player = '') LIMIT 1")
174 .query<{ id: number; text: string }, string[]>("SELECT id, text FROM game_player_event WHERE box_id = ?1 AND service = ?2 AND kind = ?3 AND clock = ?4 AND at > ?5 AND (player = ?6 OR ?6 = '' OR player = '') LIMIT 1")
122175 .get(box.id, service, f.kind, f.clock, since, f.player);
123 if (again !== null) continue;
176 if (again !== null) {
177 // The reason, and perhaps the player, came a sync after the refusal itself.
178 if (again.text === "" && f.text !== "") db.query("UPDATE game_player_event SET text = ?2, player = CASE player WHEN '' THEN ?3 ELSE player END WHERE id = ?1").run(again.id, f.text, f.player);
179 continue;
180 }
124181 db.query("INSERT INTO game_player_event (box_id, service, player, kind, at, text, clock) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)").run(box.id, service, f.player, f.kind, now.toISOString(), f.text, f.clock);
125182 added++;
126183 }
test/game-player-events.test.ts +43
@@ -88,6 +88,27 @@ Contact the server's administrator for more information!
8888 expect(told).not.toContain("said:");
8989 });
9090
91 // The forge's AutoStopper leaves a refusal to Velocity: Velocity's line, then its own.
92 test("Velocity's line and AutoStopper's for one refusal are one event, also across a second, midnight and two syncs", () => {
93 const clocks = [["12:00:02", "12:00:02"], ["12:00:59", "12:01:00"], ["23:59:59", "00:00:00"]] as const;
94 for (const [a, b] of clocks) {
95 expect(proxyFacts([down(a), refused(a), waited(b)])).toEqual([{ kind: "refused", server: "survival", player: "Alice", text: REASON, clock: a }]);
96 expect(proxyFacts([waited(b)], [down(a), refused(a)])).toEqual([]);
97 // The other order, which no log has shown yet: the reason joins the event that is there.
98 expect(proxyFacts([down(a), waited(a), refused(b)])).toEqual([{ kind: "refused", server: "survival", player: "Alice", text: REASON, clock: a }]);
99 expect(proxyFacts([refused(b)], [down(a), waited(a)])).toEqual([{ kind: "refused", server: "survival", player: "Alice", text: REASON, clock: a }]);
100 }
101 });
102
103 test("a second real try right after is an event of its own, and so is another player's", () => {
104 const tries = [down("12:00:01"), refused("12:00:01"), waited("12:00:02"), down("12:00:03"), refused("12:00:03"), waited("12:00:04")];
105 expect(proxyFacts(tries).map((f) => f.clock)).toEqual(["12:00:01", "12:00:03"]);
106 // AutoStopper's line alone, as the one from Modrinth writes it, belongs to no earlier refusal.
107 expect(proxyFacts([down("12:00:01"), refused("12:00:01"), waited("12:00:02"), down("12:00:03"), waited("12:00:03")]).map((f) => f.clock)).toEqual(["12:00:01", "12:00:03"]);
108 expect(proxyFacts([refused("12:00:01"), down("12:00:02", "Bob"), waited("12:00:02")]).map((f) => f.player)).toEqual(["Alice", "Bob"]);
109 expect(proxyFacts([down("12:00:01"), refused("12:00:01"), down("12:05:00"), waited("12:05:00")])).toHaveLength(2);
110 });
111
91112 test("a wake and how long it took; one that did not come up", () => {
92113 expect(proxyFacts([started("12:00:00")])).toEqual([{ kind: "wake", server: "survival", player: "", text: "52 s", clock: "12:00:00" }]);
93114 expect(proxyFacts([started("12:00:00", "SERVER_NOT_READY")])[0]).toMatchObject({ kind: "stuck", text: "SERVER_NOT_READY after 52 s" });
@@ -196,6 +217,28 @@ describe("on a forge", () => {
196217 expect(f!.fix[1]).toContain("Sleep off");
197218 });
198219
220 test("one refusal told by two lines a second apart, in one sync or two, is one event", async () => {
221 const before = refusals().length;
222 await sync({ velocity: [down("12:10:59"), refused("12:10:59"), waited("12:11:00")] });
223 expect(refusals()).toHaveLength(before + 1);
224 await sync({ velocity: [down("12:20:59"), refused("12:20:59")] });
225 await sync({ velocity: [waited("12:21:00")] });
226 await sync({ velocity: [waited("12:21:00")] });
227 expect(refusals()).toHaveLength(before + 2);
228 await sync({ velocity: [down("12:30:59"), waited("12:30:59")] });
229 await sync({ velocity: [refused("12:31:00")] });
230 await sync({ velocity: [refused("12:31:00")] });
231 expect(refusals()).toHaveLength(before + 3);
232 expect(app.db.query("SELECT text FROM game_player_event WHERE clock = '12:30:59'").all()).toEqual([{ text: REASON }]);
233 // AutoStopper's line named nobody: Velocity's says who.
234 await sync({ velocity: [waited("12:35:59")] });
235 await sync({ velocity: [refused("12:36:00")] });
236 expect(app.db.query("SELECT player, text FROM game_player_event WHERE clock IN ('12:35:59', '12:36:00')").all()).toEqual([{ player: "Alice", text: REASON }]);
237 // Two real tries stay two.
238 await sync({ velocity: [down("12:40:01"), refused("12:40:01"), waited("12:40:01"), down("12:40:03"), refused("12:40:03"), waited("12:40:03")] });
239 expect(refusals()).toHaveLength(before + 6);
240 });
241
199242 test("another server's log cannot say it", async () => {
200243 const before = refusals().length;
201244 await sync({ hub: [up("13:00:01"), down("13:00:01"), waited("13:00:01"), refused("13:00:02")] });

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

Keyboard shortcuts

Docs