@jgengine/node 0.1.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.
@@ -0,0 +1,248 @@
1
+ import { WebSocketServer } from "ws";
2
+ import { decidePoseSync } from "@jgengine/core/multiplayer/presenceModel";
3
+ import { decodeWsClientMessage, encodeWsMessage, subscriptionKey, } from "@jgengine/ws/protocol";
4
+ const DEFAULT_POSE_RULES = {
5
+ maxSpeed: 12,
6
+ maxVerticalOffset: 3,
7
+ minElapsedSec: 0.05,
8
+ maxElapsedSec: 0.5,
9
+ keepAliveRefreshMs: 10_000,
10
+ };
11
+ export function createGameWsServer(options) {
12
+ const host = options.host;
13
+ const now = options.now ?? Date.now;
14
+ const poseRules = options.poseRules ?? DEFAULT_POSE_RULES;
15
+ const authenticate = options.authenticate ?? (({ userId }) => userId);
16
+ const wss = options.server !== undefined
17
+ ? new WebSocketServer({ server: options.server, path: options.path })
18
+ : new WebSocketServer({ port: options.port ?? 0, path: options.path });
19
+ const connections = new Set();
20
+ const presence = new Map();
21
+ const send = (connection, message) => {
22
+ if (connection.socket.readyState !== connection.socket.OPEN)
23
+ return;
24
+ connection.socket.send(encodeWsMessage(message));
25
+ };
26
+ const reply = (connection, id, result) => {
27
+ send(connection, { v: 1, t: "reply", id, ok: true, result });
28
+ };
29
+ const replyError = (connection, id, reason) => {
30
+ send(connection, { v: 1, t: "reply", id, ok: false, reason });
31
+ };
32
+ const presenceRows = (serverId) => {
33
+ const rows = presence.get(serverId);
34
+ if (rows === undefined)
35
+ return [];
36
+ return [...rows.entries()].map(([userId, pose]) => ({
37
+ userId,
38
+ position: pose.position,
39
+ rotationY: pose.rotationY,
40
+ rotationPitch: pose.rotationPitch ?? 0,
41
+ lastSeenAt: pose.lastSeenAtMs ?? 0,
42
+ }));
43
+ };
44
+ const broadcastPresence = (serverId) => {
45
+ const key = subscriptionKey("presence", serverId);
46
+ const data = presenceRows(serverId);
47
+ for (const connection of connections) {
48
+ if (!connection.subscriptions.has(key))
49
+ continue;
50
+ send(connection, { v: 1, t: "update", channel: "presence", serverId, data });
51
+ }
52
+ };
53
+ const pushSubscription = async (connection, channel, serverId, action) => {
54
+ if (connection.userId === null)
55
+ return;
56
+ if (channel === "server") {
57
+ const data = await host.getServerView({ userId: connection.userId, serverId });
58
+ send(connection, { v: 1, t: "update", channel, serverId, data });
59
+ }
60
+ else if (channel === "player") {
61
+ const data = await host.getPlayerView({ userId: connection.userId, serverId });
62
+ send(connection, { v: 1, t: "update", channel, serverId, data });
63
+ }
64
+ else if (channel === "feed") {
65
+ const data = await host.getFeed({ userId: connection.userId, serverId, action: action ?? "" });
66
+ send(connection, { v: 1, t: "update", channel, serverId, action: action ?? "", data });
67
+ }
68
+ else {
69
+ send(connection, { v: 1, t: "update", channel, serverId, data: presenceRows(serverId) });
70
+ }
71
+ };
72
+ const onHostEvent = (event) => {
73
+ for (const connection of connections) {
74
+ if (connection.userId === null)
75
+ continue;
76
+ if (event.type === "server") {
77
+ if (connection.subscriptions.has(subscriptionKey("server", event.serverId))) {
78
+ void pushSubscription(connection, "server", event.serverId);
79
+ }
80
+ }
81
+ else if (event.type === "player") {
82
+ if (connection.userId === event.userId &&
83
+ connection.subscriptions.has(subscriptionKey("player", event.serverId))) {
84
+ void pushSubscription(connection, "player", event.serverId);
85
+ }
86
+ }
87
+ else {
88
+ if (connection.subscriptions.has(subscriptionKey("feed", event.serverId, event.action))) {
89
+ void pushSubscription(connection, "feed", event.serverId, event.action);
90
+ }
91
+ }
92
+ }
93
+ };
94
+ const unsubscribeHost = host.subscribe(onHostEvent);
95
+ const handlePose = (connection, serverId, pose) => {
96
+ if (connection.userId === null)
97
+ return;
98
+ const rows = presence.get(serverId) ?? new Map();
99
+ presence.set(serverId, rows);
100
+ const timestamp = now();
101
+ const current = rows.get(connection.userId);
102
+ if (current === undefined) {
103
+ rows.set(connection.userId, {
104
+ position: { x: pose.x, y: pose.y, z: pose.z },
105
+ rotationY: pose.rotationY,
106
+ rotationPitch: pose.rotationPitch,
107
+ lastSeenAtMs: timestamp,
108
+ });
109
+ broadcastPresence(serverId);
110
+ return;
111
+ }
112
+ const decision = decidePoseSync(current, { position: { x: pose.x, y: pose.y, z: pose.z }, rotationY: pose.rotationY, rotationPitch: pose.rotationPitch }, poseRules, timestamp);
113
+ if (decision.changed || decision.refreshKeepAlive) {
114
+ rows.set(connection.userId, {
115
+ position: decision.position,
116
+ rotationY: decision.rotationY,
117
+ rotationPitch: decision.rotationPitch,
118
+ lastSeenAtMs: timestamp,
119
+ });
120
+ }
121
+ if (decision.changed)
122
+ broadcastPresence(serverId);
123
+ };
124
+ const dropPresence = (connection) => {
125
+ if (connection.userId === null)
126
+ return;
127
+ for (const [serverId, rows] of presence) {
128
+ if (rows.delete(connection.userId)) {
129
+ if (rows.size === 0)
130
+ presence.delete(serverId);
131
+ broadcastPresence(serverId);
132
+ }
133
+ }
134
+ };
135
+ const handleMessage = async (connection, message) => {
136
+ if (message.t === "hello") {
137
+ const userId = await authenticate({ userId: message.userId, token: message.token });
138
+ if (userId === null) {
139
+ replyError(connection, message.id, "Not authenticated");
140
+ connection.socket.close();
141
+ return;
142
+ }
143
+ connection.userId = userId;
144
+ reply(connection, message.id, { userId });
145
+ return;
146
+ }
147
+ if (message.t === "pose") {
148
+ handlePose(connection, message.serverId, message.pose);
149
+ return;
150
+ }
151
+ if (connection.userId === null) {
152
+ replyError(connection, message.id, "Not authenticated");
153
+ return;
154
+ }
155
+ const userId = connection.userId;
156
+ try {
157
+ switch (message.t) {
158
+ case "join": {
159
+ const result = await host.joinServer({
160
+ userId,
161
+ gameId: message.gameId,
162
+ serverId: message.serverId,
163
+ });
164
+ reply(connection, message.id, result);
165
+ return;
166
+ }
167
+ case "leave": {
168
+ await host.leaveServer({ userId, serverId: message.serverId });
169
+ reply(connection, message.id, null);
170
+ return;
171
+ }
172
+ case "runCommand": {
173
+ const result = await host.runCommand({
174
+ userId,
175
+ serverId: message.serverId,
176
+ command: message.command,
177
+ input: message.input,
178
+ });
179
+ reply(connection, message.id, result);
180
+ return;
181
+ }
182
+ case "pushFeed": {
183
+ await host.pushFeedEntry({
184
+ userId,
185
+ serverId: message.serverId,
186
+ action: message.action,
187
+ entry: message.entry,
188
+ });
189
+ reply(connection, message.id, null);
190
+ return;
191
+ }
192
+ case "subscribe": {
193
+ connection.subscriptions.add(subscriptionKey(message.channel, message.serverId, message.action));
194
+ reply(connection, message.id, null);
195
+ await pushSubscription(connection, message.channel, message.serverId, message.action);
196
+ return;
197
+ }
198
+ case "unsubscribe": {
199
+ connection.subscriptions.delete(subscriptionKey(message.channel, message.serverId, message.action));
200
+ reply(connection, message.id, null);
201
+ return;
202
+ }
203
+ }
204
+ }
205
+ catch (error) {
206
+ replyError(connection, message.id, error instanceof Error ? error.message : "Internal error");
207
+ }
208
+ };
209
+ wss.on("connection", (socket) => {
210
+ const connection = { socket, userId: null, subscriptions: new Set() };
211
+ connections.add(connection);
212
+ socket.on("message", (raw) => {
213
+ const message = decodeWsClientMessage(raw.toString());
214
+ if (message === null)
215
+ return;
216
+ void handleMessage(connection, message);
217
+ });
218
+ socket.on("close", () => {
219
+ connections.delete(connection);
220
+ dropPresence(connection);
221
+ });
222
+ });
223
+ return {
224
+ wss,
225
+ port: () => {
226
+ const address = wss.address();
227
+ if (address === null || typeof address === "string") {
228
+ throw new Error("WebSocket server has no bound port");
229
+ }
230
+ return address.port;
231
+ },
232
+ close: () => new Promise((resolve) => {
233
+ unsubscribeHost();
234
+ for (const connection of connections) {
235
+ connection.socket.terminate();
236
+ }
237
+ let settled = false;
238
+ const finish = () => {
239
+ if (settled)
240
+ return;
241
+ settled = true;
242
+ resolve();
243
+ };
244
+ wss.close(finish);
245
+ setTimeout(finish, 500);
246
+ }),
247
+ };
248
+ }
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@jgengine/node",
3
+ "version": "0.1.0",
4
+ "description": "Standalone authoritative game host for JGengine: in-memory server snapshots, tick loop, save-cadence flush, WebSocket server, memory/file persistence.",
5
+ "license": "AGPL-3.0-only",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/Noisemaker111/jgengine.git",
11
+ "directory": "packages/node"
12
+ },
13
+ "files": ["dist"],
14
+ "exports": {
15
+ "./*": {
16
+ "types": "./dist/*.d.ts",
17
+ "default": "./dist/*.js"
18
+ }
19
+ },
20
+ "scripts": {
21
+ "build": "tsc -p tsconfig.build.json && bun ../../scripts/fix-extensions.ts dist",
22
+ "check-types": "tsc --noEmit -p tsconfig.json",
23
+ "test": "bun test src"
24
+ },
25
+ "dependencies": {
26
+ "@jgengine/core": "^0.1.0",
27
+ "@jgengine/ws": "^0.1.0",
28
+ "ws": "^8.18.0"
29
+ },
30
+ "devDependencies": {
31
+ "@jgengine/sql": "workspace:*",
32
+ "@types/node": "^24",
33
+ "@types/ws": "^8",
34
+ "pg-mem": "^3.0.5"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ }
39
+ }