@serverkgg/bridge 0.1.13 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { idleSample, sampleResultOf } from "./bridgeSample";
|
|
3
|
+
|
|
4
|
+
describe("sampleResultOf", () => {
|
|
5
|
+
it("serves no tick for an older driver that reports only its players", () => {
|
|
6
|
+
expect(
|
|
7
|
+
sampleResultOf({
|
|
8
|
+
online: 3,
|
|
9
|
+
max: 16,
|
|
10
|
+
}),
|
|
11
|
+
).toEqual({
|
|
12
|
+
online: 3,
|
|
13
|
+
max: 16,
|
|
14
|
+
tick: null,
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("carries the milliseconds per tick a driver measured", () => {
|
|
19
|
+
expect(
|
|
20
|
+
sampleResultOf({
|
|
21
|
+
online: 3,
|
|
22
|
+
max: 16,
|
|
23
|
+
tick: 21.4,
|
|
24
|
+
}).tick,
|
|
25
|
+
).toBe(21.4);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("keeps a tick a driver could not measure as null", () => {
|
|
29
|
+
expect(
|
|
30
|
+
sampleResultOf({
|
|
31
|
+
online: null,
|
|
32
|
+
max: null,
|
|
33
|
+
tick: null,
|
|
34
|
+
}).tick,
|
|
35
|
+
).toBeNull();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
for (const tick of [
|
|
39
|
+
-1,
|
|
40
|
+
Number.NaN,
|
|
41
|
+
Number.POSITIVE_INFINITY,
|
|
42
|
+
]) {
|
|
43
|
+
it(`drops a tick of ${tick}, which no game measures`, () => {
|
|
44
|
+
expect(
|
|
45
|
+
sampleResultOf({
|
|
46
|
+
online: 3,
|
|
47
|
+
max: 16,
|
|
48
|
+
tick,
|
|
49
|
+
}).tick,
|
|
50
|
+
).toBeNull();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("idleSample", () => {
|
|
56
|
+
it("reports no players and no tick while the game is not running", () => {
|
|
57
|
+
expect(idleSample()).toEqual({
|
|
58
|
+
online: null,
|
|
59
|
+
max: null,
|
|
60
|
+
tick: null,
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { BridgeSample } from "../modules";
|
|
2
|
+
import type { BridgeSampleResult } from "../protocol";
|
|
3
|
+
|
|
4
|
+
export const idleSample = (): BridgeSampleResult => {
|
|
5
|
+
return {
|
|
6
|
+
online: null,
|
|
7
|
+
max: null,
|
|
8
|
+
tick: null,
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const tickOf = (tick: number | null | undefined): number | null => {
|
|
13
|
+
return typeof tick === "number" && Number.isFinite(tick) && tick >= 0 ? tick : null;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const sampleResultOf = (sample: BridgeSample): BridgeSampleResult => {
|
|
17
|
+
return {
|
|
18
|
+
online: sample.online,
|
|
19
|
+
max: sample.max,
|
|
20
|
+
tick: tickOf(sample.tick),
|
|
21
|
+
};
|
|
22
|
+
};
|
package/src/runtime/serve.ts
CHANGED
|
@@ -29,6 +29,7 @@ import {
|
|
|
29
29
|
secretFieldKeys,
|
|
30
30
|
} from "./bridgeRedaction";
|
|
31
31
|
import { createRegistry } from "./bridgeRegistry";
|
|
32
|
+
import { idleSample, sampleResultOf } from "./bridgeSample";
|
|
32
33
|
import { createServer } from "./bridgeServer";
|
|
33
34
|
import { setupSummaryOf } from "./bridgeSetup";
|
|
34
35
|
import { createSetupHandlers } from "./bridgeSetupHandlers";
|
|
@@ -266,10 +267,7 @@ export const serve = async (driver: BridgeDriver) => {
|
|
|
266
267
|
}
|
|
267
268
|
|
|
268
269
|
if (!supervisor.running) {
|
|
269
|
-
return
|
|
270
|
-
online: null,
|
|
271
|
-
max: null,
|
|
272
|
-
};
|
|
270
|
+
return idleSample();
|
|
273
271
|
}
|
|
274
272
|
|
|
275
273
|
const stamp = performance.now();
|
|
@@ -278,12 +276,7 @@ export const serve = async (driver: BridgeDriver) => {
|
|
|
278
276
|
return sampled.value;
|
|
279
277
|
}
|
|
280
278
|
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
const value: BridgeSampleResult = {
|
|
284
|
-
online: result.online,
|
|
285
|
-
max: result.max,
|
|
286
|
-
};
|
|
279
|
+
const value = sampleResultOf(await query.sample(context));
|
|
287
280
|
|
|
288
281
|
sampled = {
|
|
289
282
|
value,
|