@pocketrts/lockstep 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +253 -2
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/lockstep.d.ts +365 -0
- package/dist/lockstep.js +842 -0
- package/dist/queue.d.ts +19 -0
- package/dist/queue.js +25 -0
- package/dist/text.d.ts +8 -0
- package/dist/text.js +26 -0
- package/package.json +41 -3
package/dist/queue.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A typed command queue over a `Lockstep`: local commands are encoded and handed to the scheduler, and what runs is
|
|
3
|
+
* always the copy decoded from the turn, identical on every peer.
|
|
4
|
+
*/
|
|
5
|
+
import type { Lockstep } from './lockstep.js';
|
|
6
|
+
/** The game's command format. `decode` returns null for data it does not accept (that entry is skipped). */
|
|
7
|
+
export interface CommandCodec<C> {
|
|
8
|
+
encode(cmd: C): string;
|
|
9
|
+
decode(data: string): C | null;
|
|
10
|
+
}
|
|
11
|
+
export declare class LockstepQueue<C> {
|
|
12
|
+
readonly lockstep: Lockstep;
|
|
13
|
+
private readonly codec;
|
|
14
|
+
constructor(lockstep: Lockstep, codec: CommandCodec<C>);
|
|
15
|
+
/** Issue a local command. */
|
|
16
|
+
submit(cmd: C): void;
|
|
17
|
+
/** Commands for the next slot, in order (malformed entries dropped); null when the turn has not arrived (no slot consumed). */
|
|
18
|
+
take(): C[] | null;
|
|
19
|
+
}
|
package/dist/queue.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export class LockstepQueue {
|
|
2
|
+
lockstep;
|
|
3
|
+
codec;
|
|
4
|
+
constructor(lockstep, codec) {
|
|
5
|
+
this.lockstep = lockstep;
|
|
6
|
+
this.codec = codec;
|
|
7
|
+
}
|
|
8
|
+
/** Issue a local command. */
|
|
9
|
+
submit(cmd) {
|
|
10
|
+
this.lockstep.submit(this.codec.encode(cmd));
|
|
11
|
+
}
|
|
12
|
+
/** Commands for the next slot, in order (malformed entries dropped); null when the turn has not arrived (no slot consumed). */
|
|
13
|
+
take() {
|
|
14
|
+
const data = this.lockstep.take();
|
|
15
|
+
if (!data)
|
|
16
|
+
return null;
|
|
17
|
+
const out = [];
|
|
18
|
+
for (const d of data) {
|
|
19
|
+
const c = this.codec.decode(d);
|
|
20
|
+
if (c !== null)
|
|
21
|
+
out.push(c);
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
}
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Binary command data as text that fits in a JSON message (base64), and back.
|
|
3
|
+
* Uses the global `btoa` / `atob` (browsers, Node.js 16+, Workers).
|
|
4
|
+
*/
|
|
5
|
+
/** Bytes → base64 text. Handles large buffers. */
|
|
6
|
+
export declare function bytesToText(bytes: Uint8Array): string;
|
|
7
|
+
/** Base64 text → bytes; null when the text is not base64 (or not a string). */
|
|
8
|
+
export declare function textToBytes(text: string): Uint8Array | null;
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Binary command data as text that fits in a JSON message (base64), and back.
|
|
3
|
+
* Uses the global `btoa` / `atob` (browsers, Node.js 16+, Workers).
|
|
4
|
+
*/
|
|
5
|
+
/** Bytes → base64 text. Handles large buffers. */
|
|
6
|
+
export function bytesToText(bytes) {
|
|
7
|
+
let s = '';
|
|
8
|
+
for (let i = 0; i < bytes.length; i += 0x8000)
|
|
9
|
+
s += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
10
|
+
return btoa(s);
|
|
11
|
+
}
|
|
12
|
+
/** Base64 text → bytes; null when the text is not base64 (or not a string). */
|
|
13
|
+
export function textToBytes(text) {
|
|
14
|
+
if (typeof text !== 'string')
|
|
15
|
+
return null;
|
|
16
|
+
try {
|
|
17
|
+
const s = atob(text);
|
|
18
|
+
const out = new Uint8Array(s.length);
|
|
19
|
+
for (let i = 0; i < s.length; i++)
|
|
20
|
+
out[i] = s.charCodeAt(i);
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pocketrts/lockstep",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Deterministic lockstep turn scheduler for small peer-to-peer games: host-ordered turns, catch-up, checksums and host migration",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Junjie Bai <bjj19851010@gmail.com>",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/baijunjie/pocketrts.git",
|
|
10
|
+
"directory": "packages/lockstep"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"lockstep",
|
|
14
|
+
"deterministic",
|
|
15
|
+
"multiplayer",
|
|
16
|
+
"netplay",
|
|
17
|
+
"rts",
|
|
18
|
+
"game"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"@pocketrts/source": "./src/index.ts",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.build.json",
|
|
41
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
42
|
+
"test": "vitest run --maxWorkers=2"
|
|
43
|
+
}
|
|
6
44
|
}
|