@table-games-engine/nakama-adapter 0.0.0-stage → 0.1.1
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.md +105 -0
- package/README.md +77 -2
- package/dist/broadcast.d.ts +27 -0
- package/dist/idempotency.d.ts +54 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +1224 -0
- package/dist/match-handler.d.ts +76 -0
- package/dist/persistence-bridge.d.ts +78 -0
- package/dist/plugins.d.ts +76 -0
- package/dist/register-plugin.d.ts +7 -0
- package/dist/registry.d.ts +84 -0
- package/dist/serialization.d.ts +135 -0
- package/dist/timeouts.d.ts +33 -0
- package/dist/types.d.ts +108 -0
- package/package.json +42 -4
package/LICENSE.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-ALv2
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 Dmitry Kucher
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,78 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @table-games-engine/nakama-adapter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Nakama runtime bindings for `@table-games-engine/core`: wraps the engine (and `@table-games-engine/persistence`) in a Nakama TypeScript-runtime match handler so players can connect over WebSocket and play your game.
|
|
4
|
+
|
|
5
|
+
Most game authors don't install this directly: it comes with [`@table-games-engine/game-kit`](https://www.npmjs.com/package/@table-games-engine/game-kit), whose `preview` command and the [Logigra](https://logigra.ru) store host your game for you. Install it to run your own Nakama server.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @table-games-engine/nakama-adapter
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## What you get
|
|
14
|
+
|
|
15
|
+
- `createMatchHandler(opts)` returns a Nakama `MatchHandler` implementing `matchInit`, `matchJoinAttempt`, `matchJoin`, `matchLeave`, `matchLoop`, `matchSignal` and `matchTerminate`.
|
|
16
|
+
- `GameRegistry` resolves `gameId` to `{ manifest, handlers, setup }` at `matchInit`, so one runtime can host many games (`registerPlugin` registers a `GamePlugin`).
|
|
17
|
+
- `IdempotencyCache` absorbs client retries.
|
|
18
|
+
- JSON wire format (`parseInbound`, `serializeOutbound`, `OPCODES`), per-player filtered broadcasts (`broadcastViews`), action timeouts (`checkTimeouts`), reconnection-aware joins.
|
|
19
|
+
- Persistence wiring: events are appended per transition and snapshots follow a `SnapshotPolicy`.
|
|
20
|
+
- `NakamaPlatformPlugin` hooks for analytics, audit and similar.
|
|
21
|
+
|
|
22
|
+
## Wiring into Nakama
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { createMatchHandler, registerPlugin, GameRegistry, IdempotencyCache } from '@table-games-engine/nakama-adapter';
|
|
26
|
+
import { SqliteAdapter, everyNTransitions } from '@table-games-engine/persistence';
|
|
27
|
+
|
|
28
|
+
const registry = new GameRegistry();
|
|
29
|
+
registerPlugin(registry, myGamePlugin); // a GamePlugin; or registry.register({ gameId, manifest, handlers, setup }) by hand
|
|
30
|
+
|
|
31
|
+
const handler = createMatchHandler({
|
|
32
|
+
registry,
|
|
33
|
+
idempotencyCache: new IdempotencyCache(),
|
|
34
|
+
adapter: new SqliteAdapter('/data/engine.db'),
|
|
35
|
+
policy: everyNTransitions(50),
|
|
36
|
+
ownerNode: 'node-A',
|
|
37
|
+
leaseSeconds: 60,
|
|
38
|
+
timeout: { timeoutMs: 30_000 },
|
|
39
|
+
plugins: [],
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
function InitModule(_ctx: nkruntime.Context, logger: nkruntime.Logger, _nk: nkruntime.Nakama, initializer: nkruntime.Initializer): void {
|
|
43
|
+
initializer.registerMatch('tabletop_match', handler);
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Create a match from your own server code:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
nk.matchCreate('tabletop_match', {
|
|
51
|
+
gameId: 'my-game',
|
|
52
|
+
playerIds: 'alice,bob', // comma-separated; passed to setup()
|
|
53
|
+
config: JSON.stringify({ /* game options */ }),
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The adapter reads no environment variables; plumb configuration into the options yourself.
|
|
58
|
+
|
|
59
|
+
## Persistence and reconnection
|
|
60
|
+
|
|
61
|
+
With `adapter` set, `matchInit` stores snapshot v0 (and takes an ownership lease if `ownerNode` is set), each transition appends its events and may snapshot, and `matchTerminate` releases the lease without closing the adapter. When the same user rejoins with a new session, the handler swaps the presence, sends a fresh filtered state to that player only, and keeps the idempotency cache.
|
|
62
|
+
|
|
63
|
+
## Plugins
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import type { NakamaPlatformPlugin } from '@table-games-engine/nakama-adapter';
|
|
67
|
+
|
|
68
|
+
const audit: NakamaPlatformPlugin = {
|
|
69
|
+
name: 'audit',
|
|
70
|
+
async onMatchEnded(ctx, result) { /* ... */ },
|
|
71
|
+
};
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Hooks: `onMatchCreated`, `onMatchStarted`, `onPlayerJoined`, `onPlayerLeft`, `onPlayerAction`, `onStateTransition`, `onMatchEnded`. All are optional and async. They are fire-and-forget: the match loop never waits for them, and a failing plugin is logged but never crashes the match.
|
|
75
|
+
|
|
76
|
+
## Licence
|
|
77
|
+
|
|
78
|
+
FSL-1.1-ALv2 (Functional Source License, converts to Apache-2.0 after two years). See `LICENSE.md` in the package.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { GameManifest, LegalMove, MatchState, PlayerId } from '@table-games-engine/core';
|
|
2
|
+
import type { NkMatchDispatcher, NkPresence } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Opcode allocation for the adapter.
|
|
5
|
+
*
|
|
6
|
+
* STATE_UPDATE = 1 per-player full snapshot (`OutboundState`)
|
|
7
|
+
* ACK = 2 per-action acknowledgement (`OutboundAck`)
|
|
8
|
+
* BROADCAST = 3 broadcast events (`OutboundBroadcast`)
|
|
9
|
+
* MATCH_ENDED = 4 terminal `match-ended`
|
|
10
|
+
* TRANSITION = 5 incremental transition deltas (future use)
|
|
11
|
+
*/
|
|
12
|
+
export declare const OPCODES: {
|
|
13
|
+
readonly STATE_UPDATE: 1;
|
|
14
|
+
readonly ACK: 2;
|
|
15
|
+
readonly BROADCAST: 3;
|
|
16
|
+
readonly MATCH_ENDED: 4;
|
|
17
|
+
readonly TRANSITION: 5;
|
|
18
|
+
};
|
|
19
|
+
export type Opcode = (typeof OPCODES)[keyof typeof OPCODES];
|
|
20
|
+
/**
|
|
21
|
+
* Compute and dispatch a filtered `state-update` envelope to each presence.
|
|
22
|
+
*
|
|
23
|
+
* Players whose `userId` is not registered as a `PlayerId` in `state.players`
|
|
24
|
+
* are skipped — they may be observers (Phase B doesn't model them) and we
|
|
25
|
+
* never broadcast a `PlayerView` for an unknown viewer.
|
|
26
|
+
*/
|
|
27
|
+
export declare function broadcastViews(state: MatchState, manifest: GameManifest, dispatcher: NkMatchDispatcher, presences: ReadonlyArray<NkPresence>, legalMovesFor?: (playerId: PlayerId) => ReadonlyArray<LegalMove>): void;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Per-match idempotency cache: when a client retries a message
|
|
3
|
+
* with the same `clientId`, the adapter should replay the original outbound
|
|
4
|
+
* response instead of executing the action twice.
|
|
5
|
+
*
|
|
6
|
+
* Cache layout:
|
|
7
|
+
* matchId -> Map<clientId, { response: OutboundEnvelope; expiresAt }>
|
|
8
|
+
*
|
|
9
|
+
* Entries expire after `ttlMs` (default 5 min). The clock is injectable for
|
|
10
|
+
* deterministic tests. Pruning runs on every `get` / `set` for the affected
|
|
11
|
+
* match — it stays cheap because each match's map is small (one entry per
|
|
12
|
+
* recent action) and we only walk one match at a time.
|
|
13
|
+
*/
|
|
14
|
+
import type { OutboundEnvelope } from './serialization.js';
|
|
15
|
+
/** A single cache entry. */
|
|
16
|
+
export interface IdempotencyEntry {
|
|
17
|
+
readonly response: OutboundEnvelope;
|
|
18
|
+
readonly expiresAt: number;
|
|
19
|
+
}
|
|
20
|
+
/** Constructor options for `IdempotencyCache`. */
|
|
21
|
+
export interface IdempotencyCacheOptions {
|
|
22
|
+
/** Entry lifetime in milliseconds. Defaults to 5 minutes. */
|
|
23
|
+
readonly ttlMs?: number;
|
|
24
|
+
/** Clock source (ms epoch). Defaults to `Date.now`. */
|
|
25
|
+
readonly now?: () => number;
|
|
26
|
+
}
|
|
27
|
+
export declare class IdempotencyCache {
|
|
28
|
+
private readonly buckets;
|
|
29
|
+
private readonly ttlMs;
|
|
30
|
+
private readonly now;
|
|
31
|
+
constructor(options?: IdempotencyCacheOptions);
|
|
32
|
+
/**
|
|
33
|
+
* Look up a cached response. Prunes expired entries for the match as a
|
|
34
|
+
* side effect, so a stale entry is never returned.
|
|
35
|
+
*/
|
|
36
|
+
get(matchId: string, clientId: string): OutboundEnvelope | null;
|
|
37
|
+
/**
|
|
38
|
+
* Cache `response` under `(matchId, clientId)` with TTL. Replaces any
|
|
39
|
+
* existing entry for the same key.
|
|
40
|
+
*/
|
|
41
|
+
set(matchId: string, clientId: string, response: OutboundEnvelope): void;
|
|
42
|
+
/**
|
|
43
|
+
* Drop the entire bucket for `matchId`. Use when a match terminates so
|
|
44
|
+
* we don't hold the response payloads in memory.
|
|
45
|
+
*/
|
|
46
|
+
clear(matchId: string): void;
|
|
47
|
+
/**
|
|
48
|
+
* Walk every bucket and drop expired entries. Empty buckets are removed.
|
|
49
|
+
*/
|
|
50
|
+
prune(): void;
|
|
51
|
+
/** Total number of live entries across all buckets. Test-only helper. */
|
|
52
|
+
get size(): number;
|
|
53
|
+
private pruneBucket;
|
|
54
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Public barrel for `@table-games-engine/nakama-adapter`.
|
|
3
|
+
*
|
|
4
|
+
* This package wraps the pure `EngineOperations` from `@table-games-engine/core`
|
|
5
|
+
* (and the persistence layer from `@table-games-engine/persistence`) behind a
|
|
6
|
+
* Nakama match handler so that multiple players can connect via WebSocket.
|
|
7
|
+
*/
|
|
8
|
+
export type { NkLogger, NkPresence, NkMatchData, NkMatchDispatcher, NkMatchState, NkMatchHandler, } from './types.js';
|
|
9
|
+
export { parseInbound, serializeOutbound, } from './serialization.js';
|
|
10
|
+
export type { InboundEnvelope, OutboundEnvelope, InboundAction, InboundPass, InboundConcede, InboundChooseTargets, InboundOrderTriggers, InboundAdminPause, InboundAdminResume, InboundMalformed, OutboundState, OutboundTransition, OutboundAck, OutboundBroadcast, OutboundMatchEnded, } from './serialization.js';
|
|
11
|
+
export { GameRegistry, RegistryError } from './registry.js';
|
|
12
|
+
export { registerPlugin } from './register-plugin.js';
|
|
13
|
+
export type { RegisteredGame, RegisterArgs, GameSetup, } from './registry.js';
|
|
14
|
+
export { IdempotencyCache } from './idempotency.js';
|
|
15
|
+
export type { IdempotencyEntry, IdempotencyCacheOptions, } from './idempotency.js';
|
|
16
|
+
export { createMatchHandler } from './match-handler.js';
|
|
17
|
+
export type { MatchStatus, NkMatchInternalState, CreateMatchHandlerOptions, } from './match-handler.js';
|
|
18
|
+
export { broadcastViews, OPCODES } from './broadcast.js';
|
|
19
|
+
export type { Opcode } from './broadcast.js';
|
|
20
|
+
export { checkTimeouts } from './timeouts.js';
|
|
21
|
+
export type { TimeoutOptions } from './timeouts.js';
|
|
22
|
+
export { createPersistenceBridge } from './persistence-bridge.js';
|
|
23
|
+
export type { PersistenceBridge, CreatePersistenceBridgeArgs, } from './persistence-bridge.js';
|
|
24
|
+
export { invokePluginHook, dispatchPluginHook, } from './plugins.js';
|
|
25
|
+
export type { NakamaPlatformPlugin, PluginContext, } from './plugins.js';
|