@lagless/create 0.0.56 → 0.0.58
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/package.json
CHANGED
|
@@ -173,6 +173,15 @@ const hooks: RoomHooks = {
|
|
|
173
173
|
reason,
|
|
174
174
|
});
|
|
175
175
|
},
|
|
176
|
+
// Inspect or reject client inputs before broadcast:
|
|
177
|
+
onInput: (ctx, player, input) => {
|
|
178
|
+
// input: { tick, playerSlot, seq, payload }
|
|
179
|
+
// Return false to reject (sends CancelInput to sender)
|
|
180
|
+
},
|
|
181
|
+
// Called when input is rejected (validation or onInput):
|
|
182
|
+
onInputDeclined: (ctx, player, tick, seq, reason) => {
|
|
183
|
+
// reason: 0=TooOld, 1=TooFarFuture, 2=InvalidSlot, 3=Rejected
|
|
184
|
+
},
|
|
176
185
|
};
|
|
177
186
|
```
|
|
178
187
|
|
|
@@ -190,6 +199,51 @@ for (const rpc of rpcs) {
|
|
|
190
199
|
|
|
191
200
|
The key difference: server events have `rpc.meta.playerSlot === SERVER_SLOT` (255).
|
|
192
201
|
|
|
202
|
+
## String Data in RPCs
|
|
203
|
+
|
|
204
|
+
RPCs use numeric fields (`uint8`, `float32`, etc.), but sometimes you need to send string data (e.g., username in `PlayerJoined`). Use `encodeStringToUint8` / `decodeStringFromUint8` from `@lagless/binary` with `uint8[N]` array fields.
|
|
205
|
+
|
|
206
|
+
**Encoding:** BMP-only UTF-16, fixed 2 bytes per character. Supports Latin, Cyrillic, CJK, Arabic, Greek. Emoji (non-BMP) are replaced with `?`.
|
|
207
|
+
|
|
208
|
+
### Schema
|
|
209
|
+
|
|
210
|
+
```yaml
|
|
211
|
+
inputs:
|
|
212
|
+
PlayerJoined:
|
|
213
|
+
slot: uint8
|
|
214
|
+
username: uint8[64] # 64 bytes = 32 characters max
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Sending (server hook)
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { encodeStringToUint8 } from '@lagless/binary';
|
|
221
|
+
|
|
222
|
+
onPlayerJoin(ctx, player) {
|
|
223
|
+
const { buffer } = encodeStringToUint8(player.username, 64);
|
|
224
|
+
ctx.emitServerEvent(PlayerJoined, { slot: player.slot, username: buffer });
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Reading (system)
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
import { decodeStringFromUint8 } from '@lagless/binary';
|
|
232
|
+
|
|
233
|
+
const rpcs = this._input.collectTickRPCs(tick, PlayerJoined);
|
|
234
|
+
for (const rpc of rpcs) {
|
|
235
|
+
const username = decodeStringFromUint8(rpc.data.username as Uint8Array);
|
|
236
|
+
// username is a JS string, e.g. "Игрок_123"
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### Key Points
|
|
241
|
+
|
|
242
|
+
- `maxBytes` must be even (2 bytes per character). `maxBytes / 2` = max characters.
|
|
243
|
+
- `encodeStringToUint8` returns `{ buffer: Uint8Array, truncated: boolean }`.
|
|
244
|
+
- Truncation never splits a character — always clean cut.
|
|
245
|
+
- Buffer is zero-padded; `decodeStringFromUint8` stops at first null (`0x0000`) or end of buffer.
|
|
246
|
+
|
|
193
247
|
## Adding a New Input Type
|
|
194
248
|
|
|
195
249
|
1. **Schema** — Add to `ecs.yaml`:
|
|
@@ -85,6 +85,7 @@ const server = new RelayGameServer({
|
|
|
85
85
|
config: {
|
|
86
86
|
maxPlayers: 4,
|
|
87
87
|
reconnectTimeoutMs: 15_000,
|
|
88
|
+
inputRecordingEnabled: true, // enable replay export
|
|
88
89
|
},
|
|
89
90
|
hooks,
|
|
90
91
|
inputRegistry: MyInputRegistry,
|
|
@@ -170,6 +171,20 @@ export const hooks: RoomHooks = {
|
|
|
170
171
|
// Persist results to database, etc.
|
|
171
172
|
},
|
|
172
173
|
|
|
174
|
+
// Inspect or reject client inputs before broadcast (sync, called per input)
|
|
175
|
+
onInput: (ctx, player, input) => {
|
|
176
|
+
// input: { tick, playerSlot, seq, payload (Uint8Array) }
|
|
177
|
+
// Decode payload: InputBinarySchema.unpackBatch(registry, input.payload.buffer)
|
|
178
|
+
// Return false to reject (sends CancelInput with Rejected reason)
|
|
179
|
+
// Return void/true to accept
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
// Called when input is rejected (by validation or onInput returning false)
|
|
183
|
+
onInputDeclined: (ctx, player, tick, seq, reason) => {
|
|
184
|
+
// reason: 0=TooOld, 1=TooFarFuture, 2=InvalidSlot, 3=Rejected
|
|
185
|
+
// Use for logging, rate-limit tracking, anti-cheat analytics
|
|
186
|
+
},
|
|
187
|
+
|
|
173
188
|
// Room is being disposed
|
|
174
189
|
onRoomDisposed: (ctx) => {
|
|
175
190
|
console.log('Room disposed:', ctx.roomId);
|
|
@@ -186,6 +201,23 @@ ctx.emitServerEvent(InputClass, data); // Send server-originated RPC
|
|
|
186
201
|
ctx.getPlayers(); // Get all player info
|
|
187
202
|
ctx.endMatch(results); // End the match
|
|
188
203
|
ctx.roomId; // Room identifier
|
|
204
|
+
ctx.exportRecordedInputs(); // RPCHistory binary (requires inputRecordingEnabled)
|
|
205
|
+
ctx.exportReplay(); // Full replay binary (seed + maxPlayers + fps + RPCHistory)
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Replay Export
|
|
209
|
+
|
|
210
|
+
Enable `inputRecordingEnabled: true` in server config. All broadcast inputs (client + server events) are stored. Export in `onMatchEnd`:
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
onMatchEnd: async (ctx, results) => {
|
|
214
|
+
const replay = ctx.exportReplay();
|
|
215
|
+
if (replay) {
|
|
216
|
+
// replay is ArrayBuffer — save to DB, file, or cloud storage
|
|
217
|
+
// Load later via ReplayInputProvider.createFromReplay(replay, inputRegistry)
|
|
218
|
+
await saveReplay(ctx.matchId, replay);
|
|
219
|
+
}
|
|
220
|
+
},
|
|
189
221
|
```
|
|
190
222
|
|
|
191
223
|
## Server Events via emitServerEvent
|
|
@@ -100,6 +100,39 @@ Tags have no fields — they only occupy a bitmask bit (zero memory per entity).
|
|
|
100
100
|
}
|
|
101
101
|
```
|
|
102
102
|
|
|
103
|
+
## Send String Data in RPCs (e.g. Username)
|
|
104
|
+
|
|
105
|
+
RPCs only support numeric fields. To send strings, use `uint8[N]` array fields with `encodeStringToUint8` / `decodeStringFromUint8`.
|
|
106
|
+
|
|
107
|
+
1. Edit `ecs.yaml`:
|
|
108
|
+
```yaml
|
|
109
|
+
inputs:
|
|
110
|
+
PlayerJoined:
|
|
111
|
+
slot: uint8
|
|
112
|
+
username: uint8[64] # 64 bytes = 32 characters max
|
|
113
|
+
```
|
|
114
|
+
2. Run `pnpm codegen`
|
|
115
|
+
3. Encode on send (server hook):
|
|
116
|
+
```typescript
|
|
117
|
+
import { encodeStringToUint8 } from '@lagless/binary';
|
|
118
|
+
|
|
119
|
+
onPlayerJoin(ctx, player) {
|
|
120
|
+
const { buffer } = encodeStringToUint8(player.username, 64);
|
|
121
|
+
ctx.emitServerEvent(PlayerJoined, { slot: player.slot, username: buffer });
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
4. Decode in system:
|
|
125
|
+
```typescript
|
|
126
|
+
import { decodeStringFromUint8 } from '@lagless/binary';
|
|
127
|
+
|
|
128
|
+
const rpcs = this._input.collectTickRPCs(tick, PlayerJoined);
|
|
129
|
+
for (const rpc of rpcs) {
|
|
130
|
+
const username = decodeStringFromUint8(rpc.data.username as Uint8Array);
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Supports:** Latin, Cyrillic, CJK, Arabic, Greek. **No emoji** — replaced with `?`.
|
|
135
|
+
|
|
103
136
|
## Add a New Entity Type
|
|
104
137
|
|
|
105
138
|
1. Define components and filter in `ecs.yaml`:
|
|
@@ -82,6 +82,22 @@ for (const rpc of rpcs) {
|
|
|
82
82
|
const buffer = inputProvider.getFrameRPCBuffer(tick);
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
## String Encoding (for RPCs)
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { encodeStringToUint8, decodeStringFromUint8 } from '@lagless/binary';
|
|
89
|
+
|
|
90
|
+
// Encode string into fixed-size buffer (BMP UTF-16, 2 bytes/char)
|
|
91
|
+
const { buffer, truncated } = encodeStringToUint8('Игрок', 64); // 32 chars max
|
|
92
|
+
|
|
93
|
+
// Decode buffer back to string
|
|
94
|
+
const str = decodeStringFromUint8(buffer); // 'Игрок'
|
|
95
|
+
|
|
96
|
+
// Use with uint8[N] RPC fields:
|
|
97
|
+
addRPC(PlayerJoined, { slot: 0, username: buffer });
|
|
98
|
+
const name = decodeStringFromUint8(rpc.data.username as Uint8Array);
|
|
99
|
+
```
|
|
100
|
+
|
|
85
101
|
## Signals
|
|
86
102
|
|
|
87
103
|
```typescript
|