@zaisma/game-server 1.30.0 → 1.31.0
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 +1 -1
- package/src/doors.d.ts +14 -0
- package/src/doors.js +26 -3
package/package.json
CHANGED
package/src/doors.d.ts
CHANGED
|
@@ -161,8 +161,22 @@ export interface DoorLimitBinding {
|
|
|
161
161
|
key: string;
|
|
162
162
|
}): Promise<object>;
|
|
163
163
|
}
|
|
164
|
+
/** Where a Worker writes what real traffic needs of each counter: Workers Analytics Engine, as Workers gives it. */
|
|
165
|
+
export interface LimitNeedsDataset {
|
|
166
|
+
writeDataPoint(point: {
|
|
167
|
+
indexes?: string[];
|
|
168
|
+
blobs?: string[];
|
|
169
|
+
doubles?: number[];
|
|
170
|
+
}): void;
|
|
171
|
+
}
|
|
164
172
|
export type DoorLimitBindings = {
|
|
165
173
|
[Level in DoorLimitLevel as `LIMIT_${Uppercase<Level>}`]?: DoorLimitBinding;
|
|
174
|
+
} & {
|
|
175
|
+
LIMIT_NEEDS?: LimitNeedsDataset;
|
|
176
|
+
};
|
|
177
|
+
/** The level just below each one, whose number a counter's real traffic is held against before a lower limit ships (scripts/limit-needs.mjs). */
|
|
178
|
+
export declare const LOWER_DOOR_LIMIT_LEVEL: {
|
|
179
|
+
readonly [Level in DoorLimitLevel]: DoorLimitLevel | null;
|
|
166
180
|
};
|
|
167
181
|
/** Whether a call goes past a level's limit: door's own counter, for one subject. Keys never leave Cloudflare's counter. */
|
|
168
182
|
export declare function isPastLevel(env: DoorLimitBindings, level: DoorLimitLevel, counterName: string, subject: string): Promise<boolean>;
|
package/src/doors.js
CHANGED
|
@@ -36,13 +36,36 @@ export const DOOR_NO_LIMIT_REASONS = {
|
|
|
36
36
|
counted_by_its_call: "All it does is make calls that have their own limit.",
|
|
37
37
|
room_token_bucket: "A room's message bucket takes a token for each message, per player, before any door runs.",
|
|
38
38
|
};
|
|
39
|
+
/** The level just below each one, whose number a counter's real traffic is held against before a lower limit ships (scripts/limit-needs.mjs). */
|
|
40
|
+
export const LOWER_DOOR_LIMIT_LEVEL = { very_rare: null, guess: "very_rare", rare: "guess", steady: "rare", busy: "steady", building: "busy" };
|
|
41
|
+
const bindingOf = (env, level) => env[`LIMIT_${level.toUpperCase()}`];
|
|
42
|
+
const isRefused = (answer) => !("success" in answer && answer.success === true);
|
|
39
43
|
/** Whether a call goes past a level's limit: door's own counter, for one subject. Keys never leave Cloudflare's counter. */
|
|
40
44
|
export async function isPastLevel(env, level, counterName, subject) {
|
|
41
|
-
const binding = env
|
|
45
|
+
const binding = bindingOf(env, level);
|
|
42
46
|
if (!binding)
|
|
43
47
|
return false;
|
|
44
|
-
|
|
45
|
-
|
|
48
|
+
if (isRefused(await binding.limit({ key: `${counterName}:${subject}` })))
|
|
49
|
+
return true;
|
|
50
|
+
await noteLimitNeed(env, level, counterName, subject);
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Notes a counter whose real traffic passed the level below its own, so a change that lowers its limit is held at ship
|
|
55
|
+
* (scripts/limit-needs.mjs). Only the counter's name and level are written, never the subject, and at most twice a
|
|
56
|
+
* minute per counter and level in each Cloudflare location. A Worker without the dataset or the level below's binding notes nothing.
|
|
57
|
+
*/
|
|
58
|
+
async function noteLimitNeed(env, level, counterName, subject) {
|
|
59
|
+
const lowerLevel = LOWER_DOOR_LIMIT_LEVEL[level];
|
|
60
|
+
const lowerBinding = lowerLevel && bindingOf(env, lowerLevel);
|
|
61
|
+
const noteBinding = bindingOf(env, "very_rare");
|
|
62
|
+
if (!env.LIMIT_NEEDS || !lowerLevel || !lowerBinding || !noteBinding)
|
|
63
|
+
return;
|
|
64
|
+
if (!isRefused(await lowerBinding.limit({ key: `need:${counterName}:${subject}` })))
|
|
65
|
+
return;
|
|
66
|
+
if (isRefused(await noteBinding.limit({ key: `need_noted:${counterName}:${level}` })))
|
|
67
|
+
return;
|
|
68
|
+
env.LIMIT_NEEDS.writeDataPoint({ indexes: [counterName], blobs: [counterName, level, lowerLevel], doubles: [DOOR_LIMIT_LEVELS[lowerLevel]] });
|
|
46
69
|
}
|
|
47
70
|
/** Whether a call goes past a door's limit. doorName names its counter unless its limit shares a group. */
|
|
48
71
|
export async function isPastDoorLimit(env, limit, doorName, subject) {
|