@volorio/games-dice 0.2.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/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # @volorio/games-dice
2
+
3
+ Typed browser client for the Volorio Dice game.
4
+
5
+ Install only the Dice package in the browser app. It depends on `@volorio/games-web` and `@volorio/games-core`; it does not accept a Volorio server API key.
6
+
7
+ ```ts
8
+ import { VolorioDiceClient } from "@volorio/games-dice";
9
+
10
+ const dice = new VolorioDiceClient({
11
+ gatewayUrl: "wss://games.volor.io/v1/games/gateway",
12
+ token: shortLivedGameTokenFromYourBackend,
13
+ gameInstanceId: "ginst_123",
14
+ playerId: "user_123",
15
+ });
16
+
17
+ await dice.connect();
18
+ dice.onRoll((roll) => console.log(roll.playerId, roll.value));
19
+ dice.roll();
20
+ ```
21
+
22
+ Your backend creates the game session and game token through `@volorio/games-node` using the server API key.
@@ -0,0 +1,51 @@
1
+ import type { VolorioGameProtocolError, VolorioGameSnapshot } from "@volorio/games-core";
2
+ import { type VolorioGameWebSocketLike } from "@volorio/games-web";
3
+ export interface VolorioDiceClientOptions {
4
+ gatewayUrl: string;
5
+ token: string;
6
+ gameInstanceId: string;
7
+ playerId: string;
8
+ websocketFactory?: (url: string) => VolorioGameWebSocketLike;
9
+ now?: () => number;
10
+ }
11
+ export interface VolorioDiceRoll {
12
+ playerId: string;
13
+ value: number;
14
+ stateVersion: number;
15
+ eventId: string;
16
+ occurredAt: string;
17
+ }
18
+ export interface VolorioDiceSnapshotState {
19
+ rolls: Array<{
20
+ actionId: string;
21
+ playerId: string;
22
+ value: number;
23
+ }>;
24
+ }
25
+ export type VolorioDiceRollHandler = (roll: VolorioDiceRoll) => void;
26
+ export type VolorioDiceSnapshotHandler = (snapshot: VolorioGameSnapshot<VolorioDiceSnapshotState>) => void;
27
+ export type VolorioDiceErrorHandler = (error: VolorioGameProtocolError | unknown) => void;
28
+ export declare class VolorioDiceClient {
29
+ private readonly client;
30
+ private readonly rolls;
31
+ private readonly rollHandlers;
32
+ private readonly snapshotHandlers;
33
+ private readonly errorHandlers;
34
+ constructor(options: VolorioDiceClientOptions);
35
+ connect(lastKnownStateVersion?: number): Promise<void>;
36
+ reconnect(): Promise<void>;
37
+ disconnect(reason?: string): void;
38
+ roll(options?: {
39
+ actionId?: string;
40
+ expectedVersion?: number;
41
+ }): void;
42
+ onRoll(handler: VolorioDiceRollHandler): () => void;
43
+ onSnapshot(handler: VolorioDiceSnapshotHandler): () => void;
44
+ onError(handler: VolorioDiceErrorHandler): () => void;
45
+ getRolls(): VolorioDiceRoll[];
46
+ getLatestStateVersion(): number;
47
+ private handleEvent;
48
+ private handleSnapshot;
49
+ private emitError;
50
+ }
51
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA4B,wBAAwB,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AACnH,OAAO,EAAyB,KAAK,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AAE1F,MAAM,WAAW,wBAAwB;IACvC,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;IACd,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,gBAAgB,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,wBAAwB,CAAC;IAC7D,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,eAAe;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,wBAAwB;IACvC,KAAK,EAAE,KAAK,CAAC;QACX,QAAQ,EAAE,MA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CAAC,SAAS;CAKlB"}
@@ -0,0 +1,95 @@
1
+ import { VolorioGamesWebClient } from "@volorio/games-web";
2
+ export class VolorioDiceClient {
3
+ client;
4
+ rolls = [];
5
+ rollHandlers = [];
6
+ snapshotHandlers = [];
7
+ errorHandlers = [];
8
+ constructor(options) {
9
+ this.client = new VolorioGamesWebClient(options);
10
+ this.client.on("event", (event) => this.handleEvent(event));
11
+ this.client.on("snapshot", (snapshot) => this.handleSnapshot(snapshot));
12
+ this.client.on("error", (error) => this.emitError(error));
13
+ }
14
+ connect(lastKnownStateVersion) {
15
+ return this.client.connect(lastKnownStateVersion);
16
+ }
17
+ reconnect() {
18
+ return this.client.reconnect();
19
+ }
20
+ disconnect(reason) {
21
+ this.client.disconnect(reason);
22
+ }
23
+ roll(options = {}) {
24
+ this.client.sendAction("dice.roll", {}, options);
25
+ }
26
+ onRoll(handler) {
27
+ this.rollHandlers.push(handler);
28
+ return () => removeHandler(this.rollHandlers, handler);
29
+ }
30
+ onSnapshot(handler) {
31
+ this.snapshotHandlers.push(handler);
32
+ return () => removeHandler(this.snapshotHandlers, handler);
33
+ }
34
+ onError(handler) {
35
+ this.errorHandlers.push(handler);
36
+ return () => removeHandler(this.errorHandlers, handler);
37
+ }
38
+ getRolls() {
39
+ return this.rolls.map((roll) => ({ ...roll }));
40
+ }
41
+ getLatestStateVersion() {
42
+ return this.client.getLatestStateVersion();
43
+ }
44
+ handleEvent(event) {
45
+ if (event.type !== "dice.rolled") {
46
+ return;
47
+ }
48
+ const roll = readDiceRoll(event);
49
+ this.rolls.push(roll);
50
+ for (const handler of this.rollHandlers) {
51
+ handler({ ...roll });
52
+ }
53
+ }
54
+ handleSnapshot(snapshot) {
55
+ this.rolls.splice(0, this.rolls.length, ...snapshot.state.rolls.map((roll) => ({
56
+ playerId: roll.playerId,
57
+ value: roll.value,
58
+ stateVersion: snapshot.stateVersion,
59
+ eventId: `snapshot_${snapshot.stateVersion}_${roll.actionId}`,
60
+ occurredAt: snapshot.createdAt,
61
+ })));
62
+ for (const handler of this.snapshotHandlers) {
63
+ handler({
64
+ ...snapshot,
65
+ state: { rolls: snapshot.state.rolls.map((roll) => ({ ...roll })) },
66
+ });
67
+ }
68
+ }
69
+ emitError(error) {
70
+ for (const handler of this.errorHandlers) {
71
+ handler(error);
72
+ }
73
+ }
74
+ }
75
+ function readDiceRoll(event) {
76
+ const payload = event.payload;
77
+ if (typeof payload.playerId !== "string" || !Number.isInteger(payload.value)) {
78
+ throw new Error("invalid_dice_roll_event");
79
+ }
80
+ const value = payload.value;
81
+ return {
82
+ playerId: payload.playerId,
83
+ value,
84
+ stateVersion: event.stateVersion,
85
+ eventId: event.eventId,
86
+ occurredAt: event.occurredAt,
87
+ };
88
+ }
89
+ function removeHandler(handlers, handler) {
90
+ const index = handlers.indexOf(handler);
91
+ if (index >= 0) {
92
+ handlers.splice(index, 1);
93
+ }
94
+ }
95
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAiC,MAAM,oBAAoB,CAAC;AA+B1F,MAAM,OAAO,iBAAiB;IACX,MAAM,CAAwB;IAC9B,KAAK,GAAsB,EAAE,CAAC;IAC9B,YAAY,GAA6B,EAAE,CAAC;IAC5C,gBAAgB,GAAiC,EAAE,CAAC;IACpD,aAAa,GAA8B,EAAE,CAAC;IAE/D,YAAY,OAAiC;QAC3C,IAAI,CAAC,MAAM,GAAG,IAAI,qBAAqB,CAAC,OAAO,CAAC,CAAC;QACjD,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,KAAiC,CAAC,CAAC,CAAC;QACxF,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,UAAU,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,CAAC,cAAc,CAAC,QAAyD,CA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AC;IAEO,SAAS,CAAC,KAAc;QAC9B,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YACzC,OAAO,CAAC,KAAK,CAAC,CAAC;QACjB,CAAC;IACH,CAAC;CACF;AAED,SAAS,YAAY,CAAC,KAA+B;IACnD,MAAM,OAAO,GAAG,KAAK,CAAC,OAAkD,CAAC;IACzE,IAAI,OAAO,OAAO,CAAC,QAAQ,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAC7E,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;IACD,MAAM,KAAK,GAAG,OAAO,CAAC,KAAe,CAAC;IACtC,OAAO;QACL,QAAQ,EAAE,OAAO,CAAC,QAAQ;QAC1B,KAAK;QACL,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,OAAO,EAAE,KAAK,CAAC,OAAO;QACtB,UAAU,EAAE,KAAK,CAAC,UAAU;KAC7B,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAI,QAAa,EAAE,OAAU;IACjD,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACxC,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;QACf,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IAC5B,CAAC;AACH,CAAC"}
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@volorio/games-dice",
3
+ "version": "0.2.0",
4
+ "private": false,
5
+ "description": "Volorio Dice game client built on the Volorio Games Web runtime.",
6
+ "main": "dist/src/index.js",
7
+ "types": "dist/src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/src/index.d.ts",
11
+ "default": "./dist/src/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist/src",
16
+ "README.md"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public",
20
+ "registry": "https://registry.npmjs.org/"
21
+ },
22
+ "scripts": {
23
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
24
+ "test": "pnpm build && rm -rf dist-test && tsc -p tsconfig.json --outDir dist-test && node --test dist-test/test/*.js",
25
+ "pack:local": "pnpm build && npm pack --pack-destination dist"
26
+ },
27
+ "dependencies": {
28
+ "@volorio/games-core": "^0.2.0",
29
+ "@volorio/games-web": "^0.2.0"
30
+ }
31
+ }