@caisual/kit 0.1.0 → 0.3.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/dist/kit.d.ts +67 -0
- package/dist/kit.js +1374 -31
- package/dist/node.d.ts +75 -0
- package/dist/node.js +1930 -0
- package/dist/runtime.d.ts +106 -0
- package/dist/runtime.js +1458 -0
- package/dist/server.d.ts +57 -0
- package/dist/server.js +38 -0
- package/package.json +33 -6
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export type JsonValue = null | boolean | number | string | JsonValue[] | {
|
|
2
|
+
[key: string]: JsonValue;
|
|
3
|
+
};
|
|
4
|
+
export type RoomStatus = 'lobby' | 'countdown' | 'playing' | 'ended';
|
|
5
|
+
export type VoiceMode = 'none' | 'room' | 'team' | 'proximity';
|
|
6
|
+
export type LeaveReason = 'left' | 'timeout' | 'kicked';
|
|
7
|
+
export interface Player {
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly guest: boolean;
|
|
11
|
+
readonly role: string | null;
|
|
12
|
+
readonly team: number | null;
|
|
13
|
+
readonly connected: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface Room<State extends JsonValue = JsonValue> {
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly mode: string | null;
|
|
18
|
+
readonly status: RoomStatus;
|
|
19
|
+
readonly tick: number;
|
|
20
|
+
state: State;
|
|
21
|
+
readonly players: readonly Player[];
|
|
22
|
+
readonly host: Player | null;
|
|
23
|
+
broadcast(message: JsonValue): void;
|
|
24
|
+
send(player: Player | string, message: JsonValue): void;
|
|
25
|
+
kick(player: Player | string): void;
|
|
26
|
+
end(result: JsonValue): void;
|
|
27
|
+
save(key: string, value: JsonValue): Promise<void>;
|
|
28
|
+
load(key: string): Promise<JsonValue | null>;
|
|
29
|
+
schedule(milliseconds: number, handler: string, payload: JsonValue): void;
|
|
30
|
+
readonly board: {
|
|
31
|
+
submit(player: Player | string, board: string, score: number, options?: {
|
|
32
|
+
daily?: boolean;
|
|
33
|
+
}): void;
|
|
34
|
+
};
|
|
35
|
+
readonly daily: {
|
|
36
|
+
readonly day: string;
|
|
37
|
+
readonly seed: number;
|
|
38
|
+
};
|
|
39
|
+
readonly time: {
|
|
40
|
+
now(): number;
|
|
41
|
+
};
|
|
42
|
+
readonly voice: {
|
|
43
|
+
readonly mode: VoiceMode;
|
|
44
|
+
setProximity(a: Player | string, b: Player | string, gain: number): void;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export interface GameDefinition<State extends JsonValue = JsonValue> {
|
|
48
|
+
tickRate: number;
|
|
49
|
+
onCreate?(room: Room<State>): void | Promise<void>;
|
|
50
|
+
onStart?(room: Room<State>): void | Promise<void>;
|
|
51
|
+
onJoin?(room: Room<State>, player: Player): void | Promise<void>;
|
|
52
|
+
onLeave?(room: Room<State>, player: Player, reason: LeaveReason): void | Promise<void>;
|
|
53
|
+
onMessage?(room: Room<State>, player: Player, message: JsonValue): void | Promise<void>;
|
|
54
|
+
onTick?(room: Room<State>, deltaSeconds: number): void | Promise<void>;
|
|
55
|
+
onEnd?(room: Room<State>): void | Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
export declare function defineGame<State extends JsonValue = JsonValue, const Definition extends GameDefinition<State> = GameDefinition<State>>(definition: Definition): Definition;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// src/server/index.ts
|
|
2
|
+
var GAME_DEFINITION = /* @__PURE__ */ Symbol.for("@caisual/kit/game-definition");
|
|
3
|
+
var CALLBACKS = [
|
|
4
|
+
"onCreate",
|
|
5
|
+
"onStart",
|
|
6
|
+
"onJoin",
|
|
7
|
+
"onLeave",
|
|
8
|
+
"onMessage",
|
|
9
|
+
"onTick",
|
|
10
|
+
"onEnd"
|
|
11
|
+
];
|
|
12
|
+
function isRecord(value) {
|
|
13
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
function defineGame(definition) {
|
|
16
|
+
if (!isRecord(definition)) {
|
|
17
|
+
throw new TypeError("Game definition must be an object.");
|
|
18
|
+
}
|
|
19
|
+
if (typeof definition.tickRate !== "number" || !Number.isInteger(definition.tickRate) || definition.tickRate < 0 || definition.tickRate > 60) {
|
|
20
|
+
throw new TypeError("Game definition tickRate must be an integer from 0 to 60.");
|
|
21
|
+
}
|
|
22
|
+
for (const callback of CALLBACKS) {
|
|
23
|
+
const value = definition[callback];
|
|
24
|
+
if (value !== void 0 && typeof value !== "function") {
|
|
25
|
+
throw new TypeError(`Game definition ${callback} must be a function.`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
Object.defineProperty(definition, GAME_DEFINITION, {
|
|
29
|
+
configurable: false,
|
|
30
|
+
enumerable: false,
|
|
31
|
+
value: true,
|
|
32
|
+
writable: false
|
|
33
|
+
});
|
|
34
|
+
return definition;
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
defineGame
|
|
38
|
+
};
|
package/package.json
CHANGED
|
@@ -1,23 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caisual/kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Client and server kit for Caisual games",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist/kit.js",
|
|
8
|
-
"dist/kit.d.ts"
|
|
8
|
+
"dist/kit.d.ts",
|
|
9
|
+
"dist/server.js",
|
|
10
|
+
"dist/server.d.ts",
|
|
11
|
+
"dist/runtime.js",
|
|
12
|
+
"dist/runtime.d.ts",
|
|
13
|
+
"dist/node.js",
|
|
14
|
+
"dist/node.d.ts"
|
|
9
15
|
],
|
|
10
16
|
"exports": {
|
|
11
|
-
".":
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/kit.d.ts",
|
|
19
|
+
"import": "./dist/kit.js",
|
|
20
|
+
"default": "./dist/kit.js"
|
|
21
|
+
},
|
|
22
|
+
"./server": {
|
|
23
|
+
"types": "./dist/server.d.ts",
|
|
24
|
+
"import": "./dist/server.js",
|
|
25
|
+
"default": "./dist/server.js"
|
|
26
|
+
},
|
|
27
|
+
"./runtime": {
|
|
28
|
+
"types": "./dist/runtime.d.ts",
|
|
29
|
+
"import": "./dist/runtime.js",
|
|
30
|
+
"default": "./dist/runtime.js"
|
|
31
|
+
},
|
|
32
|
+
"./node": {
|
|
33
|
+
"types": "./dist/node.d.ts",
|
|
34
|
+
"import": "./dist/node.js",
|
|
35
|
+
"default": "./dist/node.js"
|
|
36
|
+
}
|
|
12
37
|
},
|
|
13
38
|
"types": "./dist/kit.d.ts",
|
|
14
39
|
"scripts": {
|
|
15
40
|
"build": "node build.mjs",
|
|
16
41
|
"prepack": "pnpm build",
|
|
17
|
-
"typecheck": "tsc --noEmit",
|
|
18
|
-
"test": "vitest run"
|
|
42
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.node.json --noEmit",
|
|
43
|
+
"test": "pnpm build && vitest run && WRANGLER_LOG_PATH=/tmp/caisual-kit-wrangler.log vitest run --config vitest.workers.config.ts"
|
|
19
44
|
},
|
|
20
45
|
"devDependencies": {
|
|
46
|
+
"@cloudflare/vitest-pool-workers": "^0.20.3",
|
|
47
|
+
"@types/node": "^26.2.0",
|
|
21
48
|
"esbuild": "^0.28.1",
|
|
22
49
|
"typescript": "^7.0.2",
|
|
23
50
|
"vitest": "^4.1.10"
|