@gamerstake/game-core 0.1.0 → 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/CHANGELOG.md +142 -0
- package/CLIENT_SDK_README.md +634 -0
- package/README.md +58 -5
- package/dist/client/index.d.ts +574 -0
- package/dist/client/index.js +970 -0
- package/dist/client/index.js.map +1 -0
- package/dist/index.d.ts +90 -1
- package/dist/index.js +356 -1
- package/dist/index.js.map +1 -1
- package/docs/N1-package-overview.md +303 -0
- package/docs/N2-api-reference.md +500 -0
- package/docs/N3-implementation-guide.md +550 -0
- package/docs/N4-testing-validation.md +390 -0
- package/docs/N5-integration-checklist.md +90 -0
- package/docs/README.md +137 -0
- package/examples/simple-game/README.md +17 -6
- package/examples/simple-game/client.ts +25 -14
- package/examples/simple-game/package.json +5 -1
- package/examples/simple-game/server.ts +21 -13
- package/package.json +17 -13
- package/src/adapters/SocketIOAdapter.ts +496 -0
- package/src/client/GameClient.ts +466 -0
- package/src/client/InputBuffer.ts +148 -0
- package/src/client/Interpolator.ts +242 -0
- package/src/client/Reconciler.ts +233 -0
- package/src/client/TimeSync.ts +182 -0
- package/src/client/index.ts +32 -0
- package/src/client/types.ts +247 -0
- package/src/index.ts +8 -0
- package/tests/client/InputBuffer.test.ts +118 -0
- package/tests/client/TimeSync.test.ts +116 -0
- package/tsup.config.ts +4 -1
- package/TESTING_OVERVIEW.md +0 -378
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
# N1: @gamerstake/game-core - Package Overview & Requirements
|
|
2
|
+
|
|
3
|
+
> **Version:** 0.1.0
|
|
4
|
+
> **Status:** Production Ready
|
|
5
|
+
> **Last Updated:** January 2026
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 1. Executive Summary
|
|
10
|
+
|
|
11
|
+
`@gamerstake/game-core` is a **game-agnostic multiplayer game engine** designed for real-time multiplayer games. It provides battle-tested infrastructure for:
|
|
12
|
+
|
|
13
|
+
- Fixed tick-rate game loops (20 TPS default)
|
|
14
|
+
- Authoritative server architecture
|
|
15
|
+
- Client-server networking via Socket.io
|
|
16
|
+
- Entity management with dirty-flag tracking
|
|
17
|
+
- Spatial partitioning for efficient queries
|
|
18
|
+
- Input buffering and command processing
|
|
19
|
+
|
|
20
|
+
**Your responsibility:** Implement the `GameRules` interface to create your game logic.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## 2. System Requirements
|
|
25
|
+
|
|
26
|
+
### 2.1 Runtime Requirements
|
|
27
|
+
|
|
28
|
+
| Requirement | Minimum | Recommended |
|
|
29
|
+
| ----------- | ------- | ----------- |
|
|
30
|
+
| Node.js | 20.0.0+ | 22.x LTS |
|
|
31
|
+
| Memory | 256 MB | 512 MB+ |
|
|
32
|
+
| CPU | 1 core | 2+ cores |
|
|
33
|
+
|
|
34
|
+
### 2.2 Dependencies
|
|
35
|
+
|
|
36
|
+
**Core Dependencies (included):**
|
|
37
|
+
|
|
38
|
+
- `socket.io` ^4.7.4 - WebSocket communication
|
|
39
|
+
- `pino` ^9.0.0 - Structured logging
|
|
40
|
+
- `zod` ^3.22.4 - Runtime validation
|
|
41
|
+
|
|
42
|
+
**Dev Dependencies (for testing):**
|
|
43
|
+
|
|
44
|
+
- `typescript` ^5.3.3
|
|
45
|
+
- `jest` ^29.7.0
|
|
46
|
+
- `ts-jest` ^29.1.2
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 3. Installation
|
|
51
|
+
|
|
52
|
+
### 3.1 From NPM Registry
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# Using pnpm (recommended)
|
|
56
|
+
pnpm add @gamerstake/game-core socket.io
|
|
57
|
+
|
|
58
|
+
# Using npm
|
|
59
|
+
npm install @gamerstake/game-core socket.io
|
|
60
|
+
|
|
61
|
+
# Using yarn
|
|
62
|
+
yarn add @gamerstake/game-core socket.io
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 3.2 From Monorepo (Internal)
|
|
66
|
+
|
|
67
|
+
```json
|
|
68
|
+
{
|
|
69
|
+
"dependencies": {
|
|
70
|
+
"@gamerstake/game-core": "workspace:*"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## 4. Package Exports
|
|
78
|
+
|
|
79
|
+
The package exports the following modules:
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
// Core Systems
|
|
83
|
+
export { GameServer } from './core/GameServer';
|
|
84
|
+
export { Room } from './core/Room';
|
|
85
|
+
export { GameLoop } from './core/GameLoop';
|
|
86
|
+
export type { GameRules } from './core/GameRules';
|
|
87
|
+
|
|
88
|
+
// Entities
|
|
89
|
+
export { Entity } from './entities/Entity';
|
|
90
|
+
export { Registry } from './entities/Registry';
|
|
91
|
+
|
|
92
|
+
// Spatial Partitioning
|
|
93
|
+
export { Grid } from './spatial/Grid';
|
|
94
|
+
|
|
95
|
+
// Physics
|
|
96
|
+
export { AABBCollision, type AABB } from './physics/AABB';
|
|
97
|
+
export { Movement, type Boundary } from './physics/Movement';
|
|
98
|
+
|
|
99
|
+
// Input Processing
|
|
100
|
+
export { InputQueue } from './input/InputQueue';
|
|
101
|
+
export type { Command, MoveCommand, StopCommand, ActionCommand } from './input/Command';
|
|
102
|
+
|
|
103
|
+
// Networking
|
|
104
|
+
export { Network } from './network/Network';
|
|
105
|
+
export { Snapshot } from './network/Snapshot';
|
|
106
|
+
|
|
107
|
+
// Utilities
|
|
108
|
+
export { RingBuffer } from './utils/RingBuffer';
|
|
109
|
+
export { Logger, logger, setLogger, createChildLogger } from './utils/Logger';
|
|
110
|
+
|
|
111
|
+
// Types
|
|
112
|
+
export * from './types/index';
|
|
113
|
+
export * from './types/protocol';
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## 5. Core Concepts
|
|
119
|
+
|
|
120
|
+
### 5.1 Architecture Overview
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
124
|
+
│ GameServer │
|
|
125
|
+
│ ┌────────────────────────────────────────────────────────┐ │
|
|
126
|
+
│ │ Room │ │
|
|
127
|
+
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌─────────┐ │ │
|
|
128
|
+
│ │ │ GameLoop │ │ Registry │ │ Grid │ │ Network │ │ │
|
|
129
|
+
│ │ │ (20 TPS) │ │(Entities)│ │ (Spatial)│ │(Socket) │ │ │
|
|
130
|
+
│ │ └──────────┘ └──────────┘ └──────────┘ └─────────┘ │ │
|
|
131
|
+
│ │ ┌──────────┐ ┌─────────────────────────────────────┐ │ │
|
|
132
|
+
│ │ │InputQueue│ │ GameRules │ │ │
|
|
133
|
+
│ │ │ (Buffer) │ │ (YOUR GAME LOGIC) │ │ │
|
|
134
|
+
│ │ └──────────┘ └─────────────────────────────────────┘ │ │
|
|
135
|
+
│ └────────────────────────────────────────────────────────┘ │
|
|
136
|
+
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
|
|
137
|
+
│ │ Room 2 │ │ Room 3 │ │ Room N │ ... │
|
|
138
|
+
│ └─────────┘ └─────────┘ └─────────┘ │
|
|
139
|
+
└─────────────────────────────────────────────────────────────┘
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### 5.2 Data Flow (Per Tick)
|
|
143
|
+
|
|
144
|
+
```
|
|
145
|
+
Client Server
|
|
146
|
+
│ │
|
|
147
|
+
│ C_MOVE (input) │
|
|
148
|
+
│ ─────────────────────────────────>│
|
|
149
|
+
│ │ [InputQueue]
|
|
150
|
+
│ │ │
|
|
151
|
+
│ │ [GameLoop.onTick]
|
|
152
|
+
│ │ │
|
|
153
|
+
│ │ ├─ Process inputs (onCommand)
|
|
154
|
+
│ │ ├─ Update state (onTick)
|
|
155
|
+
│ │ ├─ Check end condition
|
|
156
|
+
│ │ └─ Broadcast deltas
|
|
157
|
+
│ │
|
|
158
|
+
│ S_UPDATE (state) │
|
|
159
|
+
│ <─────────────────────────────────│
|
|
160
|
+
│ │
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### 5.3 Tick Cycle (50ms @ 20 TPS)
|
|
164
|
+
|
|
165
|
+
1. **Process Inputs** - Drain InputQueue, call `onCommand` for each
|
|
166
|
+
2. **Update State** - Call `onTick` with delta time
|
|
167
|
+
3. **Check End** - Call `shouldEndRoom`
|
|
168
|
+
4. **Broadcast** - Send delta snapshot to all clients
|
|
169
|
+
5. **Sleep** - Wait until next tick
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## 6. GameRules Interface (Required Implementation)
|
|
174
|
+
|
|
175
|
+
The `GameRules` interface is the **core abstraction** you must implement:
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
interface GameRules<TEntity extends Entity = Entity> {
|
|
179
|
+
/**
|
|
180
|
+
* Called once when room is created.
|
|
181
|
+
* Use for initialization (spawn items, set boundaries, etc).
|
|
182
|
+
*/
|
|
183
|
+
onRoomCreated(room: Room<TEntity>): void;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Called when a player joins the room.
|
|
187
|
+
*/
|
|
188
|
+
onPlayerJoin(room: Room<TEntity>, player: TEntity): void;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Called when a player leaves the room.
|
|
192
|
+
*/
|
|
193
|
+
onPlayerLeave(room: Room<TEntity>, playerId: string): void;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Called every tick (20 TPS by default).
|
|
197
|
+
* Process inputs, update game state, check win conditions.
|
|
198
|
+
*/
|
|
199
|
+
onTick(room: Room<TEntity>, delta: number): void;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Called when a player sends a command.
|
|
203
|
+
*/
|
|
204
|
+
onCommand(room: Room<TEntity>, playerId: string, command: Command): void;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Check if the room should be destroyed.
|
|
208
|
+
* Return true for match-based games when match ends.
|
|
209
|
+
* Return false for persistent worlds.
|
|
210
|
+
*/
|
|
211
|
+
shouldEndRoom(room: Room<TEntity>): boolean;
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
---
|
|
216
|
+
|
|
217
|
+
## 7. Configuration Options
|
|
218
|
+
|
|
219
|
+
### 7.1 RoomConfig
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
interface RoomConfig {
|
|
223
|
+
/** Ticks per second (default: 20) */
|
|
224
|
+
tickRate?: number;
|
|
225
|
+
|
|
226
|
+
/** Grid cell size in world units (default: 512) */
|
|
227
|
+
cellSize?: number;
|
|
228
|
+
|
|
229
|
+
/** Maximum buffered inputs per player (default: 100) */
|
|
230
|
+
maxInputQueueSize?: number;
|
|
231
|
+
|
|
232
|
+
/** Maximum entities per room (default: 1000) */
|
|
233
|
+
maxEntities?: number;
|
|
234
|
+
|
|
235
|
+
/** Visibility range in grid cells (default: 1 = 3x3 area) */
|
|
236
|
+
visibilityRange?: number;
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### 7.2 Recommended Settings by Game Type
|
|
241
|
+
|
|
242
|
+
| Game Type | tickRate | cellSize | maxEntities |
|
|
243
|
+
| ----------- | -------- | -------- | ----------- |
|
|
244
|
+
| Action/FPS | 20-30 | 256 | 500 |
|
|
245
|
+
| MOBA/Arena | 20 | 512 | 200 |
|
|
246
|
+
| MMO Zone | 10-15 | 1024 | 1000 |
|
|
247
|
+
| Casual/Turn | 5-10 | 512 | 100 |
|
|
248
|
+
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
## 8. Performance Guarantees
|
|
252
|
+
|
|
253
|
+
### 8.1 Benchmarks
|
|
254
|
+
|
|
255
|
+
| Metric | Target | Verified |
|
|
256
|
+
| ------------- | ------ | -------- |
|
|
257
|
+
| Tick Rate | 20 TPS | Yes |
|
|
258
|
+
| Avg Tick Time | < 40ms | Yes |
|
|
259
|
+
| Max Tick Time | < 50ms | Yes |
|
|
260
|
+
| Entities/Room | 100+ | Yes |
|
|
261
|
+
| Spatial Query | O(1) | Yes |
|
|
262
|
+
|
|
263
|
+
### 8.2 Optimizations Included
|
|
264
|
+
|
|
265
|
+
- **Dirty Flag Tracking** - Only broadcast changed entities
|
|
266
|
+
- **Buffer Reuse** - Zero allocations in hot paths
|
|
267
|
+
- **RingBuffer** - O(1) input queue operations
|
|
268
|
+
- **Spatial Grid** - Avoid O(n²) collision checks
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## 9. Support & Resources
|
|
273
|
+
|
|
274
|
+
### 9.1 Documentation
|
|
275
|
+
|
|
276
|
+
| Document | Purpose |
|
|
277
|
+
| --------------------------- | --------------------------- |
|
|
278
|
+
| N1-package-overview.md | This document |
|
|
279
|
+
| N2-api-reference.md | Complete API documentation |
|
|
280
|
+
| N3-implementation-guide.md | Step-by-step implementation |
|
|
281
|
+
| N4-testing-validation.md | Testing requirements |
|
|
282
|
+
| N5-integration-checklist.md | Verification checklist |
|
|
283
|
+
|
|
284
|
+
### 9.2 Examples
|
|
285
|
+
|
|
286
|
+
- `examples/simple-game/` - Complete working example
|
|
287
|
+
- `tests/` - Unit test patterns
|
|
288
|
+
|
|
289
|
+
### 9.3 Contact
|
|
290
|
+
|
|
291
|
+
For questions or issues, contact the GamerStake engineering team.
|
|
292
|
+
|
|
293
|
+
---
|
|
294
|
+
|
|
295
|
+
## 10. Version History
|
|
296
|
+
|
|
297
|
+
| Version | Date | Changes |
|
|
298
|
+
| ------- | -------- | --------------- |
|
|
299
|
+
| 0.1.0 | Jan 2026 | Initial release |
|
|
300
|
+
|
|
301
|
+
---
|
|
302
|
+
|
|
303
|
+
**Next:** [N2 - API Reference](./N2-api-reference.md)
|