Compare commits
41 Commits
@esengine/
...
@esengine/
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35d81880a7 | ||
|
|
71022abc99 | ||
|
|
87f71e2251 | ||
|
|
b9ea8d14cf | ||
|
|
10d0fb1d5c | ||
|
|
71e111415f | ||
|
|
0de45279e6 | ||
|
|
cc6f12d470 | ||
|
|
902c0a1074 | ||
|
|
d3e489aad3 | ||
|
|
12051d987f | ||
|
|
b38fe5ebf4 | ||
|
|
f01ce1e320 | ||
|
|
094133a71a | ||
|
|
3e5b7783be | ||
|
|
ebcb4d00a8 | ||
|
|
d2af9caae9 | ||
|
|
bb696c6a60 | ||
|
|
ffd35a71cd | ||
|
|
1f3a76aabe | ||
|
|
ddc7d1f726 | ||
|
|
04b08f3f07 | ||
|
|
d9969d0b08 | ||
|
|
bdbbf8a80a | ||
|
|
1368473c71 | ||
|
|
b28169b186 | ||
|
|
e2598b2292 | ||
|
|
2e3889abed | ||
|
|
d21caa974e | ||
|
|
a08a84b7db | ||
|
|
449bd420a6 | ||
|
|
1f297ac769 | ||
|
|
4cf868a769 | ||
|
|
afdeb00b4d | ||
|
|
764ce67742 | ||
|
|
61a13baca2 | ||
|
|
1cfa64aa0f | ||
|
|
3b978384c7 | ||
|
|
10c3891abd | ||
|
|
18af48a0fc | ||
|
|
d4cef828e1 |
22
README.md
22
README.md
@@ -49,7 +49,12 @@ npm install @esengine/ecs-framework
|
||||
| **Timer** | Timer and cooldown systems | No |
|
||||
| **Spatial** | Spatial indexing and queries (QuadTree, Grid) | No |
|
||||
| **Pathfinding** | A* and navigation mesh pathfinding | No |
|
||||
| **Network** | Client/server networking with TSRPC | No |
|
||||
| **Procgen** | Procedural generation (noise, random, sampling) | No |
|
||||
| **RPC** | High-performance RPC communication framework | No |
|
||||
| **Server** | Game server framework with rooms, auth, rate limiting | No |
|
||||
| **Network** | Client networking with prediction, AOI, delta compression | No |
|
||||
| **Transaction** | Game transaction system with Redis/Memory storage | No |
|
||||
| **World Streaming** | Open world chunk loading and streaming | No |
|
||||
|
||||
> All framework modules can be used standalone with any rendering engine.
|
||||
|
||||
@@ -199,7 +204,12 @@ npm install @esengine/fsm # State machines
|
||||
npm install @esengine/timer # Timers & cooldowns
|
||||
npm install @esengine/spatial # Spatial indexing
|
||||
npm install @esengine/pathfinding # Pathfinding
|
||||
npm install @esengine/network # Networking
|
||||
npm install @esengine/procgen # Procedural generation
|
||||
npm install @esengine/rpc # RPC framework
|
||||
npm install @esengine/server # Game server
|
||||
npm install @esengine/network # Client networking
|
||||
npm install @esengine/transaction # Transaction system
|
||||
npm install @esengine/world-streaming # World streaming
|
||||
```
|
||||
|
||||
### ESEngine Runtime (Optional)
|
||||
@@ -218,6 +228,7 @@ If you want a complete engine solution with rendering:
|
||||
A visual editor built with Tauri for scene management:
|
||||
|
||||
- Download from [Releases](https://github.com/esengine/esengine/releases)
|
||||
- [Build from source](./packages/editor/editor-app/README.md)
|
||||
- Supports behavior tree editing, tilemap painting, visual scripting
|
||||
|
||||
## Project Structure
|
||||
@@ -235,7 +246,11 @@ esengine/
|
||||
│ │ ├── spatial/ # Spatial queries
|
||||
│ │ ├── pathfinding/ # Pathfinding
|
||||
│ │ ├── procgen/ # Procedural generation
|
||||
│ │ └── network/ # Networking
|
||||
│ │ ├── rpc/ # RPC framework
|
||||
│ │ ├── server/ # Game server
|
||||
│ │ ├── network/ # Client networking
|
||||
│ │ ├── transaction/ # Transaction system
|
||||
│ │ └── world-streaming/ # World streaming
|
||||
│ │
|
||||
│ ├── engine/ # ESEngine runtime
|
||||
│ ├── rendering/ # Rendering modules
|
||||
@@ -267,6 +282,7 @@ pnpm test
|
||||
|
||||
- [ECS Framework Guide](./packages/framework/core/README.md)
|
||||
- [Behavior Tree Guide](./packages/framework/behavior-tree/README.md)
|
||||
- [Editor Setup Guide](./packages/editor/editor-app/README.md) ([中文](./packages/editor/editor-app/README_CN.md))
|
||||
- [API Reference](https://esengine.cn/api/README)
|
||||
|
||||
## Community
|
||||
|
||||
22
README_CN.md
22
README_CN.md
@@ -49,7 +49,12 @@ npm install @esengine/ecs-framework
|
||||
| **定时器** | 定时器和冷却系统 | 否 |
|
||||
| **空间索引** | 空间查询(四叉树、网格) | 否 |
|
||||
| **寻路** | A* 和导航网格寻路 | 否 |
|
||||
| **网络** | 客户端/服务端网络通信 (TSRPC) | 否 |
|
||||
| **程序化生成** | 噪声、随机、采样等生成算法 | 否 |
|
||||
| **RPC** | 高性能 RPC 通信框架 | 否 |
|
||||
| **服务端** | 游戏服务器框架,支持房间、认证、速率限制 | 否 |
|
||||
| **网络** | 客户端网络,支持预测、AOI、增量压缩 | 否 |
|
||||
| **事务系统** | 游戏事务系统,支持 Redis/内存存储 | 否 |
|
||||
| **世界流送** | 开放世界分块加载和流送 | 否 |
|
||||
|
||||
> 所有框架模块都可以独立使用,无需依赖特定渲染引擎。
|
||||
|
||||
@@ -199,7 +204,12 @@ npm install @esengine/fsm # 状态机
|
||||
npm install @esengine/timer # 定时器和冷却
|
||||
npm install @esengine/spatial # 空间索引
|
||||
npm install @esengine/pathfinding # 寻路
|
||||
npm install @esengine/network # 网络
|
||||
npm install @esengine/procgen # 程序化生成
|
||||
npm install @esengine/rpc # RPC 框架
|
||||
npm install @esengine/server # 游戏服务器
|
||||
npm install @esengine/network # 客户端网络
|
||||
npm install @esengine/transaction # 事务系统
|
||||
npm install @esengine/world-streaming # 世界流送
|
||||
```
|
||||
|
||||
### ESEngine 运行时(可选)
|
||||
@@ -218,6 +228,7 @@ npm install @esengine/network # 网络
|
||||
基于 Tauri 构建的可视化编辑器:
|
||||
|
||||
- 从 [Releases](https://github.com/esengine/esengine/releases) 下载
|
||||
- [从源码构建](./packages/editor/editor-app/README.md)
|
||||
- 支持行为树编辑、Tilemap 绘制、可视化脚本
|
||||
|
||||
## 项目结构
|
||||
@@ -235,7 +246,11 @@ esengine/
|
||||
│ │ ├── spatial/ # 空间查询
|
||||
│ │ ├── pathfinding/ # 寻路
|
||||
│ │ ├── procgen/ # 程序化生成
|
||||
│ │ └── network/ # 网络
|
||||
│ │ ├── rpc/ # RPC 框架
|
||||
│ │ ├── server/ # 游戏服务器
|
||||
│ │ ├── network/ # 客户端网络
|
||||
│ │ ├── transaction/ # 事务系统
|
||||
│ │ └── world-streaming/ # 世界流送
|
||||
│ │
|
||||
│ ├── engine/ # ESEngine 运行时
|
||||
│ ├── rendering/ # 渲染模块
|
||||
@@ -267,6 +282,7 @@ pnpm test
|
||||
|
||||
- [ECS 框架指南](./packages/framework/core/README.md)
|
||||
- [行为树指南](./packages/framework/behavior-tree/README.md)
|
||||
- [编辑器启动指南](./packages/editor/editor-app/README_CN.md) ([English](./packages/editor/editor-app/README.md))
|
||||
- [API 参考](https://esengine.cn/api/README)
|
||||
|
||||
## 社区
|
||||
|
||||
@@ -255,6 +255,9 @@ export default defineConfig({
|
||||
translations: { en: 'RPC' },
|
||||
items: [
|
||||
{ label: '概述', slug: 'modules/rpc', translations: { en: 'Overview' } },
|
||||
{ label: '服务端', slug: 'modules/rpc/server', translations: { en: 'Server' } },
|
||||
{ label: '客户端', slug: 'modules/rpc/client', translations: { en: 'Client' } },
|
||||
{ label: '编解码', slug: 'modules/rpc/codec', translations: { en: 'Codec' } },
|
||||
],
|
||||
},
|
||||
{
|
||||
@@ -264,10 +267,26 @@ export default defineConfig({
|
||||
{ label: '概述', slug: 'modules/network', translations: { en: 'Overview' } },
|
||||
{ label: '客户端', slug: 'modules/network/client', translations: { en: 'Client' } },
|
||||
{ label: '服务器', slug: 'modules/network/server', translations: { en: 'Server' } },
|
||||
{ label: '认证系统', slug: 'modules/network/auth', translations: { en: 'Authentication' } },
|
||||
{ label: '速率限制', slug: 'modules/network/rate-limit', translations: { en: 'Rate Limiting' } },
|
||||
{ label: '状态同步', slug: 'modules/network/sync', translations: { en: 'State Sync' } },
|
||||
{ label: '客户端预测', slug: 'modules/network/prediction', translations: { en: 'Prediction' } },
|
||||
{ label: 'AOI 兴趣区域', slug: 'modules/network/aoi', translations: { en: 'AOI' } },
|
||||
{ label: '增量压缩', slug: 'modules/network/delta', translations: { en: 'Delta Compression' } },
|
||||
{ label: 'API 参考', slug: 'modules/network/api', translations: { en: 'API Reference' } },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: '事务系统',
|
||||
translations: { en: 'Transaction' },
|
||||
items: [
|
||||
{ label: '概述', slug: 'modules/transaction', translations: { en: 'Overview' } },
|
||||
{ label: '核心概念', slug: 'modules/transaction/core', translations: { en: 'Core Concepts' } },
|
||||
{ label: '存储层', slug: 'modules/transaction/storage', translations: { en: 'Storage Layer' } },
|
||||
{ label: '操作', slug: 'modules/transaction/operations', translations: { en: 'Operations' } },
|
||||
{ label: '分布式事务', slug: 'modules/transaction/distributed', translations: { en: 'Distributed' } },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: '世界流式加载',
|
||||
translations: { en: 'World Streaming' },
|
||||
@@ -303,6 +322,8 @@ export default defineConfig({
|
||||
{ label: '@esengine/fsm', link: 'https://github.com/esengine/esengine/blob/master/packages/framework/fsm/CHANGELOG.md', attrs: { target: '_blank' } },
|
||||
{ label: '@esengine/timer', link: 'https://github.com/esengine/esengine/blob/master/packages/framework/timer/CHANGELOG.md', attrs: { target: '_blank' } },
|
||||
{ label: '@esengine/network', link: 'https://github.com/esengine/esengine/blob/master/packages/framework/network/CHANGELOG.md', attrs: { target: '_blank' } },
|
||||
{ label: '@esengine/transaction', link: 'https://github.com/esengine/esengine/blob/master/packages/framework/transaction/CHANGELOG.md', attrs: { target: '_blank' } },
|
||||
{ label: '@esengine/rpc', link: 'https://github.com/esengine/esengine/blob/master/packages/framework/rpc/CHANGELOG.md', attrs: { target: '_blank' } },
|
||||
{ label: '@esengine/cli', link: 'https://github.com/esengine/esengine/blob/master/packages/tools/cli/CHANGELOG.md', attrs: { target: '_blank' } },
|
||||
],
|
||||
},
|
||||
|
||||
@@ -71,6 +71,55 @@ class ConfiguredScene extends Scene {
|
||||
}
|
||||
```
|
||||
|
||||
## Runtime Environment
|
||||
|
||||
For networked games, you can configure the runtime environment to distinguish between server and client logic.
|
||||
|
||||
### Global Configuration (Recommended)
|
||||
|
||||
Set the runtime environment once at the Core level - all Scenes will inherit this setting:
|
||||
|
||||
```typescript
|
||||
import { Core } from '@esengine/ecs-framework';
|
||||
|
||||
// Method 1: Set in Core.create()
|
||||
Core.create({ runtimeEnvironment: 'server' });
|
||||
|
||||
// Method 2: Set static property directly
|
||||
Core.runtimeEnvironment = 'server';
|
||||
```
|
||||
|
||||
### Per-Scene Override
|
||||
|
||||
Individual scenes can override the global setting:
|
||||
|
||||
```typescript
|
||||
const clientScene = new Scene({ runtimeEnvironment: 'client' });
|
||||
```
|
||||
|
||||
### Environment Types
|
||||
|
||||
| Environment | Use Case |
|
||||
|-------------|----------|
|
||||
| `'standalone'` | Single-player games (default) |
|
||||
| `'server'` | Game server, authoritative logic |
|
||||
| `'client'` | Game client, rendering/input |
|
||||
|
||||
### Checking Environment in Systems
|
||||
|
||||
```typescript
|
||||
class CollectibleSpawnSystem extends EntitySystem {
|
||||
private checkCollections(): void {
|
||||
// Skip on client - only server handles authoritative logic
|
||||
if (!this.scene.isServer) return;
|
||||
|
||||
// Server-authoritative spawn logic...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
See [System Runtime Decorators](/en/guide/system/index#runtime-environment-decorators) for decorator-based approach.
|
||||
|
||||
### Running a Scene
|
||||
|
||||
```typescript
|
||||
|
||||
@@ -160,6 +160,53 @@ scene.addSystem(new SystemA()); // addOrder = 0, executes first
|
||||
scene.addSystem(new SystemB()); // addOrder = 1, executes second
|
||||
```
|
||||
|
||||
## Runtime Environment Decorators
|
||||
|
||||
For networked games, you can use decorators to control which environment a system method runs in.
|
||||
|
||||
### Available Decorators
|
||||
|
||||
| Decorator | Effect |
|
||||
|-----------|--------|
|
||||
| `@ServerOnly()` | Method only executes on server |
|
||||
| `@ClientOnly()` | Method only executes on client |
|
||||
| `@NotServer()` | Method skipped on server |
|
||||
| `@NotClient()` | Method skipped on client |
|
||||
|
||||
### Usage Example
|
||||
|
||||
```typescript
|
||||
import { EntitySystem, ServerOnly, ClientOnly } from '@esengine/ecs-framework';
|
||||
|
||||
class GameSystem extends EntitySystem {
|
||||
@ServerOnly()
|
||||
private spawnEnemies(): void {
|
||||
// Only runs on server - authoritative spawn logic
|
||||
}
|
||||
|
||||
@ClientOnly()
|
||||
private playEffects(): void {
|
||||
// Only runs on client - visual effects
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Simple Conditional Check
|
||||
|
||||
For simple cases, a direct check is often clearer than decorators:
|
||||
|
||||
```typescript
|
||||
class CollectibleSystem extends EntitySystem {
|
||||
private checkCollections(): void {
|
||||
if (!this.scene.isServer) return; // Skip on client
|
||||
|
||||
// Server-authoritative logic...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
See [Scene Runtime Environment](/en/guide/scene/index#runtime-environment) for configuration details.
|
||||
|
||||
## Next Steps
|
||||
|
||||
- [System Types](/en/guide/system/types) - Learn about different system base classes
|
||||
|
||||
@@ -182,6 +182,70 @@ export class IsHealthLow implements INodeExecutor {
|
||||
}
|
||||
```
|
||||
|
||||
## Using Custom Executors in BehaviorTreeBuilder
|
||||
|
||||
After defining a custom executor with `@NodeExecutorMetadata`, use the `.action()` method in the builder:
|
||||
|
||||
```typescript
|
||||
import { BehaviorTreeBuilder, BehaviorTreeStarter } from '@esengine/behavior-tree';
|
||||
|
||||
// Use custom executor in behavior tree
|
||||
const tree = BehaviorTreeBuilder.create('CombatAI')
|
||||
.defineBlackboardVariable('health', 100)
|
||||
.defineBlackboardVariable('target', null)
|
||||
.selector('Root')
|
||||
.sequence('AttackSequence')
|
||||
// Use custom action - matches implementationType in decorator
|
||||
.action('AttackAction', 'Attack', { damage: 25 })
|
||||
.action('MoveToTarget', 'Chase')
|
||||
.end()
|
||||
.action('WaitAction', 'Idle', { duration: 1000 })
|
||||
.end()
|
||||
.build();
|
||||
|
||||
// Start the behavior tree
|
||||
const entity = scene.createEntity('Enemy');
|
||||
BehaviorTreeStarter.start(entity, tree);
|
||||
```
|
||||
|
||||
### Builder Methods for Custom Nodes
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `.action(type, name?, config?)` | Add custom action node |
|
||||
| `.condition(type, name?, config?)` | Add custom condition node |
|
||||
| `.executeAction(name)` | Use blackboard function `action_{name}` |
|
||||
| `.executeCondition(name)` | Use blackboard function `condition_{name}` |
|
||||
|
||||
### Complete Example
|
||||
|
||||
```typescript
|
||||
// 1. Define custom executor
|
||||
@NodeExecutorMetadata({
|
||||
implementationType: 'AttackAction',
|
||||
nodeType: NodeType.Action,
|
||||
displayName: 'Attack',
|
||||
category: 'Combat',
|
||||
configSchema: {
|
||||
damage: { type: 'number', default: 10, supportBinding: true }
|
||||
}
|
||||
})
|
||||
class AttackAction implements INodeExecutor {
|
||||
execute(context: NodeExecutionContext): TaskStatus {
|
||||
const damage = BindingHelper.getValue<number>(context, 'damage', 10);
|
||||
console.log(`Attacking with ${damage} damage!`);
|
||||
return TaskStatus.Success;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Build and use
|
||||
const tree = BehaviorTreeBuilder.create('AI')
|
||||
.selector('Root')
|
||||
.action('AttackAction', 'Attack', { damage: 50 })
|
||||
.end()
|
||||
.build();
|
||||
```
|
||||
|
||||
## Registering Custom Executors
|
||||
|
||||
Executors are auto-registered via the decorator. To manually register:
|
||||
|
||||
136
docs/src/content/docs/en/modules/database-drivers/index.md
Normal file
136
docs/src/content/docs/en/modules/database-drivers/index.md
Normal file
@@ -0,0 +1,136 @@
|
||||
---
|
||||
title: "Database Drivers"
|
||||
description: "MongoDB, Redis connection management and driver abstraction"
|
||||
---
|
||||
|
||||
`@esengine/database-drivers` is ESEngine's database connection management layer, providing unified connection management for MongoDB, Redis, and more.
|
||||
|
||||
## Features
|
||||
|
||||
- **Connection Pool** - Automatic connection pool management
|
||||
- **Auto Reconnect** - Automatic reconnection on disconnect
|
||||
- **Event Notification** - Connection state change events
|
||||
- **Type Decoupling** - Simplified interfaces, no dependency on native driver types
|
||||
- **Shared Connections** - Single connection shared across modules
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install @esengine/database-drivers
|
||||
```
|
||||
|
||||
**Peer Dependencies:**
|
||||
```bash
|
||||
npm install mongodb # For MongoDB support
|
||||
npm install ioredis # For Redis support
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ @esengine/database-drivers (Layer 1) │
|
||||
│ ┌─────────────────────┐ ┌─────────────────────┐ │
|
||||
│ │ MongoConnection │ │ RedisConnection │ │
|
||||
│ │ - Pool management │ │ - Auto-reconnect │ │
|
||||
│ │ - Auto-reconnect │ │ - Key prefix │ │
|
||||
│ │ - Event emitter │ │ - Event emitter │ │
|
||||
│ └──────────┬──────────┘ └─────────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌──────────▼──────────┐ │
|
||||
│ │ IMongoCollection<T> │ ← Type-safe interface │
|
||||
│ │ (Adapter pattern) │ decoupled from mongodb types │
|
||||
│ └─────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌───────────────────────┐ ┌───────────────────────┐
|
||||
│ @esengine/database │ │ @esengine/transaction │
|
||||
│ (Repository pattern) │ │ (Distributed tx) │
|
||||
└───────────────────────┘ └───────────────────────┘
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### MongoDB Connection
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
|
||||
// Create connection
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game',
|
||||
pool: {
|
||||
minSize: 5,
|
||||
maxSize: 20
|
||||
},
|
||||
autoReconnect: true
|
||||
})
|
||||
|
||||
// Listen to events
|
||||
mongo.on('connected', () => console.log('MongoDB connected'))
|
||||
mongo.on('disconnected', () => console.log('MongoDB disconnected'))
|
||||
mongo.on('error', (e) => console.error('Error:', e.error))
|
||||
|
||||
// Connect
|
||||
await mongo.connect()
|
||||
|
||||
// Use collections
|
||||
const users = mongo.collection<User>('users')
|
||||
await users.insertOne({ name: 'John', score: 100 })
|
||||
|
||||
const user = await users.findOne({ name: 'John' })
|
||||
|
||||
// Disconnect when done
|
||||
await mongo.disconnect()
|
||||
```
|
||||
|
||||
### Redis Connection
|
||||
|
||||
```typescript
|
||||
import { createRedisConnection } from '@esengine/database-drivers'
|
||||
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
keyPrefix: 'game:',
|
||||
autoReconnect: true
|
||||
})
|
||||
|
||||
await redis.connect()
|
||||
|
||||
// Basic operations
|
||||
await redis.set('session:123', 'data', 3600) // With TTL
|
||||
const value = await redis.get('session:123')
|
||||
|
||||
await redis.disconnect()
|
||||
```
|
||||
|
||||
## Service Container Integration
|
||||
|
||||
```typescript
|
||||
import { ServiceContainer } from '@esengine/ecs-framework'
|
||||
import {
|
||||
createMongoConnection,
|
||||
MongoConnectionToken,
|
||||
RedisConnectionToken
|
||||
} from '@esengine/database-drivers'
|
||||
|
||||
const services = new ServiceContainer()
|
||||
|
||||
// Register connections
|
||||
const mongo = createMongoConnection({ uri: '...', database: 'game' })
|
||||
await mongo.connect()
|
||||
services.register(MongoConnectionToken, mongo)
|
||||
|
||||
// Retrieve in other modules
|
||||
const connection = services.get(MongoConnectionToken)
|
||||
const users = connection.collection('users')
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
- [MongoDB Connection](/en/modules/database-drivers/mongo/) - MongoDB configuration details
|
||||
- [Redis Connection](/en/modules/database-drivers/redis/) - Redis configuration details
|
||||
- [Service Tokens](/en/modules/database-drivers/tokens/) - Dependency injection integration
|
||||
217
docs/src/content/docs/en/modules/database/index.md
Normal file
217
docs/src/content/docs/en/modules/database/index.md
Normal file
@@ -0,0 +1,217 @@
|
||||
---
|
||||
title: "Database Repository"
|
||||
description: "Repository pattern database layer with CRUD, pagination, and soft delete"
|
||||
---
|
||||
|
||||
`@esengine/database` is ESEngine's database operation layer, providing type-safe CRUD operations based on the Repository pattern.
|
||||
|
||||
## Features
|
||||
|
||||
- **Repository Pattern** - Generic CRUD operations with type safety
|
||||
- **Pagination** - Built-in pagination support
|
||||
- **Soft Delete** - Optional soft delete with restore
|
||||
- **User Management** - Ready-to-use UserRepository
|
||||
- **Password Security** - Secure password hashing with scrypt
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install @esengine/database @esengine/database-drivers
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Basic Repository
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { Repository, createRepository } from '@esengine/database'
|
||||
|
||||
// Define entity
|
||||
interface Player {
|
||||
id: string
|
||||
name: string
|
||||
score: number
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
}
|
||||
|
||||
// Create connection
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
// Create repository
|
||||
const playerRepo = createRepository<Player>(mongo, 'players')
|
||||
|
||||
// CRUD operations
|
||||
const player = await playerRepo.create({
|
||||
name: 'John',
|
||||
score: 0
|
||||
})
|
||||
|
||||
const found = await playerRepo.findById(player.id)
|
||||
|
||||
await playerRepo.update(player.id, { score: 100 })
|
||||
|
||||
await playerRepo.delete(player.id)
|
||||
```
|
||||
|
||||
### Custom Repository
|
||||
|
||||
```typescript
|
||||
import { Repository, BaseEntity } from '@esengine/database'
|
||||
import type { IMongoConnection } from '@esengine/database-drivers'
|
||||
|
||||
interface Player extends BaseEntity {
|
||||
name: string
|
||||
score: number
|
||||
rank?: string
|
||||
}
|
||||
|
||||
class PlayerRepository extends Repository<Player> {
|
||||
constructor(connection: IMongoConnection) {
|
||||
super(connection, 'players')
|
||||
}
|
||||
|
||||
async findTopPlayers(limit: number = 10): Promise<Player[]> {
|
||||
return this.findMany({
|
||||
sort: { score: 'desc' },
|
||||
limit
|
||||
})
|
||||
}
|
||||
|
||||
async findByRank(rank: string): Promise<Player[]> {
|
||||
return this.findMany({
|
||||
where: { rank }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Usage
|
||||
const playerRepo = new PlayerRepository(mongo)
|
||||
const topPlayers = await playerRepo.findTopPlayers(5)
|
||||
```
|
||||
|
||||
### User Repository
|
||||
|
||||
```typescript
|
||||
import { UserRepository } from '@esengine/database'
|
||||
|
||||
const userRepo = new UserRepository(mongo)
|
||||
|
||||
// Register new user
|
||||
const user = await userRepo.register({
|
||||
username: 'john',
|
||||
password: 'securePassword123',
|
||||
email: 'john@example.com'
|
||||
})
|
||||
|
||||
// Authenticate
|
||||
const authenticated = await userRepo.authenticate('john', 'securePassword123')
|
||||
if (authenticated) {
|
||||
console.log('Login successful:', authenticated.username)
|
||||
}
|
||||
|
||||
// Change password
|
||||
await userRepo.changePassword(user.id, 'securePassword123', 'newPassword456')
|
||||
|
||||
// Role management
|
||||
await userRepo.addRole(user.id, 'admin')
|
||||
await userRepo.removeRole(user.id, 'admin')
|
||||
|
||||
// Find users
|
||||
const admins = await userRepo.findByRole('admin')
|
||||
const john = await userRepo.findByUsername('john')
|
||||
```
|
||||
|
||||
### Pagination
|
||||
|
||||
```typescript
|
||||
const result = await playerRepo.findPaginated(
|
||||
{ page: 1, pageSize: 20 },
|
||||
{
|
||||
where: { rank: 'gold' },
|
||||
sort: { score: 'desc' }
|
||||
}
|
||||
)
|
||||
|
||||
console.log(result.data) // Player[]
|
||||
console.log(result.total) // Total count
|
||||
console.log(result.totalPages) // Total pages
|
||||
console.log(result.hasNext) // Has next page
|
||||
console.log(result.hasPrev) // Has previous page
|
||||
```
|
||||
|
||||
### Soft Delete
|
||||
|
||||
```typescript
|
||||
// Enable soft delete
|
||||
const playerRepo = createRepository<Player>(mongo, 'players', true)
|
||||
|
||||
// Delete (marks as deleted)
|
||||
await playerRepo.delete(playerId)
|
||||
|
||||
// Find excludes soft-deleted by default
|
||||
const players = await playerRepo.findMany()
|
||||
|
||||
// Include soft-deleted records
|
||||
const allPlayers = await playerRepo.findMany({
|
||||
includeSoftDeleted: true
|
||||
})
|
||||
|
||||
// Restore soft-deleted record
|
||||
await playerRepo.restore(playerId)
|
||||
```
|
||||
|
||||
### Query Options
|
||||
|
||||
```typescript
|
||||
// Complex queries
|
||||
const players = await playerRepo.findMany({
|
||||
where: {
|
||||
score: { $gte: 100 },
|
||||
rank: { $in: ['gold', 'platinum'] },
|
||||
name: { $like: 'John%' }
|
||||
},
|
||||
sort: {
|
||||
score: 'desc',
|
||||
name: 'asc'
|
||||
},
|
||||
limit: 10,
|
||||
offset: 0
|
||||
})
|
||||
|
||||
// OR conditions
|
||||
const players = await playerRepo.findMany({
|
||||
where: {
|
||||
$or: [
|
||||
{ score: { $gte: 1000 } },
|
||||
{ rank: 'legendary' }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## Query Operators
|
||||
|
||||
| Operator | Description | Example |
|
||||
|----------|-------------|---------|
|
||||
| `$eq` | Equal | `{ score: { $eq: 100 } }` |
|
||||
| `$ne` | Not equal | `{ status: { $ne: 'banned' } }` |
|
||||
| `$gt` | Greater than | `{ score: { $gt: 50 } }` |
|
||||
| `$gte` | Greater or equal | `{ level: { $gte: 10 } }` |
|
||||
| `$lt` | Less than | `{ age: { $lt: 18 } }` |
|
||||
| `$lte` | Less or equal | `{ price: { $lte: 100 } }` |
|
||||
| `$in` | In array | `{ rank: { $in: ['gold', 'platinum'] } }` |
|
||||
| `$nin` | Not in array | `{ status: { $nin: ['banned'] } }` |
|
||||
| `$like` | Pattern match | `{ name: { $like: '%john%' } }` |
|
||||
| `$regex` | Regex match | `{ email: { $regex: '@gmail.com$' } }` |
|
||||
|
||||
## Documentation
|
||||
|
||||
- [Repository API](/en/modules/database/repository/) - Repository detailed API
|
||||
- [User Management](/en/modules/database/user/) - UserRepository usage
|
||||
- [Query Syntax](/en/modules/database/query/) - Query condition syntax
|
||||
@@ -34,6 +34,14 @@ ESEngine provides a rich set of modules that can be imported as needed.
|
||||
| Module | Package | Description |
|
||||
|--------|---------|-------------|
|
||||
| [Network](/en/modules/network/) | `@esengine/network` | Multiplayer game networking |
|
||||
| [Transaction](/en/modules/transaction/) | `@esengine/transaction` | Game transactions with distributed support |
|
||||
|
||||
### Database
|
||||
|
||||
| Module | Package | Description |
|
||||
|--------|---------|-------------|
|
||||
| [Database Drivers](/en/modules/database-drivers/) | `@esengine/database-drivers` | MongoDB, Redis connection management |
|
||||
| [Database Repository](/en/modules/database/) | `@esengine/database` | Repository pattern data operations |
|
||||
|
||||
## Installation
|
||||
|
||||
|
||||
855
docs/src/content/docs/en/modules/network/auth.md
Normal file
855
docs/src/content/docs/en/modules/network/auth.md
Normal file
@@ -0,0 +1,855 @@
|
||||
---
|
||||
title: "Authentication"
|
||||
description: "Add authentication to your game server with JWT and Session providers"
|
||||
---
|
||||
|
||||
The `@esengine/server` package includes a pluggable authentication system that supports JWT, session-based auth, and custom providers.
|
||||
|
||||
## Installation
|
||||
|
||||
Authentication is included in the server package:
|
||||
|
||||
```bash
|
||||
npm install @esengine/server jsonwebtoken
|
||||
```
|
||||
|
||||
> Note: `jsonwebtoken` is an optional peer dependency, required only for JWT authentication.
|
||||
|
||||
## Quick Start
|
||||
|
||||
### JWT Authentication
|
||||
|
||||
```typescript
|
||||
import { createServer } from '@esengine/server'
|
||||
import { withAuth, createJwtAuthProvider, withRoomAuth, requireAuth } from '@esengine/server/auth'
|
||||
|
||||
// Create JWT provider
|
||||
const jwtProvider = createJwtAuthProvider({
|
||||
secret: process.env.JWT_SECRET!,
|
||||
expiresIn: 3600, // 1 hour
|
||||
})
|
||||
|
||||
// Wrap server with authentication
|
||||
const server = withAuth(await createServer({ port: 3000 }), {
|
||||
provider: jwtProvider,
|
||||
extractCredentials: (req) => {
|
||||
const url = new URL(req.url ?? '', 'http://localhost')
|
||||
return url.searchParams.get('token')
|
||||
},
|
||||
})
|
||||
|
||||
// Define authenticated room
|
||||
class GameRoom extends withRoomAuth(Room, { requireAuth: true }) {
|
||||
onJoin(player) {
|
||||
console.log(`${player.user?.name} joined!`)
|
||||
}
|
||||
}
|
||||
|
||||
server.define('game', GameRoom)
|
||||
await server.start()
|
||||
```
|
||||
|
||||
## Auth Providers
|
||||
|
||||
### JWT Provider
|
||||
|
||||
Use JSON Web Tokens for stateless authentication:
|
||||
|
||||
```typescript
|
||||
import { createJwtAuthProvider } from '@esengine/server/auth'
|
||||
|
||||
const jwtProvider = createJwtAuthProvider({
|
||||
// Required: secret key
|
||||
secret: 'your-secret-key',
|
||||
|
||||
// Optional: algorithm (default: HS256)
|
||||
algorithm: 'HS256',
|
||||
|
||||
// Optional: expiration in seconds (default: 3600)
|
||||
expiresIn: 3600,
|
||||
|
||||
// Optional: issuer for validation
|
||||
issuer: 'my-game-server',
|
||||
|
||||
// Optional: audience for validation
|
||||
audience: 'my-game-client',
|
||||
|
||||
// Optional: custom user extraction
|
||||
getUser: async (payload) => {
|
||||
// Fetch user from database
|
||||
return await db.users.findById(payload.sub)
|
||||
},
|
||||
})
|
||||
|
||||
// Sign a token (for login endpoints)
|
||||
const token = jwtProvider.sign({
|
||||
sub: user.id,
|
||||
name: user.name,
|
||||
roles: ['player'],
|
||||
})
|
||||
|
||||
// Decode without verification (for debugging)
|
||||
const payload = jwtProvider.decode(token)
|
||||
```
|
||||
|
||||
### Custom Provider
|
||||
|
||||
You can create custom authentication providers by implementing the `IAuthProvider` interface to integrate with any authentication system (OAuth, LDAP, custom database auth, etc.).
|
||||
|
||||
#### IAuthProvider Interface
|
||||
|
||||
```typescript
|
||||
interface IAuthProvider<TUser = unknown, TCredentials = unknown> {
|
||||
/** Provider name */
|
||||
readonly name: string;
|
||||
|
||||
/** Verify credentials */
|
||||
verify(credentials: TCredentials): Promise<AuthResult<TUser>>;
|
||||
|
||||
/** Refresh token (optional) */
|
||||
refresh?(token: string): Promise<AuthResult<TUser>>;
|
||||
|
||||
/** Revoke token (optional) */
|
||||
revoke?(token: string): Promise<boolean>;
|
||||
}
|
||||
|
||||
interface AuthResult<TUser> {
|
||||
success: boolean;
|
||||
user?: TUser;
|
||||
error?: string;
|
||||
errorCode?: AuthErrorCode;
|
||||
token?: string;
|
||||
expiresAt?: number;
|
||||
}
|
||||
|
||||
type AuthErrorCode =
|
||||
| 'INVALID_CREDENTIALS'
|
||||
| 'EXPIRED_TOKEN'
|
||||
| 'INVALID_TOKEN'
|
||||
| 'USER_NOT_FOUND'
|
||||
| 'ACCOUNT_DISABLED'
|
||||
| 'RATE_LIMITED'
|
||||
| 'INSUFFICIENT_PERMISSIONS';
|
||||
```
|
||||
|
||||
#### Custom Provider Examples
|
||||
|
||||
**Example 1: Database Password Authentication**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
username: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
interface PasswordCredentials {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
class DatabaseAuthProvider implements IAuthProvider<User, PasswordCredentials> {
|
||||
readonly name = 'database'
|
||||
|
||||
async verify(credentials: PasswordCredentials): Promise<AuthResult<User>> {
|
||||
const { username, password } = credentials
|
||||
|
||||
// Query user from database
|
||||
const user = await db.users.findByUsername(username)
|
||||
if (!user) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'User not found',
|
||||
errorCode: 'USER_NOT_FOUND'
|
||||
}
|
||||
}
|
||||
|
||||
// Verify password (using bcrypt or similar)
|
||||
const isValid = await bcrypt.compare(password, user.passwordHash)
|
||||
if (!isValid) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Invalid password',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
|
||||
// Check account status
|
||||
if (user.disabled) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Account is disabled',
|
||||
errorCode: 'ACCOUNT_DISABLED'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
roles: user.roles
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Example 2: OAuth/Third-party Authentication**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface OAuthUser {
|
||||
id: string
|
||||
email: string
|
||||
name: string
|
||||
provider: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
interface OAuthCredentials {
|
||||
provider: 'google' | 'github' | 'discord'
|
||||
accessToken: string
|
||||
}
|
||||
|
||||
class OAuthProvider implements IAuthProvider<OAuthUser, OAuthCredentials> {
|
||||
readonly name = 'oauth'
|
||||
|
||||
async verify(credentials: OAuthCredentials): Promise<AuthResult<OAuthUser>> {
|
||||
const { provider, accessToken } = credentials
|
||||
|
||||
try {
|
||||
// Verify token with provider
|
||||
const profile = await this.fetchUserProfile(provider, accessToken)
|
||||
|
||||
// Find or create local user
|
||||
let user = await db.users.findByOAuth(provider, profile.id)
|
||||
if (!user) {
|
||||
user = await db.users.create({
|
||||
oauthProvider: provider,
|
||||
oauthId: profile.id,
|
||||
email: profile.email,
|
||||
name: profile.name,
|
||||
roles: ['player']
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
provider,
|
||||
roles: user.roles
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'OAuth verification failed',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchUserProfile(provider: string, token: string) {
|
||||
switch (provider) {
|
||||
case 'google':
|
||||
return fetch('https://www.googleapis.com/oauth2/v2/userinfo', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
}).then(r => r.json())
|
||||
case 'github':
|
||||
return fetch('https://api.github.com/user', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
}).then(r => r.json())
|
||||
// Other providers...
|
||||
default:
|
||||
throw new Error(`Unsupported provider: ${provider}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Example 3: API Key Authentication**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface ApiUser {
|
||||
id: string
|
||||
name: string
|
||||
roles: string[]
|
||||
rateLimit: number
|
||||
}
|
||||
|
||||
class ApiKeyAuthProvider implements IAuthProvider<ApiUser, string> {
|
||||
readonly name = 'api-key'
|
||||
|
||||
private revokedKeys = new Set<string>()
|
||||
|
||||
async verify(apiKey: string): Promise<AuthResult<ApiUser>> {
|
||||
if (!apiKey || !apiKey.startsWith('sk_')) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'Invalid API Key format',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
if (this.revokedKeys.has(apiKey)) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key has been revoked',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
// Query API Key from database
|
||||
const keyData = await db.apiKeys.findByKey(apiKey)
|
||||
if (!keyData) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key not found',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
|
||||
// Check expiration
|
||||
if (keyData.expiresAt && keyData.expiresAt < Date.now()) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key has expired',
|
||||
errorCode: 'EXPIRED_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: keyData.userId,
|
||||
name: keyData.name,
|
||||
roles: keyData.roles,
|
||||
rateLimit: keyData.rateLimit
|
||||
},
|
||||
expiresAt: keyData.expiresAt
|
||||
}
|
||||
}
|
||||
|
||||
async revoke(apiKey: string): Promise<boolean> {
|
||||
this.revokedKeys.add(apiKey)
|
||||
await db.apiKeys.revoke(apiKey)
|
||||
return true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Using Custom Providers
|
||||
|
||||
```typescript
|
||||
import { createServer } from '@esengine/server'
|
||||
import { withAuth } from '@esengine/server/auth'
|
||||
|
||||
// Create custom provider
|
||||
const dbAuthProvider = new DatabaseAuthProvider()
|
||||
|
||||
// Or use OAuth provider
|
||||
const oauthProvider = new OAuthProvider()
|
||||
|
||||
// Use custom provider
|
||||
const server = withAuth(await createServer({ port: 3000 }), {
|
||||
provider: dbAuthProvider, // or oauthProvider
|
||||
|
||||
// Extract credentials from WebSocket connection request
|
||||
extractCredentials: (req) => {
|
||||
const url = new URL(req.url, 'http://localhost')
|
||||
|
||||
// For database auth: get from query params
|
||||
const username = url.searchParams.get('username')
|
||||
const password = url.searchParams.get('password')
|
||||
if (username && password) {
|
||||
return { username, password }
|
||||
}
|
||||
|
||||
// For OAuth: get from token param
|
||||
const provider = url.searchParams.get('provider')
|
||||
const accessToken = url.searchParams.get('access_token')
|
||||
if (provider && accessToken) {
|
||||
return { provider, accessToken }
|
||||
}
|
||||
|
||||
// For API Key: get from header
|
||||
const apiKey = req.headers['x-api-key']
|
||||
if (apiKey) {
|
||||
return apiKey as string
|
||||
}
|
||||
|
||||
return null
|
||||
},
|
||||
|
||||
onAuthFailure: (conn, error) => {
|
||||
console.log(`Auth failed: ${error.errorCode} - ${error.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
await server.start()
|
||||
```
|
||||
|
||||
#### Combining Multiple Providers
|
||||
|
||||
You can create a composite provider to support multiple authentication methods:
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface MultiAuthCredentials {
|
||||
type: 'jwt' | 'oauth' | 'apikey' | 'password'
|
||||
data: unknown
|
||||
}
|
||||
|
||||
class MultiAuthProvider implements IAuthProvider<User, MultiAuthCredentials> {
|
||||
readonly name = 'multi'
|
||||
|
||||
constructor(
|
||||
private jwtProvider: JwtAuthProvider<User>,
|
||||
private oauthProvider: OAuthProvider,
|
||||
private apiKeyProvider: ApiKeyAuthProvider,
|
||||
private dbProvider: DatabaseAuthProvider
|
||||
) {}
|
||||
|
||||
async verify(credentials: MultiAuthCredentials): Promise<AuthResult<User>> {
|
||||
switch (credentials.type) {
|
||||
case 'jwt':
|
||||
return this.jwtProvider.verify(credentials.data as string)
|
||||
case 'oauth':
|
||||
return this.oauthProvider.verify(credentials.data as OAuthCredentials)
|
||||
case 'apikey':
|
||||
return this.apiKeyProvider.verify(credentials.data as string)
|
||||
case 'password':
|
||||
return this.dbProvider.verify(credentials.data as PasswordCredentials)
|
||||
default:
|
||||
return {
|
||||
success: false,
|
||||
error: 'Unsupported authentication type',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Session Provider
|
||||
|
||||
Use server-side sessions for stateful authentication:
|
||||
|
||||
```typescript
|
||||
import { createSessionAuthProvider, type ISessionStorage } from '@esengine/server/auth'
|
||||
|
||||
// Custom storage implementation
|
||||
const storage: ISessionStorage = {
|
||||
async get<T>(key: string): Promise<T | null> {
|
||||
return await redis.get(key)
|
||||
},
|
||||
async set<T>(key: string, value: T): Promise<void> {
|
||||
await redis.set(key, value)
|
||||
},
|
||||
async delete(key: string): Promise<boolean> {
|
||||
return await redis.del(key) > 0
|
||||
},
|
||||
}
|
||||
|
||||
const sessionProvider = createSessionAuthProvider({
|
||||
storage,
|
||||
sessionTTL: 86400000, // 24 hours in ms
|
||||
|
||||
// Optional: validate user on each request
|
||||
validateUser: (user) => !user.banned,
|
||||
})
|
||||
|
||||
// Create session (for login endpoints)
|
||||
const sessionId = await sessionProvider.createSession(user, {
|
||||
ipAddress: req.ip,
|
||||
userAgent: req.headers['user-agent'],
|
||||
})
|
||||
|
||||
// Revoke session (for logout)
|
||||
await sessionProvider.revoke(sessionId)
|
||||
```
|
||||
|
||||
## Server Auth Mixin
|
||||
|
||||
The `withAuth` function wraps your server to add authentication:
|
||||
|
||||
```typescript
|
||||
import { withAuth } from '@esengine/server/auth'
|
||||
|
||||
const server = withAuth(baseServer, {
|
||||
// Required: auth provider
|
||||
provider: jwtProvider,
|
||||
|
||||
// Required: extract credentials from request
|
||||
extractCredentials: (req) => {
|
||||
// From query string
|
||||
return new URL(req.url, 'http://localhost').searchParams.get('token')
|
||||
|
||||
// Or from headers
|
||||
// return req.headers['authorization']?.replace('Bearer ', '')
|
||||
},
|
||||
|
||||
// Optional: handle auth failure
|
||||
onAuthFailed: (conn, error) => {
|
||||
console.log(`Auth failed: ${error}`)
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
### Accessing Auth Context
|
||||
|
||||
After authentication, the auth context is available on connections:
|
||||
|
||||
```typescript
|
||||
import { getAuthContext } from '@esengine/server/auth'
|
||||
|
||||
server.onConnect = (conn) => {
|
||||
const auth = getAuthContext(conn)
|
||||
|
||||
if (auth.isAuthenticated) {
|
||||
console.log(`User ${auth.userId} connected`)
|
||||
console.log(`Roles: ${auth.roles}`)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Room Auth Mixin
|
||||
|
||||
The `withRoomAuth` function adds authentication checks to rooms:
|
||||
|
||||
```typescript
|
||||
import { withRoomAuth, type AuthPlayer } from '@esengine/server/auth'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
name: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
class GameRoom extends withRoomAuth<User>(Room, {
|
||||
// Require authentication to join
|
||||
requireAuth: true,
|
||||
|
||||
// Optional: require specific roles
|
||||
allowedRoles: ['player', 'premium'],
|
||||
|
||||
// Optional: role check mode ('any' or 'all')
|
||||
roleCheckMode: 'any',
|
||||
}) {
|
||||
// player has .auth and .user properties
|
||||
onJoin(player: AuthPlayer<User>) {
|
||||
console.log(`${player.user?.name} joined`)
|
||||
console.log(`Is premium: ${player.auth.hasRole('premium')}`)
|
||||
}
|
||||
|
||||
// Optional: custom auth validation
|
||||
async onAuth(player: AuthPlayer<User>): Promise<boolean> {
|
||||
// Additional validation logic
|
||||
if (player.auth.hasRole('banned')) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@onMessage('Chat')
|
||||
handleChat(data: { text: string }, player: AuthPlayer<User>) {
|
||||
this.broadcast('Chat', {
|
||||
from: player.user?.name ?? 'Guest',
|
||||
text: data.text,
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### AuthPlayer Interface
|
||||
|
||||
Players in auth rooms have additional properties:
|
||||
|
||||
```typescript
|
||||
interface AuthPlayer<TUser> extends Player {
|
||||
// Full auth context
|
||||
readonly auth: IAuthContext<TUser>
|
||||
|
||||
// User info (shortcut for auth.user)
|
||||
readonly user: TUser | null
|
||||
}
|
||||
```
|
||||
|
||||
### Room Auth Helpers
|
||||
|
||||
```typescript
|
||||
class GameRoom extends withRoomAuth<User>(Room) {
|
||||
someMethod() {
|
||||
// Get player by user ID
|
||||
const player = this.getPlayerByUserId('user-123')
|
||||
|
||||
// Get all players with a role
|
||||
const admins = this.getPlayersByRole('admin')
|
||||
|
||||
// Get player with auth info
|
||||
const authPlayer = this.getAuthPlayer(playerId)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Auth Decorators
|
||||
|
||||
### @requireAuth
|
||||
|
||||
Mark message handlers as requiring authentication:
|
||||
|
||||
```typescript
|
||||
import { requireAuth, requireRole, onMessage } from '@esengine/server/auth'
|
||||
|
||||
class GameRoom extends withRoomAuth(Room) {
|
||||
@requireAuth()
|
||||
@onMessage('Trade')
|
||||
handleTrade(data: TradeData, player: AuthPlayer) {
|
||||
// Only authenticated players can trade
|
||||
}
|
||||
|
||||
@requireAuth({ allowGuest: true })
|
||||
@onMessage('Chat')
|
||||
handleChat(data: ChatData, player: AuthPlayer) {
|
||||
// Guests can also chat
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### @requireRole
|
||||
|
||||
Require specific roles for message handlers:
|
||||
|
||||
```typescript
|
||||
class AdminRoom extends withRoomAuth(Room) {
|
||||
@requireRole('admin')
|
||||
@onMessage('Ban')
|
||||
handleBan(data: BanData, player: AuthPlayer) {
|
||||
// Only admins can ban
|
||||
}
|
||||
|
||||
@requireRole(['moderator', 'admin'])
|
||||
@onMessage('Mute')
|
||||
handleMute(data: MuteData, player: AuthPlayer) {
|
||||
// Moderators OR admins can mute
|
||||
}
|
||||
|
||||
@requireRole(['verified', 'premium'], { mode: 'all' })
|
||||
@onMessage('SpecialFeature')
|
||||
handleSpecial(data: any, player: AuthPlayer) {
|
||||
// Requires BOTH verified AND premium roles
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Auth Context API
|
||||
|
||||
The auth context provides various methods for checking authentication state:
|
||||
|
||||
```typescript
|
||||
interface IAuthContext<TUser> {
|
||||
// Authentication state
|
||||
readonly isAuthenticated: boolean
|
||||
readonly user: TUser | null
|
||||
readonly userId: string | null
|
||||
readonly roles: ReadonlyArray<string>
|
||||
readonly authenticatedAt: number | null
|
||||
readonly expiresAt: number | null
|
||||
|
||||
// Role checking
|
||||
hasRole(role: string): boolean
|
||||
hasAnyRole(roles: string[]): boolean
|
||||
hasAllRoles(roles: string[]): boolean
|
||||
}
|
||||
```
|
||||
|
||||
The `AuthContext` class (implementation) also provides:
|
||||
|
||||
```typescript
|
||||
class AuthContext<TUser> implements IAuthContext<TUser> {
|
||||
// Set authentication from result
|
||||
setAuthenticated(result: AuthResult<TUser>): void
|
||||
|
||||
// Clear authentication state
|
||||
clear(): void
|
||||
}
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
Use the mock auth provider for unit tests:
|
||||
|
||||
```typescript
|
||||
import { createMockAuthProvider } from '@esengine/server/auth/testing'
|
||||
|
||||
// Create mock provider with preset users
|
||||
const mockProvider = createMockAuthProvider({
|
||||
users: [
|
||||
{ id: '1', name: 'Alice', roles: ['player'] },
|
||||
{ id: '2', name: 'Bob', roles: ['admin', 'player'] },
|
||||
],
|
||||
autoCreate: true, // Create users for unknown tokens
|
||||
})
|
||||
|
||||
// Use in tests
|
||||
const server = withAuth(testServer, {
|
||||
provider: mockProvider,
|
||||
extractCredentials: (req) => req.headers['x-token'],
|
||||
})
|
||||
|
||||
// Verify with user ID as token
|
||||
const result = await mockProvider.verify('1')
|
||||
// result.user = { id: '1', name: 'Alice', roles: ['player'] }
|
||||
|
||||
// Add/remove users dynamically
|
||||
mockProvider.addUser({ id: '3', name: 'Charlie', roles: ['guest'] })
|
||||
mockProvider.removeUser('3')
|
||||
|
||||
// Revoke tokens
|
||||
await mockProvider.revoke('1')
|
||||
|
||||
// Reset to initial state
|
||||
mockProvider.clear()
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
Auth errors include error codes for programmatic handling:
|
||||
|
||||
```typescript
|
||||
type AuthErrorCode =
|
||||
| 'INVALID_CREDENTIALS' // Invalid username/password
|
||||
| 'INVALID_TOKEN' // Token is malformed or invalid
|
||||
| 'EXPIRED_TOKEN' // Token has expired
|
||||
| 'USER_NOT_FOUND' // User lookup failed
|
||||
| 'ACCOUNT_DISABLED' // User account is disabled
|
||||
| 'RATE_LIMITED' // Too many requests
|
||||
| 'INSUFFICIENT_PERMISSIONS' // Insufficient permissions
|
||||
|
||||
// In your auth failure handler
|
||||
const server = withAuth(baseServer, {
|
||||
provider: jwtProvider,
|
||||
extractCredentials,
|
||||
onAuthFailed: (conn, error) => {
|
||||
switch (error.errorCode) {
|
||||
case 'EXPIRED_TOKEN':
|
||||
conn.send('AuthError', { code: 'TOKEN_EXPIRED' })
|
||||
break
|
||||
case 'INVALID_TOKEN':
|
||||
conn.send('AuthError', { code: 'INVALID_TOKEN' })
|
||||
break
|
||||
default:
|
||||
conn.close()
|
||||
}
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## Complete Example
|
||||
|
||||
Here's a complete example with JWT authentication:
|
||||
|
||||
```typescript
|
||||
// server.ts
|
||||
import { createServer } from '@esengine/server'
|
||||
import {
|
||||
withAuth,
|
||||
withRoomAuth,
|
||||
createJwtAuthProvider,
|
||||
requireAuth,
|
||||
requireRole,
|
||||
type AuthPlayer,
|
||||
} from '@esengine/server/auth'
|
||||
|
||||
// Types
|
||||
interface User {
|
||||
id: string
|
||||
name: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
// JWT Provider
|
||||
const jwtProvider = createJwtAuthProvider<User>({
|
||||
secret: process.env.JWT_SECRET!,
|
||||
expiresIn: 3600,
|
||||
getUser: async (payload) => ({
|
||||
id: payload.sub as string,
|
||||
name: payload.name as string,
|
||||
roles: (payload.roles as string[]) ?? [],
|
||||
}),
|
||||
})
|
||||
|
||||
// Create authenticated server
|
||||
const server = withAuth(
|
||||
await createServer({ port: 3000 }),
|
||||
{
|
||||
provider: jwtProvider,
|
||||
extractCredentials: (req) => {
|
||||
return new URL(req.url ?? '', 'http://localhost')
|
||||
.searchParams.get('token')
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// Game Room with auth
|
||||
class GameRoom extends withRoomAuth<User>(Room, {
|
||||
requireAuth: true,
|
||||
allowedRoles: ['player'],
|
||||
}) {
|
||||
onCreate() {
|
||||
console.log('Game room created')
|
||||
}
|
||||
|
||||
onJoin(player: AuthPlayer<User>) {
|
||||
console.log(`${player.user?.name} joined!`)
|
||||
this.broadcast('PlayerJoined', {
|
||||
id: player.id,
|
||||
name: player.user?.name,
|
||||
})
|
||||
}
|
||||
|
||||
@requireAuth()
|
||||
@onMessage('Move')
|
||||
handleMove(data: { x: number; y: number }, player: AuthPlayer<User>) {
|
||||
// Handle movement
|
||||
}
|
||||
|
||||
@requireRole('admin')
|
||||
@onMessage('Kick')
|
||||
handleKick(data: { playerId: string }, player: AuthPlayer<User>) {
|
||||
const target = this.getPlayer(data.playerId)
|
||||
if (target) {
|
||||
this.kick(target, 'Kicked by admin')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
server.define('game', GameRoom)
|
||||
await server.start()
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Secure your secrets**: Never hardcode JWT secrets. Use environment variables.
|
||||
|
||||
2. **Set reasonable expiration**: Balance security and user experience when setting token TTL.
|
||||
|
||||
3. **Validate on critical actions**: Use `@requireAuth` on sensitive message handlers.
|
||||
|
||||
4. **Use role-based access**: Implement proper role hierarchy for admin functions.
|
||||
|
||||
5. **Handle token refresh**: Implement token refresh logic for long sessions.
|
||||
|
||||
6. **Log auth events**: Track login attempts and failures for security monitoring.
|
||||
|
||||
7. **Test auth flows**: Use `MockAuthProvider` to test authentication scenarios.
|
||||
458
docs/src/content/docs/en/modules/network/rate-limit.md
Normal file
458
docs/src/content/docs/en/modules/network/rate-limit.md
Normal file
@@ -0,0 +1,458 @@
|
||||
---
|
||||
title: "Rate Limiting"
|
||||
description: "Protect your game server from abuse with configurable rate limiting"
|
||||
---
|
||||
|
||||
The `@esengine/server` package includes a pluggable rate limiting system to protect against DDoS attacks, message flooding, and other abuse.
|
||||
|
||||
## Installation
|
||||
|
||||
Rate limiting is included in the server package:
|
||||
|
||||
```bash
|
||||
npm install @esengine/server
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
```typescript
|
||||
import { Room, onMessage } from '@esengine/server'
|
||||
import { withRateLimit, rateLimit, noRateLimit } from '@esengine/server/ratelimit'
|
||||
|
||||
class GameRoom extends withRateLimit(Room, {
|
||||
messagesPerSecond: 10,
|
||||
burstSize: 20,
|
||||
onLimited: (player, type, result) => {
|
||||
player.send('Error', {
|
||||
code: 'RATE_LIMITED',
|
||||
retryAfter: result.retryAfter,
|
||||
})
|
||||
},
|
||||
}) {
|
||||
@onMessage('Move')
|
||||
handleMove(data: { x: number; y: number }, player: Player) {
|
||||
// Protected by rate limit (10 msg/s default)
|
||||
}
|
||||
|
||||
@rateLimit({ messagesPerSecond: 1 })
|
||||
@onMessage('Trade')
|
||||
handleTrade(data: TradeData, player: Player) {
|
||||
// Stricter limit for trading
|
||||
}
|
||||
|
||||
@noRateLimit()
|
||||
@onMessage('Heartbeat')
|
||||
handleHeartbeat(data: any, player: Player) {
|
||||
// No rate limit for heartbeat
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Rate Limit Strategies
|
||||
|
||||
### Token Bucket (Default)
|
||||
|
||||
The token bucket algorithm allows burst traffic while maintaining long-term rate limits. Tokens are added at a fixed rate, and each request consumes tokens.
|
||||
|
||||
```typescript
|
||||
import { withRateLimit } from '@esengine/server/ratelimit'
|
||||
|
||||
class GameRoom extends withRateLimit(Room, {
|
||||
strategy: 'token-bucket',
|
||||
messagesPerSecond: 10, // Refill rate
|
||||
burstSize: 20, // Bucket capacity
|
||||
}) { }
|
||||
```
|
||||
|
||||
**How it works:**
|
||||
```
|
||||
Config: rate=10/s, burstSize=20
|
||||
|
||||
[0s] Bucket full: 20 tokens
|
||||
[0s] 15 messages → allowed, 5 remaining
|
||||
[0.5s] Refill 5 tokens → 10 tokens
|
||||
[0.5s] 8 messages → allowed, 2 remaining
|
||||
[0.6s] Refill 1 token → 3 tokens
|
||||
[0.6s] 5 messages → 3 allowed, 2 rejected
|
||||
```
|
||||
|
||||
**Best for:** Most general use cases, balances burst tolerance with protection.
|
||||
|
||||
### Sliding Window
|
||||
|
||||
The sliding window algorithm precisely tracks requests within a time window. More accurate than fixed window but uses slightly more memory.
|
||||
|
||||
```typescript
|
||||
class GameRoom extends withRateLimit(Room, {
|
||||
strategy: 'sliding-window',
|
||||
messagesPerSecond: 10,
|
||||
burstSize: 10,
|
||||
}) { }
|
||||
```
|
||||
|
||||
**Best for:** When you need precise rate limiting without burst tolerance.
|
||||
|
||||
### Fixed Window
|
||||
|
||||
The fixed window algorithm divides time into fixed intervals and counts requests per interval. Simple and memory-efficient but allows 2x burst at window boundaries.
|
||||
|
||||
```typescript
|
||||
class GameRoom extends withRateLimit(Room, {
|
||||
strategy: 'fixed-window',
|
||||
messagesPerSecond: 10,
|
||||
burstSize: 10,
|
||||
}) { }
|
||||
```
|
||||
|
||||
**Best for:** Simple scenarios where boundary burst is acceptable.
|
||||
|
||||
## Configuration
|
||||
|
||||
### Room Configuration
|
||||
|
||||
```typescript
|
||||
import { withRateLimit } from '@esengine/server/ratelimit'
|
||||
|
||||
class GameRoom extends withRateLimit(Room, {
|
||||
// Messages allowed per second (default: 10)
|
||||
messagesPerSecond: 10,
|
||||
|
||||
// Burst capacity / bucket size (default: 20)
|
||||
burstSize: 20,
|
||||
|
||||
// Strategy: 'token-bucket' | 'sliding-window' | 'fixed-window'
|
||||
strategy: 'token-bucket',
|
||||
|
||||
// Callback when rate limited
|
||||
onLimited: (player, messageType, result) => {
|
||||
player.send('RateLimited', {
|
||||
type: messageType,
|
||||
retryAfter: result.retryAfter,
|
||||
})
|
||||
},
|
||||
|
||||
// Disconnect on rate limit (default: false)
|
||||
disconnectOnLimit: false,
|
||||
|
||||
// Disconnect after N consecutive limits (0 = never)
|
||||
maxConsecutiveLimits: 10,
|
||||
|
||||
// Custom key function (default: player.id)
|
||||
getKey: (player) => player.id,
|
||||
|
||||
// Cleanup interval in ms (default: 60000)
|
||||
cleanupInterval: 60000,
|
||||
}) { }
|
||||
```
|
||||
|
||||
### Per-Message Configuration
|
||||
|
||||
Use decorators to configure rate limits for specific messages:
|
||||
|
||||
```typescript
|
||||
import { rateLimit, noRateLimit, rateLimitMessage } from '@esengine/server/ratelimit'
|
||||
|
||||
class GameRoom extends withRateLimit(Room) {
|
||||
// Custom rate limit for this message
|
||||
@rateLimit({ messagesPerSecond: 1, burstSize: 2 })
|
||||
@onMessage('Trade')
|
||||
handleTrade(data: TradeData, player: Player) { }
|
||||
|
||||
// This message costs 5 tokens
|
||||
@rateLimit({ cost: 5 })
|
||||
@onMessage('ExpensiveAction')
|
||||
handleExpensive(data: any, player: Player) { }
|
||||
|
||||
// Exempt from rate limiting
|
||||
@noRateLimit()
|
||||
@onMessage('Heartbeat')
|
||||
handleHeartbeat(data: any, player: Player) { }
|
||||
|
||||
// Alternative: specify message type explicitly
|
||||
@rateLimitMessage('SpecialAction', { messagesPerSecond: 2 })
|
||||
@onMessage('SpecialAction')
|
||||
handleSpecial(data: any, player: Player) { }
|
||||
}
|
||||
```
|
||||
|
||||
## Combining with Authentication
|
||||
|
||||
Rate limiting works seamlessly with the authentication system:
|
||||
|
||||
```typescript
|
||||
import { withRoomAuth } from '@esengine/server/auth'
|
||||
import { withRateLimit } from '@esengine/server/ratelimit'
|
||||
|
||||
// Apply both mixins
|
||||
class GameRoom extends withRateLimit(
|
||||
withRoomAuth(Room, { requireAuth: true }),
|
||||
{ messagesPerSecond: 10 }
|
||||
) {
|
||||
onJoin(player: AuthPlayer) {
|
||||
console.log(`${player.user?.name} joined with rate limit protection`)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Rate Limit Result
|
||||
|
||||
When a message is rate limited, the callback receives a result object:
|
||||
|
||||
```typescript
|
||||
interface RateLimitResult {
|
||||
// Whether the request was allowed
|
||||
allowed: boolean
|
||||
|
||||
// Remaining quota
|
||||
remaining: number
|
||||
|
||||
// When the quota resets (timestamp)
|
||||
resetAt: number
|
||||
|
||||
// How long to wait before retrying (ms)
|
||||
retryAfter?: number
|
||||
}
|
||||
```
|
||||
|
||||
## Accessing Rate Limit Context
|
||||
|
||||
You can access the rate limit context for any player:
|
||||
|
||||
```typescript
|
||||
import { getPlayerRateLimitContext } from '@esengine/server/ratelimit'
|
||||
|
||||
class GameRoom extends withRateLimit(Room) {
|
||||
someMethod(player: Player) {
|
||||
const context = this.getRateLimitContext(player)
|
||||
|
||||
// Check without consuming
|
||||
const status = context?.check()
|
||||
console.log(`Remaining: ${status?.remaining}`)
|
||||
|
||||
// Get consecutive limit count
|
||||
console.log(`Consecutive limits: ${context?.consecutiveLimitCount}`)
|
||||
}
|
||||
}
|
||||
|
||||
// Or use the standalone function
|
||||
const context = getPlayerRateLimitContext(player)
|
||||
```
|
||||
|
||||
## Custom Strategies
|
||||
|
||||
You can use the strategies directly for custom implementations:
|
||||
|
||||
```typescript
|
||||
import {
|
||||
TokenBucketStrategy,
|
||||
SlidingWindowStrategy,
|
||||
FixedWindowStrategy,
|
||||
createTokenBucketStrategy,
|
||||
} from '@esengine/server/ratelimit'
|
||||
|
||||
// Create strategy directly
|
||||
const strategy = createTokenBucketStrategy({
|
||||
rate: 10, // tokens per second
|
||||
capacity: 20, // max tokens
|
||||
})
|
||||
|
||||
// Check and consume
|
||||
const result = strategy.consume('player-123')
|
||||
if (result.allowed) {
|
||||
// Process message
|
||||
} else {
|
||||
// Rate limited, wait result.retryAfter ms
|
||||
}
|
||||
|
||||
// Check without consuming
|
||||
const status = strategy.getStatus('player-123')
|
||||
|
||||
// Reset a key
|
||||
strategy.reset('player-123')
|
||||
|
||||
// Cleanup expired records
|
||||
strategy.cleanup()
|
||||
```
|
||||
|
||||
## Rate Limit Context
|
||||
|
||||
The `RateLimitContext` class manages rate limiting for a single player:
|
||||
|
||||
```typescript
|
||||
import { RateLimitContext, TokenBucketStrategy } from '@esengine/server/ratelimit'
|
||||
|
||||
const strategy = new TokenBucketStrategy({ rate: 10, capacity: 20 })
|
||||
const context = new RateLimitContext('player-123', strategy)
|
||||
|
||||
// Check without consuming
|
||||
context.check()
|
||||
|
||||
// Consume quota
|
||||
context.consume()
|
||||
|
||||
// Consume with cost
|
||||
context.consume(undefined, 5)
|
||||
|
||||
// Consume for specific message type
|
||||
context.consume('Trade')
|
||||
|
||||
// Set per-message strategy
|
||||
context.setMessageStrategy('Trade', new TokenBucketStrategy({ rate: 1, capacity: 2 }))
|
||||
|
||||
// Reset
|
||||
context.reset()
|
||||
|
||||
// Get consecutive limit count
|
||||
console.log(context.consecutiveLimitCount)
|
||||
```
|
||||
|
||||
## Room Lifecycle Hook
|
||||
|
||||
You can override the `onRateLimited` hook for custom handling:
|
||||
|
||||
```typescript
|
||||
class GameRoom extends withRateLimit(Room) {
|
||||
onRateLimited(player: Player, messageType: string, result: RateLimitResult) {
|
||||
// Log the event
|
||||
console.log(`Player ${player.id} rate limited on ${messageType}`)
|
||||
|
||||
// Send custom error
|
||||
player.send('SystemMessage', {
|
||||
type: 'warning',
|
||||
message: `Slow down! Try again in ${result.retryAfter}ms`,
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Start with token bucket**: It's the most flexible algorithm for games.
|
||||
|
||||
2. **Set appropriate limits**: Consider your game's mechanics:
|
||||
- Movement messages: Higher limits (20-60/s)
|
||||
- Chat messages: Lower limits (1-5/s)
|
||||
- Trade/purchase: Very low limits (0.5-1/s)
|
||||
|
||||
3. **Use burst capacity**: Allow short bursts for responsive gameplay:
|
||||
```typescript
|
||||
messagesPerSecond: 10,
|
||||
burstSize: 30, // Allow 3s worth of burst
|
||||
```
|
||||
|
||||
4. **Exempt critical messages**: Don't rate limit heartbeats or system messages:
|
||||
```typescript
|
||||
@noRateLimit()
|
||||
@onMessage('Heartbeat')
|
||||
handleHeartbeat() { }
|
||||
```
|
||||
|
||||
5. **Combine with auth**: Rate limit by user ID for authenticated users:
|
||||
```typescript
|
||||
getKey: (player) => player.auth?.userId ?? player.id
|
||||
```
|
||||
|
||||
6. **Monitor and adjust**: Log rate limit events to tune your limits:
|
||||
```typescript
|
||||
onLimited: (player, type, result) => {
|
||||
metrics.increment('rate_limit', { messageType: type })
|
||||
}
|
||||
```
|
||||
|
||||
7. **Graceful degradation**: Send informative errors instead of just disconnecting:
|
||||
```typescript
|
||||
onLimited: (player, type, result) => {
|
||||
player.send('Error', {
|
||||
code: 'RATE_LIMITED',
|
||||
message: 'Too many requests',
|
||||
retryAfter: result.retryAfter,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
## Complete Example
|
||||
|
||||
```typescript
|
||||
import { Room, onMessage, type Player } from '@esengine/server'
|
||||
import { withRoomAuth, type AuthPlayer } from '@esengine/server/auth'
|
||||
import {
|
||||
withRateLimit,
|
||||
rateLimit,
|
||||
noRateLimit,
|
||||
type RateLimitResult,
|
||||
} from '@esengine/server/ratelimit'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
name: string
|
||||
premium: boolean
|
||||
}
|
||||
|
||||
// Combine auth and rate limit
|
||||
class GameRoom extends withRateLimit(
|
||||
withRoomAuth<User>(Room, { requireAuth: true }),
|
||||
{
|
||||
messagesPerSecond: 10,
|
||||
burstSize: 30,
|
||||
strategy: 'token-bucket',
|
||||
|
||||
// Use user ID for rate limiting
|
||||
getKey: (player) => (player as AuthPlayer<User>).user?.id ?? player.id,
|
||||
|
||||
// Handle rate limits
|
||||
onLimited: (player, type, result) => {
|
||||
player.send('Error', {
|
||||
code: 'RATE_LIMITED',
|
||||
messageType: type,
|
||||
retryAfter: result.retryAfter,
|
||||
})
|
||||
},
|
||||
|
||||
// Disconnect after 20 consecutive rate limits
|
||||
maxConsecutiveLimits: 20,
|
||||
}
|
||||
) {
|
||||
onCreate() {
|
||||
console.log('Room created with auth + rate limit protection')
|
||||
}
|
||||
|
||||
onJoin(player: AuthPlayer<User>) {
|
||||
this.broadcast('PlayerJoined', { name: player.user?.name })
|
||||
}
|
||||
|
||||
// High-frequency movement (default rate limit)
|
||||
@onMessage('Move')
|
||||
handleMove(data: { x: number; y: number }, player: AuthPlayer<User>) {
|
||||
this.broadcast('PlayerMoved', { id: player.id, ...data })
|
||||
}
|
||||
|
||||
// Low-frequency trading (strict limit)
|
||||
@rateLimit({ messagesPerSecond: 0.5, burstSize: 2 })
|
||||
@onMessage('Trade')
|
||||
handleTrade(data: TradeData, player: AuthPlayer<User>) {
|
||||
// Process trade...
|
||||
}
|
||||
|
||||
// Chat with moderate limit
|
||||
@rateLimit({ messagesPerSecond: 2, burstSize: 5 })
|
||||
@onMessage('Chat')
|
||||
handleChat(data: { text: string }, player: AuthPlayer<User>) {
|
||||
this.broadcast('Chat', {
|
||||
from: player.user?.name,
|
||||
text: data.text,
|
||||
})
|
||||
}
|
||||
|
||||
// System messages - no limit
|
||||
@noRateLimit()
|
||||
@onMessage('Heartbeat')
|
||||
handleHeartbeat(data: any, player: Player) {
|
||||
player.send('Pong', { time: Date.now() })
|
||||
}
|
||||
|
||||
// Custom rate limit handling
|
||||
onRateLimited(player: Player, messageType: string, result: RateLimitResult) {
|
||||
console.warn(`[RateLimit] Player ${player.id} limited on ${messageType}`)
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -79,10 +79,140 @@ await server.start()
|
||||
| `tickRate` | `number` | `20` | Global tick rate (Hz) |
|
||||
| `apiDir` | `string` | `'src/api'` | API handlers directory |
|
||||
| `msgDir` | `string` | `'src/msg'` | Message handlers directory |
|
||||
| `httpDir` | `string` | `'src/http'` | HTTP routes directory |
|
||||
| `httpPrefix` | `string` | `'/api'` | HTTP routes prefix |
|
||||
| `cors` | `boolean \| CorsOptions` | - | CORS configuration |
|
||||
| `onStart` | `(port) => void` | - | Start callback |
|
||||
| `onConnect` | `(conn) => void` | - | Connection callback |
|
||||
| `onDisconnect` | `(conn) => void` | - | Disconnect callback |
|
||||
|
||||
## HTTP Routing
|
||||
|
||||
Supports HTTP API sharing the same port with WebSocket, ideal for login, registration, and similar scenarios.
|
||||
|
||||
### File-based Routing
|
||||
|
||||
Create route files in the `httpDir` directory, automatically mapped to HTTP endpoints:
|
||||
|
||||
```
|
||||
src/http/
|
||||
├── login.ts → POST /api/login
|
||||
├── register.ts → POST /api/register
|
||||
├── health.ts → GET /api/health (set method: 'GET')
|
||||
└── users/
|
||||
└── [id].ts → POST /api/users/:id (dynamic route)
|
||||
```
|
||||
|
||||
### Define Routes
|
||||
|
||||
Use `defineHttp` to define type-safe route handlers:
|
||||
|
||||
```typescript
|
||||
// src/http/login.ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
interface LoginBody {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export default defineHttp<LoginBody>({
|
||||
method: 'POST', // Default POST, options: GET/PUT/DELETE/PATCH
|
||||
handler(req, res) {
|
||||
const { username, password } = req.body
|
||||
|
||||
// Validate credentials...
|
||||
if (!isValid(username, password)) {
|
||||
res.error(401, 'Invalid credentials')
|
||||
return
|
||||
}
|
||||
|
||||
// Generate token...
|
||||
res.json({ token: '...', userId: '...' })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### Request Object (HttpRequest)
|
||||
|
||||
```typescript
|
||||
interface HttpRequest {
|
||||
raw: IncomingMessage // Node.js raw request
|
||||
method: string // Request method
|
||||
path: string // Request path
|
||||
query: Record<string, string> // Query parameters
|
||||
headers: Record<string, string | string[] | undefined>
|
||||
body: unknown // Parsed JSON body
|
||||
ip: string // Client IP
|
||||
}
|
||||
```
|
||||
|
||||
### Response Object (HttpResponse)
|
||||
|
||||
```typescript
|
||||
interface HttpResponse {
|
||||
raw: ServerResponse // Node.js raw response
|
||||
status(code: number): HttpResponse // Set status code (chainable)
|
||||
header(name: string, value: string): HttpResponse // Set header (chainable)
|
||||
json(data: unknown): void // Send JSON
|
||||
text(data: string): void // Send text
|
||||
error(code: number, message: string): void // Send error
|
||||
}
|
||||
```
|
||||
|
||||
### Usage Example
|
||||
|
||||
```typescript
|
||||
// Complete login server example
|
||||
import { createServer, defineHttp } from '@esengine/server'
|
||||
import { createJwtAuthProvider, withAuth } from '@esengine/server/auth'
|
||||
|
||||
const jwtProvider = createJwtAuthProvider({
|
||||
secret: process.env.JWT_SECRET!,
|
||||
expiresIn: 3600 * 24,
|
||||
})
|
||||
|
||||
const server = await createServer({
|
||||
port: 8080,
|
||||
httpDir: 'src/http',
|
||||
httpPrefix: '/api',
|
||||
cors: true,
|
||||
})
|
||||
|
||||
// Wrap with auth (WebSocket connections validate token)
|
||||
const authServer = withAuth(server, {
|
||||
provider: jwtProvider,
|
||||
extractCredentials: (req) => {
|
||||
const url = new URL(req.url, 'http://localhost')
|
||||
return url.searchParams.get('token')
|
||||
},
|
||||
})
|
||||
|
||||
await authServer.start()
|
||||
// HTTP: http://localhost:8080/api/*
|
||||
// WebSocket: ws://localhost:8080?token=xxx
|
||||
```
|
||||
|
||||
### Inline Routes
|
||||
|
||||
Routes can also be defined directly in configuration (merged with file routes, inline takes priority):
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 8080,
|
||||
http: {
|
||||
'/health': {
|
||||
GET: (req, res) => res.json({ status: 'ok' }),
|
||||
},
|
||||
'/webhook': async (req, res) => {
|
||||
// Accepts all methods
|
||||
await handleWebhook(req.body)
|
||||
res.json({ received: true })
|
||||
},
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## Room System
|
||||
|
||||
Room is the base class for game rooms, managing players and game state.
|
||||
@@ -311,6 +441,93 @@ client.send('RoomMessage', {
|
||||
})
|
||||
```
|
||||
|
||||
## ECSRoom
|
||||
|
||||
`ECSRoom` is a room base class with ECS World support, suitable for games that need ECS architecture.
|
||||
|
||||
### Server Startup
|
||||
|
||||
```typescript
|
||||
import { Core } from '@esengine/ecs-framework';
|
||||
import { createServer } from '@esengine/server';
|
||||
import { GameRoom } from './rooms/GameRoom.js';
|
||||
|
||||
// Initialize Core
|
||||
Core.create();
|
||||
|
||||
// Global game loop
|
||||
setInterval(() => Core.update(1/60), 16);
|
||||
|
||||
// Create server
|
||||
const server = await createServer({ port: 3000 });
|
||||
server.define('game', GameRoom);
|
||||
await server.start();
|
||||
```
|
||||
|
||||
### Define ECSRoom
|
||||
|
||||
```typescript
|
||||
import { ECSRoom, Player } from '@esengine/server/ecs';
|
||||
import { Component, ECSComponent, sync } from '@esengine/ecs-framework';
|
||||
|
||||
// Define sync component
|
||||
@ECSComponent('Player')
|
||||
class PlayerComponent extends Component {
|
||||
@sync("string") name: string = "";
|
||||
@sync("uint16") score: number = 0;
|
||||
@sync("float32") x: number = 0;
|
||||
@sync("float32") y: number = 0;
|
||||
}
|
||||
|
||||
// Define room
|
||||
class GameRoom extends ECSRoom {
|
||||
onCreate() {
|
||||
this.addSystem(new MovementSystem());
|
||||
}
|
||||
|
||||
onJoin(player: Player) {
|
||||
const entity = this.createPlayerEntity(player.id);
|
||||
const comp = entity.addComponent(new PlayerComponent());
|
||||
comp.name = player.id;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### ECSRoom API
|
||||
|
||||
```typescript
|
||||
abstract class ECSRoom<TState, TPlayerData> extends Room<TState, TPlayerData> {
|
||||
protected readonly world: World; // ECS World
|
||||
protected readonly scene: Scene; // Main scene
|
||||
|
||||
// Scene management
|
||||
protected addSystem(system: EntitySystem): void;
|
||||
protected createEntity(name?: string): Entity;
|
||||
protected createPlayerEntity(playerId: string, name?: string): Entity;
|
||||
protected getPlayerEntity(playerId: string): Entity | undefined;
|
||||
protected destroyPlayerEntity(playerId: string): void;
|
||||
|
||||
// State sync
|
||||
protected sendFullState(player: Player): void;
|
||||
protected broadcastSpawn(entity: Entity, prefabType?: string): void;
|
||||
protected broadcastDelta(): void;
|
||||
}
|
||||
```
|
||||
|
||||
### @sync Decorator
|
||||
|
||||
Mark component fields that need network synchronization:
|
||||
|
||||
| Type | Description | Bytes |
|
||||
|------|-------------|-------|
|
||||
| `"boolean"` | Boolean | 1 |
|
||||
| `"int8"` / `"uint8"` | 8-bit integer | 1 |
|
||||
| `"int16"` / `"uint16"` | 16-bit integer | 2 |
|
||||
| `"int32"` / `"uint32"` | 32-bit integer | 4 |
|
||||
| `"float32"` | 32-bit float | 4 |
|
||||
| `"float64"` | 64-bit float | 8 |
|
||||
| `"string"` | String | Variable |
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Set Appropriate Tick Rate**
|
||||
|
||||
@@ -1,8 +1,176 @@
|
||||
---
|
||||
title: "State Sync"
|
||||
description: "Interpolation, prediction and snapshot buffers"
|
||||
description: "Component sync, interpolation, prediction and snapshot buffers"
|
||||
---
|
||||
|
||||
## @NetworkEntity Decorator
|
||||
|
||||
The `@NetworkEntity` decorator marks components for automatic spawn/despawn broadcasting. When an entity containing this component is created or destroyed, ECSRoom automatically broadcasts the corresponding message to all clients.
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```typescript
|
||||
import { Component, ECSComponent, sync, NetworkEntity } from '@esengine/ecs-framework';
|
||||
|
||||
@ECSComponent('Enemy')
|
||||
@NetworkEntity('Enemy')
|
||||
class EnemyComponent extends Component {
|
||||
@sync('float32') x: number = 0;
|
||||
@sync('float32') y: number = 0;
|
||||
@sync('uint16') health: number = 100;
|
||||
}
|
||||
```
|
||||
|
||||
When adding this component to an entity, ECSRoom automatically broadcasts the spawn message:
|
||||
|
||||
```typescript
|
||||
// Server-side
|
||||
const entity = scene.createEntity('Enemy');
|
||||
entity.addComponent(new EnemyComponent()); // Auto-broadcasts spawn
|
||||
|
||||
// Destroying auto-broadcasts despawn
|
||||
entity.destroy(); // Auto-broadcasts despawn
|
||||
```
|
||||
|
||||
### Configuration Options
|
||||
|
||||
```typescript
|
||||
@NetworkEntity('Bullet', {
|
||||
autoSpawn: true, // Auto-broadcast spawn (default true)
|
||||
autoDespawn: false // Disable auto-broadcast despawn
|
||||
})
|
||||
class BulletComponent extends Component { }
|
||||
```
|
||||
|
||||
| Option | Type | Default | Description |
|
||||
|--------|------|---------|-------------|
|
||||
| `autoSpawn` | `boolean` | `true` | Auto-broadcast spawn when component is added |
|
||||
| `autoDespawn` | `boolean` | `true` | Auto-broadcast despawn when entity is destroyed |
|
||||
|
||||
### Initialization Order
|
||||
|
||||
When using `@NetworkEntity`, initialize data **before** adding the component:
|
||||
|
||||
```typescript
|
||||
// ✅ Correct: Initialize first, then add
|
||||
const comp = new PlayerComponent();
|
||||
comp.playerId = player.id;
|
||||
comp.x = 100;
|
||||
comp.y = 200;
|
||||
entity.addComponent(comp); // Data is correct at spawn
|
||||
|
||||
// ❌ Wrong: Add first, then initialize
|
||||
const comp = entity.addComponent(new PlayerComponent());
|
||||
comp.playerId = player.id; // Data has default values at spawn
|
||||
```
|
||||
|
||||
### Simplified GameRoom
|
||||
|
||||
With `@NetworkEntity`, GameRoom becomes much cleaner:
|
||||
|
||||
```typescript
|
||||
// No manual callbacks needed
|
||||
class GameRoom extends ECSRoom {
|
||||
private setupSystems(): void {
|
||||
// Enemy spawn system (auto-broadcasts spawn)
|
||||
this.addSystem(new EnemySpawnSystem());
|
||||
|
||||
// Enemy AI system
|
||||
const enemyAI = new EnemyAISystem();
|
||||
enemyAI.onDeath((enemy) => {
|
||||
enemy.destroy(); // Auto-broadcasts despawn
|
||||
});
|
||||
this.addSystem(enemyAI);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### ECSRoom Configuration
|
||||
|
||||
You can disable the auto network entity feature in ECSRoom:
|
||||
|
||||
```typescript
|
||||
class GameRoom extends ECSRoom {
|
||||
constructor() {
|
||||
super({
|
||||
enableAutoNetworkEntity: false // Disable auto-broadcasting
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Component Sync System
|
||||
|
||||
ECS component state synchronization based on `@sync` decorator.
|
||||
|
||||
### Define Sync Component
|
||||
|
||||
```typescript
|
||||
import { Component, ECSComponent, sync } from '@esengine/ecs-framework';
|
||||
|
||||
@ECSComponent('Player')
|
||||
class PlayerComponent extends Component {
|
||||
@sync("string") name: string = "";
|
||||
@sync("uint16") score: number = 0;
|
||||
@sync("float32") x: number = 0;
|
||||
@sync("float32") y: number = 0;
|
||||
|
||||
// Fields without @sync won't be synced
|
||||
localData: any;
|
||||
}
|
||||
```
|
||||
|
||||
### Server-side Encoding
|
||||
|
||||
```typescript
|
||||
import { ComponentSyncSystem } from '@esengine/network';
|
||||
|
||||
const syncSystem = new ComponentSyncSystem({}, true);
|
||||
scene.addSystem(syncSystem);
|
||||
|
||||
// Encode all entities (initial connection)
|
||||
const fullData = syncSystem.encodeAllEntities(true);
|
||||
sendToClient(fullData);
|
||||
|
||||
// Encode delta (only send changes)
|
||||
const deltaData = syncSystem.encodeDelta();
|
||||
if (deltaData) {
|
||||
broadcast(deltaData);
|
||||
}
|
||||
```
|
||||
|
||||
### Client-side Decoding
|
||||
|
||||
```typescript
|
||||
const syncSystem = new ComponentSyncSystem();
|
||||
scene.addSystem(syncSystem);
|
||||
|
||||
// Register component types
|
||||
syncSystem.registerComponent(PlayerComponent);
|
||||
|
||||
// Listen for sync events
|
||||
syncSystem.addSyncListener((event) => {
|
||||
if (event.type === 'entitySpawned') {
|
||||
console.log('New entity:', event.entityId);
|
||||
}
|
||||
});
|
||||
|
||||
// Apply state
|
||||
syncSystem.applySnapshot(data);
|
||||
```
|
||||
|
||||
### Sync Types
|
||||
|
||||
| Type | Description | Bytes |
|
||||
|------|-------------|-------|
|
||||
| `"boolean"` | Boolean | 1 |
|
||||
| `"int8"` / `"uint8"` | 8-bit integer | 1 |
|
||||
| `"int16"` / `"uint16"` | 16-bit integer | 2 |
|
||||
| `"int32"` / `"uint32"` | 32-bit integer | 4 |
|
||||
| `"float32"` | 32-bit float | 4 |
|
||||
| `"float64"` | 64-bit float | 8 |
|
||||
| `"string"` | String | Variable |
|
||||
|
||||
## Snapshot Buffer
|
||||
|
||||
Stores server state snapshots for interpolation:
|
||||
|
||||
261
docs/src/content/docs/en/modules/transaction/core.md
Normal file
261
docs/src/content/docs/en/modules/transaction/core.md
Normal file
@@ -0,0 +1,261 @@
|
||||
---
|
||||
title: "Core Concepts"
|
||||
description: "Transaction system core concepts: context, manager, and Saga pattern"
|
||||
---
|
||||
|
||||
## Transaction State
|
||||
|
||||
A transaction can be in the following states:
|
||||
|
||||
```typescript
|
||||
type TransactionState =
|
||||
| 'pending' // Waiting to execute
|
||||
| 'executing' // Executing
|
||||
| 'committed' // Committed
|
||||
| 'rolledback' // Rolled back
|
||||
| 'failed' // Failed
|
||||
```
|
||||
|
||||
## TransactionContext
|
||||
|
||||
The transaction context encapsulates transaction state, operations, and execution logic.
|
||||
|
||||
### Creating Transactions
|
||||
|
||||
```typescript
|
||||
import { TransactionManager } from '@esengine/transaction';
|
||||
|
||||
const manager = new TransactionManager();
|
||||
|
||||
// Method 1: Manual management with begin()
|
||||
const tx = manager.begin({ timeout: 5000 });
|
||||
tx.addOperation(op1);
|
||||
tx.addOperation(op2);
|
||||
const result = await tx.execute();
|
||||
|
||||
// Method 2: Automatic management with run()
|
||||
const result = await manager.run((tx) => {
|
||||
tx.addOperation(op1);
|
||||
tx.addOperation(op2);
|
||||
});
|
||||
```
|
||||
|
||||
### Chaining Operations
|
||||
|
||||
```typescript
|
||||
const result = await manager.run((tx) => {
|
||||
tx.addOperation(new CurrencyOperation({ ... }))
|
||||
.addOperation(new InventoryOperation({ ... }))
|
||||
.addOperation(new InventoryOperation({ ... }));
|
||||
});
|
||||
```
|
||||
|
||||
### Context Data
|
||||
|
||||
Operations can share data through the context:
|
||||
|
||||
```typescript
|
||||
class CustomOperation extends BaseOperation<MyData, MyResult> {
|
||||
async execute(ctx: ITransactionContext): Promise<OperationResult<MyResult>> {
|
||||
// Read data set by previous operations
|
||||
const previousResult = ctx.get<number>('previousValue');
|
||||
|
||||
// Set data for subsequent operations
|
||||
ctx.set('myResult', { value: 123 });
|
||||
|
||||
return this.success({ ... });
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## TransactionManager
|
||||
|
||||
The transaction manager is responsible for creating, executing, and recovering transactions.
|
||||
|
||||
### Configuration Options
|
||||
|
||||
```typescript
|
||||
interface TransactionManagerConfig {
|
||||
storage?: ITransactionStorage; // Storage instance
|
||||
defaultTimeout?: number; // Default timeout (ms)
|
||||
serverId?: string; // Server ID (for distributed)
|
||||
autoRecover?: boolean; // Auto-recover pending transactions
|
||||
}
|
||||
|
||||
const manager = new TransactionManager({
|
||||
storage: new RedisStorage({ client: redis }),
|
||||
defaultTimeout: 10000,
|
||||
serverId: 'server-1',
|
||||
autoRecover: true,
|
||||
});
|
||||
```
|
||||
|
||||
### Distributed Locking
|
||||
|
||||
```typescript
|
||||
// Acquire lock
|
||||
const token = await manager.acquireLock('player:123:inventory', 10000);
|
||||
|
||||
if (token) {
|
||||
try {
|
||||
// Perform operations
|
||||
await doSomething();
|
||||
} finally {
|
||||
// Release lock
|
||||
await manager.releaseLock('player:123:inventory', token);
|
||||
}
|
||||
}
|
||||
|
||||
// Or use withLock for convenience
|
||||
await manager.withLock('player:123:inventory', async () => {
|
||||
await doSomething();
|
||||
}, 10000);
|
||||
```
|
||||
|
||||
### Transaction Recovery
|
||||
|
||||
Recover pending transactions after server restart:
|
||||
|
||||
```typescript
|
||||
const manager = new TransactionManager({
|
||||
storage: new RedisStorage({ client: redis }),
|
||||
serverId: 'server-1',
|
||||
});
|
||||
|
||||
// Recover pending transactions
|
||||
const recoveredCount = await manager.recover();
|
||||
console.log(`Recovered ${recoveredCount} transactions`);
|
||||
```
|
||||
|
||||
## Saga Pattern
|
||||
|
||||
The transaction system uses the Saga pattern. Each operation must implement `execute` and `compensate` methods:
|
||||
|
||||
```typescript
|
||||
interface ITransactionOperation<TData, TResult> {
|
||||
readonly name: string;
|
||||
readonly data: TData;
|
||||
|
||||
// Validate preconditions
|
||||
validate(ctx: ITransactionContext): Promise<boolean>;
|
||||
|
||||
// Forward execution
|
||||
execute(ctx: ITransactionContext): Promise<OperationResult<TResult>>;
|
||||
|
||||
// Compensate (rollback)
|
||||
compensate(ctx: ITransactionContext): Promise<void>;
|
||||
}
|
||||
```
|
||||
|
||||
### Execution Flow
|
||||
|
||||
```
|
||||
Begin Transaction
|
||||
│
|
||||
▼
|
||||
┌─────────────────────┐
|
||||
│ validate(op1) │──fail──► Return failure
|
||||
└─────────────────────┘
|
||||
│success
|
||||
▼
|
||||
┌─────────────────────┐
|
||||
│ execute(op1) │──fail──┐
|
||||
└─────────────────────┘ │
|
||||
│success │
|
||||
▼ │
|
||||
┌─────────────────────┐ │
|
||||
│ validate(op2) │──fail──┤
|
||||
└─────────────────────┘ │
|
||||
│success │
|
||||
▼ │
|
||||
┌─────────────────────┐ │
|
||||
│ execute(op2) │──fail──┤
|
||||
└─────────────────────┘ │
|
||||
│success ▼
|
||||
▼ ┌─────────────────────┐
|
||||
Commit Transaction │ compensate(op1) │
|
||||
└─────────────────────┘
|
||||
│
|
||||
▼
|
||||
Return failure (rolled back)
|
||||
```
|
||||
|
||||
### Custom Operations
|
||||
|
||||
```typescript
|
||||
import { BaseOperation, ITransactionContext, OperationResult } from '@esengine/transaction';
|
||||
|
||||
interface UpgradeData {
|
||||
playerId: string;
|
||||
itemId: string;
|
||||
targetLevel: number;
|
||||
}
|
||||
|
||||
interface UpgradeResult {
|
||||
newLevel: number;
|
||||
}
|
||||
|
||||
class UpgradeOperation extends BaseOperation<UpgradeData, UpgradeResult> {
|
||||
readonly name = 'upgrade';
|
||||
|
||||
private _previousLevel: number = 0;
|
||||
|
||||
async validate(ctx: ITransactionContext): Promise<boolean> {
|
||||
// Validate item exists and can be upgraded
|
||||
const item = await this.getItem(ctx);
|
||||
return item !== null && item.level < this.data.targetLevel;
|
||||
}
|
||||
|
||||
async execute(ctx: ITransactionContext): Promise<OperationResult<UpgradeResult>> {
|
||||
const item = await this.getItem(ctx);
|
||||
if (!item) {
|
||||
return this.failure('Item not found', 'ITEM_NOT_FOUND');
|
||||
}
|
||||
|
||||
this._previousLevel = item.level;
|
||||
item.level = this.data.targetLevel;
|
||||
await this.saveItem(ctx, item);
|
||||
|
||||
return this.success({ newLevel: item.level });
|
||||
}
|
||||
|
||||
async compensate(ctx: ITransactionContext): Promise<void> {
|
||||
const item = await this.getItem(ctx);
|
||||
if (item) {
|
||||
item.level = this._previousLevel;
|
||||
await this.saveItem(ctx, item);
|
||||
}
|
||||
}
|
||||
|
||||
private async getItem(ctx: ITransactionContext) {
|
||||
// Get item from storage
|
||||
}
|
||||
|
||||
private async saveItem(ctx: ITransactionContext, item: any) {
|
||||
// Save item to storage
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Transaction Result
|
||||
|
||||
```typescript
|
||||
interface TransactionResult<T = unknown> {
|
||||
success: boolean; // Whether succeeded
|
||||
transactionId: string; // Transaction ID
|
||||
results: OperationResult[]; // Operation results
|
||||
data?: T; // Final data
|
||||
error?: string; // Error message
|
||||
duration: number; // Execution time (ms)
|
||||
}
|
||||
|
||||
const result = await manager.run((tx) => { ... });
|
||||
|
||||
console.log(`Transaction ${result.transactionId}`);
|
||||
console.log(`Success: ${result.success}`);
|
||||
console.log(`Duration: ${result.duration}ms`);
|
||||
|
||||
if (!result.success) {
|
||||
console.log(`Error: ${result.error}`);
|
||||
}
|
||||
```
|
||||
355
docs/src/content/docs/en/modules/transaction/distributed.md
Normal file
355
docs/src/content/docs/en/modules/transaction/distributed.md
Normal file
@@ -0,0 +1,355 @@
|
||||
---
|
||||
title: "Distributed Transactions"
|
||||
description: "Saga orchestrator and cross-server transaction support"
|
||||
---
|
||||
|
||||
## Saga Orchestrator
|
||||
|
||||
`SagaOrchestrator` is used to orchestrate distributed transactions across servers.
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```typescript
|
||||
import { SagaOrchestrator, RedisStorage } from '@esengine/transaction';
|
||||
|
||||
const orchestrator = new SagaOrchestrator({
|
||||
storage: new RedisStorage({ client: redis }),
|
||||
timeout: 30000,
|
||||
serverId: 'orchestrator-1',
|
||||
});
|
||||
|
||||
const result = await orchestrator.execute([
|
||||
{
|
||||
name: 'deduct_currency',
|
||||
serverId: 'game-server-1',
|
||||
data: { playerId: 'player1', amount: 100 },
|
||||
execute: async (data) => {
|
||||
// Call game server API to deduct currency
|
||||
const response = await gameServerApi.deductCurrency(data);
|
||||
return { success: response.ok };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
// Call game server API to restore currency
|
||||
await gameServerApi.addCurrency(data);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'add_item',
|
||||
serverId: 'inventory-server-1',
|
||||
data: { playerId: 'player1', itemId: 'sword' },
|
||||
execute: async (data) => {
|
||||
const response = await inventoryServerApi.addItem(data);
|
||||
return { success: response.ok };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await inventoryServerApi.removeItem(data);
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
if (result.success) {
|
||||
console.log('Saga completed successfully');
|
||||
} else {
|
||||
console.log('Saga failed:', result.error);
|
||||
console.log('Completed steps:', result.completedSteps);
|
||||
console.log('Failed at:', result.failedStep);
|
||||
}
|
||||
```
|
||||
|
||||
### Configuration Options
|
||||
|
||||
```typescript
|
||||
interface SagaOrchestratorConfig {
|
||||
storage?: ITransactionStorage; // Storage instance
|
||||
timeout?: number; // Timeout in milliseconds
|
||||
serverId?: string; // Orchestrator server ID
|
||||
}
|
||||
```
|
||||
|
||||
### Saga Step
|
||||
|
||||
```typescript
|
||||
interface SagaStep<T = unknown> {
|
||||
name: string; // Step name
|
||||
serverId?: string; // Target server ID
|
||||
data: T; // Step data
|
||||
execute: (data: T) => Promise<OperationResult>; // Execute function
|
||||
compensate: (data: T) => Promise<void>; // Compensate function
|
||||
}
|
||||
```
|
||||
|
||||
### Saga Result
|
||||
|
||||
```typescript
|
||||
interface SagaResult {
|
||||
success: boolean; // Whether succeeded
|
||||
sagaId: string; // Saga ID
|
||||
completedSteps: string[]; // Completed steps
|
||||
failedStep?: string; // Failed step
|
||||
error?: string; // Error message
|
||||
duration: number; // Execution time (ms)
|
||||
}
|
||||
```
|
||||
|
||||
## Execution Flow
|
||||
|
||||
```
|
||||
Start Saga
|
||||
│
|
||||
▼
|
||||
┌─────────────────────┐
|
||||
│ Step 1: execute │──fail──┐
|
||||
└─────────────────────┘ │
|
||||
│success │
|
||||
▼ │
|
||||
┌─────────────────────┐ │
|
||||
│ Step 2: execute │──fail──┤
|
||||
└─────────────────────┘ │
|
||||
│success │
|
||||
▼ │
|
||||
┌─────────────────────┐ │
|
||||
│ Step 3: execute │──fail──┤
|
||||
└─────────────────────┘ │
|
||||
│success ▼
|
||||
▼ ┌─────────────────────┐
|
||||
Saga Complete │ Step 2: compensate │
|
||||
└─────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────┐
|
||||
│ Step 1: compensate │
|
||||
└─────────────────────┘
|
||||
│
|
||||
▼
|
||||
Saga Failed (compensated)
|
||||
```
|
||||
|
||||
## Saga Logs
|
||||
|
||||
The orchestrator records detailed execution logs:
|
||||
|
||||
```typescript
|
||||
interface SagaLog {
|
||||
id: string; // Saga ID
|
||||
state: SagaLogState; // State
|
||||
steps: SagaStepLog[]; // Step logs
|
||||
createdAt: number; // Creation time
|
||||
updatedAt: number; // Update time
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
type SagaLogState =
|
||||
| 'pending' // Waiting to execute
|
||||
| 'running' // Executing
|
||||
| 'completed' // Completed
|
||||
| 'compensating' // Compensating
|
||||
| 'compensated' // Compensated
|
||||
| 'failed' // Failed
|
||||
|
||||
interface SagaStepLog {
|
||||
name: string; // Step name
|
||||
serverId?: string; // Server ID
|
||||
state: SagaStepState; // State
|
||||
startedAt?: number; // Start time
|
||||
completedAt?: number; // Completion time
|
||||
error?: string; // Error message
|
||||
}
|
||||
|
||||
type SagaStepState =
|
||||
| 'pending' // Waiting to execute
|
||||
| 'executing' // Executing
|
||||
| 'completed' // Completed
|
||||
| 'compensating' // Compensating
|
||||
| 'compensated' // Compensated
|
||||
| 'failed' // Failed
|
||||
```
|
||||
|
||||
### Query Saga Logs
|
||||
|
||||
```typescript
|
||||
const log = await orchestrator.getSagaLog('saga_xxx');
|
||||
|
||||
if (log) {
|
||||
console.log('Saga state:', log.state);
|
||||
for (const step of log.steps) {
|
||||
console.log(` ${step.name}: ${step.state}`);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Cross-Server Transaction Examples
|
||||
|
||||
### Scenario: Cross-Server Purchase
|
||||
|
||||
A player purchases an item on a game server, with currency on an account server and items on an inventory server.
|
||||
|
||||
```typescript
|
||||
const orchestrator = new SagaOrchestrator({
|
||||
storage: redisStorage,
|
||||
serverId: 'purchase-orchestrator',
|
||||
});
|
||||
|
||||
async function crossServerPurchase(
|
||||
playerId: string,
|
||||
itemId: string,
|
||||
price: number
|
||||
): Promise<SagaResult> {
|
||||
return orchestrator.execute([
|
||||
// Step 1: Deduct balance on account server
|
||||
{
|
||||
name: 'deduct_balance',
|
||||
serverId: 'account-server',
|
||||
data: { playerId, amount: price },
|
||||
execute: async (data) => {
|
||||
const result = await accountService.deduct(data.playerId, data.amount);
|
||||
return { success: result.ok, error: result.error };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await accountService.refund(data.playerId, data.amount);
|
||||
},
|
||||
},
|
||||
|
||||
// Step 2: Add item on inventory server
|
||||
{
|
||||
name: 'add_item',
|
||||
serverId: 'inventory-server',
|
||||
data: { playerId, itemId },
|
||||
execute: async (data) => {
|
||||
const result = await inventoryService.addItem(data.playerId, data.itemId);
|
||||
return { success: result.ok, error: result.error };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await inventoryService.removeItem(data.playerId, data.itemId);
|
||||
},
|
||||
},
|
||||
|
||||
// Step 3: Record purchase log
|
||||
{
|
||||
name: 'log_purchase',
|
||||
serverId: 'log-server',
|
||||
data: { playerId, itemId, price, timestamp: Date.now() },
|
||||
execute: async (data) => {
|
||||
await logService.recordPurchase(data);
|
||||
return { success: true };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await logService.cancelPurchase(data);
|
||||
},
|
||||
},
|
||||
]);
|
||||
}
|
||||
```
|
||||
|
||||
### Scenario: Cross-Server Trade
|
||||
|
||||
Two players on different servers trade with each other.
|
||||
|
||||
```typescript
|
||||
async function crossServerTrade(
|
||||
playerA: { id: string; server: string; items: string[] },
|
||||
playerB: { id: string; server: string; items: string[] }
|
||||
): Promise<SagaResult> {
|
||||
const steps: SagaStep[] = [];
|
||||
|
||||
// Remove items from player A
|
||||
for (const itemId of playerA.items) {
|
||||
steps.push({
|
||||
name: `remove_${playerA.id}_${itemId}`,
|
||||
serverId: playerA.server,
|
||||
data: { playerId: playerA.id, itemId },
|
||||
execute: async (data) => {
|
||||
return await inventoryService.removeItem(data.playerId, data.itemId);
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await inventoryService.addItem(data.playerId, data.itemId);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Add items to player B (from A)
|
||||
for (const itemId of playerA.items) {
|
||||
steps.push({
|
||||
name: `add_${playerB.id}_${itemId}`,
|
||||
serverId: playerB.server,
|
||||
data: { playerId: playerB.id, itemId },
|
||||
execute: async (data) => {
|
||||
return await inventoryService.addItem(data.playerId, data.itemId);
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await inventoryService.removeItem(data.playerId, data.itemId);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Similarly handle player B's items...
|
||||
|
||||
return orchestrator.execute(steps);
|
||||
}
|
||||
```
|
||||
|
||||
## Recovering Incomplete Sagas
|
||||
|
||||
Recover incomplete Sagas after server restart:
|
||||
|
||||
```typescript
|
||||
const orchestrator = new SagaOrchestrator({
|
||||
storage: redisStorage,
|
||||
serverId: 'my-orchestrator',
|
||||
});
|
||||
|
||||
// Recover incomplete Sagas (will execute compensation)
|
||||
const recoveredCount = await orchestrator.recover();
|
||||
console.log(`Recovered ${recoveredCount} sagas`);
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Idempotency
|
||||
|
||||
Ensure all operations are idempotent:
|
||||
|
||||
```typescript
|
||||
{
|
||||
execute: async (data) => {
|
||||
// Use unique ID to ensure idempotency
|
||||
const result = await service.process(data.requestId, data);
|
||||
return { success: result.ok };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
// Compensation must also be idempotent
|
||||
await service.rollback(data.requestId);
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Timeout Handling
|
||||
|
||||
Set appropriate timeout values:
|
||||
|
||||
```typescript
|
||||
const orchestrator = new SagaOrchestrator({
|
||||
timeout: 60000, // Cross-server operations need longer timeout
|
||||
});
|
||||
```
|
||||
|
||||
### 3. Monitoring and Alerts
|
||||
|
||||
Log Saga execution results:
|
||||
|
||||
```typescript
|
||||
const result = await orchestrator.execute(steps);
|
||||
|
||||
if (!result.success) {
|
||||
// Send alert
|
||||
alertService.send({
|
||||
type: 'saga_failed',
|
||||
sagaId: result.sagaId,
|
||||
failedStep: result.failedStep,
|
||||
error: result.error,
|
||||
});
|
||||
|
||||
// Log details
|
||||
const log = await orchestrator.getSagaLog(result.sagaId);
|
||||
logger.error('Saga failed', { log });
|
||||
}
|
||||
```
|
||||
238
docs/src/content/docs/en/modules/transaction/index.md
Normal file
238
docs/src/content/docs/en/modules/transaction/index.md
Normal file
@@ -0,0 +1,238 @@
|
||||
---
|
||||
title: "Transaction System"
|
||||
description: "Game transaction system with distributed support for shop purchases, player trading, and more"
|
||||
---
|
||||
|
||||
`@esengine/transaction` provides comprehensive game transaction capabilities based on the Saga pattern, supporting shop purchases, player trading, multi-step tasks, and distributed transactions with Redis/MongoDB.
|
||||
|
||||
## Overview
|
||||
|
||||
The transaction system solves common data consistency problems in games:
|
||||
|
||||
| Scenario | Problem | Solution |
|
||||
|----------|---------|----------|
|
||||
| Shop Purchase | Payment succeeded but item not delivered | Atomic transaction with auto-rollback |
|
||||
| Player Trade | One party transferred items but other didn't receive | Saga compensation mechanism |
|
||||
| Cross-Server | Data inconsistency across servers | Distributed lock + transaction log |
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install @esengine/transaction
|
||||
```
|
||||
|
||||
Optional dependencies (install based on storage needs):
|
||||
```bash
|
||||
npm install ioredis # Redis storage
|
||||
npm install mongodb # MongoDB storage
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ Transaction Layer │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ TransactionManager - Manages transaction lifecycle │
|
||||
│ TransactionContext - Encapsulates operations and state │
|
||||
│ SagaOrchestrator - Distributed Saga orchestrator │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ Storage Layer │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ MemoryStorage - In-memory (dev/test) │
|
||||
│ RedisStorage - Redis (distributed lock + cache) │
|
||||
│ MongoStorage - MongoDB (persistent log) │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ Operation Layer │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ CurrencyOperation - Currency operations │
|
||||
│ InventoryOperation - Inventory operations │
|
||||
│ TradeOperation - Trade operations │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```typescript
|
||||
import {
|
||||
TransactionManager,
|
||||
MemoryStorage,
|
||||
CurrencyOperation,
|
||||
InventoryOperation,
|
||||
} from '@esengine/transaction';
|
||||
|
||||
// Create transaction manager
|
||||
const manager = new TransactionManager({
|
||||
storage: new MemoryStorage(),
|
||||
defaultTimeout: 10000,
|
||||
});
|
||||
|
||||
// Execute transaction
|
||||
const result = await manager.run((tx) => {
|
||||
// Deduct gold
|
||||
tx.addOperation(new CurrencyOperation({
|
||||
type: 'deduct',
|
||||
playerId: 'player1',
|
||||
currency: 'gold',
|
||||
amount: 100,
|
||||
}));
|
||||
|
||||
// Add item
|
||||
tx.addOperation(new InventoryOperation({
|
||||
type: 'add',
|
||||
playerId: 'player1',
|
||||
itemId: 'sword_001',
|
||||
quantity: 1,
|
||||
}));
|
||||
});
|
||||
|
||||
if (result.success) {
|
||||
console.log('Purchase successful!');
|
||||
} else {
|
||||
console.log('Purchase failed:', result.error);
|
||||
}
|
||||
```
|
||||
|
||||
### Player Trading
|
||||
|
||||
```typescript
|
||||
import { TradeOperation } from '@esengine/transaction';
|
||||
|
||||
const result = await manager.run((tx) => {
|
||||
tx.addOperation(new TradeOperation({
|
||||
tradeId: 'trade_001',
|
||||
partyA: {
|
||||
playerId: 'player1',
|
||||
items: [{ itemId: 'sword', quantity: 1 }],
|
||||
},
|
||||
partyB: {
|
||||
playerId: 'player2',
|
||||
currencies: [{ currency: 'gold', amount: 1000 }],
|
||||
},
|
||||
}));
|
||||
}, { timeout: 30000 });
|
||||
```
|
||||
|
||||
### Using Redis Storage
|
||||
|
||||
```typescript
|
||||
import Redis from 'ioredis';
|
||||
import { TransactionManager, RedisStorage } from '@esengine/transaction';
|
||||
|
||||
const redis = new Redis('redis://localhost:6379');
|
||||
const storage = new RedisStorage({ client: redis });
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
```
|
||||
|
||||
### Using MongoDB Storage
|
||||
|
||||
```typescript
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { TransactionManager, MongoStorage } from '@esengine/transaction';
|
||||
|
||||
const client = new MongoClient('mongodb://localhost:27017');
|
||||
await client.connect();
|
||||
const db = client.db('game');
|
||||
|
||||
const storage = new MongoStorage({ db });
|
||||
await storage.ensureIndexes();
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
```
|
||||
|
||||
## Room Integration
|
||||
|
||||
```typescript
|
||||
import { Room } from '@esengine/server';
|
||||
import { withTransactions, CurrencyOperation, RedisStorage } from '@esengine/transaction';
|
||||
|
||||
class GameRoom extends withTransactions(Room, {
|
||||
storage: new RedisStorage({ client: redisClient }),
|
||||
}) {
|
||||
@onMessage('Buy')
|
||||
async handleBuy(data: { itemId: string }, player: Player) {
|
||||
const result = await this.runTransaction((tx) => {
|
||||
tx.addOperation(new CurrencyOperation({
|
||||
type: 'deduct',
|
||||
playerId: player.id,
|
||||
currency: 'gold',
|
||||
amount: getItemPrice(data.itemId),
|
||||
}));
|
||||
});
|
||||
|
||||
if (result.success) {
|
||||
player.send('buy_success', { itemId: data.itemId });
|
||||
} else {
|
||||
player.send('buy_failed', { error: result.error });
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
- [Core Concepts](/en/modules/transaction/core/) - Transaction context, manager, Saga pattern
|
||||
- [Storage Layer](/en/modules/transaction/storage/) - MemoryStorage, RedisStorage, MongoStorage
|
||||
- [Operations](/en/modules/transaction/operations/) - Currency, inventory, trade operations
|
||||
- [Distributed Transactions](/en/modules/transaction/distributed/) - Saga orchestrator, cross-server transactions
|
||||
- [API Reference](/en/modules/transaction/api/) - Complete API documentation
|
||||
|
||||
## Service Tokens
|
||||
|
||||
For dependency injection:
|
||||
|
||||
```typescript
|
||||
import {
|
||||
TransactionManagerToken,
|
||||
TransactionStorageToken,
|
||||
} from '@esengine/transaction';
|
||||
|
||||
const manager = services.get(TransactionManagerToken);
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
### 1. Operation Granularity
|
||||
|
||||
```typescript
|
||||
// ✅ Good: Fine-grained operations, easy to rollback
|
||||
tx.addOperation(new CurrencyOperation({ type: 'deduct', ... }));
|
||||
tx.addOperation(new InventoryOperation({ type: 'add', ... }));
|
||||
|
||||
// ❌ Bad: Coarse-grained operation, hard to partially rollback
|
||||
tx.addOperation(new ComplexPurchaseOperation({ ... }));
|
||||
```
|
||||
|
||||
### 2. Timeout Settings
|
||||
|
||||
```typescript
|
||||
// Simple operations: short timeout
|
||||
await manager.run(tx => { ... }, { timeout: 5000 });
|
||||
|
||||
// Complex trades: longer timeout
|
||||
await manager.run(tx => { ... }, { timeout: 30000 });
|
||||
|
||||
// Cross-server: even longer timeout
|
||||
await manager.run(tx => { ... }, { timeout: 60000, distributed: true });
|
||||
```
|
||||
|
||||
### 3. Error Handling
|
||||
|
||||
```typescript
|
||||
const result = await manager.run((tx) => { ... });
|
||||
|
||||
if (!result.success) {
|
||||
// Log the error
|
||||
logger.error('Transaction failed', {
|
||||
transactionId: result.transactionId,
|
||||
error: result.error,
|
||||
duration: result.duration,
|
||||
});
|
||||
|
||||
// Notify user
|
||||
player.send('error', { message: getErrorMessage(result.error) });
|
||||
}
|
||||
```
|
||||
313
docs/src/content/docs/en/modules/transaction/operations.md
Normal file
313
docs/src/content/docs/en/modules/transaction/operations.md
Normal file
@@ -0,0 +1,313 @@
|
||||
---
|
||||
title: "Operations"
|
||||
description: "Built-in transaction operations: currency, inventory, trade"
|
||||
---
|
||||
|
||||
## BaseOperation
|
||||
|
||||
Base class for all operations, providing a common implementation template.
|
||||
|
||||
```typescript
|
||||
import { BaseOperation, ITransactionContext, OperationResult } from '@esengine/transaction';
|
||||
|
||||
class MyOperation extends BaseOperation<MyData, MyResult> {
|
||||
readonly name = 'myOperation';
|
||||
|
||||
async validate(ctx: ITransactionContext): Promise<boolean> {
|
||||
// Validate preconditions
|
||||
return true;
|
||||
}
|
||||
|
||||
async execute(ctx: ITransactionContext): Promise<OperationResult<MyResult>> {
|
||||
// Execute operation
|
||||
return this.success({ result: 'ok' });
|
||||
// or
|
||||
return this.failure('Something went wrong', 'ERROR_CODE');
|
||||
}
|
||||
|
||||
async compensate(ctx: ITransactionContext): Promise<void> {
|
||||
// Rollback operation
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## CurrencyOperation
|
||||
|
||||
Handles currency addition and deduction.
|
||||
|
||||
### Deduct Currency
|
||||
|
||||
```typescript
|
||||
import { CurrencyOperation } from '@esengine/transaction';
|
||||
|
||||
tx.addOperation(new CurrencyOperation({
|
||||
type: 'deduct',
|
||||
playerId: 'player1',
|
||||
currency: 'gold',
|
||||
amount: 100,
|
||||
reason: 'purchase_item',
|
||||
}));
|
||||
```
|
||||
|
||||
### Add Currency
|
||||
|
||||
```typescript
|
||||
tx.addOperation(new CurrencyOperation({
|
||||
type: 'add',
|
||||
playerId: 'player1',
|
||||
currency: 'diamond',
|
||||
amount: 50,
|
||||
reason: 'daily_reward',
|
||||
}));
|
||||
```
|
||||
|
||||
### Operation Data
|
||||
|
||||
```typescript
|
||||
interface CurrencyOperationData {
|
||||
type: 'add' | 'deduct'; // Operation type
|
||||
playerId: string; // Player ID
|
||||
currency: string; // Currency type
|
||||
amount: number; // Amount
|
||||
reason?: string; // Reason/source
|
||||
}
|
||||
```
|
||||
|
||||
### Operation Result
|
||||
|
||||
```typescript
|
||||
interface CurrencyOperationResult {
|
||||
beforeBalance: number; // Balance before operation
|
||||
afterBalance: number; // Balance after operation
|
||||
}
|
||||
```
|
||||
|
||||
### Custom Data Provider
|
||||
|
||||
```typescript
|
||||
interface ICurrencyProvider {
|
||||
getBalance(playerId: string, currency: string): Promise<number>;
|
||||
setBalance(playerId: string, currency: string, amount: number): Promise<void>;
|
||||
}
|
||||
|
||||
class MyCurrencyProvider implements ICurrencyProvider {
|
||||
async getBalance(playerId: string, currency: string): Promise<number> {
|
||||
// Get balance from database
|
||||
return await db.getCurrency(playerId, currency);
|
||||
}
|
||||
|
||||
async setBalance(playerId: string, currency: string, amount: number): Promise<void> {
|
||||
// Save to database
|
||||
await db.setCurrency(playerId, currency, amount);
|
||||
}
|
||||
}
|
||||
|
||||
// Use custom provider
|
||||
const op = new CurrencyOperation({ ... });
|
||||
op.setProvider(new MyCurrencyProvider());
|
||||
tx.addOperation(op);
|
||||
```
|
||||
|
||||
## InventoryOperation
|
||||
|
||||
Handles item addition, removal, and updates.
|
||||
|
||||
### Add Item
|
||||
|
||||
```typescript
|
||||
import { InventoryOperation } from '@esengine/transaction';
|
||||
|
||||
tx.addOperation(new InventoryOperation({
|
||||
type: 'add',
|
||||
playerId: 'player1',
|
||||
itemId: 'sword_001',
|
||||
quantity: 1,
|
||||
properties: { enchant: 'fire' },
|
||||
}));
|
||||
```
|
||||
|
||||
### Remove Item
|
||||
|
||||
```typescript
|
||||
tx.addOperation(new InventoryOperation({
|
||||
type: 'remove',
|
||||
playerId: 'player1',
|
||||
itemId: 'potion_hp',
|
||||
quantity: 5,
|
||||
}));
|
||||
```
|
||||
|
||||
### Update Item
|
||||
|
||||
```typescript
|
||||
tx.addOperation(new InventoryOperation({
|
||||
type: 'update',
|
||||
playerId: 'player1',
|
||||
itemId: 'sword_001',
|
||||
quantity: 1, // Optional, keeps original if not provided
|
||||
properties: { enchant: 'lightning', level: 5 },
|
||||
}));
|
||||
```
|
||||
|
||||
### Operation Data
|
||||
|
||||
```typescript
|
||||
interface InventoryOperationData {
|
||||
type: 'add' | 'remove' | 'update'; // Operation type
|
||||
playerId: string; // Player ID
|
||||
itemId: string; // Item ID
|
||||
quantity: number; // Quantity
|
||||
properties?: Record<string, unknown>; // Item properties
|
||||
reason?: string; // Reason/source
|
||||
}
|
||||
```
|
||||
|
||||
### Operation Result
|
||||
|
||||
```typescript
|
||||
interface InventoryOperationResult {
|
||||
beforeItem?: ItemData; // Item before operation
|
||||
afterItem?: ItemData; // Item after operation
|
||||
}
|
||||
|
||||
interface ItemData {
|
||||
itemId: string;
|
||||
quantity: number;
|
||||
properties?: Record<string, unknown>;
|
||||
}
|
||||
```
|
||||
|
||||
### Custom Data Provider
|
||||
|
||||
```typescript
|
||||
interface IInventoryProvider {
|
||||
getItem(playerId: string, itemId: string): Promise<ItemData | null>;
|
||||
setItem(playerId: string, itemId: string, item: ItemData | null): Promise<void>;
|
||||
hasCapacity?(playerId: string, count: number): Promise<boolean>;
|
||||
}
|
||||
|
||||
class MyInventoryProvider implements IInventoryProvider {
|
||||
async getItem(playerId: string, itemId: string): Promise<ItemData | null> {
|
||||
return await db.getItem(playerId, itemId);
|
||||
}
|
||||
|
||||
async setItem(playerId: string, itemId: string, item: ItemData | null): Promise<void> {
|
||||
if (item) {
|
||||
await db.saveItem(playerId, itemId, item);
|
||||
} else {
|
||||
await db.deleteItem(playerId, itemId);
|
||||
}
|
||||
}
|
||||
|
||||
async hasCapacity(playerId: string, count: number): Promise<boolean> {
|
||||
const current = await db.getItemCount(playerId);
|
||||
const max = await db.getMaxCapacity(playerId);
|
||||
return current + count <= max;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## TradeOperation
|
||||
|
||||
Handles item and currency exchange between players.
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```typescript
|
||||
import { TradeOperation } from '@esengine/transaction';
|
||||
|
||||
tx.addOperation(new TradeOperation({
|
||||
tradeId: 'trade_001',
|
||||
partyA: {
|
||||
playerId: 'player1',
|
||||
items: [{ itemId: 'sword', quantity: 1 }],
|
||||
currencies: [{ currency: 'diamond', amount: 10 }],
|
||||
},
|
||||
partyB: {
|
||||
playerId: 'player2',
|
||||
currencies: [{ currency: 'gold', amount: 1000 }],
|
||||
},
|
||||
reason: 'player_trade',
|
||||
}));
|
||||
```
|
||||
|
||||
### Operation Data
|
||||
|
||||
```typescript
|
||||
interface TradeOperationData {
|
||||
tradeId: string; // Trade ID
|
||||
partyA: TradeParty; // Trade initiator
|
||||
partyB: TradeParty; // Trade receiver
|
||||
reason?: string; // Reason/note
|
||||
}
|
||||
|
||||
interface TradeParty {
|
||||
playerId: string; // Player ID
|
||||
items?: TradeItem[]; // Items to give
|
||||
currencies?: TradeCurrency[]; // Currencies to give
|
||||
}
|
||||
|
||||
interface TradeItem {
|
||||
itemId: string;
|
||||
quantity: number;
|
||||
}
|
||||
|
||||
interface TradeCurrency {
|
||||
currency: string;
|
||||
amount: number;
|
||||
}
|
||||
```
|
||||
|
||||
### Execution Flow
|
||||
|
||||
TradeOperation internally generates the following sub-operation sequence:
|
||||
|
||||
```
|
||||
1. Remove partyA's items
|
||||
2. Add items to partyB (from partyA)
|
||||
3. Deduct partyA's currencies
|
||||
4. Add currencies to partyB (from partyA)
|
||||
5. Remove partyB's items
|
||||
6. Add items to partyA (from partyB)
|
||||
7. Deduct partyB's currencies
|
||||
8. Add currencies to partyA (from partyB)
|
||||
```
|
||||
|
||||
If any step fails, all previous operations are rolled back.
|
||||
|
||||
### Using Custom Providers
|
||||
|
||||
```typescript
|
||||
const op = new TradeOperation({ ... });
|
||||
op.setProvider({
|
||||
currencyProvider: new MyCurrencyProvider(),
|
||||
inventoryProvider: new MyInventoryProvider(),
|
||||
});
|
||||
tx.addOperation(op);
|
||||
```
|
||||
|
||||
## Factory Functions
|
||||
|
||||
Each operation class provides a factory function:
|
||||
|
||||
```typescript
|
||||
import {
|
||||
createCurrencyOperation,
|
||||
createInventoryOperation,
|
||||
createTradeOperation,
|
||||
} from '@esengine/transaction';
|
||||
|
||||
tx.addOperation(createCurrencyOperation({
|
||||
type: 'deduct',
|
||||
playerId: 'player1',
|
||||
currency: 'gold',
|
||||
amount: 100,
|
||||
}));
|
||||
|
||||
tx.addOperation(createInventoryOperation({
|
||||
type: 'add',
|
||||
playerId: 'player1',
|
||||
itemId: 'sword',
|
||||
quantity: 1,
|
||||
}));
|
||||
```
|
||||
238
docs/src/content/docs/en/modules/transaction/storage.md
Normal file
238
docs/src/content/docs/en/modules/transaction/storage.md
Normal file
@@ -0,0 +1,238 @@
|
||||
---
|
||||
title: "Storage Layer"
|
||||
description: "Transaction storage interface and implementations: MemoryStorage, RedisStorage, MongoStorage"
|
||||
---
|
||||
|
||||
## Storage Interface
|
||||
|
||||
All storage implementations must implement the `ITransactionStorage` interface:
|
||||
|
||||
```typescript
|
||||
interface ITransactionStorage {
|
||||
// Lifecycle
|
||||
close?(): Promise<void>;
|
||||
|
||||
// Distributed lock
|
||||
acquireLock(key: string, ttl: number): Promise<string | null>;
|
||||
releaseLock(key: string, token: string): Promise<boolean>;
|
||||
|
||||
// Transaction log
|
||||
saveTransaction(tx: TransactionLog): Promise<void>;
|
||||
getTransaction(id: string): Promise<TransactionLog | null>;
|
||||
updateTransactionState(id: string, state: TransactionState): Promise<void>;
|
||||
updateOperationState(txId: string, opIndex: number, state: string, error?: string): Promise<void>;
|
||||
getPendingTransactions(serverId?: string): Promise<TransactionLog[]>;
|
||||
deleteTransaction(id: string): Promise<void>;
|
||||
|
||||
// Data operations
|
||||
get<T>(key: string): Promise<T | null>;
|
||||
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
||||
delete(key: string): Promise<boolean>;
|
||||
}
|
||||
```
|
||||
|
||||
## MemoryStorage
|
||||
|
||||
In-memory storage, suitable for development and testing.
|
||||
|
||||
```typescript
|
||||
import { MemoryStorage } from '@esengine/transaction';
|
||||
|
||||
const storage = new MemoryStorage({
|
||||
maxTransactions: 1000, // Maximum transaction log count
|
||||
});
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
```
|
||||
|
||||
### Characteristics
|
||||
|
||||
- ✅ No external dependencies
|
||||
- ✅ Fast, good for debugging
|
||||
- ❌ Data only stored in memory
|
||||
- ❌ No true distributed locking
|
||||
- ❌ Data lost on restart
|
||||
|
||||
### Test Helpers
|
||||
|
||||
```typescript
|
||||
// Clear all data
|
||||
storage.clear();
|
||||
|
||||
// Get transaction count
|
||||
console.log(storage.transactionCount);
|
||||
```
|
||||
|
||||
## RedisStorage
|
||||
|
||||
Redis storage, suitable for production distributed systems. Uses factory pattern with lazy connection.
|
||||
|
||||
```typescript
|
||||
import Redis from 'ioredis';
|
||||
import { RedisStorage } from '@esengine/transaction';
|
||||
|
||||
// Factory pattern: lazy connection, connects on first operation
|
||||
const storage = new RedisStorage({
|
||||
factory: () => new Redis('redis://localhost:6379'),
|
||||
prefix: 'tx:', // Key prefix
|
||||
transactionTTL: 86400, // Transaction log TTL (seconds)
|
||||
});
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
|
||||
// Close connection when done
|
||||
await storage.close();
|
||||
|
||||
// Or use await using for automatic cleanup (TypeScript 5.2+)
|
||||
await using storage = new RedisStorage({
|
||||
factory: () => new Redis('redis://localhost:6379')
|
||||
});
|
||||
// Automatically closed when scope ends
|
||||
```
|
||||
|
||||
### Characteristics
|
||||
|
||||
- ✅ High-performance distributed locking
|
||||
- ✅ Fast read/write
|
||||
- ✅ Supports TTL auto-expiration
|
||||
- ✅ Suitable for high concurrency
|
||||
- ❌ Requires Redis server
|
||||
|
||||
### Distributed Lock Implementation
|
||||
|
||||
Uses Redis `SET NX EX` for distributed locking:
|
||||
|
||||
```typescript
|
||||
// Acquire lock (atomic operation)
|
||||
SET tx:lock:player:123 <token> NX EX 10
|
||||
|
||||
// Release lock (Lua script for atomicity)
|
||||
if redis.call("get", KEYS[1]) == ARGV[1] then
|
||||
return redis.call("del", KEYS[1])
|
||||
else
|
||||
return 0
|
||||
end
|
||||
```
|
||||
|
||||
### Key Structure
|
||||
|
||||
```
|
||||
tx:lock:{key} - Distributed locks
|
||||
tx:tx:{id} - Transaction logs
|
||||
tx:server:{id}:txs - Server transaction index
|
||||
tx:data:{key} - Business data
|
||||
```
|
||||
|
||||
## MongoStorage
|
||||
|
||||
MongoDB storage, suitable for scenarios requiring persistence and complex queries. Uses shared connection from `@esengine/database-drivers`.
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers';
|
||||
import { createMongoStorage, TransactionManager } from '@esengine/transaction';
|
||||
|
||||
// Create shared connection
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
});
|
||||
await mongo.connect();
|
||||
|
||||
// Create storage using shared connection
|
||||
const storage = createMongoStorage(mongo, {
|
||||
transactionCollection: 'transactions', // Transaction log collection (optional)
|
||||
dataCollection: 'transaction_data', // Business data collection (optional)
|
||||
lockCollection: 'transaction_locks', // Lock collection (optional)
|
||||
});
|
||||
|
||||
// Create indexes (run on first startup)
|
||||
await storage.ensureIndexes();
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
|
||||
// Close storage (does not close shared connection)
|
||||
await storage.close();
|
||||
|
||||
// Shared connection can continue to be used by other modules
|
||||
const userRepo = new UserRepository(mongo); // @esengine/database
|
||||
|
||||
// Finally close the shared connection
|
||||
await mongo.disconnect();
|
||||
```
|
||||
|
||||
### Characteristics
|
||||
|
||||
- ✅ Persistent storage
|
||||
- ✅ Supports complex queries
|
||||
- ✅ Transaction logs are traceable
|
||||
- ✅ Suitable for audit requirements
|
||||
- ❌ Slightly lower performance than Redis
|
||||
- ❌ Requires MongoDB server
|
||||
|
||||
### Index Structure
|
||||
|
||||
```javascript
|
||||
// transactions collection
|
||||
{ state: 1 }
|
||||
{ 'metadata.serverId': 1 }
|
||||
{ createdAt: 1 }
|
||||
|
||||
// transaction_locks collection
|
||||
{ expireAt: 1 } // TTL index
|
||||
|
||||
// transaction_data collection
|
||||
{ expireAt: 1 } // TTL index
|
||||
```
|
||||
|
||||
### Distributed Lock Implementation
|
||||
|
||||
Uses MongoDB unique index for distributed locking:
|
||||
|
||||
```typescript
|
||||
// Acquire lock
|
||||
db.transaction_locks.insertOne({
|
||||
_id: 'player:123',
|
||||
token: '<token>',
|
||||
expireAt: new Date(Date.now() + 10000)
|
||||
});
|
||||
|
||||
// If key exists, check if expired
|
||||
db.transaction_locks.updateOne(
|
||||
{ _id: 'player:123', expireAt: { $lt: new Date() } },
|
||||
{ $set: { token: '<token>', expireAt: new Date(Date.now() + 10000) } }
|
||||
);
|
||||
```
|
||||
|
||||
## Storage Selection Guide
|
||||
|
||||
| Scenario | Recommended Storage | Reason |
|
||||
|----------|---------------------|--------|
|
||||
| Development/Testing | MemoryStorage | No dependencies, fast startup |
|
||||
| Single-machine Production | RedisStorage | High performance, simple |
|
||||
| Distributed System | RedisStorage | True distributed locking |
|
||||
| Audit Required | MongoStorage | Persistent logs |
|
||||
| Mixed Requirements | Redis + Mongo | Redis for locks, Mongo for logs |
|
||||
|
||||
## Custom Storage
|
||||
|
||||
Implement `ITransactionStorage` interface to create custom storage:
|
||||
|
||||
```typescript
|
||||
import { ITransactionStorage, TransactionLog, TransactionState } from '@esengine/transaction';
|
||||
|
||||
class MyCustomStorage implements ITransactionStorage {
|
||||
async acquireLock(key: string, ttl: number): Promise<string | null> {
|
||||
// Implement distributed lock acquisition
|
||||
}
|
||||
|
||||
async releaseLock(key: string, token: string): Promise<boolean> {
|
||||
// Implement distributed lock release
|
||||
}
|
||||
|
||||
async saveTransaction(tx: TransactionLog): Promise<void> {
|
||||
// Save transaction log
|
||||
}
|
||||
|
||||
// ... implement other methods
|
||||
}
|
||||
```
|
||||
@@ -71,6 +71,55 @@ class ConfiguredScene extends Scene {
|
||||
}
|
||||
```
|
||||
|
||||
## 运行时环境
|
||||
|
||||
对于网络游戏,你可以配置运行时环境来区分服务端和客户端逻辑。
|
||||
|
||||
### 全局配置(推荐)
|
||||
|
||||
在 Core 层级设置一次运行时环境,所有场景都会继承此设置:
|
||||
|
||||
```typescript
|
||||
import { Core } from '@esengine/ecs-framework';
|
||||
|
||||
// 方式1:在 Core.create() 中设置
|
||||
Core.create({ runtimeEnvironment: 'server' });
|
||||
|
||||
// 方式2:直接设置静态属性
|
||||
Core.runtimeEnvironment = 'server';
|
||||
```
|
||||
|
||||
### 单个场景覆盖
|
||||
|
||||
个别场景可以覆盖全局设置:
|
||||
|
||||
```typescript
|
||||
const clientScene = new Scene({ runtimeEnvironment: 'client' });
|
||||
```
|
||||
|
||||
### 环境类型
|
||||
|
||||
| 环境 | 使用场景 |
|
||||
|------|----------|
|
||||
| `'standalone'` | 单机游戏(默认) |
|
||||
| `'server'` | 游戏服务器,权威逻辑 |
|
||||
| `'client'` | 游戏客户端,渲染/输入 |
|
||||
|
||||
### 在系统中检查环境
|
||||
|
||||
```typescript
|
||||
class CollectibleSpawnSystem extends EntitySystem {
|
||||
private checkCollections(): void {
|
||||
// 客户端跳过 - 只有服务端处理权威逻辑
|
||||
if (!this.scene.isServer) return;
|
||||
|
||||
// 服务端权威生成逻辑...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
参见 [系统运行时装饰器](/guide/system/index#运行时环境装饰器) 了解基于装饰器的方式。
|
||||
|
||||
### 运行场景
|
||||
|
||||
```typescript
|
||||
|
||||
@@ -160,6 +160,53 @@ scene.addSystem(new SystemA()); // addOrder = 0,先执行
|
||||
scene.addSystem(new SystemB()); // addOrder = 1,后执行
|
||||
```
|
||||
|
||||
## 运行时环境装饰器
|
||||
|
||||
对于网络游戏,你可以使用装饰器来控制系统方法在哪个环境下执行。
|
||||
|
||||
### 可用装饰器
|
||||
|
||||
| 装饰器 | 效果 |
|
||||
|--------|------|
|
||||
| `@ServerOnly()` | 方法仅在服务端执行 |
|
||||
| `@ClientOnly()` | 方法仅在客户端执行 |
|
||||
| `@NotServer()` | 方法在服务端跳过 |
|
||||
| `@NotClient()` | 方法在客户端跳过 |
|
||||
|
||||
### 使用示例
|
||||
|
||||
```typescript
|
||||
import { EntitySystem, ServerOnly, ClientOnly } from '@esengine/ecs-framework';
|
||||
|
||||
class GameSystem extends EntitySystem {
|
||||
@ServerOnly()
|
||||
private spawnEnemies(): void {
|
||||
// 仅在服务端运行 - 权威生成逻辑
|
||||
}
|
||||
|
||||
@ClientOnly()
|
||||
private playEffects(): void {
|
||||
// 仅在客户端运行 - 视觉效果
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 简单条件检查
|
||||
|
||||
对于简单场景,直接检查通常比装饰器更清晰:
|
||||
|
||||
```typescript
|
||||
class CollectibleSystem extends EntitySystem {
|
||||
private checkCollections(): void {
|
||||
if (!this.scene.isServer) return; // 客户端跳过
|
||||
|
||||
// 服务端权威逻辑...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
参见 [场景运行时环境](/guide/scene/index#运行时环境) 了解配置详情。
|
||||
|
||||
## 下一步
|
||||
|
||||
- [系统类型](/guide/system/types) - 了解不同类型的系统基类
|
||||
|
||||
@@ -606,6 +606,107 @@ export class RetryDecorator implements INodeExecutor {
|
||||
}
|
||||
```
|
||||
|
||||
## 在代码中使用自定义执行器
|
||||
|
||||
定义了自定义执行器后,可以通过 `BehaviorTreeBuilder` 的 `.action()` 和 `.condition()` 方法在代码中使用:
|
||||
|
||||
### 使用 action() 方法
|
||||
|
||||
```typescript
|
||||
import { BehaviorTreeBuilder, BehaviorTreeStarter } from '@esengine/behavior-tree';
|
||||
|
||||
// 使用自定义执行器构建行为树
|
||||
const tree = BehaviorTreeBuilder.create('CombatAI')
|
||||
.defineBlackboardVariable('health', 100)
|
||||
.defineBlackboardVariable('target', null)
|
||||
.selector('Root')
|
||||
.sequence('AttackSequence')
|
||||
// 使用自定义动作 - implementationType 匹配装饰器中的定义
|
||||
.action('AttackAction', 'Attack', { damage: 25 })
|
||||
.action('MoveToPosition', 'Chase', { speed: 10 })
|
||||
.end()
|
||||
.action('DelayAction', 'Idle', { duration: 1.0 })
|
||||
.end()
|
||||
.build();
|
||||
|
||||
// 启动行为树
|
||||
const entity = scene.createEntity('Enemy');
|
||||
BehaviorTreeStarter.start(entity, tree);
|
||||
```
|
||||
|
||||
### 使用 condition() 方法
|
||||
|
||||
```typescript
|
||||
const tree = BehaviorTreeBuilder.create('AI')
|
||||
.selector('Root')
|
||||
.sequence('AttackBranch')
|
||||
// 使用自定义条件
|
||||
.condition('CheckHealth', 'IsHealthy', { threshold: 50, operator: 'greater' })
|
||||
.action('AttackAction', 'Attack')
|
||||
.end()
|
||||
.end()
|
||||
.build();
|
||||
```
|
||||
|
||||
### Builder 方法对照表
|
||||
|
||||
| 方法 | 说明 | 使用场景 |
|
||||
|------|------|----------|
|
||||
| `.action(type, name?, config?)` | 使用自定义动作执行器 | 自定义 Action 类 |
|
||||
| `.condition(type, name?, config?)` | 使用自定义条件执行器 | 自定义 Condition 类 |
|
||||
| `.executeAction(name)` | 调用黑板函数 `action_{name}` | 简单逻辑、快速原型 |
|
||||
| `.executeCondition(name)` | 调用黑板函数 `condition_{name}` | 简单条件判断 |
|
||||
|
||||
### 完整示例
|
||||
|
||||
```typescript
|
||||
import {
|
||||
BehaviorTreeBuilder,
|
||||
BehaviorTreeStarter,
|
||||
NodeExecutorMetadata,
|
||||
INodeExecutor,
|
||||
NodeExecutionContext,
|
||||
TaskStatus,
|
||||
NodeType,
|
||||
BindingHelper
|
||||
} from '@esengine/behavior-tree';
|
||||
|
||||
// 1. 定义自定义执行器
|
||||
@NodeExecutorMetadata({
|
||||
implementationType: 'AttackAction',
|
||||
nodeType: NodeType.Action,
|
||||
displayName: '攻击',
|
||||
category: 'Combat',
|
||||
configSchema: {
|
||||
damage: { type: 'number', default: 10, supportBinding: true }
|
||||
}
|
||||
})
|
||||
class AttackAction implements INodeExecutor {
|
||||
execute(context: NodeExecutionContext): TaskStatus {
|
||||
const damage = BindingHelper.getValue<number>(context, 'damage', 10);
|
||||
console.log(`执行攻击,造成 ${damage} 点伤害!`);
|
||||
return TaskStatus.Success;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 构建行为树
|
||||
const enemyAI = BehaviorTreeBuilder.create('EnemyAI')
|
||||
.defineBlackboardVariable('health', 100)
|
||||
.defineBlackboardVariable('target', null)
|
||||
.selector('MainBehavior')
|
||||
.sequence('AttackBranch')
|
||||
.condition('CheckHealth', 'HasEnoughHealth', { threshold: 20, operator: 'greater' })
|
||||
.action('AttackAction', 'Attack', { damage: 50 })
|
||||
.end()
|
||||
.log('逃跑', 'Flee')
|
||||
.end()
|
||||
.build();
|
||||
|
||||
// 3. 启动行为树
|
||||
const entity = scene.createEntity('Enemy');
|
||||
BehaviorTreeStarter.start(entity, enemyAI);
|
||||
```
|
||||
|
||||
## 注册执行器
|
||||
|
||||
### 自动注册
|
||||
|
||||
136
docs/src/content/docs/modules/database-drivers/index.md
Normal file
136
docs/src/content/docs/modules/database-drivers/index.md
Normal file
@@ -0,0 +1,136 @@
|
||||
---
|
||||
title: "数据库驱动"
|
||||
description: "MongoDB、Redis 等数据库的连接管理和驱动封装"
|
||||
---
|
||||
|
||||
`@esengine/database-drivers` 是 ESEngine 的数据库连接管理层,提供 MongoDB、Redis 等数据库的统一连接管理。
|
||||
|
||||
## 特性
|
||||
|
||||
- **连接池管理** - 自动管理连接池,优化资源使用
|
||||
- **自动重连** - 连接断开时自动重连
|
||||
- **事件通知** - 连接状态变化事件
|
||||
- **类型解耦** - 简化接口,不依赖原生驱动类型
|
||||
- **共享连接** - 单一连接可供多个模块共享
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
npm install @esengine/database-drivers
|
||||
```
|
||||
|
||||
**对等依赖:**
|
||||
```bash
|
||||
npm install mongodb # MongoDB 支持
|
||||
npm install ioredis # Redis 支持
|
||||
```
|
||||
|
||||
## 架构
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ @esengine/database-drivers (Layer 1) │
|
||||
│ ┌─────────────────────┐ ┌─────────────────────┐ │
|
||||
│ │ MongoConnection │ │ RedisConnection │ │
|
||||
│ │ - 连接池管理 │ │ - 自动重连 │ │
|
||||
│ │ - 自动重连 │ │ - Key 前缀 │ │
|
||||
│ │ - 事件发射器 │ │ - 事件发射器 │ │
|
||||
│ └──────────┬──────────┘ └─────────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌──────────▼──────────┐ │
|
||||
│ │ IMongoCollection<T> │ ← 类型安全接口 │
|
||||
│ │ (适配器模式) │ 与 mongodb 类型解耦 │
|
||||
│ └─────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌───────────────────────┐ ┌───────────────────────┐
|
||||
│ @esengine/database │ │ @esengine/transaction │
|
||||
│ (仓库模式) │ │ (分布式事务) │
|
||||
└───────────────────────┘ └───────────────────────┘
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### MongoDB 连接
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
|
||||
// 创建连接
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game',
|
||||
pool: {
|
||||
minSize: 5,
|
||||
maxSize: 20
|
||||
},
|
||||
autoReconnect: true
|
||||
})
|
||||
|
||||
// 监听事件
|
||||
mongo.on('connected', () => console.log('MongoDB 已连接'))
|
||||
mongo.on('disconnected', () => console.log('MongoDB 已断开'))
|
||||
mongo.on('error', (e) => console.error('错误:', e.error))
|
||||
|
||||
// 建立连接
|
||||
await mongo.connect()
|
||||
|
||||
// 使用集合
|
||||
const users = mongo.collection<User>('users')
|
||||
await users.insertOne({ name: 'John', score: 100 })
|
||||
|
||||
const user = await users.findOne({ name: 'John' })
|
||||
|
||||
// 完成后断开连接
|
||||
await mongo.disconnect()
|
||||
```
|
||||
|
||||
### Redis 连接
|
||||
|
||||
```typescript
|
||||
import { createRedisConnection } from '@esengine/database-drivers'
|
||||
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
keyPrefix: 'game:',
|
||||
autoReconnect: true
|
||||
})
|
||||
|
||||
await redis.connect()
|
||||
|
||||
// 基本操作
|
||||
await redis.set('session:123', 'data', 3600) // 带 TTL
|
||||
const value = await redis.get('session:123')
|
||||
|
||||
await redis.disconnect()
|
||||
```
|
||||
|
||||
## 服务容器集成
|
||||
|
||||
```typescript
|
||||
import { ServiceContainer } from '@esengine/ecs-framework'
|
||||
import {
|
||||
createMongoConnection,
|
||||
MongoConnectionToken,
|
||||
RedisConnectionToken
|
||||
} from '@esengine/database-drivers'
|
||||
|
||||
const services = new ServiceContainer()
|
||||
|
||||
// 注册连接
|
||||
const mongo = createMongoConnection({ uri: '...', database: 'game' })
|
||||
await mongo.connect()
|
||||
services.register(MongoConnectionToken, mongo)
|
||||
|
||||
// 在其他模块中获取
|
||||
const connection = services.get(MongoConnectionToken)
|
||||
const users = connection.collection('users')
|
||||
```
|
||||
|
||||
## 文档
|
||||
|
||||
- [MongoDB 连接](/modules/database-drivers/mongo/) - MongoDB 连接详细配置
|
||||
- [Redis 连接](/modules/database-drivers/redis/) - Redis 连接详细配置
|
||||
- [服务令牌](/modules/database-drivers/tokens/) - 依赖注入集成
|
||||
265
docs/src/content/docs/modules/database-drivers/mongo.md
Normal file
265
docs/src/content/docs/modules/database-drivers/mongo.md
Normal file
@@ -0,0 +1,265 @@
|
||||
---
|
||||
title: "MongoDB 连接"
|
||||
description: "MongoDB 连接管理、连接池、自动重连"
|
||||
---
|
||||
|
||||
## 配置选项
|
||||
|
||||
```typescript
|
||||
interface MongoConnectionConfig {
|
||||
/** MongoDB 连接 URI */
|
||||
uri: string
|
||||
|
||||
/** 数据库名称 */
|
||||
database: string
|
||||
|
||||
/** 连接池配置 */
|
||||
pool?: {
|
||||
minSize?: number // 最小连接数
|
||||
maxSize?: number // 最大连接数
|
||||
acquireTimeout?: number // 获取连接超时(毫秒)
|
||||
maxLifetime?: number // 连接最大生命周期(毫秒)
|
||||
}
|
||||
|
||||
/** 是否自动重连(默认 true) */
|
||||
autoReconnect?: boolean
|
||||
|
||||
/** 重连间隔(毫秒,默认 5000) */
|
||||
reconnectInterval?: number
|
||||
|
||||
/** 最大重连次数(默认 10) */
|
||||
maxReconnectAttempts?: number
|
||||
}
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection, MongoConnectionToken } from '@esengine/database-drivers'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game',
|
||||
pool: {
|
||||
minSize: 5,
|
||||
maxSize: 20,
|
||||
acquireTimeout: 5000,
|
||||
maxLifetime: 300000
|
||||
},
|
||||
autoReconnect: true,
|
||||
reconnectInterval: 5000,
|
||||
maxReconnectAttempts: 10
|
||||
})
|
||||
|
||||
// 事件监听
|
||||
mongo.on('connected', () => {
|
||||
console.log('MongoDB 已连接')
|
||||
})
|
||||
|
||||
mongo.on('disconnected', () => {
|
||||
console.log('MongoDB 已断开')
|
||||
})
|
||||
|
||||
mongo.on('reconnecting', () => {
|
||||
console.log('MongoDB 正在重连...')
|
||||
})
|
||||
|
||||
mongo.on('reconnected', () => {
|
||||
console.log('MongoDB 重连成功')
|
||||
})
|
||||
|
||||
mongo.on('error', (event) => {
|
||||
console.error('MongoDB 错误:', event.error)
|
||||
})
|
||||
|
||||
// 连接
|
||||
await mongo.connect()
|
||||
|
||||
// 检查状态
|
||||
console.log('已连接:', mongo.isConnected())
|
||||
console.log('Ping:', await mongo.ping())
|
||||
```
|
||||
|
||||
## IMongoConnection 接口
|
||||
|
||||
```typescript
|
||||
interface IMongoConnection {
|
||||
/** 连接 ID */
|
||||
readonly id: string
|
||||
|
||||
/** 连接状态 */
|
||||
readonly state: ConnectionState
|
||||
|
||||
/** 建立连接 */
|
||||
connect(): Promise<void>
|
||||
|
||||
/** 断开连接 */
|
||||
disconnect(): Promise<void>
|
||||
|
||||
/** 检查是否已连接 */
|
||||
isConnected(): boolean
|
||||
|
||||
/** 测试连接 */
|
||||
ping(): Promise<boolean>
|
||||
|
||||
/** 获取类型化集合 */
|
||||
collection<T extends object>(name: string): IMongoCollection<T>
|
||||
|
||||
/** 获取数据库接口 */
|
||||
getDatabase(): IMongoDatabase
|
||||
|
||||
/** 获取原生客户端(高级用法) */
|
||||
getNativeClient(): MongoClientType
|
||||
|
||||
/** 获取原生数据库(高级用法) */
|
||||
getNativeDatabase(): Db
|
||||
}
|
||||
```
|
||||
|
||||
## IMongoCollection 接口
|
||||
|
||||
类型安全的集合接口,与原生 MongoDB 类型解耦:
|
||||
|
||||
```typescript
|
||||
interface IMongoCollection<T extends object> {
|
||||
readonly name: string
|
||||
|
||||
// 查询
|
||||
findOne(filter: object, options?: FindOptions): Promise<T | null>
|
||||
find(filter: object, options?: FindOptions): Promise<T[]>
|
||||
countDocuments(filter?: object): Promise<number>
|
||||
|
||||
// 插入
|
||||
insertOne(doc: T): Promise<InsertOneResult>
|
||||
insertMany(docs: T[]): Promise<InsertManyResult>
|
||||
|
||||
// 更新
|
||||
updateOne(filter: object, update: object): Promise<UpdateResult>
|
||||
updateMany(filter: object, update: object): Promise<UpdateResult>
|
||||
findOneAndUpdate(
|
||||
filter: object,
|
||||
update: object,
|
||||
options?: FindOneAndUpdateOptions
|
||||
): Promise<T | null>
|
||||
|
||||
// 删除
|
||||
deleteOne(filter: object): Promise<DeleteResult>
|
||||
deleteMany(filter: object): Promise<DeleteResult>
|
||||
|
||||
// 索引
|
||||
createIndex(
|
||||
spec: Record<string, 1 | -1>,
|
||||
options?: IndexOptions
|
||||
): Promise<string>
|
||||
}
|
||||
```
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 基本 CRUD
|
||||
|
||||
```typescript
|
||||
interface User {
|
||||
id: string
|
||||
name: string
|
||||
email: string
|
||||
score: number
|
||||
}
|
||||
|
||||
const users = mongo.collection<User>('users')
|
||||
|
||||
// 插入
|
||||
await users.insertOne({
|
||||
id: '1',
|
||||
name: 'John',
|
||||
email: 'john@example.com',
|
||||
score: 100
|
||||
})
|
||||
|
||||
// 查询
|
||||
const user = await users.findOne({ name: 'John' })
|
||||
|
||||
const topUsers = await users.find(
|
||||
{ score: { $gte: 100 } },
|
||||
{ sort: { score: -1 }, limit: 10 }
|
||||
)
|
||||
|
||||
// 更新
|
||||
await users.updateOne(
|
||||
{ id: '1' },
|
||||
{ $inc: { score: 10 } }
|
||||
)
|
||||
|
||||
// 删除
|
||||
await users.deleteOne({ id: '1' })
|
||||
```
|
||||
|
||||
### 批量操作
|
||||
|
||||
```typescript
|
||||
// 批量插入
|
||||
await users.insertMany([
|
||||
{ id: '1', name: 'Alice', email: 'alice@example.com', score: 100 },
|
||||
{ id: '2', name: 'Bob', email: 'bob@example.com', score: 200 },
|
||||
{ id: '3', name: 'Carol', email: 'carol@example.com', score: 150 }
|
||||
])
|
||||
|
||||
// 批量更新
|
||||
await users.updateMany(
|
||||
{ score: { $lt: 100 } },
|
||||
{ $set: { status: 'inactive' } }
|
||||
)
|
||||
|
||||
// 批量删除
|
||||
await users.deleteMany({ status: 'inactive' })
|
||||
```
|
||||
|
||||
### 索引管理
|
||||
|
||||
```typescript
|
||||
// 创建索引
|
||||
await users.createIndex({ email: 1 }, { unique: true })
|
||||
await users.createIndex({ score: -1 })
|
||||
await users.createIndex({ name: 1, score: -1 })
|
||||
```
|
||||
|
||||
## 与其他模块集成
|
||||
|
||||
### 与 @esengine/database 集成
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { UserRepository, createRepository } from '@esengine/database'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
// 使用 UserRepository
|
||||
const userRepo = new UserRepository(mongo)
|
||||
await userRepo.register({ username: 'john', password: '123456' })
|
||||
|
||||
// 使用通用仓库
|
||||
const playerRepo = createRepository<Player>(mongo, 'players')
|
||||
```
|
||||
|
||||
### 与 @esengine/transaction 集成
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { createMongoStorage, TransactionManager } from '@esengine/transaction'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
// 创建事务存储(共享连接)
|
||||
const storage = createMongoStorage(mongo)
|
||||
await storage.ensureIndexes()
|
||||
|
||||
const txManager = new TransactionManager({ storage })
|
||||
```
|
||||
228
docs/src/content/docs/modules/database-drivers/redis.md
Normal file
228
docs/src/content/docs/modules/database-drivers/redis.md
Normal file
@@ -0,0 +1,228 @@
|
||||
---
|
||||
title: "Redis 连接"
|
||||
description: "Redis 连接管理、自动重连、键前缀"
|
||||
---
|
||||
|
||||
## 配置选项
|
||||
|
||||
```typescript
|
||||
interface RedisConnectionConfig {
|
||||
/** Redis 主机 */
|
||||
host?: string
|
||||
|
||||
/** Redis 端口 */
|
||||
port?: number
|
||||
|
||||
/** 认证密码 */
|
||||
password?: string
|
||||
|
||||
/** 数据库编号 */
|
||||
db?: number
|
||||
|
||||
/** 键前缀 */
|
||||
keyPrefix?: string
|
||||
|
||||
/** 是否自动重连(默认 true) */
|
||||
autoReconnect?: boolean
|
||||
|
||||
/** 重连间隔(毫秒,默认 5000) */
|
||||
reconnectInterval?: number
|
||||
|
||||
/** 最大重连次数(默认 10) */
|
||||
maxReconnectAttempts?: number
|
||||
}
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
```typescript
|
||||
import { createRedisConnection, RedisConnectionToken } from '@esengine/database-drivers'
|
||||
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
password: 'your-password',
|
||||
db: 0,
|
||||
keyPrefix: 'game:',
|
||||
autoReconnect: true,
|
||||
reconnectInterval: 5000,
|
||||
maxReconnectAttempts: 10
|
||||
})
|
||||
|
||||
// 事件监听
|
||||
redis.on('connected', () => {
|
||||
console.log('Redis 已连接')
|
||||
})
|
||||
|
||||
redis.on('disconnected', () => {
|
||||
console.log('Redis 已断开')
|
||||
})
|
||||
|
||||
redis.on('error', (event) => {
|
||||
console.error('Redis 错误:', event.error)
|
||||
})
|
||||
|
||||
// 连接
|
||||
await redis.connect()
|
||||
|
||||
// 检查状态
|
||||
console.log('已连接:', redis.isConnected())
|
||||
console.log('Ping:', await redis.ping())
|
||||
```
|
||||
|
||||
## IRedisConnection 接口
|
||||
|
||||
```typescript
|
||||
interface IRedisConnection {
|
||||
/** 连接 ID */
|
||||
readonly id: string
|
||||
|
||||
/** 连接状态 */
|
||||
readonly state: ConnectionState
|
||||
|
||||
/** 建立连接 */
|
||||
connect(): Promise<void>
|
||||
|
||||
/** 断开连接 */
|
||||
disconnect(): Promise<void>
|
||||
|
||||
/** 检查是否已连接 */
|
||||
isConnected(): boolean
|
||||
|
||||
/** 测试连接 */
|
||||
ping(): Promise<boolean>
|
||||
|
||||
/** 获取值 */
|
||||
get(key: string): Promise<string | null>
|
||||
|
||||
/** 设置值(可选 TTL,单位秒) */
|
||||
set(key: string, value: string, ttl?: number): Promise<void>
|
||||
|
||||
/** 删除键 */
|
||||
del(key: string): Promise<boolean>
|
||||
|
||||
/** 检查键是否存在 */
|
||||
exists(key: string): Promise<boolean>
|
||||
|
||||
/** 设置过期时间(秒) */
|
||||
expire(key: string, seconds: number): Promise<boolean>
|
||||
|
||||
/** 获取剩余过期时间(秒) */
|
||||
ttl(key: string): Promise<number>
|
||||
|
||||
/** 获取原生客户端(高级用法) */
|
||||
getNativeClient(): Redis
|
||||
}
|
||||
```
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 基本操作
|
||||
|
||||
```typescript
|
||||
// 设置值
|
||||
await redis.set('user:1:name', 'John')
|
||||
|
||||
// 设置带过期时间的值(1 小时)
|
||||
await redis.set('session:abc123', 'user-data', 3600)
|
||||
|
||||
// 获取值
|
||||
const name = await redis.get('user:1:name')
|
||||
|
||||
// 检查键是否存在
|
||||
const exists = await redis.exists('user:1:name')
|
||||
|
||||
// 删除键
|
||||
await redis.del('user:1:name')
|
||||
|
||||
// 获取剩余过期时间
|
||||
const ttl = await redis.ttl('session:abc123')
|
||||
```
|
||||
|
||||
### 键前缀
|
||||
|
||||
配置 `keyPrefix` 后,所有操作自动添加前缀:
|
||||
|
||||
```typescript
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
keyPrefix: 'game:'
|
||||
})
|
||||
|
||||
// 实际操作的键是 'game:user:1'
|
||||
await redis.set('user:1', 'data')
|
||||
|
||||
// 实际查询的键是 'game:user:1'
|
||||
const data = await redis.get('user:1')
|
||||
```
|
||||
|
||||
### 高级操作
|
||||
|
||||
使用原生客户端进行高级操作:
|
||||
|
||||
```typescript
|
||||
const client = redis.getNativeClient()
|
||||
|
||||
// 使用 Pipeline
|
||||
const pipeline = client.pipeline()
|
||||
pipeline.set('key1', 'value1')
|
||||
pipeline.set('key2', 'value2')
|
||||
pipeline.set('key3', 'value3')
|
||||
await pipeline.exec()
|
||||
|
||||
// 使用事务
|
||||
const multi = client.multi()
|
||||
multi.incr('counter')
|
||||
multi.get('counter')
|
||||
const results = await multi.exec()
|
||||
|
||||
// 使用 Lua 脚本
|
||||
const result = await client.eval(
|
||||
`return redis.call('get', KEYS[1])`,
|
||||
1,
|
||||
'mykey'
|
||||
)
|
||||
```
|
||||
|
||||
## 与事务系统集成
|
||||
|
||||
```typescript
|
||||
import { createRedisConnection } from '@esengine/database-drivers'
|
||||
import { RedisStorage, TransactionManager } from '@esengine/transaction'
|
||||
|
||||
const redis = createRedisConnection({
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
keyPrefix: 'tx:'
|
||||
})
|
||||
await redis.connect()
|
||||
|
||||
// 创建事务存储
|
||||
const storage = new RedisStorage({
|
||||
factory: () => redis.getNativeClient(),
|
||||
prefix: 'tx:'
|
||||
})
|
||||
|
||||
const txManager = new TransactionManager({ storage })
|
||||
```
|
||||
|
||||
## 连接状态
|
||||
|
||||
```typescript
|
||||
type ConnectionState =
|
||||
| 'disconnected' // 未连接
|
||||
| 'connecting' // 连接中
|
||||
| 'connected' // 已连接
|
||||
| 'disconnecting' // 断开中
|
||||
| 'error' // 错误状态
|
||||
```
|
||||
|
||||
## 事件
|
||||
|
||||
| 事件 | 描述 |
|
||||
|------|------|
|
||||
| `connected` | 连接成功 |
|
||||
| `disconnected` | 连接断开 |
|
||||
| `reconnecting` | 正在重连 |
|
||||
| `reconnected` | 重连成功 |
|
||||
| `error` | 发生错误 |
|
||||
140
docs/src/content/docs/modules/database/index.md
Normal file
140
docs/src/content/docs/modules/database/index.md
Normal file
@@ -0,0 +1,140 @@
|
||||
---
|
||||
title: "数据库仓库"
|
||||
description: "Repository 模式的数据库操作层,支持 CRUD、分页、软删除"
|
||||
---
|
||||
|
||||
`@esengine/database` 是 ESEngine 的数据库操作层,基于 Repository 模式提供类型安全的 CRUD 操作。
|
||||
|
||||
## 特性
|
||||
|
||||
- **Repository 模式** - 泛型 CRUD 操作,类型安全
|
||||
- **分页查询** - 内置分页支持
|
||||
- **软删除** - 可选的软删除与恢复
|
||||
- **用户管理** - 开箱即用的 UserRepository
|
||||
- **密码安全** - 使用 scrypt 的密码哈希工具
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
npm install @esengine/database @esengine/database-drivers
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 基本仓库
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { Repository, createRepository } from '@esengine/database'
|
||||
|
||||
// 定义实体
|
||||
interface Player {
|
||||
id: string
|
||||
name: string
|
||||
score: number
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
}
|
||||
|
||||
// 创建连接
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
// 创建仓库
|
||||
const playerRepo = createRepository<Player>(mongo, 'players')
|
||||
|
||||
// CRUD 操作
|
||||
const player = await playerRepo.create({
|
||||
name: 'John',
|
||||
score: 0
|
||||
})
|
||||
|
||||
const found = await playerRepo.findById(player.id)
|
||||
|
||||
await playerRepo.update(player.id, { score: 100 })
|
||||
|
||||
await playerRepo.delete(player.id)
|
||||
```
|
||||
|
||||
### 自定义仓库
|
||||
|
||||
```typescript
|
||||
import { Repository, BaseEntity } from '@esengine/database'
|
||||
import type { IMongoConnection } from '@esengine/database-drivers'
|
||||
|
||||
interface Player extends BaseEntity {
|
||||
name: string
|
||||
score: number
|
||||
rank?: string
|
||||
}
|
||||
|
||||
class PlayerRepository extends Repository<Player> {
|
||||
constructor(connection: IMongoConnection) {
|
||||
super(connection, 'players')
|
||||
}
|
||||
|
||||
async findTopPlayers(limit: number = 10): Promise<Player[]> {
|
||||
return this.findMany({
|
||||
sort: { score: 'desc' },
|
||||
limit
|
||||
})
|
||||
}
|
||||
|
||||
async findByRank(rank: string): Promise<Player[]> {
|
||||
return this.findMany({
|
||||
where: { rank }
|
||||
})
|
||||
}
|
||||
|
||||
async incrementScore(playerId: string, amount: number): Promise<Player | null> {
|
||||
const player = await this.findById(playerId)
|
||||
if (!player) return null
|
||||
return this.update(playerId, { score: player.score + amount })
|
||||
}
|
||||
}
|
||||
|
||||
// 使用
|
||||
const playerRepo = new PlayerRepository(mongo)
|
||||
const topPlayers = await playerRepo.findTopPlayers(5)
|
||||
```
|
||||
|
||||
### 用户仓库
|
||||
|
||||
```typescript
|
||||
import { UserRepository } from '@esengine/database'
|
||||
|
||||
const userRepo = new UserRepository(mongo)
|
||||
|
||||
// 注册新用户
|
||||
const user = await userRepo.register({
|
||||
username: 'john',
|
||||
password: 'securePassword123',
|
||||
email: 'john@example.com'
|
||||
})
|
||||
|
||||
// 认证
|
||||
const authenticated = await userRepo.authenticate('john', 'securePassword123')
|
||||
if (authenticated) {
|
||||
console.log('登录成功:', authenticated.username)
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
await userRepo.changePassword(user.id, 'securePassword123', 'newPassword456')
|
||||
|
||||
// 角色管理
|
||||
await userRepo.addRole(user.id, 'admin')
|
||||
await userRepo.removeRole(user.id, 'admin')
|
||||
|
||||
// 查询用户
|
||||
const admins = await userRepo.findByRole('admin')
|
||||
const john = await userRepo.findByUsername('john')
|
||||
```
|
||||
|
||||
## 文档
|
||||
|
||||
- [仓库 API](/modules/database/repository/) - Repository 详细 API
|
||||
- [用户管理](/modules/database/user/) - UserRepository 用法
|
||||
- [查询语法](/modules/database/query/) - 查询条件语法
|
||||
185
docs/src/content/docs/modules/database/query.md
Normal file
185
docs/src/content/docs/modules/database/query.md
Normal file
@@ -0,0 +1,185 @@
|
||||
---
|
||||
title: "查询语法"
|
||||
description: "查询条件操作符和语法"
|
||||
---
|
||||
|
||||
## 基本查询
|
||||
|
||||
### 精确匹配
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
name: 'John',
|
||||
status: 'active'
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 使用操作符
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
score: { $gte: 100 },
|
||||
rank: { $in: ['gold', 'platinum'] }
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 查询操作符
|
||||
|
||||
| 操作符 | 描述 | 示例 |
|
||||
|--------|------|------|
|
||||
| `$eq` | 等于 | `{ score: { $eq: 100 } }` |
|
||||
| `$ne` | 不等于 | `{ status: { $ne: 'banned' } }` |
|
||||
| `$gt` | 大于 | `{ score: { $gt: 50 } }` |
|
||||
| `$gte` | 大于等于 | `{ level: { $gte: 10 } }` |
|
||||
| `$lt` | 小于 | `{ age: { $lt: 18 } }` |
|
||||
| `$lte` | 小于等于 | `{ price: { $lte: 100 } }` |
|
||||
| `$in` | 在数组中 | `{ rank: { $in: ['gold', 'platinum'] } }` |
|
||||
| `$nin` | 不在数组中 | `{ status: { $nin: ['banned', 'suspended'] } }` |
|
||||
| `$like` | 模式匹配 | `{ name: { $like: '%john%' } }` |
|
||||
| `$regex` | 正则匹配 | `{ email: { $regex: '@gmail.com$' } }` |
|
||||
|
||||
## 逻辑操作符
|
||||
|
||||
### $or
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
$or: [
|
||||
{ score: { $gte: 1000 } },
|
||||
{ rank: 'legendary' }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### $and
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
$and: [
|
||||
{ score: { $gte: 100 } },
|
||||
{ score: { $lte: 500 } }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 组合使用
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
where: {
|
||||
status: 'active',
|
||||
$or: [
|
||||
{ rank: 'gold' },
|
||||
{ score: { $gte: 1000 } }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 模式匹配
|
||||
|
||||
### $like 语法
|
||||
|
||||
- `%` - 匹配任意字符序列
|
||||
- `_` - 匹配单个字符
|
||||
|
||||
```typescript
|
||||
// 以 'John' 开头
|
||||
{ name: { $like: 'John%' } }
|
||||
|
||||
// 以 'son' 结尾
|
||||
{ name: { $like: '%son' } }
|
||||
|
||||
// 包含 'oh'
|
||||
{ name: { $like: '%oh%' } }
|
||||
|
||||
// 第二个字符是 'o'
|
||||
{ name: { $like: '_o%' } }
|
||||
```
|
||||
|
||||
### $regex 语法
|
||||
|
||||
使用标准正则表达式:
|
||||
|
||||
```typescript
|
||||
// 以 'John' 开头(大小写不敏感)
|
||||
{ name: { $regex: '^john' } }
|
||||
|
||||
// Gmail 邮箱
|
||||
{ email: { $regex: '@gmail\\.com$' } }
|
||||
|
||||
// 包含数字
|
||||
{ username: { $regex: '\\d+' } }
|
||||
```
|
||||
|
||||
## 排序
|
||||
|
||||
```typescript
|
||||
await repo.findMany({
|
||||
sort: {
|
||||
score: 'desc', // 降序
|
||||
name: 'asc' // 升序
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 分页
|
||||
|
||||
### 使用 limit/offset
|
||||
|
||||
```typescript
|
||||
// 第一页
|
||||
await repo.findMany({
|
||||
limit: 20,
|
||||
offset: 0
|
||||
})
|
||||
|
||||
// 第二页
|
||||
await repo.findMany({
|
||||
limit: 20,
|
||||
offset: 20
|
||||
})
|
||||
```
|
||||
|
||||
### 使用 findPaginated
|
||||
|
||||
```typescript
|
||||
const result = await repo.findPaginated(
|
||||
{ page: 2, pageSize: 20 },
|
||||
{ sort: { createdAt: 'desc' } }
|
||||
)
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
```typescript
|
||||
// 查找活跃的金牌玩家,分数在 100-1000 之间
|
||||
// 按分数降序排列,取前 10 个
|
||||
const players = await repo.findMany({
|
||||
where: {
|
||||
status: 'active',
|
||||
rank: 'gold',
|
||||
score: { $gte: 100, $lte: 1000 }
|
||||
},
|
||||
sort: { score: 'desc' },
|
||||
limit: 10
|
||||
})
|
||||
|
||||
// 搜索用户名包含 'john' 或邮箱是 gmail 的用户
|
||||
const users = await repo.findMany({
|
||||
where: {
|
||||
$or: [
|
||||
{ username: { $like: '%john%' } },
|
||||
{ email: { $regex: '@gmail\\.com$' } }
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
244
docs/src/content/docs/modules/database/repository.md
Normal file
244
docs/src/content/docs/modules/database/repository.md
Normal file
@@ -0,0 +1,244 @@
|
||||
---
|
||||
title: "Repository API"
|
||||
description: "泛型仓库接口,CRUD 操作、分页、软删除"
|
||||
---
|
||||
|
||||
## 创建仓库
|
||||
|
||||
### 使用工厂函数
|
||||
|
||||
```typescript
|
||||
import { createRepository } from '@esengine/database'
|
||||
|
||||
const playerRepo = createRepository<Player>(mongo, 'players')
|
||||
|
||||
// 启用软删除
|
||||
const playerRepo = createRepository<Player>(mongo, 'players', true)
|
||||
```
|
||||
|
||||
### 继承 Repository
|
||||
|
||||
```typescript
|
||||
import { Repository, BaseEntity } from '@esengine/database'
|
||||
|
||||
interface Player extends BaseEntity {
|
||||
name: string
|
||||
score: number
|
||||
}
|
||||
|
||||
class PlayerRepository extends Repository<Player> {
|
||||
constructor(connection: IMongoConnection) {
|
||||
super(connection, 'players', false) // 第三个参数:启用软删除
|
||||
}
|
||||
|
||||
// 添加自定义方法
|
||||
async findTopPlayers(limit: number): Promise<Player[]> {
|
||||
return this.findMany({
|
||||
sort: { score: 'desc' },
|
||||
limit
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## BaseEntity 接口
|
||||
|
||||
所有实体必须继承 `BaseEntity`:
|
||||
|
||||
```typescript
|
||||
interface BaseEntity {
|
||||
id: string
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
deletedAt?: Date // 软删除时使用
|
||||
}
|
||||
```
|
||||
|
||||
## 查询方法
|
||||
|
||||
### findById
|
||||
|
||||
```typescript
|
||||
const player = await repo.findById('player-123')
|
||||
```
|
||||
|
||||
### findOne
|
||||
|
||||
```typescript
|
||||
const player = await repo.findOne({
|
||||
where: { name: 'John' }
|
||||
})
|
||||
|
||||
const topPlayer = await repo.findOne({
|
||||
sort: { score: 'desc' }
|
||||
})
|
||||
```
|
||||
|
||||
### findMany
|
||||
|
||||
```typescript
|
||||
// 简单查询
|
||||
const players = await repo.findMany({
|
||||
where: { rank: 'gold' }
|
||||
})
|
||||
|
||||
// 复杂查询
|
||||
const players = await repo.findMany({
|
||||
where: {
|
||||
score: { $gte: 100 },
|
||||
rank: { $in: ['gold', 'platinum'] }
|
||||
},
|
||||
sort: { score: 'desc', name: 'asc' },
|
||||
limit: 10,
|
||||
offset: 0
|
||||
})
|
||||
```
|
||||
|
||||
### findPaginated
|
||||
|
||||
```typescript
|
||||
const result = await repo.findPaginated(
|
||||
{ page: 1, pageSize: 20 },
|
||||
{
|
||||
where: { rank: 'gold' },
|
||||
sort: { score: 'desc' }
|
||||
}
|
||||
)
|
||||
|
||||
console.log(result.data) // Player[]
|
||||
console.log(result.total) // 总数量
|
||||
console.log(result.totalPages) // 总页数
|
||||
console.log(result.hasNext) // 是否有下一页
|
||||
console.log(result.hasPrev) // 是否有上一页
|
||||
```
|
||||
|
||||
### count
|
||||
|
||||
```typescript
|
||||
const count = await repo.count({
|
||||
where: { rank: 'gold' }
|
||||
})
|
||||
```
|
||||
|
||||
### exists
|
||||
|
||||
```typescript
|
||||
const exists = await repo.exists({
|
||||
where: { email: 'john@example.com' }
|
||||
})
|
||||
```
|
||||
|
||||
## 创建方法
|
||||
|
||||
### create
|
||||
|
||||
```typescript
|
||||
const player = await repo.create({
|
||||
name: 'John',
|
||||
score: 0
|
||||
})
|
||||
// 自动生成 id, createdAt, updatedAt
|
||||
```
|
||||
|
||||
### createMany
|
||||
|
||||
```typescript
|
||||
const players = await repo.createMany([
|
||||
{ name: 'Alice', score: 100 },
|
||||
{ name: 'Bob', score: 200 },
|
||||
{ name: 'Carol', score: 150 }
|
||||
])
|
||||
```
|
||||
|
||||
## 更新方法
|
||||
|
||||
### update
|
||||
|
||||
```typescript
|
||||
const updated = await repo.update('player-123', {
|
||||
score: 200,
|
||||
rank: 'gold'
|
||||
})
|
||||
// 自动更新 updatedAt
|
||||
```
|
||||
|
||||
## 删除方法
|
||||
|
||||
### delete
|
||||
|
||||
```typescript
|
||||
// 普通删除
|
||||
await repo.delete('player-123')
|
||||
|
||||
// 软删除(如果启用)
|
||||
// 实际是设置 deletedAt 字段
|
||||
```
|
||||
|
||||
### deleteMany
|
||||
|
||||
```typescript
|
||||
const count = await repo.deleteMany({
|
||||
where: { score: { $lt: 10 } }
|
||||
})
|
||||
```
|
||||
|
||||
## 软删除
|
||||
|
||||
### 启用软删除
|
||||
|
||||
```typescript
|
||||
const repo = createRepository<Player>(mongo, 'players', true)
|
||||
```
|
||||
|
||||
### 查询行为
|
||||
|
||||
```typescript
|
||||
// 默认排除软删除记录
|
||||
const players = await repo.findMany()
|
||||
|
||||
// 包含软删除记录
|
||||
const allPlayers = await repo.findMany({
|
||||
includeSoftDeleted: true
|
||||
})
|
||||
```
|
||||
|
||||
### 恢复记录
|
||||
|
||||
```typescript
|
||||
await repo.restore('player-123')
|
||||
```
|
||||
|
||||
## QueryOptions
|
||||
|
||||
```typescript
|
||||
interface QueryOptions<T> {
|
||||
/** 查询条件 */
|
||||
where?: WhereCondition<T>
|
||||
|
||||
/** 排序 */
|
||||
sort?: Partial<Record<keyof T, 'asc' | 'desc'>>
|
||||
|
||||
/** 限制数量 */
|
||||
limit?: number
|
||||
|
||||
/** 偏移量 */
|
||||
offset?: number
|
||||
|
||||
/** 包含软删除记录(仅在启用软删除时有效) */
|
||||
includeSoftDeleted?: boolean
|
||||
}
|
||||
```
|
||||
|
||||
## PaginatedResult
|
||||
|
||||
```typescript
|
||||
interface PaginatedResult<T> {
|
||||
data: T[]
|
||||
total: number
|
||||
page: number
|
||||
pageSize: number
|
||||
totalPages: number
|
||||
hasNext: boolean
|
||||
hasPrev: boolean
|
||||
}
|
||||
```
|
||||
277
docs/src/content/docs/modules/database/user.md
Normal file
277
docs/src/content/docs/modules/database/user.md
Normal file
@@ -0,0 +1,277 @@
|
||||
---
|
||||
title: "用户管理"
|
||||
description: "UserRepository 用户注册、认证、角色管理"
|
||||
---
|
||||
|
||||
## 概述
|
||||
|
||||
`UserRepository` 提供开箱即用的用户管理功能:
|
||||
|
||||
- 用户注册与认证
|
||||
- 密码哈希(使用 scrypt)
|
||||
- 角色管理
|
||||
- 账户状态管理
|
||||
|
||||
## 快速开始
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers'
|
||||
import { UserRepository } from '@esengine/database'
|
||||
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
})
|
||||
await mongo.connect()
|
||||
|
||||
const userRepo = new UserRepository(mongo)
|
||||
```
|
||||
|
||||
## 用户注册
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.register({
|
||||
username: 'john',
|
||||
password: 'securePassword123',
|
||||
email: 'john@example.com', // 可选
|
||||
displayName: 'John Doe', // 可选
|
||||
roles: ['player'] // 可选,默认 []
|
||||
})
|
||||
|
||||
console.log(user)
|
||||
// {
|
||||
// id: 'uuid-...',
|
||||
// username: 'john',
|
||||
// email: 'john@example.com',
|
||||
// displayName: 'John Doe',
|
||||
// roles: ['player'],
|
||||
// status: 'active',
|
||||
// createdAt: Date,
|
||||
// updatedAt: Date
|
||||
// }
|
||||
```
|
||||
|
||||
**注意**:`register` 返回的 `SafeUser` 不包含密码哈希。
|
||||
|
||||
## 用户认证
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.authenticate('john', 'securePassword123')
|
||||
|
||||
if (user) {
|
||||
console.log('登录成功:', user.username)
|
||||
} else {
|
||||
console.log('用户名或密码错误')
|
||||
}
|
||||
```
|
||||
|
||||
## 密码管理
|
||||
|
||||
### 修改密码
|
||||
|
||||
```typescript
|
||||
const success = await userRepo.changePassword(
|
||||
userId,
|
||||
'oldPassword123',
|
||||
'newPassword456'
|
||||
)
|
||||
|
||||
if (success) {
|
||||
console.log('密码修改成功')
|
||||
} else {
|
||||
console.log('原密码错误')
|
||||
}
|
||||
```
|
||||
|
||||
### 重置密码
|
||||
|
||||
```typescript
|
||||
// 管理员直接重置密码
|
||||
const success = await userRepo.resetPassword(userId, 'newPassword123')
|
||||
```
|
||||
|
||||
## 角色管理
|
||||
|
||||
### 添加角色
|
||||
|
||||
```typescript
|
||||
await userRepo.addRole(userId, 'admin')
|
||||
await userRepo.addRole(userId, 'moderator')
|
||||
```
|
||||
|
||||
### 移除角色
|
||||
|
||||
```typescript
|
||||
await userRepo.removeRole(userId, 'moderator')
|
||||
```
|
||||
|
||||
### 查询角色
|
||||
|
||||
```typescript
|
||||
// 查找所有管理员
|
||||
const admins = await userRepo.findByRole('admin')
|
||||
|
||||
// 检查用户是否有某角色
|
||||
const user = await userRepo.findById(userId)
|
||||
const isAdmin = user?.roles.includes('admin')
|
||||
```
|
||||
|
||||
## 查询用户
|
||||
|
||||
### 按用户名查找
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.findByUsername('john')
|
||||
```
|
||||
|
||||
### 按邮箱查找
|
||||
|
||||
```typescript
|
||||
const user = await userRepo.findByEmail('john@example.com')
|
||||
```
|
||||
|
||||
### 按角色查找
|
||||
|
||||
```typescript
|
||||
const admins = await userRepo.findByRole('admin')
|
||||
```
|
||||
|
||||
### 使用继承的方法
|
||||
|
||||
```typescript
|
||||
// 分页查询
|
||||
const result = await userRepo.findPaginated(
|
||||
{ page: 1, pageSize: 20 },
|
||||
{
|
||||
where: { status: 'active' },
|
||||
sort: { createdAt: 'desc' }
|
||||
}
|
||||
)
|
||||
|
||||
// 复杂查询
|
||||
const users = await userRepo.findMany({
|
||||
where: {
|
||||
status: 'active',
|
||||
roles: { $in: ['admin', 'moderator'] }
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 账户状态
|
||||
|
||||
```typescript
|
||||
type UserStatus = 'active' | 'inactive' | 'banned' | 'suspended'
|
||||
```
|
||||
|
||||
### 更新状态
|
||||
|
||||
```typescript
|
||||
await userRepo.update(userId, { status: 'banned' })
|
||||
```
|
||||
|
||||
### 查询特定状态
|
||||
|
||||
```typescript
|
||||
const activeUsers = await userRepo.findMany({
|
||||
where: { status: 'active' }
|
||||
})
|
||||
|
||||
const bannedUsers = await userRepo.findMany({
|
||||
where: { status: 'banned' }
|
||||
})
|
||||
```
|
||||
|
||||
## 类型定义
|
||||
|
||||
### UserEntity
|
||||
|
||||
```typescript
|
||||
interface UserEntity extends BaseEntity {
|
||||
username: string
|
||||
passwordHash: string
|
||||
email?: string
|
||||
displayName?: string
|
||||
roles: string[]
|
||||
status: UserStatus
|
||||
lastLoginAt?: Date
|
||||
}
|
||||
```
|
||||
|
||||
### SafeUser
|
||||
|
||||
```typescript
|
||||
type SafeUser = Omit<UserEntity, 'passwordHash'>
|
||||
```
|
||||
|
||||
### CreateUserParams
|
||||
|
||||
```typescript
|
||||
interface CreateUserParams {
|
||||
username: string
|
||||
password: string
|
||||
email?: string
|
||||
displayName?: string
|
||||
roles?: string[]
|
||||
}
|
||||
```
|
||||
|
||||
## 密码工具
|
||||
|
||||
独立的密码工具函数:
|
||||
|
||||
```typescript
|
||||
import { hashPassword, verifyPassword } from '@esengine/database'
|
||||
|
||||
// 哈希密码
|
||||
const hash = await hashPassword('myPassword123')
|
||||
|
||||
// 验证密码
|
||||
const isValid = await verifyPassword('myPassword123', hash)
|
||||
```
|
||||
|
||||
### 安全说明
|
||||
|
||||
- 使用 Node.js 内置的 `scrypt` 算法
|
||||
- 自动生成随机盐值
|
||||
- 默认使用安全的迭代参数
|
||||
- 哈希格式:`salt:hash`(均为 hex 编码)
|
||||
|
||||
## 扩展 UserRepository
|
||||
|
||||
```typescript
|
||||
import { UserRepository, UserEntity } from '@esengine/database'
|
||||
|
||||
interface GameUser extends UserEntity {
|
||||
level: number
|
||||
experience: number
|
||||
coins: number
|
||||
}
|
||||
|
||||
class GameUserRepository extends UserRepository {
|
||||
// 重写集合名
|
||||
constructor(connection: IMongoConnection) {
|
||||
super(connection, 'game_users')
|
||||
}
|
||||
|
||||
// 添加游戏相关方法
|
||||
async addExperience(userId: string, amount: number): Promise<GameUser | null> {
|
||||
const user = await this.findById(userId) as GameUser | null
|
||||
if (!user) return null
|
||||
|
||||
const newExp = user.experience + amount
|
||||
const newLevel = Math.floor(newExp / 1000) + 1
|
||||
|
||||
return this.update(userId, {
|
||||
experience: newExp,
|
||||
level: newLevel
|
||||
}) as Promise<GameUser | null>
|
||||
}
|
||||
|
||||
async findTopPlayers(limit: number = 10): Promise<GameUser[]> {
|
||||
return this.findMany({
|
||||
sort: { level: 'desc', experience: 'desc' },
|
||||
limit
|
||||
}) as Promise<GameUser[]>
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -35,6 +35,14 @@ ESEngine 提供了丰富的功能模块,可以按需引入到你的项目中
|
||||
| 模块 | 包名 | 描述 |
|
||||
|------|------|------|
|
||||
| [网络同步](/modules/network/) | `@esengine/network` | 多人游戏网络同步 |
|
||||
| [事务系统](/modules/transaction/) | `@esengine/transaction` | 游戏事务处理,支持分布式事务 |
|
||||
|
||||
### 数据库模块
|
||||
|
||||
| 模块 | 包名 | 描述 |
|
||||
|------|------|------|
|
||||
| [数据库驱动](/modules/database-drivers/) | `@esengine/database-drivers` | MongoDB、Redis 连接管理 |
|
||||
| [数据库仓库](/modules/database/) | `@esengine/database` | Repository 模式数据操作 |
|
||||
|
||||
## 安装
|
||||
|
||||
|
||||
855
docs/src/content/docs/modules/network/auth.md
Normal file
855
docs/src/content/docs/modules/network/auth.md
Normal file
@@ -0,0 +1,855 @@
|
||||
---
|
||||
title: "认证系统"
|
||||
description: "使用 JWT 和 Session 提供者为游戏服务器添加认证功能"
|
||||
---
|
||||
|
||||
`@esengine/server` 包内置了可插拔的认证系统,支持 JWT、会话认证和自定义提供者。
|
||||
|
||||
## 安装
|
||||
|
||||
认证功能已包含在 server 包中:
|
||||
|
||||
```bash
|
||||
npm install @esengine/server jsonwebtoken
|
||||
```
|
||||
|
||||
> 注意:`jsonwebtoken` 是可选的 peer dependency,仅在使用 JWT 认证时需要。
|
||||
|
||||
## 快速开始
|
||||
|
||||
### JWT 认证
|
||||
|
||||
```typescript
|
||||
import { createServer } from '@esengine/server'
|
||||
import { withAuth, createJwtAuthProvider, withRoomAuth, requireAuth } from '@esengine/server/auth'
|
||||
|
||||
// 创建 JWT 提供者
|
||||
const jwtProvider = createJwtAuthProvider({
|
||||
secret: process.env.JWT_SECRET!,
|
||||
expiresIn: 3600, // 1 小时
|
||||
})
|
||||
|
||||
// 用认证包装服务器
|
||||
const server = withAuth(await createServer({ port: 3000 }), {
|
||||
provider: jwtProvider,
|
||||
extractCredentials: (req) => {
|
||||
const url = new URL(req.url ?? '', 'http://localhost')
|
||||
return url.searchParams.get('token')
|
||||
},
|
||||
})
|
||||
|
||||
// 定义需要认证的房间
|
||||
class GameRoom extends withRoomAuth(Room, { requireAuth: true }) {
|
||||
onJoin(player) {
|
||||
console.log(`${player.user?.name} 加入了游戏!`)
|
||||
}
|
||||
}
|
||||
|
||||
server.define('game', GameRoom)
|
||||
await server.start()
|
||||
```
|
||||
|
||||
## 认证提供者
|
||||
|
||||
### JWT 提供者
|
||||
|
||||
使用 JSON Web Tokens 实现无状态认证:
|
||||
|
||||
```typescript
|
||||
import { createJwtAuthProvider } from '@esengine/server/auth'
|
||||
|
||||
const jwtProvider = createJwtAuthProvider({
|
||||
// 必填:密钥
|
||||
secret: 'your-secret-key',
|
||||
|
||||
// 可选:算法(默认:HS256)
|
||||
algorithm: 'HS256',
|
||||
|
||||
// 可选:过期时间(秒,默认:3600)
|
||||
expiresIn: 3600,
|
||||
|
||||
// 可选:签发者(用于验证)
|
||||
issuer: 'my-game-server',
|
||||
|
||||
// 可选:受众(用于验证)
|
||||
audience: 'my-game-client',
|
||||
|
||||
// 可选:自定义用户提取
|
||||
getUser: async (payload) => {
|
||||
// 从数据库获取用户
|
||||
return await db.users.findById(payload.sub)
|
||||
},
|
||||
})
|
||||
|
||||
// 签发令牌(用于登录接口)
|
||||
const token = jwtProvider.sign({
|
||||
sub: user.id,
|
||||
name: user.name,
|
||||
roles: ['player'],
|
||||
})
|
||||
|
||||
// 解码但不验证(用于调试)
|
||||
const payload = jwtProvider.decode(token)
|
||||
```
|
||||
|
||||
### 自定义提供者
|
||||
|
||||
你可以通过实现 `IAuthProvider` 接口来创建自定义认证提供者,以集成任何认证系统(如 OAuth、LDAP、自定义数据库认证等)。
|
||||
|
||||
#### IAuthProvider 接口
|
||||
|
||||
```typescript
|
||||
interface IAuthProvider<TUser = unknown, TCredentials = unknown> {
|
||||
/** 提供者名称 */
|
||||
readonly name: string;
|
||||
|
||||
/** 验证凭证 */
|
||||
verify(credentials: TCredentials): Promise<AuthResult<TUser>>;
|
||||
|
||||
/** 刷新令牌(可选) */
|
||||
refresh?(token: string): Promise<AuthResult<TUser>>;
|
||||
|
||||
/** 撤销令牌(可选) */
|
||||
revoke?(token: string): Promise<boolean>;
|
||||
}
|
||||
|
||||
interface AuthResult<TUser> {
|
||||
success: boolean;
|
||||
user?: TUser;
|
||||
error?: string;
|
||||
errorCode?: AuthErrorCode;
|
||||
token?: string;
|
||||
expiresAt?: number;
|
||||
}
|
||||
|
||||
type AuthErrorCode =
|
||||
| 'INVALID_CREDENTIALS'
|
||||
| 'EXPIRED_TOKEN'
|
||||
| 'INVALID_TOKEN'
|
||||
| 'USER_NOT_FOUND'
|
||||
| 'ACCOUNT_DISABLED'
|
||||
| 'RATE_LIMITED'
|
||||
| 'INSUFFICIENT_PERMISSIONS';
|
||||
```
|
||||
|
||||
#### 自定义提供者示例
|
||||
|
||||
**示例 1:数据库密码认证**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
username: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
interface PasswordCredentials {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
class DatabaseAuthProvider implements IAuthProvider<User, PasswordCredentials> {
|
||||
readonly name = 'database'
|
||||
|
||||
async verify(credentials: PasswordCredentials): Promise<AuthResult<User>> {
|
||||
const { username, password } = credentials
|
||||
|
||||
// 从数据库查询用户
|
||||
const user = await db.users.findByUsername(username)
|
||||
if (!user) {
|
||||
return {
|
||||
success: false,
|
||||
error: '用户不存在',
|
||||
errorCode: 'USER_NOT_FOUND'
|
||||
}
|
||||
}
|
||||
|
||||
// 验证密码(使用 bcrypt 等库)
|
||||
const isValid = await bcrypt.compare(password, user.passwordHash)
|
||||
if (!isValid) {
|
||||
return {
|
||||
success: false,
|
||||
error: '密码错误',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
|
||||
// 检查账号状态
|
||||
if (user.disabled) {
|
||||
return {
|
||||
success: false,
|
||||
error: '账号已禁用',
|
||||
errorCode: 'ACCOUNT_DISABLED'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
roles: user.roles
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**示例 2:OAuth/第三方认证**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface OAuthUser {
|
||||
id: string
|
||||
email: string
|
||||
name: string
|
||||
provider: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
interface OAuthCredentials {
|
||||
provider: 'google' | 'github' | 'discord'
|
||||
accessToken: string
|
||||
}
|
||||
|
||||
class OAuthProvider implements IAuthProvider<OAuthUser, OAuthCredentials> {
|
||||
readonly name = 'oauth'
|
||||
|
||||
async verify(credentials: OAuthCredentials): Promise<AuthResult<OAuthUser>> {
|
||||
const { provider, accessToken } = credentials
|
||||
|
||||
try {
|
||||
// 根据提供商验证 token
|
||||
const profile = await this.fetchUserProfile(provider, accessToken)
|
||||
|
||||
// 查找或创建本地用户
|
||||
let user = await db.users.findByOAuth(provider, profile.id)
|
||||
if (!user) {
|
||||
user = await db.users.create({
|
||||
oauthProvider: provider,
|
||||
oauthId: profile.id,
|
||||
email: profile.email,
|
||||
name: profile.name,
|
||||
roles: ['player']
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
provider,
|
||||
roles: user.roles
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'OAuth 验证失败',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchUserProfile(provider: string, token: string) {
|
||||
switch (provider) {
|
||||
case 'google':
|
||||
return fetch('https://www.googleapis.com/oauth2/v2/userinfo', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
}).then(r => r.json())
|
||||
case 'github':
|
||||
return fetch('https://api.github.com/user', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
}).then(r => r.json())
|
||||
// 其他提供商...
|
||||
default:
|
||||
throw new Error(`不支持的提供商: ${provider}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**示例 3:API Key 认证**
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface ApiUser {
|
||||
id: string
|
||||
name: string
|
||||
roles: string[]
|
||||
rateLimit: number
|
||||
}
|
||||
|
||||
class ApiKeyAuthProvider implements IAuthProvider<ApiUser, string> {
|
||||
readonly name = 'api-key'
|
||||
|
||||
private revokedKeys = new Set<string>()
|
||||
|
||||
async verify(apiKey: string): Promise<AuthResult<ApiUser>> {
|
||||
if (!apiKey || !apiKey.startsWith('sk_')) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key 格式无效',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
if (this.revokedKeys.has(apiKey)) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key 已被撤销',
|
||||
errorCode: 'INVALID_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
// 从数据库查询 API Key
|
||||
const keyData = await db.apiKeys.findByKey(apiKey)
|
||||
if (!keyData) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key 不存在',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
|
||||
// 检查过期
|
||||
if (keyData.expiresAt && keyData.expiresAt < Date.now()) {
|
||||
return {
|
||||
success: false,
|
||||
error: 'API Key 已过期',
|
||||
errorCode: 'EXPIRED_TOKEN'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: keyData.userId,
|
||||
name: keyData.name,
|
||||
roles: keyData.roles,
|
||||
rateLimit: keyData.rateLimit
|
||||
},
|
||||
expiresAt: keyData.expiresAt
|
||||
}
|
||||
}
|
||||
|
||||
async revoke(apiKey: string): Promise<boolean> {
|
||||
this.revokedKeys.add(apiKey)
|
||||
await db.apiKeys.revoke(apiKey)
|
||||
return true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 使用自定义提供者
|
||||
|
||||
```typescript
|
||||
import { createServer } from '@esengine/server'
|
||||
import { withAuth } from '@esengine/server/auth'
|
||||
|
||||
// 创建自定义提供者
|
||||
const dbAuthProvider = new DatabaseAuthProvider()
|
||||
|
||||
// 或使用 OAuth 提供者
|
||||
const oauthProvider = new OAuthProvider()
|
||||
|
||||
// 使用自定义提供者
|
||||
const server = withAuth(await createServer({ port: 3000 }), {
|
||||
provider: dbAuthProvider, // 或 oauthProvider
|
||||
|
||||
// 从 WebSocket 连接请求中提取凭证
|
||||
extractCredentials: (req) => {
|
||||
const url = new URL(req.url, 'http://localhost')
|
||||
|
||||
// 对于数据库认证:从查询参数获取
|
||||
const username = url.searchParams.get('username')
|
||||
const password = url.searchParams.get('password')
|
||||
if (username && password) {
|
||||
return { username, password }
|
||||
}
|
||||
|
||||
// 对于 OAuth:从 token 参数获取
|
||||
const provider = url.searchParams.get('provider')
|
||||
const accessToken = url.searchParams.get('access_token')
|
||||
if (provider && accessToken) {
|
||||
return { provider, accessToken }
|
||||
}
|
||||
|
||||
// 对于 API Key:从请求头获取
|
||||
const apiKey = req.headers['x-api-key']
|
||||
if (apiKey) {
|
||||
return apiKey as string
|
||||
}
|
||||
|
||||
return null
|
||||
},
|
||||
|
||||
onAuthFailure: (conn, error) => {
|
||||
console.log(`认证失败: ${error.errorCode} - ${error.error}`)
|
||||
}
|
||||
})
|
||||
|
||||
await server.start()
|
||||
```
|
||||
|
||||
#### 组合多个提供者
|
||||
|
||||
你可以创建一个复合提供者来支持多种认证方式:
|
||||
|
||||
```typescript
|
||||
import type { IAuthProvider, AuthResult } from '@esengine/server/auth'
|
||||
|
||||
interface MultiAuthCredentials {
|
||||
type: 'jwt' | 'oauth' | 'apikey' | 'password'
|
||||
data: unknown
|
||||
}
|
||||
|
||||
class MultiAuthProvider implements IAuthProvider<User, MultiAuthCredentials> {
|
||||
readonly name = 'multi'
|
||||
|
||||
constructor(
|
||||
private jwtProvider: JwtAuthProvider<User>,
|
||||
private oauthProvider: OAuthProvider,
|
||||
private apiKeyProvider: ApiKeyAuthProvider,
|
||||
private dbProvider: DatabaseAuthProvider
|
||||
) {}
|
||||
|
||||
async verify(credentials: MultiAuthCredentials): Promise<AuthResult<User>> {
|
||||
switch (credentials.type) {
|
||||
case 'jwt':
|
||||
return this.jwtProvider.verify(credentials.data as string)
|
||||
case 'oauth':
|
||||
return this.oauthProvider.verify(credentials.data as OAuthCredentials)
|
||||
case 'apikey':
|
||||
return this.apiKeyProvider.verify(credentials.data as string)
|
||||
case 'password':
|
||||
return this.dbProvider.verify(credentials.data as PasswordCredentials)
|
||||
default:
|
||||
return {
|
||||
success: false,
|
||||
error: '不支持的认证类型',
|
||||
errorCode: 'INVALID_CREDENTIALS'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Session 提供者
|
||||
|
||||
使用服务端会话实现有状态认证:
|
||||
|
||||
```typescript
|
||||
import { createSessionAuthProvider, type ISessionStorage } from '@esengine/server/auth'
|
||||
|
||||
// 自定义存储实现
|
||||
const storage: ISessionStorage = {
|
||||
async get<T>(key: string): Promise<T | null> {
|
||||
return await redis.get(key)
|
||||
},
|
||||
async set<T>(key: string, value: T): Promise<void> {
|
||||
await redis.set(key, value)
|
||||
},
|
||||
async delete(key: string): Promise<boolean> {
|
||||
return await redis.del(key) > 0
|
||||
},
|
||||
}
|
||||
|
||||
const sessionProvider = createSessionAuthProvider({
|
||||
storage,
|
||||
sessionTTL: 86400000, // 24 小时(毫秒)
|
||||
|
||||
// 可选:每次请求时验证用户
|
||||
validateUser: (user) => !user.banned,
|
||||
})
|
||||
|
||||
// 创建会话(用于登录接口)
|
||||
const sessionId = await sessionProvider.createSession(user, {
|
||||
ipAddress: req.ip,
|
||||
userAgent: req.headers['user-agent'],
|
||||
})
|
||||
|
||||
// 撤销会话(用于登出)
|
||||
await sessionProvider.revoke(sessionId)
|
||||
```
|
||||
|
||||
## 服务器认证 Mixin
|
||||
|
||||
`withAuth` 函数用于包装服务器添加认证功能:
|
||||
|
||||
```typescript
|
||||
import { withAuth } from '@esengine/server/auth'
|
||||
|
||||
const server = withAuth(baseServer, {
|
||||
// 必填:认证提供者
|
||||
provider: jwtProvider,
|
||||
|
||||
// 必填:从请求中提取凭证
|
||||
extractCredentials: (req) => {
|
||||
// 从查询字符串获取
|
||||
return new URL(req.url, 'http://localhost').searchParams.get('token')
|
||||
|
||||
// 或从请求头获取
|
||||
// return req.headers['authorization']?.replace('Bearer ', '')
|
||||
},
|
||||
|
||||
// 可选:处理认证失败
|
||||
onAuthFailed: (conn, error) => {
|
||||
console.log(`认证失败: ${error}`)
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
### 访问认证上下文
|
||||
|
||||
认证后,可以从连接获取认证上下文:
|
||||
|
||||
```typescript
|
||||
import { getAuthContext } from '@esengine/server/auth'
|
||||
|
||||
server.onConnect = (conn) => {
|
||||
const auth = getAuthContext(conn)
|
||||
|
||||
if (auth.isAuthenticated) {
|
||||
console.log(`用户 ${auth.userId} 已连接`)
|
||||
console.log(`角色: ${auth.roles}`)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 房间认证 Mixin
|
||||
|
||||
`withRoomAuth` 函数为房间添加认证检查:
|
||||
|
||||
```typescript
|
||||
import { withRoomAuth, type AuthPlayer } from '@esengine/server/auth'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
name: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
class GameRoom extends withRoomAuth<User>(Room, {
|
||||
// 要求认证才能加入
|
||||
requireAuth: true,
|
||||
|
||||
// 可选:要求特定角色
|
||||
allowedRoles: ['player', 'premium'],
|
||||
|
||||
// 可选:角色检查模式('any' 或 'all')
|
||||
roleCheckMode: 'any',
|
||||
}) {
|
||||
// player 拥有 .auth 和 .user 属性
|
||||
onJoin(player: AuthPlayer<User>) {
|
||||
console.log(`${player.user?.name} 加入了`)
|
||||
console.log(`是否高级会员: ${player.auth.hasRole('premium')}`)
|
||||
}
|
||||
|
||||
// 可选:自定义认证验证
|
||||
async onAuth(player: AuthPlayer<User>): Promise<boolean> {
|
||||
// 额外的验证逻辑
|
||||
if (player.auth.hasRole('banned')) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@onMessage('Chat')
|
||||
handleChat(data: { text: string }, player: AuthPlayer<User>) {
|
||||
this.broadcast('Chat', {
|
||||
from: player.user?.name ?? '访客',
|
||||
text: data.text,
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### AuthPlayer 接口
|
||||
|
||||
认证房间中的玩家拥有额外属性:
|
||||
|
||||
```typescript
|
||||
interface AuthPlayer<TUser> extends Player {
|
||||
// 完整认证上下文
|
||||
readonly auth: IAuthContext<TUser>
|
||||
|
||||
// 用户信息(auth.user 的快捷方式)
|
||||
readonly user: TUser | null
|
||||
}
|
||||
```
|
||||
|
||||
### 房间认证辅助方法
|
||||
|
||||
```typescript
|
||||
class GameRoom extends withRoomAuth<User>(Room) {
|
||||
someMethod() {
|
||||
// 通过用户 ID 获取玩家
|
||||
const player = this.getPlayerByUserId('user-123')
|
||||
|
||||
// 获取拥有特定角色的所有玩家
|
||||
const admins = this.getPlayersByRole('admin')
|
||||
|
||||
// 获取带认证信息的玩家
|
||||
const authPlayer = this.getAuthPlayer(playerId)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 认证装饰器
|
||||
|
||||
### @requireAuth
|
||||
|
||||
标记消息处理器需要认证:
|
||||
|
||||
```typescript
|
||||
import { requireAuth, requireRole, onMessage } from '@esengine/server/auth'
|
||||
|
||||
class GameRoom extends withRoomAuth(Room) {
|
||||
@requireAuth()
|
||||
@onMessage('Trade')
|
||||
handleTrade(data: TradeData, player: AuthPlayer) {
|
||||
// 只有已认证玩家才能交易
|
||||
}
|
||||
|
||||
@requireAuth({ allowGuest: true })
|
||||
@onMessage('Chat')
|
||||
handleChat(data: ChatData, player: AuthPlayer) {
|
||||
// 访客也可以聊天
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### @requireRole
|
||||
|
||||
要求特定角色才能访问消息处理器:
|
||||
|
||||
```typescript
|
||||
class AdminRoom extends withRoomAuth(Room) {
|
||||
@requireRole('admin')
|
||||
@onMessage('Ban')
|
||||
handleBan(data: BanData, player: AuthPlayer) {
|
||||
// 只有管理员才能封禁
|
||||
}
|
||||
|
||||
@requireRole(['moderator', 'admin'])
|
||||
@onMessage('Mute')
|
||||
handleMute(data: MuteData, player: AuthPlayer) {
|
||||
// 版主或管理员可以禁言
|
||||
}
|
||||
|
||||
@requireRole(['verified', 'premium'], { mode: 'all' })
|
||||
@onMessage('SpecialFeature')
|
||||
handleSpecial(data: any, player: AuthPlayer) {
|
||||
// 需要同时拥有 verified 和 premium 角色
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 认证上下文 API
|
||||
|
||||
认证上下文提供多种检查认证状态的方法:
|
||||
|
||||
```typescript
|
||||
interface IAuthContext<TUser> {
|
||||
// 认证状态
|
||||
readonly isAuthenticated: boolean
|
||||
readonly user: TUser | null
|
||||
readonly userId: string | null
|
||||
readonly roles: ReadonlyArray<string>
|
||||
readonly authenticatedAt: number | null
|
||||
readonly expiresAt: number | null
|
||||
|
||||
// 角色检查
|
||||
hasRole(role: string): boolean
|
||||
hasAnyRole(roles: string[]): boolean
|
||||
hasAllRoles(roles: string[]): boolean
|
||||
}
|
||||
```
|
||||
|
||||
`AuthContext` 类(实现类)还提供:
|
||||
|
||||
```typescript
|
||||
class AuthContext<TUser> implements IAuthContext<TUser> {
|
||||
// 从认证结果设置认证状态
|
||||
setAuthenticated(result: AuthResult<TUser>): void
|
||||
|
||||
// 清除认证状态
|
||||
clear(): void
|
||||
}
|
||||
```
|
||||
|
||||
## 测试
|
||||
|
||||
使用模拟认证提供者进行单元测试:
|
||||
|
||||
```typescript
|
||||
import { createMockAuthProvider } from '@esengine/server/auth/testing'
|
||||
|
||||
// 创建带预设用户的模拟提供者
|
||||
const mockProvider = createMockAuthProvider({
|
||||
users: [
|
||||
{ id: '1', name: 'Alice', roles: ['player'] },
|
||||
{ id: '2', name: 'Bob', roles: ['admin', 'player'] },
|
||||
],
|
||||
autoCreate: true, // 为未知令牌创建用户
|
||||
})
|
||||
|
||||
// 在测试中使用
|
||||
const server = withAuth(testServer, {
|
||||
provider: mockProvider,
|
||||
extractCredentials: (req) => req.headers['x-token'],
|
||||
})
|
||||
|
||||
// 使用用户 ID 作为令牌进行验证
|
||||
const result = await mockProvider.verify('1')
|
||||
// result.user = { id: '1', name: 'Alice', roles: ['player'] }
|
||||
|
||||
// 动态添加/移除用户
|
||||
mockProvider.addUser({ id: '3', name: 'Charlie', roles: ['guest'] })
|
||||
mockProvider.removeUser('3')
|
||||
|
||||
// 撤销令牌
|
||||
await mockProvider.revoke('1')
|
||||
|
||||
// 重置到初始状态
|
||||
mockProvider.clear()
|
||||
```
|
||||
|
||||
## 错误处理
|
||||
|
||||
认证错误包含错误码用于程序化处理:
|
||||
|
||||
```typescript
|
||||
type AuthErrorCode =
|
||||
| 'INVALID_CREDENTIALS' // 用户名/密码无效
|
||||
| 'INVALID_TOKEN' // 令牌格式错误或无效
|
||||
| 'EXPIRED_TOKEN' // 令牌已过期
|
||||
| 'USER_NOT_FOUND' // 用户查找失败
|
||||
| 'ACCOUNT_DISABLED' // 用户账号已禁用
|
||||
| 'RATE_LIMITED' // 请求过于频繁
|
||||
| 'INSUFFICIENT_PERMISSIONS' // 权限不足
|
||||
|
||||
// 在认证失败处理器中
|
||||
const server = withAuth(baseServer, {
|
||||
provider: jwtProvider,
|
||||
extractCredentials,
|
||||
onAuthFailed: (conn, error) => {
|
||||
switch (error.errorCode) {
|
||||
case 'EXPIRED_TOKEN':
|
||||
conn.send('AuthError', { code: 'TOKEN_EXPIRED' })
|
||||
break
|
||||
case 'INVALID_TOKEN':
|
||||
conn.send('AuthError', { code: 'INVALID_TOKEN' })
|
||||
break
|
||||
default:
|
||||
conn.close()
|
||||
}
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
以下是使用 JWT 认证的完整示例:
|
||||
|
||||
```typescript
|
||||
// server.ts
|
||||
import { createServer } from '@esengine/server'
|
||||
import {
|
||||
withAuth,
|
||||
withRoomAuth,
|
||||
createJwtAuthProvider,
|
||||
requireAuth,
|
||||
requireRole,
|
||||
type AuthPlayer,
|
||||
} from '@esengine/server/auth'
|
||||
|
||||
// 类型定义
|
||||
interface User {
|
||||
id: string
|
||||
name: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
// JWT 提供者
|
||||
const jwtProvider = createJwtAuthProvider<User>({
|
||||
secret: process.env.JWT_SECRET!,
|
||||
expiresIn: 3600,
|
||||
getUser: async (payload) => ({
|
||||
id: payload.sub as string,
|
||||
name: payload.name as string,
|
||||
roles: (payload.roles as string[]) ?? [],
|
||||
}),
|
||||
})
|
||||
|
||||
// 创建带认证的服务器
|
||||
const server = withAuth(
|
||||
await createServer({ port: 3000 }),
|
||||
{
|
||||
provider: jwtProvider,
|
||||
extractCredentials: (req) => {
|
||||
return new URL(req.url ?? '', 'http://localhost')
|
||||
.searchParams.get('token')
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// 带认证的游戏房间
|
||||
class GameRoom extends withRoomAuth<User>(Room, {
|
||||
requireAuth: true,
|
||||
allowedRoles: ['player'],
|
||||
}) {
|
||||
onCreate() {
|
||||
console.log('游戏房间已创建')
|
||||
}
|
||||
|
||||
onJoin(player: AuthPlayer<User>) {
|
||||
console.log(`${player.user?.name} 加入了!`)
|
||||
this.broadcast('PlayerJoined', {
|
||||
id: player.id,
|
||||
name: player.user?.name,
|
||||
})
|
||||
}
|
||||
|
||||
@requireAuth()
|
||||
@onMessage('Move')
|
||||
handleMove(data: { x: number; y: number }, player: AuthPlayer<User>) {
|
||||
// 处理移动
|
||||
}
|
||||
|
||||
@requireRole('admin')
|
||||
@onMessage('Kick')
|
||||
handleKick(data: { playerId: string }, player: AuthPlayer<User>) {
|
||||
const target = this.getPlayer(data.playerId)
|
||||
if (target) {
|
||||
this.kick(target, '被管理员踢出')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
server.define('game', GameRoom)
|
||||
await server.start()
|
||||
```
|
||||
|
||||
## 最佳实践
|
||||
|
||||
1. **保护密钥安全**:永远不要硬编码 JWT 密钥,使用环境变量。
|
||||
|
||||
2. **设置合理的过期时间**:在安全性和用户体验之间平衡令牌 TTL。
|
||||
|
||||
3. **在关键操作上验证**:在敏感消息处理器上使用 `@requireAuth`。
|
||||
|
||||
4. **使用基于角色的访问控制**:为管理功能实现适当的角色层级。
|
||||
|
||||
5. **处理令牌刷新**:为长会话实现令牌刷新逻辑。
|
||||
|
||||
6. **记录认证事件**:跟踪登录尝试和失败以进行安全监控。
|
||||
|
||||
7. **测试认证流程**:使用 `MockAuthProvider` 测试认证场景。
|
||||
458
docs/src/content/docs/modules/network/rate-limit.md
Normal file
458
docs/src/content/docs/modules/network/rate-limit.md
Normal file
@@ -0,0 +1,458 @@
|
||||
---
|
||||
title: "速率限制"
|
||||
description: "使用可配置的速率限制保护你的游戏服务器免受滥用"
|
||||
---
|
||||
|
||||
`@esengine/server` 包含可插拔的速率限制系统,用于防止 DDoS 攻击、消息洪水和其他滥用行为。
|
||||
|
||||
## 安装
|
||||
|
||||
速率限制包含在 server 包中:
|
||||
|
||||
```bash
|
||||
npm install @esengine/server
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
```typescript
|
||||
import { Room, onMessage } from '@esengine/server'
|
||||
import { withRateLimit, rateLimit, noRateLimit } from '@esengine/server/ratelimit'
|
||||
|
||||
class GameRoom extends withRateLimit(Room, {
|
||||
messagesPerSecond: 10,
|
||||
burstSize: 20,
|
||||
onLimited: (player, type, result) => {
|
||||
player.send('Error', {
|
||||
code: 'RATE_LIMITED',
|
||||
retryAfter: result.retryAfter,
|
||||
})
|
||||
},
|
||||
}) {
|
||||
@onMessage('Move')
|
||||
handleMove(data: { x: number; y: number }, player: Player) {
|
||||
// 受速率限制保护(默认 10 msg/s)
|
||||
}
|
||||
|
||||
@rateLimit({ messagesPerSecond: 1 })
|
||||
@onMessage('Trade')
|
||||
handleTrade(data: TradeData, player: Player) {
|
||||
// 交易使用更严格的限制
|
||||
}
|
||||
|
||||
@noRateLimit()
|
||||
@onMessage('Heartbeat')
|
||||
handleHeartbeat(data: any, player: Player) {
|
||||
// 心跳不限制
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 速率限制策略
|
||||
|
||||
### 令牌桶(默认)
|
||||
|
||||
令牌桶算法允许突发流量,同时保持长期速率限制。令牌以固定速率添加,每个请求消耗令牌。
|
||||
|
||||
```typescript
|
||||
import { withRateLimit } from '@esengine/server/ratelimit'
|
||||
|
||||
class GameRoom extends withRateLimit(Room, {
|
||||
strategy: 'token-bucket',
|
||||
messagesPerSecond: 10, // 补充速率
|
||||
burstSize: 20, // 桶容量
|
||||
}) { }
|
||||
```
|
||||
|
||||
**工作原理:**
|
||||
```
|
||||
配置: rate=10/s, burstSize=20
|
||||
|
||||
[0s] 桶满: 20 令牌
|
||||
[0s] 收到 15 条消息 → 允许,剩余 5
|
||||
[0.5s] 补充 5 令牌 → 10 令牌
|
||||
[0.5s] 收到 8 条消息 → 允许,剩余 2
|
||||
[0.6s] 补充 1 令牌 → 3 令牌
|
||||
[0.6s] 收到 5 条消息 → 允许 3,拒绝 2
|
||||
```
|
||||
|
||||
**最适合:** 大多数通用场景,平衡突发容忍度与保护。
|
||||
|
||||
### 滑动窗口
|
||||
|
||||
滑动窗口算法精确跟踪时间窗口内的请求。比固定窗口更准确,但内存使用稍多。
|
||||
|
||||
```typescript
|
||||
class GameRoom extends withRateLimit(Room, {
|
||||
strategy: 'sliding-window',
|
||||
messagesPerSecond: 10,
|
||||
burstSize: 10,
|
||||
}) { }
|
||||
```
|
||||
|
||||
**最适合:** 需要精确限流且不需要突发容忍的场景。
|
||||
|
||||
### 固定窗口
|
||||
|
||||
固定窗口算法将时间划分为固定间隔,并计算每个间隔内的请求数。简单且内存高效,但在窗口边界允许 2 倍突发。
|
||||
|
||||
```typescript
|
||||
class GameRoom extends withRateLimit(Room, {
|
||||
strategy: 'fixed-window',
|
||||
messagesPerSecond: 10,
|
||||
burstSize: 10,
|
||||
}) { }
|
||||
```
|
||||
|
||||
**最适合:** 简单场景,可接受边界突发。
|
||||
|
||||
## 配置
|
||||
|
||||
### 房间配置
|
||||
|
||||
```typescript
|
||||
import { withRateLimit } from '@esengine/server/ratelimit'
|
||||
|
||||
class GameRoom extends withRateLimit(Room, {
|
||||
// 每秒允许的消息数(默认: 10)
|
||||
messagesPerSecond: 10,
|
||||
|
||||
// 突发容量 / 桶大小(默认: 20)
|
||||
burstSize: 20,
|
||||
|
||||
// 策略: 'token-bucket' | 'sliding-window' | 'fixed-window'
|
||||
strategy: 'token-bucket',
|
||||
|
||||
// 被限流时的回调
|
||||
onLimited: (player, messageType, result) => {
|
||||
player.send('RateLimited', {
|
||||
type: messageType,
|
||||
retryAfter: result.retryAfter,
|
||||
})
|
||||
},
|
||||
|
||||
// 限流时断开连接(默认: false)
|
||||
disconnectOnLimit: false,
|
||||
|
||||
// 连续 N 次限流后断开(0 = 永不)
|
||||
maxConsecutiveLimits: 10,
|
||||
|
||||
// 自定义键函数(默认: player.id)
|
||||
getKey: (player) => player.id,
|
||||
|
||||
// 清理间隔(毫秒,默认: 60000)
|
||||
cleanupInterval: 60000,
|
||||
}) { }
|
||||
```
|
||||
|
||||
### 单消息配置
|
||||
|
||||
使用装饰器为特定消息配置速率限制:
|
||||
|
||||
```typescript
|
||||
import { rateLimit, noRateLimit, rateLimitMessage } from '@esengine/server/ratelimit'
|
||||
|
||||
class GameRoom extends withRateLimit(Room) {
|
||||
// 此消息使用自定义速率限制
|
||||
@rateLimit({ messagesPerSecond: 1, burstSize: 2 })
|
||||
@onMessage('Trade')
|
||||
handleTrade(data: TradeData, player: Player) { }
|
||||
|
||||
// 此消息消耗 5 个令牌
|
||||
@rateLimit({ cost: 5 })
|
||||
@onMessage('ExpensiveAction')
|
||||
handleExpensive(data: any, player: Player) { }
|
||||
|
||||
// 豁免速率限制
|
||||
@noRateLimit()
|
||||
@onMessage('Heartbeat')
|
||||
handleHeartbeat(data: any, player: Player) { }
|
||||
|
||||
// 替代方案:显式指定消息类型
|
||||
@rateLimitMessage('SpecialAction', { messagesPerSecond: 2 })
|
||||
@onMessage('SpecialAction')
|
||||
handleSpecial(data: any, player: Player) { }
|
||||
}
|
||||
```
|
||||
|
||||
## 与认证系统组合
|
||||
|
||||
速率限制可与认证系统无缝配合:
|
||||
|
||||
```typescript
|
||||
import { withRoomAuth } from '@esengine/server/auth'
|
||||
import { withRateLimit } from '@esengine/server/ratelimit'
|
||||
|
||||
// 同时应用两个 mixin
|
||||
class GameRoom extends withRateLimit(
|
||||
withRoomAuth(Room, { requireAuth: true }),
|
||||
{ messagesPerSecond: 10 }
|
||||
) {
|
||||
onJoin(player: AuthPlayer) {
|
||||
console.log(`${player.user?.name} 已加入,受速率限制保护`)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 速率限制结果
|
||||
|
||||
当消息被限流时,回调会收到结果对象:
|
||||
|
||||
```typescript
|
||||
interface RateLimitResult {
|
||||
// 是否允许请求
|
||||
allowed: boolean
|
||||
|
||||
// 剩余配额
|
||||
remaining: number
|
||||
|
||||
// 配额重置时间(时间戳)
|
||||
resetAt: number
|
||||
|
||||
// 重试等待时间(毫秒)
|
||||
retryAfter?: number
|
||||
}
|
||||
```
|
||||
|
||||
## 访问速率限制上下文
|
||||
|
||||
你可以访问任何玩家的速率限制上下文:
|
||||
|
||||
```typescript
|
||||
import { getPlayerRateLimitContext } from '@esengine/server/ratelimit'
|
||||
|
||||
class GameRoom extends withRateLimit(Room) {
|
||||
someMethod(player: Player) {
|
||||
const context = this.getRateLimitContext(player)
|
||||
|
||||
// 检查但不消费
|
||||
const status = context?.check()
|
||||
console.log(`剩余: ${status?.remaining}`)
|
||||
|
||||
// 获取连续限流次数
|
||||
console.log(`连续限流: ${context?.consecutiveLimitCount}`)
|
||||
}
|
||||
}
|
||||
|
||||
// 或使用独立函数
|
||||
const context = getPlayerRateLimitContext(player)
|
||||
```
|
||||
|
||||
## 自定义策略
|
||||
|
||||
你可以直接使用策略进行自定义实现:
|
||||
|
||||
```typescript
|
||||
import {
|
||||
TokenBucketStrategy,
|
||||
SlidingWindowStrategy,
|
||||
FixedWindowStrategy,
|
||||
createTokenBucketStrategy,
|
||||
} from '@esengine/server/ratelimit'
|
||||
|
||||
// 直接创建策略
|
||||
const strategy = createTokenBucketStrategy({
|
||||
rate: 10, // 每秒令牌数
|
||||
capacity: 20, // 最大令牌数
|
||||
})
|
||||
|
||||
// 检查并消费
|
||||
const result = strategy.consume('player-123')
|
||||
if (result.allowed) {
|
||||
// 处理消息
|
||||
} else {
|
||||
// 被限流,等待 result.retryAfter 毫秒
|
||||
}
|
||||
|
||||
// 检查但不消费
|
||||
const status = strategy.getStatus('player-123')
|
||||
|
||||
// 重置某个键
|
||||
strategy.reset('player-123')
|
||||
|
||||
// 清理过期记录
|
||||
strategy.cleanup()
|
||||
```
|
||||
|
||||
## 速率限制上下文
|
||||
|
||||
`RateLimitContext` 类管理单个玩家的速率限制:
|
||||
|
||||
```typescript
|
||||
import { RateLimitContext, TokenBucketStrategy } from '@esengine/server/ratelimit'
|
||||
|
||||
const strategy = new TokenBucketStrategy({ rate: 10, capacity: 20 })
|
||||
const context = new RateLimitContext('player-123', strategy)
|
||||
|
||||
// 检查但不消费
|
||||
context.check()
|
||||
|
||||
// 消费配额
|
||||
context.consume()
|
||||
|
||||
// 带消耗量消费
|
||||
context.consume(undefined, 5)
|
||||
|
||||
// 为特定消息类型消费
|
||||
context.consume('Trade')
|
||||
|
||||
// 设置单消息策略
|
||||
context.setMessageStrategy('Trade', new TokenBucketStrategy({ rate: 1, capacity: 2 }))
|
||||
|
||||
// 重置
|
||||
context.reset()
|
||||
|
||||
// 获取连续限流次数
|
||||
console.log(context.consecutiveLimitCount)
|
||||
```
|
||||
|
||||
## 房间生命周期钩子
|
||||
|
||||
你可以重写 `onRateLimited` 钩子进行自定义处理:
|
||||
|
||||
```typescript
|
||||
class GameRoom extends withRateLimit(Room) {
|
||||
onRateLimited(player: Player, messageType: string, result: RateLimitResult) {
|
||||
// 记录事件
|
||||
console.log(`玩家 ${player.id} 在 ${messageType} 上被限流`)
|
||||
|
||||
// 发送自定义错误
|
||||
player.send('SystemMessage', {
|
||||
type: 'warning',
|
||||
message: `请慢一点!${result.retryAfter}ms 后重试`,
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 最佳实践
|
||||
|
||||
1. **从令牌桶开始**:对于游戏来说是最灵活的算法。
|
||||
|
||||
2. **设置合适的限制**:考虑你的游戏机制:
|
||||
- 移动消息:较高限制(20-60/s)
|
||||
- 聊天消息:较低限制(1-5/s)
|
||||
- 交易/购买:非常低的限制(0.5-1/s)
|
||||
|
||||
3. **使用突发容量**:允许短暂突发以获得响应式体验:
|
||||
```typescript
|
||||
messagesPerSecond: 10,
|
||||
burstSize: 30, // 允许 3 秒的突发
|
||||
```
|
||||
|
||||
4. **豁免关键消息**:不要限制心跳或系统消息:
|
||||
```typescript
|
||||
@noRateLimit()
|
||||
@onMessage('Heartbeat')
|
||||
handleHeartbeat() { }
|
||||
```
|
||||
|
||||
5. **与认证结合**:对已认证用户按用户 ID 限流:
|
||||
```typescript
|
||||
getKey: (player) => player.auth?.userId ?? player.id
|
||||
```
|
||||
|
||||
6. **监控和调整**:记录限流事件以调整限制:
|
||||
```typescript
|
||||
onLimited: (player, type, result) => {
|
||||
metrics.increment('rate_limit', { messageType: type })
|
||||
}
|
||||
```
|
||||
|
||||
7. **优雅降级**:发送信息性错误而不是直接断开:
|
||||
```typescript
|
||||
onLimited: (player, type, result) => {
|
||||
player.send('Error', {
|
||||
code: 'RATE_LIMITED',
|
||||
message: '请求过于频繁',
|
||||
retryAfter: result.retryAfter,
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
```typescript
|
||||
import { Room, onMessage, type Player } from '@esengine/server'
|
||||
import { withRoomAuth, type AuthPlayer } from '@esengine/server/auth'
|
||||
import {
|
||||
withRateLimit,
|
||||
rateLimit,
|
||||
noRateLimit,
|
||||
type RateLimitResult,
|
||||
} from '@esengine/server/ratelimit'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
name: string
|
||||
premium: boolean
|
||||
}
|
||||
|
||||
// 组合认证和速率限制
|
||||
class GameRoom extends withRateLimit(
|
||||
withRoomAuth<User>(Room, { requireAuth: true }),
|
||||
{
|
||||
messagesPerSecond: 10,
|
||||
burstSize: 30,
|
||||
strategy: 'token-bucket',
|
||||
|
||||
// 使用用户 ID 进行限流
|
||||
getKey: (player) => (player as AuthPlayer<User>).user?.id ?? player.id,
|
||||
|
||||
// 处理限流
|
||||
onLimited: (player, type, result) => {
|
||||
player.send('Error', {
|
||||
code: 'RATE_LIMITED',
|
||||
messageType: type,
|
||||
retryAfter: result.retryAfter,
|
||||
})
|
||||
},
|
||||
|
||||
// 连续 20 次限流后断开
|
||||
maxConsecutiveLimits: 20,
|
||||
}
|
||||
) {
|
||||
onCreate() {
|
||||
console.log('房间已创建,具有认证 + 速率限制保护')
|
||||
}
|
||||
|
||||
onJoin(player: AuthPlayer<User>) {
|
||||
this.broadcast('PlayerJoined', { name: player.user?.name })
|
||||
}
|
||||
|
||||
// 高频移动(默认速率限制)
|
||||
@onMessage('Move')
|
||||
handleMove(data: { x: number; y: number }, player: AuthPlayer<User>) {
|
||||
this.broadcast('PlayerMoved', { id: player.id, ...data })
|
||||
}
|
||||
|
||||
// 低频交易(严格限制)
|
||||
@rateLimit({ messagesPerSecond: 0.5, burstSize: 2 })
|
||||
@onMessage('Trade')
|
||||
handleTrade(data: TradeData, player: AuthPlayer<User>) {
|
||||
// 处理交易...
|
||||
}
|
||||
|
||||
// 聊天使用中等限制
|
||||
@rateLimit({ messagesPerSecond: 2, burstSize: 5 })
|
||||
@onMessage('Chat')
|
||||
handleChat(data: { text: string }, player: AuthPlayer<User>) {
|
||||
this.broadcast('Chat', {
|
||||
from: player.user?.name,
|
||||
text: data.text,
|
||||
})
|
||||
}
|
||||
|
||||
// 系统消息 - 不限制
|
||||
@noRateLimit()
|
||||
@onMessage('Heartbeat')
|
||||
handleHeartbeat(data: any, player: Player) {
|
||||
player.send('Pong', { time: Date.now() })
|
||||
}
|
||||
|
||||
// 自定义限流处理
|
||||
onRateLimited(player: Player, messageType: string, result: RateLimitResult) {
|
||||
console.warn(`[限流] 玩家 ${player.id} 在 ${messageType} 上被限流`)
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -79,10 +79,140 @@ await server.start()
|
||||
| `tickRate` | `number` | `20` | 全局 Tick 频率 (Hz) |
|
||||
| `apiDir` | `string` | `'src/api'` | API 处理器目录 |
|
||||
| `msgDir` | `string` | `'src/msg'` | 消息处理器目录 |
|
||||
| `httpDir` | `string` | `'src/http'` | HTTP 路由目录 |
|
||||
| `httpPrefix` | `string` | `'/api'` | HTTP 路由前缀 |
|
||||
| `cors` | `boolean \| CorsOptions` | - | CORS 配置 |
|
||||
| `onStart` | `(port) => void` | - | 启动回调 |
|
||||
| `onConnect` | `(conn) => void` | - | 连接回调 |
|
||||
| `onDisconnect` | `(conn) => void` | - | 断开回调 |
|
||||
|
||||
## HTTP 路由
|
||||
|
||||
支持 HTTP API 与 WebSocket 共用端口,适用于登录、注册等场景。
|
||||
|
||||
### 文件路由
|
||||
|
||||
在 `httpDir` 目录下创建路由文件,自动映射为 HTTP 端点:
|
||||
|
||||
```
|
||||
src/http/
|
||||
├── login.ts → POST /api/login
|
||||
├── register.ts → POST /api/register
|
||||
├── health.ts → GET /api/health (需设置 method: 'GET')
|
||||
└── users/
|
||||
└── [id].ts → POST /api/users/:id (动态路由)
|
||||
```
|
||||
|
||||
### 定义路由
|
||||
|
||||
使用 `defineHttp` 定义类型安全的路由处理器:
|
||||
|
||||
```typescript
|
||||
// src/http/login.ts
|
||||
import { defineHttp } from '@esengine/server'
|
||||
|
||||
interface LoginBody {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export default defineHttp<LoginBody>({
|
||||
method: 'POST', // 默认 POST,可选 GET/PUT/DELETE/PATCH
|
||||
handler(req, res) {
|
||||
const { username, password } = req.body
|
||||
|
||||
// 验证凭证...
|
||||
if (!isValid(username, password)) {
|
||||
res.error(401, 'Invalid credentials')
|
||||
return
|
||||
}
|
||||
|
||||
// 生成 token...
|
||||
res.json({ token: '...', userId: '...' })
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 请求对象 (HttpRequest)
|
||||
|
||||
```typescript
|
||||
interface HttpRequest {
|
||||
raw: IncomingMessage // Node.js 原始请求
|
||||
method: string // 请求方法
|
||||
path: string // 请求路径
|
||||
query: Record<string, string> // 查询参数
|
||||
headers: Record<string, string | string[] | undefined>
|
||||
body: unknown // 解析后的 JSON 请求体
|
||||
ip: string // 客户端 IP
|
||||
}
|
||||
```
|
||||
|
||||
### 响应对象 (HttpResponse)
|
||||
|
||||
```typescript
|
||||
interface HttpResponse {
|
||||
raw: ServerResponse // Node.js 原始响应
|
||||
status(code: number): HttpResponse // 设置状态码(链式)
|
||||
header(name: string, value: string): HttpResponse // 设置头(链式)
|
||||
json(data: unknown): void // 发送 JSON
|
||||
text(data: string): void // 发送文本
|
||||
error(code: number, message: string): void // 发送错误
|
||||
}
|
||||
```
|
||||
|
||||
### 使用示例
|
||||
|
||||
```typescript
|
||||
// 完整的登录服务器示例
|
||||
import { createServer, defineHttp } from '@esengine/server'
|
||||
import { createJwtAuthProvider, withAuth } from '@esengine/server/auth'
|
||||
|
||||
const jwtProvider = createJwtAuthProvider({
|
||||
secret: process.env.JWT_SECRET!,
|
||||
expiresIn: 3600 * 24,
|
||||
})
|
||||
|
||||
const server = await createServer({
|
||||
port: 8080,
|
||||
httpDir: 'src/http',
|
||||
httpPrefix: '/api',
|
||||
cors: true,
|
||||
})
|
||||
|
||||
// 包装认证(WebSocket 连接验证 token)
|
||||
const authServer = withAuth(server, {
|
||||
provider: jwtProvider,
|
||||
extractCredentials: (req) => {
|
||||
const url = new URL(req.url, 'http://localhost')
|
||||
return url.searchParams.get('token')
|
||||
},
|
||||
})
|
||||
|
||||
await authServer.start()
|
||||
// HTTP: http://localhost:8080/api/*
|
||||
// WebSocket: ws://localhost:8080?token=xxx
|
||||
```
|
||||
|
||||
### 内联路由
|
||||
|
||||
也可以直接在配置中定义路由(与文件路由合并,内联优先):
|
||||
|
||||
```typescript
|
||||
const server = await createServer({
|
||||
port: 8080,
|
||||
http: {
|
||||
'/health': {
|
||||
GET: (req, res) => res.json({ status: 'ok' }),
|
||||
},
|
||||
'/webhook': async (req, res) => {
|
||||
// 接受所有方法
|
||||
await handleWebhook(req.body)
|
||||
res.json({ received: true })
|
||||
},
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## Room 系统
|
||||
|
||||
Room 是游戏房间的基类,管理玩家和游戏状态。
|
||||
@@ -311,6 +441,93 @@ client.send('RoomMessage', {
|
||||
})
|
||||
```
|
||||
|
||||
## ECSRoom
|
||||
|
||||
`ECSRoom` 是带有 ECS World 支持的房间基类,适用于需要 ECS 架构的游戏。
|
||||
|
||||
### 服务端启动
|
||||
|
||||
```typescript
|
||||
import { Core } from '@esengine/ecs-framework';
|
||||
import { createServer } from '@esengine/server';
|
||||
import { GameRoom } from './rooms/GameRoom.js';
|
||||
|
||||
// 初始化 Core
|
||||
Core.create();
|
||||
|
||||
// 全局游戏循环
|
||||
setInterval(() => Core.update(1/60), 16);
|
||||
|
||||
// 创建服务器
|
||||
const server = await createServer({ port: 3000 });
|
||||
server.define('game', GameRoom);
|
||||
await server.start();
|
||||
```
|
||||
|
||||
### 定义 ECSRoom
|
||||
|
||||
```typescript
|
||||
import { ECSRoom, Player } from '@esengine/server/ecs';
|
||||
import { Component, ECSComponent, sync } from '@esengine/ecs-framework';
|
||||
|
||||
// 定义同步组件
|
||||
@ECSComponent('Player')
|
||||
class PlayerComponent extends Component {
|
||||
@sync("string") name: string = "";
|
||||
@sync("uint16") score: number = 0;
|
||||
@sync("float32") x: number = 0;
|
||||
@sync("float32") y: number = 0;
|
||||
}
|
||||
|
||||
// 定义房间
|
||||
class GameRoom extends ECSRoom {
|
||||
onCreate() {
|
||||
this.addSystem(new MovementSystem());
|
||||
}
|
||||
|
||||
onJoin(player: Player) {
|
||||
const entity = this.createPlayerEntity(player.id);
|
||||
const comp = entity.addComponent(new PlayerComponent());
|
||||
comp.name = player.id;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### ECSRoom API
|
||||
|
||||
```typescript
|
||||
abstract class ECSRoom<TState, TPlayerData> extends Room<TState, TPlayerData> {
|
||||
protected readonly world: World; // ECS World
|
||||
protected readonly scene: Scene; // 主场景
|
||||
|
||||
// 场景管理
|
||||
protected addSystem(system: EntitySystem): void;
|
||||
protected createEntity(name?: string): Entity;
|
||||
protected createPlayerEntity(playerId: string, name?: string): Entity;
|
||||
protected getPlayerEntity(playerId: string): Entity | undefined;
|
||||
protected destroyPlayerEntity(playerId: string): void;
|
||||
|
||||
// 状态同步
|
||||
protected sendFullState(player: Player): void;
|
||||
protected broadcastSpawn(entity: Entity, prefabType?: string): void;
|
||||
protected broadcastDelta(): void;
|
||||
}
|
||||
```
|
||||
|
||||
### @sync 装饰器
|
||||
|
||||
标记需要网络同步的组件字段:
|
||||
|
||||
| 类型 | 描述 | 字节数 |
|
||||
|------|------|--------|
|
||||
| `"boolean"` | 布尔值 | 1 |
|
||||
| `"int8"` / `"uint8"` | 8位整数 | 1 |
|
||||
| `"int16"` / `"uint16"` | 16位整数 | 2 |
|
||||
| `"int32"` / `"uint32"` | 32位整数 | 4 |
|
||||
| `"float32"` | 32位浮点 | 4 |
|
||||
| `"float64"` | 64位浮点 | 8 |
|
||||
| `"string"` | 字符串 | 变长 |
|
||||
|
||||
## 最佳实践
|
||||
|
||||
1. **合理设置 Tick 频率**
|
||||
|
||||
@@ -1,8 +1,176 @@
|
||||
---
|
||||
title: "状态同步"
|
||||
description: "插值、预测和快照缓冲区"
|
||||
description: "组件同步、插值、预测和快照缓冲区"
|
||||
---
|
||||
|
||||
## @NetworkEntity 装饰器
|
||||
|
||||
`@NetworkEntity` 装饰器用于标记需要自动广播生成/销毁的组件。当包含此组件的实体被创建或销毁时,ECSRoom 会自动广播相应的消息给所有客户端。
|
||||
|
||||
### 基本用法
|
||||
|
||||
```typescript
|
||||
import { Component, ECSComponent, sync, NetworkEntity } from '@esengine/ecs-framework';
|
||||
|
||||
@ECSComponent('Enemy')
|
||||
@NetworkEntity('Enemy')
|
||||
class EnemyComponent extends Component {
|
||||
@sync('float32') x: number = 0;
|
||||
@sync('float32') y: number = 0;
|
||||
@sync('uint16') health: number = 100;
|
||||
}
|
||||
```
|
||||
|
||||
当添加此组件到实体时,ECSRoom 会自动广播 spawn 消息:
|
||||
|
||||
```typescript
|
||||
// 服务端
|
||||
const entity = scene.createEntity('Enemy');
|
||||
entity.addComponent(new EnemyComponent()); // 自动广播 spawn
|
||||
|
||||
// 销毁时自动广播 despawn
|
||||
entity.destroy(); // 自动广播 despawn
|
||||
```
|
||||
|
||||
### 配置选项
|
||||
|
||||
```typescript
|
||||
@NetworkEntity('Bullet', {
|
||||
autoSpawn: true, // 自动广播生成(默认 true)
|
||||
autoDespawn: false // 禁用自动广播销毁
|
||||
})
|
||||
class BulletComponent extends Component { }
|
||||
```
|
||||
|
||||
| 选项 | 类型 | 默认值 | 描述 |
|
||||
|------|------|--------|------|
|
||||
| `autoSpawn` | `boolean` | `true` | 添加组件时自动广播 spawn |
|
||||
| `autoDespawn` | `boolean` | `true` | 销毁实体时自动广播 despawn |
|
||||
|
||||
### 初始化顺序
|
||||
|
||||
使用 `@NetworkEntity` 时,应在添加组件**之前**初始化数据:
|
||||
|
||||
```typescript
|
||||
// ✅ 正确:先初始化,再添加
|
||||
const comp = new PlayerComponent();
|
||||
comp.playerId = player.id;
|
||||
comp.x = 100;
|
||||
comp.y = 200;
|
||||
entity.addComponent(comp); // spawn 时数据已正确
|
||||
|
||||
// ❌ 错误:先添加,再初始化
|
||||
const comp = entity.addComponent(new PlayerComponent());
|
||||
comp.playerId = player.id; // spawn 时数据是默认值
|
||||
```
|
||||
|
||||
### 简化 GameRoom
|
||||
|
||||
使用 `@NetworkEntity` 后,GameRoom 变得更加简洁:
|
||||
|
||||
```typescript
|
||||
// 无需手动回调
|
||||
class GameRoom extends ECSRoom {
|
||||
private setupSystems(): void {
|
||||
// 敌人生成系统(自动广播 spawn)
|
||||
this.addSystem(new EnemySpawnSystem());
|
||||
|
||||
// 敌人 AI 系统
|
||||
const enemyAI = new EnemyAISystem();
|
||||
enemyAI.onDeath((enemy) => {
|
||||
enemy.destroy(); // 自动广播 despawn
|
||||
});
|
||||
this.addSystem(enemyAI);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### ECSRoom 配置
|
||||
|
||||
可以在 ECSRoom 中禁用自动网络实体功能:
|
||||
|
||||
```typescript
|
||||
class GameRoom extends ECSRoom {
|
||||
constructor() {
|
||||
super({
|
||||
enableAutoNetworkEntity: false // 禁用自动广播
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 组件同步系统
|
||||
|
||||
基于 `@sync` 装饰器的 ECS 组件状态同步。
|
||||
|
||||
### 定义同步组件
|
||||
|
||||
```typescript
|
||||
import { Component, ECSComponent, sync } from '@esengine/ecs-framework';
|
||||
|
||||
@ECSComponent('Player')
|
||||
class PlayerComponent extends Component {
|
||||
@sync("string") name: string = "";
|
||||
@sync("uint16") score: number = 0;
|
||||
@sync("float32") x: number = 0;
|
||||
@sync("float32") y: number = 0;
|
||||
|
||||
// 不带 @sync 的字段不会同步
|
||||
localData: any;
|
||||
}
|
||||
```
|
||||
|
||||
### 服务端编码
|
||||
|
||||
```typescript
|
||||
import { ComponentSyncSystem } from '@esengine/network';
|
||||
|
||||
const syncSystem = new ComponentSyncSystem({}, true);
|
||||
scene.addSystem(syncSystem);
|
||||
|
||||
// 编码所有实体(首次连接)
|
||||
const fullData = syncSystem.encodeAllEntities(true);
|
||||
sendToClient(fullData);
|
||||
|
||||
// 编码增量(只发送变更)
|
||||
const deltaData = syncSystem.encodeDelta();
|
||||
if (deltaData) {
|
||||
broadcast(deltaData);
|
||||
}
|
||||
```
|
||||
|
||||
### 客户端解码
|
||||
|
||||
```typescript
|
||||
const syncSystem = new ComponentSyncSystem();
|
||||
scene.addSystem(syncSystem);
|
||||
|
||||
// 注册组件类型
|
||||
syncSystem.registerComponent(PlayerComponent);
|
||||
|
||||
// 监听同步事件
|
||||
syncSystem.addSyncListener((event) => {
|
||||
if (event.type === 'entitySpawned') {
|
||||
console.log('New entity:', event.entityId);
|
||||
}
|
||||
});
|
||||
|
||||
// 应用状态
|
||||
syncSystem.applySnapshot(data);
|
||||
```
|
||||
|
||||
### 同步类型
|
||||
|
||||
| 类型 | 描述 | 字节数 |
|
||||
|------|------|--------|
|
||||
| `"boolean"` | 布尔值 | 1 |
|
||||
| `"int8"` / `"uint8"` | 8位整数 | 1 |
|
||||
| `"int16"` / `"uint16"` | 16位整数 | 2 |
|
||||
| `"int32"` / `"uint32"` | 32位整数 | 4 |
|
||||
| `"float32"` | 32位浮点 | 4 |
|
||||
| `"float64"` | 64位浮点 | 8 |
|
||||
| `"string"` | 字符串 | 变长 |
|
||||
|
||||
## 快照缓冲区
|
||||
|
||||
用于存储服务器状态快照并进行插值:
|
||||
|
||||
261
docs/src/content/docs/modules/transaction/core.md
Normal file
261
docs/src/content/docs/modules/transaction/core.md
Normal file
@@ -0,0 +1,261 @@
|
||||
---
|
||||
title: "核心概念"
|
||||
description: "事务系统的核心概念:事务上下文、事务管理器、Saga 模式"
|
||||
---
|
||||
|
||||
## 事务状态
|
||||
|
||||
事务有以下几种状态:
|
||||
|
||||
```typescript
|
||||
type TransactionState =
|
||||
| 'pending' // 等待执行
|
||||
| 'executing' // 执行中
|
||||
| 'committed' // 已提交
|
||||
| 'rolledback' // 已回滚
|
||||
| 'failed' // 失败
|
||||
```
|
||||
|
||||
## TransactionContext
|
||||
|
||||
事务上下文封装了事务的状态、操作和执行逻辑。
|
||||
|
||||
### 创建事务
|
||||
|
||||
```typescript
|
||||
import { TransactionManager } from '@esengine/transaction';
|
||||
|
||||
const manager = new TransactionManager();
|
||||
|
||||
// 方式 1:使用 begin() 手动管理
|
||||
const tx = manager.begin({ timeout: 5000 });
|
||||
tx.addOperation(op1);
|
||||
tx.addOperation(op2);
|
||||
const result = await tx.execute();
|
||||
|
||||
// 方式 2:使用 run() 自动管理
|
||||
const result = await manager.run((tx) => {
|
||||
tx.addOperation(op1);
|
||||
tx.addOperation(op2);
|
||||
});
|
||||
```
|
||||
|
||||
### 链式添加操作
|
||||
|
||||
```typescript
|
||||
const result = await manager.run((tx) => {
|
||||
tx.addOperation(new CurrencyOperation({ ... }))
|
||||
.addOperation(new InventoryOperation({ ... }))
|
||||
.addOperation(new InventoryOperation({ ... }));
|
||||
});
|
||||
```
|
||||
|
||||
### 上下文数据
|
||||
|
||||
操作之间可以通过上下文共享数据:
|
||||
|
||||
```typescript
|
||||
class CustomOperation extends BaseOperation<MyData, MyResult> {
|
||||
async execute(ctx: ITransactionContext): Promise<OperationResult<MyResult>> {
|
||||
// 读取之前操作设置的数据
|
||||
const previousResult = ctx.get<number>('previousValue');
|
||||
|
||||
// 设置数据供后续操作使用
|
||||
ctx.set('myResult', { value: 123 });
|
||||
|
||||
return this.success({ ... });
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## TransactionManager
|
||||
|
||||
事务管理器负责创建、执行和恢复事务。
|
||||
|
||||
### 配置选项
|
||||
|
||||
```typescript
|
||||
interface TransactionManagerConfig {
|
||||
storage?: ITransactionStorage; // 存储实例
|
||||
defaultTimeout?: number; // 默认超时(毫秒)
|
||||
serverId?: string; // 服务器 ID(分布式用)
|
||||
autoRecover?: boolean; // 自动恢复未完成事务
|
||||
}
|
||||
|
||||
const manager = new TransactionManager({
|
||||
storage: new RedisStorage({ client: redis }),
|
||||
defaultTimeout: 10000,
|
||||
serverId: 'server-1',
|
||||
autoRecover: true,
|
||||
});
|
||||
```
|
||||
|
||||
### 分布式锁
|
||||
|
||||
```typescript
|
||||
// 获取锁
|
||||
const token = await manager.acquireLock('player:123:inventory', 10000);
|
||||
|
||||
if (token) {
|
||||
try {
|
||||
// 执行操作
|
||||
await doSomething();
|
||||
} finally {
|
||||
// 释放锁
|
||||
await manager.releaseLock('player:123:inventory', token);
|
||||
}
|
||||
}
|
||||
|
||||
// 或使用 withLock 简化
|
||||
await manager.withLock('player:123:inventory', async () => {
|
||||
await doSomething();
|
||||
}, 10000);
|
||||
```
|
||||
|
||||
### 事务恢复
|
||||
|
||||
服务器重启时恢复未完成的事务:
|
||||
|
||||
```typescript
|
||||
const manager = new TransactionManager({
|
||||
storage: new RedisStorage({ client: redis }),
|
||||
serverId: 'server-1',
|
||||
});
|
||||
|
||||
// 恢复未完成的事务
|
||||
const recoveredCount = await manager.recover();
|
||||
console.log(`Recovered ${recoveredCount} transactions`);
|
||||
```
|
||||
|
||||
## Saga 模式
|
||||
|
||||
事务系统采用 Saga 模式,每个操作必须实现 `execute` 和 `compensate` 方法:
|
||||
|
||||
```typescript
|
||||
interface ITransactionOperation<TData, TResult> {
|
||||
readonly name: string;
|
||||
readonly data: TData;
|
||||
|
||||
// 验证前置条件
|
||||
validate(ctx: ITransactionContext): Promise<boolean>;
|
||||
|
||||
// 正向执行
|
||||
execute(ctx: ITransactionContext): Promise<OperationResult<TResult>>;
|
||||
|
||||
// 补偿操作(回滚)
|
||||
compensate(ctx: ITransactionContext): Promise<void>;
|
||||
}
|
||||
```
|
||||
|
||||
### 执行流程
|
||||
|
||||
```
|
||||
开始事务
|
||||
│
|
||||
▼
|
||||
┌─────────────────────┐
|
||||
│ validate(op1) │──失败──► 返回失败
|
||||
└─────────────────────┘
|
||||
│成功
|
||||
▼
|
||||
┌─────────────────────┐
|
||||
│ execute(op1) │──失败──┐
|
||||
└─────────────────────┘ │
|
||||
│成功 │
|
||||
▼ │
|
||||
┌─────────────────────┐ │
|
||||
│ validate(op2) │──失败──┤
|
||||
└─────────────────────┘ │
|
||||
│成功 │
|
||||
▼ │
|
||||
┌─────────────────────┐ │
|
||||
│ execute(op2) │──失败──┤
|
||||
└─────────────────────┘ │
|
||||
│成功 ▼
|
||||
▼ ┌─────────────────────┐
|
||||
提交事务 │ compensate(op1) │
|
||||
└─────────────────────┘
|
||||
│
|
||||
▼
|
||||
返回失败(已回滚)
|
||||
```
|
||||
|
||||
### 自定义操作
|
||||
|
||||
```typescript
|
||||
import { BaseOperation, ITransactionContext, OperationResult } from '@esengine/transaction';
|
||||
|
||||
interface UpgradeData {
|
||||
playerId: string;
|
||||
itemId: string;
|
||||
targetLevel: number;
|
||||
}
|
||||
|
||||
interface UpgradeResult {
|
||||
newLevel: number;
|
||||
}
|
||||
|
||||
class UpgradeOperation extends BaseOperation<UpgradeData, UpgradeResult> {
|
||||
readonly name = 'upgrade';
|
||||
|
||||
private _previousLevel: number = 0;
|
||||
|
||||
async validate(ctx: ITransactionContext): Promise<boolean> {
|
||||
// 验证物品存在且可升级
|
||||
const item = await this.getItem(ctx);
|
||||
return item !== null && item.level < this.data.targetLevel;
|
||||
}
|
||||
|
||||
async execute(ctx: ITransactionContext): Promise<OperationResult<UpgradeResult>> {
|
||||
const item = await this.getItem(ctx);
|
||||
if (!item) {
|
||||
return this.failure('Item not found', 'ITEM_NOT_FOUND');
|
||||
}
|
||||
|
||||
this._previousLevel = item.level;
|
||||
item.level = this.data.targetLevel;
|
||||
await this.saveItem(ctx, item);
|
||||
|
||||
return this.success({ newLevel: item.level });
|
||||
}
|
||||
|
||||
async compensate(ctx: ITransactionContext): Promise<void> {
|
||||
const item = await this.getItem(ctx);
|
||||
if (item) {
|
||||
item.level = this._previousLevel;
|
||||
await this.saveItem(ctx, item);
|
||||
}
|
||||
}
|
||||
|
||||
private async getItem(ctx: ITransactionContext) {
|
||||
// 从存储获取物品
|
||||
}
|
||||
|
||||
private async saveItem(ctx: ITransactionContext, item: any) {
|
||||
// 保存物品到存储
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 事务结果
|
||||
|
||||
```typescript
|
||||
interface TransactionResult<T = unknown> {
|
||||
success: boolean; // 是否成功
|
||||
transactionId: string; // 事务 ID
|
||||
results: OperationResult[]; // 各操作结果
|
||||
data?: T; // 最终数据
|
||||
error?: string; // 错误信息
|
||||
duration: number; // 执行时间(毫秒)
|
||||
}
|
||||
|
||||
const result = await manager.run((tx) => { ... });
|
||||
|
||||
console.log(`Transaction ${result.transactionId}`);
|
||||
console.log(`Success: ${result.success}`);
|
||||
console.log(`Duration: ${result.duration}ms`);
|
||||
|
||||
if (!result.success) {
|
||||
console.log(`Error: ${result.error}`);
|
||||
}
|
||||
```
|
||||
355
docs/src/content/docs/modules/transaction/distributed.md
Normal file
355
docs/src/content/docs/modules/transaction/distributed.md
Normal file
@@ -0,0 +1,355 @@
|
||||
---
|
||||
title: "分布式事务"
|
||||
description: "Saga 编排器和跨服务器事务支持"
|
||||
---
|
||||
|
||||
## Saga 编排器
|
||||
|
||||
`SagaOrchestrator` 用于编排跨服务器的分布式事务。
|
||||
|
||||
### 基本用法
|
||||
|
||||
```typescript
|
||||
import { SagaOrchestrator, RedisStorage } from '@esengine/transaction';
|
||||
|
||||
const orchestrator = new SagaOrchestrator({
|
||||
storage: new RedisStorage({ client: redis }),
|
||||
timeout: 30000,
|
||||
serverId: 'orchestrator-1',
|
||||
});
|
||||
|
||||
const result = await orchestrator.execute([
|
||||
{
|
||||
name: 'deduct_currency',
|
||||
serverId: 'game-server-1',
|
||||
data: { playerId: 'player1', amount: 100 },
|
||||
execute: async (data) => {
|
||||
// 调用游戏服务器 API 扣除货币
|
||||
const response = await gameServerApi.deductCurrency(data);
|
||||
return { success: response.ok };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
// 调用游戏服务器 API 恢复货币
|
||||
await gameServerApi.addCurrency(data);
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'add_item',
|
||||
serverId: 'inventory-server-1',
|
||||
data: { playerId: 'player1', itemId: 'sword' },
|
||||
execute: async (data) => {
|
||||
const response = await inventoryServerApi.addItem(data);
|
||||
return { success: response.ok };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await inventoryServerApi.removeItem(data);
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
if (result.success) {
|
||||
console.log('Saga completed successfully');
|
||||
} else {
|
||||
console.log('Saga failed:', result.error);
|
||||
console.log('Completed steps:', result.completedSteps);
|
||||
console.log('Failed at:', result.failedStep);
|
||||
}
|
||||
```
|
||||
|
||||
### 配置选项
|
||||
|
||||
```typescript
|
||||
interface SagaOrchestratorConfig {
|
||||
storage?: ITransactionStorage; // 存储实例
|
||||
timeout?: number; // 超时时间(毫秒)
|
||||
serverId?: string; // 编排器服务器 ID
|
||||
}
|
||||
```
|
||||
|
||||
### Saga 步骤
|
||||
|
||||
```typescript
|
||||
interface SagaStep<T = unknown> {
|
||||
name: string; // 步骤名称
|
||||
serverId?: string; // 目标服务器 ID
|
||||
data: T; // 步骤数据
|
||||
execute: (data: T) => Promise<OperationResult>; // 执行函数
|
||||
compensate: (data: T) => Promise<void>; // 补偿函数
|
||||
}
|
||||
```
|
||||
|
||||
### Saga 结果
|
||||
|
||||
```typescript
|
||||
interface SagaResult {
|
||||
success: boolean; // 是否成功
|
||||
sagaId: string; // Saga ID
|
||||
completedSteps: string[]; // 已完成的步骤
|
||||
failedStep?: string; // 失败的步骤
|
||||
error?: string; // 错误信息
|
||||
duration: number; // 执行时间(毫秒)
|
||||
}
|
||||
```
|
||||
|
||||
## 执行流程
|
||||
|
||||
```
|
||||
开始 Saga
|
||||
│
|
||||
▼
|
||||
┌─────────────────────┐
|
||||
│ Step 1: execute │──失败──┐
|
||||
└─────────────────────┘ │
|
||||
│成功 │
|
||||
▼ │
|
||||
┌─────────────────────┐ │
|
||||
│ Step 2: execute │──失败──┤
|
||||
└─────────────────────┘ │
|
||||
│成功 │
|
||||
▼ │
|
||||
┌─────────────────────┐ │
|
||||
│ Step 3: execute │──失败──┤
|
||||
└─────────────────────┘ │
|
||||
│成功 ▼
|
||||
▼ ┌─────────────────────┐
|
||||
Saga 完成 │ Step 2: compensate │
|
||||
└─────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────┐
|
||||
│ Step 1: compensate │
|
||||
└─────────────────────┘
|
||||
│
|
||||
▼
|
||||
Saga 失败(已补偿)
|
||||
```
|
||||
|
||||
## Saga 日志
|
||||
|
||||
编排器会记录详细的执行日志:
|
||||
|
||||
```typescript
|
||||
interface SagaLog {
|
||||
id: string; // Saga ID
|
||||
state: SagaLogState; // 状态
|
||||
steps: SagaStepLog[]; // 步骤日志
|
||||
createdAt: number; // 创建时间
|
||||
updatedAt: number; // 更新时间
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
type SagaLogState =
|
||||
| 'pending' // 等待执行
|
||||
| 'running' // 执行中
|
||||
| 'completed' // 已完成
|
||||
| 'compensating' // 补偿中
|
||||
| 'compensated' // 已补偿
|
||||
| 'failed' // 失败
|
||||
|
||||
interface SagaStepLog {
|
||||
name: string; // 步骤名称
|
||||
serverId?: string; // 服务器 ID
|
||||
state: SagaStepState; // 状态
|
||||
startedAt?: number; // 开始时间
|
||||
completedAt?: number; // 完成时间
|
||||
error?: string; // 错误信息
|
||||
}
|
||||
|
||||
type SagaStepState =
|
||||
| 'pending' // 等待执行
|
||||
| 'executing' // 执行中
|
||||
| 'completed' // 已完成
|
||||
| 'compensating' // 补偿中
|
||||
| 'compensated' // 已补偿
|
||||
| 'failed' // 失败
|
||||
```
|
||||
|
||||
### 查询 Saga 日志
|
||||
|
||||
```typescript
|
||||
const log = await orchestrator.getSagaLog('saga_xxx');
|
||||
|
||||
if (log) {
|
||||
console.log('Saga state:', log.state);
|
||||
for (const step of log.steps) {
|
||||
console.log(` ${step.name}: ${step.state}`);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 跨服务器事务示例
|
||||
|
||||
### 场景:跨服购买
|
||||
|
||||
玩家在游戏服务器购买物品,货币在账户服务器,物品在背包服务器。
|
||||
|
||||
```typescript
|
||||
const orchestrator = new SagaOrchestrator({
|
||||
storage: redisStorage,
|
||||
serverId: 'purchase-orchestrator',
|
||||
});
|
||||
|
||||
async function crossServerPurchase(
|
||||
playerId: string,
|
||||
itemId: string,
|
||||
price: number
|
||||
): Promise<SagaResult> {
|
||||
return orchestrator.execute([
|
||||
// 步骤 1:在账户服务器扣款
|
||||
{
|
||||
name: 'deduct_balance',
|
||||
serverId: 'account-server',
|
||||
data: { playerId, amount: price },
|
||||
execute: async (data) => {
|
||||
const result = await accountService.deduct(data.playerId, data.amount);
|
||||
return { success: result.ok, error: result.error };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await accountService.refund(data.playerId, data.amount);
|
||||
},
|
||||
},
|
||||
|
||||
// 步骤 2:在背包服务器添加物品
|
||||
{
|
||||
name: 'add_item',
|
||||
serverId: 'inventory-server',
|
||||
data: { playerId, itemId },
|
||||
execute: async (data) => {
|
||||
const result = await inventoryService.addItem(data.playerId, data.itemId);
|
||||
return { success: result.ok, error: result.error };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await inventoryService.removeItem(data.playerId, data.itemId);
|
||||
},
|
||||
},
|
||||
|
||||
// 步骤 3:记录购买日志
|
||||
{
|
||||
name: 'log_purchase',
|
||||
serverId: 'log-server',
|
||||
data: { playerId, itemId, price, timestamp: Date.now() },
|
||||
execute: async (data) => {
|
||||
await logService.recordPurchase(data);
|
||||
return { success: true };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await logService.cancelPurchase(data);
|
||||
},
|
||||
},
|
||||
]);
|
||||
}
|
||||
```
|
||||
|
||||
### 场景:跨服交易
|
||||
|
||||
两个玩家在不同服务器上进行交易。
|
||||
|
||||
```typescript
|
||||
async function crossServerTrade(
|
||||
playerA: { id: string; server: string; items: string[] },
|
||||
playerB: { id: string; server: string; items: string[] }
|
||||
): Promise<SagaResult> {
|
||||
const steps: SagaStep[] = [];
|
||||
|
||||
// 移除 A 的物品
|
||||
for (const itemId of playerA.items) {
|
||||
steps.push({
|
||||
name: `remove_${playerA.id}_${itemId}`,
|
||||
serverId: playerA.server,
|
||||
data: { playerId: playerA.id, itemId },
|
||||
execute: async (data) => {
|
||||
return await inventoryService.removeItem(data.playerId, data.itemId);
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await inventoryService.addItem(data.playerId, data.itemId);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 添加物品到 B
|
||||
for (const itemId of playerA.items) {
|
||||
steps.push({
|
||||
name: `add_${playerB.id}_${itemId}`,
|
||||
serverId: playerB.server,
|
||||
data: { playerId: playerB.id, itemId },
|
||||
execute: async (data) => {
|
||||
return await inventoryService.addItem(data.playerId, data.itemId);
|
||||
},
|
||||
compensate: async (data) => {
|
||||
await inventoryService.removeItem(data.playerId, data.itemId);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 类似地处理 B 的物品...
|
||||
|
||||
return orchestrator.execute(steps);
|
||||
}
|
||||
```
|
||||
|
||||
## 恢复未完成的 Saga
|
||||
|
||||
服务器重启后恢复未完成的 Saga:
|
||||
|
||||
```typescript
|
||||
const orchestrator = new SagaOrchestrator({
|
||||
storage: redisStorage,
|
||||
serverId: 'my-orchestrator',
|
||||
});
|
||||
|
||||
// 恢复未完成的 Saga(会执行补偿)
|
||||
const recoveredCount = await orchestrator.recover();
|
||||
console.log(`Recovered ${recoveredCount} sagas`);
|
||||
```
|
||||
|
||||
## 最佳实践
|
||||
|
||||
### 1. 幂等性
|
||||
|
||||
确保所有操作都是幂等的:
|
||||
|
||||
```typescript
|
||||
{
|
||||
execute: async (data) => {
|
||||
// 使用唯一 ID 确保幂等
|
||||
const result = await service.process(data.requestId, data);
|
||||
return { success: result.ok };
|
||||
},
|
||||
compensate: async (data) => {
|
||||
// 补偿也要幂等
|
||||
await service.rollback(data.requestId);
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 超时处理
|
||||
|
||||
设置合适的超时时间:
|
||||
|
||||
```typescript
|
||||
const orchestrator = new SagaOrchestrator({
|
||||
timeout: 60000, // 跨服务器操作需要更长超时
|
||||
});
|
||||
```
|
||||
|
||||
### 3. 监控和告警
|
||||
|
||||
记录 Saga 执行结果:
|
||||
|
||||
```typescript
|
||||
const result = await orchestrator.execute(steps);
|
||||
|
||||
if (!result.success) {
|
||||
// 发送告警
|
||||
alertService.send({
|
||||
type: 'saga_failed',
|
||||
sagaId: result.sagaId,
|
||||
failedStep: result.failedStep,
|
||||
error: result.error,
|
||||
});
|
||||
|
||||
// 记录详细日志
|
||||
const log = await orchestrator.getSagaLog(result.sagaId);
|
||||
logger.error('Saga failed', { log });
|
||||
}
|
||||
```
|
||||
238
docs/src/content/docs/modules/transaction/index.md
Normal file
238
docs/src/content/docs/modules/transaction/index.md
Normal file
@@ -0,0 +1,238 @@
|
||||
---
|
||||
title: "事务系统 (Transaction)"
|
||||
description: "游戏事务处理系统,支持商店购买、玩家交易、分布式事务"
|
||||
---
|
||||
|
||||
`@esengine/transaction` 提供完整的游戏事务处理能力,基于 Saga 模式实现,支持商店购买、玩家交易、多步骤任务等场景,并提供 Redis/MongoDB 分布式事务支持。
|
||||
|
||||
## 概述
|
||||
|
||||
事务系统解决游戏中常见的数据一致性问题:
|
||||
|
||||
| 场景 | 问题 | 解决方案 |
|
||||
|------|------|----------|
|
||||
| 商店购买 | 扣款成功但物品未发放 | 原子事务,失败自动回滚 |
|
||||
| 玩家交易 | 一方物品转移另一方未收到 | Saga 补偿机制 |
|
||||
| 跨服操作 | 多服务器数据不一致 | 分布式锁 + 事务日志 |
|
||||
|
||||
## 安装
|
||||
|
||||
```bash
|
||||
npm install @esengine/transaction
|
||||
```
|
||||
|
||||
可选依赖(根据存储需求安装):
|
||||
```bash
|
||||
npm install ioredis # Redis 存储
|
||||
npm install mongodb # MongoDB 存储
|
||||
```
|
||||
|
||||
## 架构
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ Transaction Layer │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ TransactionManager - 事务管理器,协调事务生命周期 │
|
||||
│ TransactionContext - 事务上下文,封装操作和状态 │
|
||||
│ SagaOrchestrator - 分布式 Saga 编排器 │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ Storage Layer │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ MemoryStorage - 内存存储(开发/测试) │
|
||||
│ RedisStorage - Redis(分布式锁 + 缓存) │
|
||||
│ MongoStorage - MongoDB(持久化日志) │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ Operation Layer │
|
||||
├─────────────────────────────────────────────────────────────────┤
|
||||
│ CurrencyOperation - 货币操作 │
|
||||
│ InventoryOperation - 背包操作 │
|
||||
│ TradeOperation - 交易操作 │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 基础用法
|
||||
|
||||
```typescript
|
||||
import {
|
||||
TransactionManager,
|
||||
MemoryStorage,
|
||||
CurrencyOperation,
|
||||
InventoryOperation,
|
||||
} from '@esengine/transaction';
|
||||
|
||||
// 创建事务管理器
|
||||
const manager = new TransactionManager({
|
||||
storage: new MemoryStorage(),
|
||||
defaultTimeout: 10000,
|
||||
});
|
||||
|
||||
// 执行事务
|
||||
const result = await manager.run((tx) => {
|
||||
// 扣除金币
|
||||
tx.addOperation(new CurrencyOperation({
|
||||
type: 'deduct',
|
||||
playerId: 'player1',
|
||||
currency: 'gold',
|
||||
amount: 100,
|
||||
}));
|
||||
|
||||
// 添加物品
|
||||
tx.addOperation(new InventoryOperation({
|
||||
type: 'add',
|
||||
playerId: 'player1',
|
||||
itemId: 'sword_001',
|
||||
quantity: 1,
|
||||
}));
|
||||
});
|
||||
|
||||
if (result.success) {
|
||||
console.log('购买成功!');
|
||||
} else {
|
||||
console.log('购买失败:', result.error);
|
||||
}
|
||||
```
|
||||
|
||||
### 玩家交易
|
||||
|
||||
```typescript
|
||||
import { TradeOperation } from '@esengine/transaction';
|
||||
|
||||
const result = await manager.run((tx) => {
|
||||
tx.addOperation(new TradeOperation({
|
||||
tradeId: 'trade_001',
|
||||
partyA: {
|
||||
playerId: 'player1',
|
||||
items: [{ itemId: 'sword', quantity: 1 }],
|
||||
},
|
||||
partyB: {
|
||||
playerId: 'player2',
|
||||
currencies: [{ currency: 'gold', amount: 1000 }],
|
||||
},
|
||||
}));
|
||||
}, { timeout: 30000 });
|
||||
```
|
||||
|
||||
### 使用 Redis 存储
|
||||
|
||||
```typescript
|
||||
import Redis from 'ioredis';
|
||||
import { TransactionManager, RedisStorage } from '@esengine/transaction';
|
||||
|
||||
const redis = new Redis('redis://localhost:6379');
|
||||
const storage = new RedisStorage({ client: redis });
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
```
|
||||
|
||||
### 使用 MongoDB 存储
|
||||
|
||||
```typescript
|
||||
import { MongoClient } from 'mongodb';
|
||||
import { TransactionManager, MongoStorage } from '@esengine/transaction';
|
||||
|
||||
const client = new MongoClient('mongodb://localhost:27017');
|
||||
await client.connect();
|
||||
const db = client.db('game');
|
||||
|
||||
const storage = new MongoStorage({ db });
|
||||
await storage.ensureIndexes();
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
```
|
||||
|
||||
## 与 Room 集成
|
||||
|
||||
```typescript
|
||||
import { Room } from '@esengine/server';
|
||||
import { withTransactions, CurrencyOperation, RedisStorage } from '@esengine/transaction';
|
||||
|
||||
class GameRoom extends withTransactions(Room, {
|
||||
storage: new RedisStorage({ client: redisClient }),
|
||||
}) {
|
||||
@onMessage('Buy')
|
||||
async handleBuy(data: { itemId: string }, player: Player) {
|
||||
const result = await this.runTransaction((tx) => {
|
||||
tx.addOperation(new CurrencyOperation({
|
||||
type: 'deduct',
|
||||
playerId: player.id,
|
||||
currency: 'gold',
|
||||
amount: getItemPrice(data.itemId),
|
||||
}));
|
||||
});
|
||||
|
||||
if (result.success) {
|
||||
player.send('buy_success', { itemId: data.itemId });
|
||||
} else {
|
||||
player.send('buy_failed', { error: result.error });
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 文档导航
|
||||
|
||||
- [核心概念](/modules/transaction/core/) - 事务上下文、管理器、Saga 模式
|
||||
- [存储层](/modules/transaction/storage/) - MemoryStorage、RedisStorage、MongoStorage
|
||||
- [操作类](/modules/transaction/operations/) - 货币、背包、交易操作
|
||||
- [分布式事务](/modules/transaction/distributed/) - Saga 编排器、跨服务器事务
|
||||
- [API 参考](/modules/transaction/api/) - 完整 API 文档
|
||||
|
||||
## 服务令牌
|
||||
|
||||
用于依赖注入:
|
||||
|
||||
```typescript
|
||||
import {
|
||||
TransactionManagerToken,
|
||||
TransactionStorageToken,
|
||||
} from '@esengine/transaction';
|
||||
|
||||
const manager = services.get(TransactionManagerToken);
|
||||
```
|
||||
|
||||
## 最佳实践
|
||||
|
||||
### 1. 操作粒度
|
||||
|
||||
```typescript
|
||||
// ✅ 好:细粒度操作,便于回滚
|
||||
tx.addOperation(new CurrencyOperation({ type: 'deduct', ... }));
|
||||
tx.addOperation(new InventoryOperation({ type: 'add', ... }));
|
||||
|
||||
// ❌ 差:粗粒度操作,难以部分回滚
|
||||
tx.addOperation(new ComplexPurchaseOperation({ ... }));
|
||||
```
|
||||
|
||||
### 2. 超时设置
|
||||
|
||||
```typescript
|
||||
// 简单操作:短超时
|
||||
await manager.run(tx => { ... }, { timeout: 5000 });
|
||||
|
||||
// 复杂交易:长超时
|
||||
await manager.run(tx => { ... }, { timeout: 30000 });
|
||||
|
||||
// 跨服务器:更长超时
|
||||
await manager.run(tx => { ... }, { timeout: 60000, distributed: true });
|
||||
```
|
||||
|
||||
### 3. 错误处理
|
||||
|
||||
```typescript
|
||||
const result = await manager.run((tx) => { ... });
|
||||
|
||||
if (!result.success) {
|
||||
// 记录日志
|
||||
logger.error('Transaction failed', {
|
||||
transactionId: result.transactionId,
|
||||
error: result.error,
|
||||
duration: result.duration,
|
||||
});
|
||||
|
||||
// 通知用户
|
||||
player.send('error', { message: getErrorMessage(result.error) });
|
||||
}
|
||||
```
|
||||
313
docs/src/content/docs/modules/transaction/operations.md
Normal file
313
docs/src/content/docs/modules/transaction/operations.md
Normal file
@@ -0,0 +1,313 @@
|
||||
---
|
||||
title: "操作类"
|
||||
description: "内置的事务操作:货币、背包、交易"
|
||||
---
|
||||
|
||||
## BaseOperation
|
||||
|
||||
所有操作类的基类,提供通用的实现模板。
|
||||
|
||||
```typescript
|
||||
import { BaseOperation, ITransactionContext, OperationResult } from '@esengine/transaction';
|
||||
|
||||
class MyOperation extends BaseOperation<MyData, MyResult> {
|
||||
readonly name = 'myOperation';
|
||||
|
||||
async validate(ctx: ITransactionContext): Promise<boolean> {
|
||||
// 验证前置条件
|
||||
return true;
|
||||
}
|
||||
|
||||
async execute(ctx: ITransactionContext): Promise<OperationResult<MyResult>> {
|
||||
// 执行操作
|
||||
return this.success({ result: 'ok' });
|
||||
// 或
|
||||
return this.failure('Something went wrong', 'ERROR_CODE');
|
||||
}
|
||||
|
||||
async compensate(ctx: ITransactionContext): Promise<void> {
|
||||
// 回滚操作
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## CurrencyOperation
|
||||
|
||||
处理货币的增加和扣除。
|
||||
|
||||
### 扣除货币
|
||||
|
||||
```typescript
|
||||
import { CurrencyOperation } from '@esengine/transaction';
|
||||
|
||||
tx.addOperation(new CurrencyOperation({
|
||||
type: 'deduct',
|
||||
playerId: 'player1',
|
||||
currency: 'gold',
|
||||
amount: 100,
|
||||
reason: 'purchase_item',
|
||||
}));
|
||||
```
|
||||
|
||||
### 增加货币
|
||||
|
||||
```typescript
|
||||
tx.addOperation(new CurrencyOperation({
|
||||
type: 'add',
|
||||
playerId: 'player1',
|
||||
currency: 'diamond',
|
||||
amount: 50,
|
||||
reason: 'daily_reward',
|
||||
}));
|
||||
```
|
||||
|
||||
### 操作数据
|
||||
|
||||
```typescript
|
||||
interface CurrencyOperationData {
|
||||
type: 'add' | 'deduct'; // 操作类型
|
||||
playerId: string; // 玩家 ID
|
||||
currency: string; // 货币类型
|
||||
amount: number; // 数量
|
||||
reason?: string; // 原因/来源
|
||||
}
|
||||
```
|
||||
|
||||
### 操作结果
|
||||
|
||||
```typescript
|
||||
interface CurrencyOperationResult {
|
||||
beforeBalance: number; // 操作前余额
|
||||
afterBalance: number; // 操作后余额
|
||||
}
|
||||
```
|
||||
|
||||
### 自定义数据提供者
|
||||
|
||||
```typescript
|
||||
interface ICurrencyProvider {
|
||||
getBalance(playerId: string, currency: string): Promise<number>;
|
||||
setBalance(playerId: string, currency: string, amount: number): Promise<void>;
|
||||
}
|
||||
|
||||
class MyCurrencyProvider implements ICurrencyProvider {
|
||||
async getBalance(playerId: string, currency: string): Promise<number> {
|
||||
// 从数据库获取余额
|
||||
return await db.getCurrency(playerId, currency);
|
||||
}
|
||||
|
||||
async setBalance(playerId: string, currency: string, amount: number): Promise<void> {
|
||||
// 保存到数据库
|
||||
await db.setCurrency(playerId, currency, amount);
|
||||
}
|
||||
}
|
||||
|
||||
// 使用自定义提供者
|
||||
const op = new CurrencyOperation({ ... });
|
||||
op.setProvider(new MyCurrencyProvider());
|
||||
tx.addOperation(op);
|
||||
```
|
||||
|
||||
## InventoryOperation
|
||||
|
||||
处理物品的添加、移除和更新。
|
||||
|
||||
### 添加物品
|
||||
|
||||
```typescript
|
||||
import { InventoryOperation } from '@esengine/transaction';
|
||||
|
||||
tx.addOperation(new InventoryOperation({
|
||||
type: 'add',
|
||||
playerId: 'player1',
|
||||
itemId: 'sword_001',
|
||||
quantity: 1,
|
||||
properties: { enchant: 'fire' },
|
||||
}));
|
||||
```
|
||||
|
||||
### 移除物品
|
||||
|
||||
```typescript
|
||||
tx.addOperation(new InventoryOperation({
|
||||
type: 'remove',
|
||||
playerId: 'player1',
|
||||
itemId: 'potion_hp',
|
||||
quantity: 5,
|
||||
}));
|
||||
```
|
||||
|
||||
### 更新物品
|
||||
|
||||
```typescript
|
||||
tx.addOperation(new InventoryOperation({
|
||||
type: 'update',
|
||||
playerId: 'player1',
|
||||
itemId: 'sword_001',
|
||||
quantity: 1, // 可选,不传则保持原数量
|
||||
properties: { enchant: 'lightning', level: 5 },
|
||||
}));
|
||||
```
|
||||
|
||||
### 操作数据
|
||||
|
||||
```typescript
|
||||
interface InventoryOperationData {
|
||||
type: 'add' | 'remove' | 'update'; // 操作类型
|
||||
playerId: string; // 玩家 ID
|
||||
itemId: string; // 物品 ID
|
||||
quantity: number; // 数量
|
||||
properties?: Record<string, unknown>; // 物品属性
|
||||
reason?: string; // 原因/来源
|
||||
}
|
||||
```
|
||||
|
||||
### 操作结果
|
||||
|
||||
```typescript
|
||||
interface InventoryOperationResult {
|
||||
beforeItem?: ItemData; // 操作前物品
|
||||
afterItem?: ItemData; // 操作后物品
|
||||
}
|
||||
|
||||
interface ItemData {
|
||||
itemId: string;
|
||||
quantity: number;
|
||||
properties?: Record<string, unknown>;
|
||||
}
|
||||
```
|
||||
|
||||
### 自定义数据提供者
|
||||
|
||||
```typescript
|
||||
interface IInventoryProvider {
|
||||
getItem(playerId: string, itemId: string): Promise<ItemData | null>;
|
||||
setItem(playerId: string, itemId: string, item: ItemData | null): Promise<void>;
|
||||
hasCapacity?(playerId: string, count: number): Promise<boolean>;
|
||||
}
|
||||
|
||||
class MyInventoryProvider implements IInventoryProvider {
|
||||
async getItem(playerId: string, itemId: string): Promise<ItemData | null> {
|
||||
return await db.getItem(playerId, itemId);
|
||||
}
|
||||
|
||||
async setItem(playerId: string, itemId: string, item: ItemData | null): Promise<void> {
|
||||
if (item) {
|
||||
await db.saveItem(playerId, itemId, item);
|
||||
} else {
|
||||
await db.deleteItem(playerId, itemId);
|
||||
}
|
||||
}
|
||||
|
||||
async hasCapacity(playerId: string, count: number): Promise<boolean> {
|
||||
const current = await db.getItemCount(playerId);
|
||||
const max = await db.getMaxCapacity(playerId);
|
||||
return current + count <= max;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## TradeOperation
|
||||
|
||||
处理玩家之间的物品和货币交换。
|
||||
|
||||
### 基本用法
|
||||
|
||||
```typescript
|
||||
import { TradeOperation } from '@esengine/transaction';
|
||||
|
||||
tx.addOperation(new TradeOperation({
|
||||
tradeId: 'trade_001',
|
||||
partyA: {
|
||||
playerId: 'player1',
|
||||
items: [{ itemId: 'sword', quantity: 1 }],
|
||||
currencies: [{ currency: 'diamond', amount: 10 }],
|
||||
},
|
||||
partyB: {
|
||||
playerId: 'player2',
|
||||
currencies: [{ currency: 'gold', amount: 1000 }],
|
||||
},
|
||||
reason: 'player_trade',
|
||||
}));
|
||||
```
|
||||
|
||||
### 操作数据
|
||||
|
||||
```typescript
|
||||
interface TradeOperationData {
|
||||
tradeId: string; // 交易 ID
|
||||
partyA: TradeParty; // 交易发起方
|
||||
partyB: TradeParty; // 交易接收方
|
||||
reason?: string; // 原因/备注
|
||||
}
|
||||
|
||||
interface TradeParty {
|
||||
playerId: string; // 玩家 ID
|
||||
items?: TradeItem[]; // 给出的物品
|
||||
currencies?: TradeCurrency[]; // 给出的货币
|
||||
}
|
||||
|
||||
interface TradeItem {
|
||||
itemId: string;
|
||||
quantity: number;
|
||||
}
|
||||
|
||||
interface TradeCurrency {
|
||||
currency: string;
|
||||
amount: number;
|
||||
}
|
||||
```
|
||||
|
||||
### 执行流程
|
||||
|
||||
TradeOperation 内部会生成以下子操作序列:
|
||||
|
||||
```
|
||||
1. 移除 partyA 的物品
|
||||
2. 添加 partyB 的物品(来自 partyA)
|
||||
3. 扣除 partyA 的货币
|
||||
4. 增加 partyB 的货币(来自 partyA)
|
||||
5. 移除 partyB 的物品
|
||||
6. 添加 partyA 的物品(来自 partyB)
|
||||
7. 扣除 partyB 的货币
|
||||
8. 增加 partyA 的货币(来自 partyB)
|
||||
```
|
||||
|
||||
任何一步失败都会回滚之前的所有操作。
|
||||
|
||||
### 使用自定义提供者
|
||||
|
||||
```typescript
|
||||
const op = new TradeOperation({ ... });
|
||||
op.setProvider({
|
||||
currencyProvider: new MyCurrencyProvider(),
|
||||
inventoryProvider: new MyInventoryProvider(),
|
||||
});
|
||||
tx.addOperation(op);
|
||||
```
|
||||
|
||||
## 创建工厂函数
|
||||
|
||||
每个操作类都提供工厂函数:
|
||||
|
||||
```typescript
|
||||
import {
|
||||
createCurrencyOperation,
|
||||
createInventoryOperation,
|
||||
createTradeOperation,
|
||||
} from '@esengine/transaction';
|
||||
|
||||
tx.addOperation(createCurrencyOperation({
|
||||
type: 'deduct',
|
||||
playerId: 'player1',
|
||||
currency: 'gold',
|
||||
amount: 100,
|
||||
}));
|
||||
|
||||
tx.addOperation(createInventoryOperation({
|
||||
type: 'add',
|
||||
playerId: 'player1',
|
||||
itemId: 'sword',
|
||||
quantity: 1,
|
||||
}));
|
||||
```
|
||||
238
docs/src/content/docs/modules/transaction/storage.md
Normal file
238
docs/src/content/docs/modules/transaction/storage.md
Normal file
@@ -0,0 +1,238 @@
|
||||
---
|
||||
title: "存储层"
|
||||
description: "事务存储接口和实现:MemoryStorage、RedisStorage、MongoStorage"
|
||||
---
|
||||
|
||||
## 存储接口
|
||||
|
||||
所有存储实现都需要实现 `ITransactionStorage` 接口:
|
||||
|
||||
```typescript
|
||||
interface ITransactionStorage {
|
||||
// 生命周期
|
||||
close?(): Promise<void>;
|
||||
|
||||
// 分布式锁
|
||||
acquireLock(key: string, ttl: number): Promise<string | null>;
|
||||
releaseLock(key: string, token: string): Promise<boolean>;
|
||||
|
||||
// 事务日志
|
||||
saveTransaction(tx: TransactionLog): Promise<void>;
|
||||
getTransaction(id: string): Promise<TransactionLog | null>;
|
||||
updateTransactionState(id: string, state: TransactionState): Promise<void>;
|
||||
updateOperationState(txId: string, opIndex: number, state: string, error?: string): Promise<void>;
|
||||
getPendingTransactions(serverId?: string): Promise<TransactionLog[]>;
|
||||
deleteTransaction(id: string): Promise<void>;
|
||||
|
||||
// 数据操作
|
||||
get<T>(key: string): Promise<T | null>;
|
||||
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
||||
delete(key: string): Promise<boolean>;
|
||||
}
|
||||
```
|
||||
|
||||
## MemoryStorage
|
||||
|
||||
内存存储,适用于开发和测试环境。
|
||||
|
||||
```typescript
|
||||
import { MemoryStorage } from '@esengine/transaction';
|
||||
|
||||
const storage = new MemoryStorage({
|
||||
maxTransactions: 1000, // 最大事务日志数量
|
||||
});
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
```
|
||||
|
||||
### 特点
|
||||
|
||||
- ✅ 无需外部依赖
|
||||
- ✅ 快速,适合开发调试
|
||||
- ❌ 数据仅保存在内存中
|
||||
- ❌ 不支持真正的分布式锁
|
||||
- ❌ 服务重启后数据丢失
|
||||
|
||||
### 测试辅助
|
||||
|
||||
```typescript
|
||||
// 清空所有数据
|
||||
storage.clear();
|
||||
|
||||
// 获取事务数量
|
||||
console.log(storage.transactionCount);
|
||||
```
|
||||
|
||||
## RedisStorage
|
||||
|
||||
Redis 存储,适用于生产环境的分布式系统。使用工厂模式实现惰性连接。
|
||||
|
||||
```typescript
|
||||
import Redis from 'ioredis';
|
||||
import { RedisStorage } from '@esengine/transaction';
|
||||
|
||||
// 工厂模式:惰性连接,首次操作时才创建连接
|
||||
const storage = new RedisStorage({
|
||||
factory: () => new Redis('redis://localhost:6379'),
|
||||
prefix: 'tx:', // 键前缀
|
||||
transactionTTL: 86400, // 事务日志过期时间(秒)
|
||||
});
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
|
||||
// 使用后关闭连接
|
||||
await storage.close();
|
||||
|
||||
// 或使用 await using 自动关闭 (TypeScript 5.2+)
|
||||
await using storage = new RedisStorage({
|
||||
factory: () => new Redis('redis://localhost:6379')
|
||||
});
|
||||
// 作用域结束时自动关闭
|
||||
```
|
||||
|
||||
### 特点
|
||||
|
||||
- ✅ 高性能分布式锁
|
||||
- ✅ 快速读写
|
||||
- ✅ 支持 TTL 自动过期
|
||||
- ✅ 适合高并发场景
|
||||
- ❌ 需要 Redis 服务器
|
||||
|
||||
### 分布式锁实现
|
||||
|
||||
使用 Redis `SET NX EX` 实现分布式锁:
|
||||
|
||||
```typescript
|
||||
// 获取锁(原子操作)
|
||||
SET tx:lock:player:123 <token> NX EX 10
|
||||
|
||||
// 释放锁(Lua 脚本保证原子性)
|
||||
if redis.call("get", KEYS[1]) == ARGV[1] then
|
||||
return redis.call("del", KEYS[1])
|
||||
else
|
||||
return 0
|
||||
end
|
||||
```
|
||||
|
||||
### 键结构
|
||||
|
||||
```
|
||||
tx:lock:{key} - 分布式锁
|
||||
tx:tx:{id} - 事务日志
|
||||
tx:server:{id}:txs - 服务器事务索引
|
||||
tx:data:{key} - 业务数据
|
||||
```
|
||||
|
||||
## MongoStorage
|
||||
|
||||
MongoDB 存储,适用于需要持久化和复杂查询的场景。使用 `@esengine/database-drivers` 的共享连接。
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers';
|
||||
import { createMongoStorage, TransactionManager } from '@esengine/transaction';
|
||||
|
||||
// 创建共享连接
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
});
|
||||
await mongo.connect();
|
||||
|
||||
// 使用共享连接创建存储
|
||||
const storage = createMongoStorage(mongo, {
|
||||
transactionCollection: 'transactions', // 事务日志集合(可选)
|
||||
dataCollection: 'transaction_data', // 业务数据集合(可选)
|
||||
lockCollection: 'transaction_locks', // 锁集合(可选)
|
||||
});
|
||||
|
||||
// 创建索引(首次运行时执行)
|
||||
await storage.ensureIndexes();
|
||||
|
||||
const manager = new TransactionManager({ storage });
|
||||
|
||||
// 关闭存储(不会关闭共享连接)
|
||||
await storage.close();
|
||||
|
||||
// 共享连接可继续用于其他模块
|
||||
const userRepo = new UserRepository(mongo); // @esengine/database
|
||||
|
||||
// 最后关闭共享连接
|
||||
await mongo.disconnect();
|
||||
```
|
||||
|
||||
### 特点
|
||||
|
||||
- ✅ 持久化存储
|
||||
- ✅ 支持复杂查询
|
||||
- ✅ 事务日志可追溯
|
||||
- ✅ 适合需要审计的场景
|
||||
- ❌ 相比 Redis 性能略低
|
||||
- ❌ 需要 MongoDB 服务器
|
||||
|
||||
### 索引结构
|
||||
|
||||
```javascript
|
||||
// transactions 集合
|
||||
{ state: 1 }
|
||||
{ 'metadata.serverId': 1 }
|
||||
{ createdAt: 1 }
|
||||
|
||||
// transaction_locks 集合
|
||||
{ expireAt: 1 } // TTL 索引
|
||||
|
||||
// transaction_data 集合
|
||||
{ expireAt: 1 } // TTL 索引
|
||||
```
|
||||
|
||||
### 分布式锁实现
|
||||
|
||||
使用 MongoDB 唯一索引实现分布式锁:
|
||||
|
||||
```typescript
|
||||
// 获取锁
|
||||
db.transaction_locks.insertOne({
|
||||
_id: 'player:123',
|
||||
token: '<token>',
|
||||
expireAt: new Date(Date.now() + 10000)
|
||||
});
|
||||
|
||||
// 如果键已存在,检查是否过期
|
||||
db.transaction_locks.updateOne(
|
||||
{ _id: 'player:123', expireAt: { $lt: new Date() } },
|
||||
{ $set: { token: '<token>', expireAt: new Date(Date.now() + 10000) } }
|
||||
);
|
||||
```
|
||||
|
||||
## 存储选择指南
|
||||
|
||||
| 场景 | 推荐存储 | 理由 |
|
||||
|------|----------|------|
|
||||
| 开发/测试 | MemoryStorage | 无依赖,快速启动 |
|
||||
| 单机生产 | RedisStorage | 高性能,简单 |
|
||||
| 分布式系统 | RedisStorage | 真正的分布式锁 |
|
||||
| 需要审计 | MongoStorage | 持久化日志 |
|
||||
| 混合需求 | Redis + Mongo | Redis 做锁,Mongo 做日志 |
|
||||
|
||||
## 自定义存储
|
||||
|
||||
实现 `ITransactionStorage` 接口创建自定义存储:
|
||||
|
||||
```typescript
|
||||
import { ITransactionStorage, TransactionLog, TransactionState } from '@esengine/transaction';
|
||||
|
||||
class MyCustomStorage implements ITransactionStorage {
|
||||
async acquireLock(key: string, ttl: number): Promise<string | null> {
|
||||
// 实现分布式锁获取逻辑
|
||||
}
|
||||
|
||||
async releaseLock(key: string, token: string): Promise<boolean> {
|
||||
// 实现分布式锁释放逻辑
|
||||
}
|
||||
|
||||
async saveTransaction(tx: TransactionLog): Promise<void> {
|
||||
// 保存事务日志
|
||||
}
|
||||
|
||||
// ... 实现其他方法
|
||||
}
|
||||
```
|
||||
Submodule examples/lawn-mower-demo updated: 5a4976b192...ede033422b
@@ -74,6 +74,7 @@
|
||||
"lint:fix": "turbo run lint:fix",
|
||||
"build:wasm": "cd packages/rust/engine && wasm-pack build --dev --out-dir pkg",
|
||||
"build:wasm:release": "cd packages/rust/engine && wasm-pack build --release --out-dir pkg",
|
||||
"build:rapier2d": "node scripts/build-rapier2d.mjs",
|
||||
"copy-modules": "node scripts/copy-engine-modules.mjs"
|
||||
},
|
||||
"author": "yhh",
|
||||
|
||||
132
packages/editor/editor-app/README.md
Normal file
132
packages/editor/editor-app/README.md
Normal file
@@ -0,0 +1,132 @@
|
||||
# ESEngine Editor
|
||||
|
||||
A cross-platform desktop visual editor built with Tauri 2.x + React 18.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Before running the editor, ensure you have the following installed:
|
||||
|
||||
- **Node.js** >= 18.x
|
||||
- **pnpm** >= 10.x
|
||||
- **Rust** >= 1.70 (for Tauri and WASM builds)
|
||||
- **wasm-pack** (for building Rapier2D physics engine)
|
||||
- **Platform-specific dependencies**:
|
||||
- **Windows**: Microsoft Visual Studio C++ Build Tools
|
||||
- **macOS**: Xcode Command Line Tools (`xcode-select --install`)
|
||||
- **Linux**: See [Tauri prerequisites](https://tauri.app/v1/guides/getting-started/prerequisites)
|
||||
|
||||
### Installing wasm-pack
|
||||
|
||||
```bash
|
||||
# Using cargo
|
||||
cargo install wasm-pack
|
||||
|
||||
# Or using the official installer script (Linux/macOS)
|
||||
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### 1. Clone and Install
|
||||
|
||||
```bash
|
||||
git clone https://github.com/esengine/esengine.git
|
||||
cd esengine
|
||||
pnpm install
|
||||
```
|
||||
|
||||
### 2. Build Rapier2D WASM
|
||||
|
||||
The editor depends on Rapier2D physics engine WASM artifacts. First-time setup only requires one command:
|
||||
|
||||
```bash
|
||||
pnpm build:rapier2d
|
||||
```
|
||||
|
||||
This command automatically:
|
||||
1. Prepares the Rust project
|
||||
2. Builds WASM
|
||||
3. Copies artifacts to `packages/physics/rapier2d/pkg`
|
||||
4. Generates TypeScript source code
|
||||
|
||||
> **Note**: Requires Rust and wasm-pack to be installed.
|
||||
|
||||
### 3. Build Editor
|
||||
|
||||
From the project root:
|
||||
|
||||
```bash
|
||||
pnpm build:editor
|
||||
```
|
||||
|
||||
### 4. Run Editor
|
||||
|
||||
```bash
|
||||
cd packages/editor/editor-app
|
||||
pnpm tauri:dev
|
||||
```
|
||||
|
||||
## Available Scripts
|
||||
|
||||
| Script | Description |
|
||||
|--------|-------------|
|
||||
| `pnpm build:rapier2d` | Build Rapier2D WASM (required for first-time setup) |
|
||||
| `pnpm build:editor` | Build editor and all dependencies |
|
||||
| `pnpm tauri:dev` | Run editor in development mode with hot-reload |
|
||||
| `pnpm tauri:build` | Build production application |
|
||||
| `pnpm build:sdk` | Build editor-runtime SDK |
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
editor-app/
|
||||
├── src/ # React application source
|
||||
│ ├── components/ # UI components
|
||||
│ ├── panels/ # Editor panels
|
||||
│ └── services/ # Core services
|
||||
├── src-tauri/ # Tauri (Rust) backend
|
||||
├── public/ # Static assets
|
||||
└── scripts/ # Build scripts
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Rapier2D WASM Build Failed
|
||||
|
||||
**Error**: `Could not resolve "../pkg/rapier_wasm2d"`
|
||||
|
||||
**Cause**: Missing Rapier2D WASM artifacts.
|
||||
|
||||
**Solution**:
|
||||
1. Ensure `wasm-pack` is installed: `cargo install wasm-pack`
|
||||
2. Run `pnpm build:rapier2d`
|
||||
3. Verify `packages/physics/rapier2d/pkg/` directory exists and contains `rapier_wasm2d_bg.wasm` file
|
||||
|
||||
### Build Errors
|
||||
|
||||
```bash
|
||||
pnpm clean
|
||||
pnpm install
|
||||
pnpm build:editor
|
||||
```
|
||||
|
||||
### Rust/Tauri Errors
|
||||
|
||||
```bash
|
||||
rustup update
|
||||
```
|
||||
|
||||
### Windows Users Building WASM
|
||||
|
||||
The `pnpm build:rapier2d` script works directly on Windows. If you encounter issues:
|
||||
1. Use Git Bash or WSL
|
||||
2. Or download pre-built WASM artifacts from [Releases](https://github.com/esengine/esengine/releases)
|
||||
|
||||
## Documentation
|
||||
|
||||
- [ESEngine Documentation](https://esengine.cn/)
|
||||
- [Tauri Documentation](https://tauri.app/)
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
132
packages/editor/editor-app/README_CN.md
Normal file
132
packages/editor/editor-app/README_CN.md
Normal file
@@ -0,0 +1,132 @@
|
||||
# ESEngine 编辑器
|
||||
|
||||
基于 Tauri 2.x + React 18 构建的跨平台桌面可视化编辑器。
|
||||
|
||||
## 环境要求
|
||||
|
||||
运行编辑器前,请确保已安装以下环境:
|
||||
|
||||
- **Node.js** >= 18.x
|
||||
- **pnpm** >= 10.x
|
||||
- **Rust** >= 1.70 (Tauri 和 WASM 构建需要)
|
||||
- **wasm-pack** (构建 Rapier2D 物理引擎需要)
|
||||
- **平台相关依赖**:
|
||||
- **Windows**: Microsoft Visual Studio C++ Build Tools
|
||||
- **macOS**: Xcode Command Line Tools (`xcode-select --install`)
|
||||
- **Linux**: 参考 [Tauri 环境配置](https://tauri.app/v1/guides/getting-started/prerequisites)
|
||||
|
||||
### 安装 wasm-pack
|
||||
|
||||
```bash
|
||||
# 使用 cargo 安装
|
||||
cargo install wasm-pack
|
||||
|
||||
# 或使用官方安装脚本 (Linux/macOS)
|
||||
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 1. 克隆并安装
|
||||
|
||||
```bash
|
||||
git clone https://github.com/esengine/esengine.git
|
||||
cd esengine
|
||||
pnpm install
|
||||
```
|
||||
|
||||
### 2. 构建 Rapier2D WASM
|
||||
|
||||
编辑器依赖 Rapier2D 物理引擎的 WASM 产物。首次构建只需执行一条命令:
|
||||
|
||||
```bash
|
||||
pnpm build:rapier2d
|
||||
```
|
||||
|
||||
该命令会自动完成以下步骤:
|
||||
1. 准备 Rust 项目
|
||||
2. 构建 WASM
|
||||
3. 复制产物到 `packages/physics/rapier2d/pkg`
|
||||
4. 生成 TypeScript 源码
|
||||
|
||||
> **注意**:需要已安装 Rust 和 wasm-pack。
|
||||
|
||||
### 3. 构建编辑器
|
||||
|
||||
在项目根目录执行:
|
||||
|
||||
```bash
|
||||
pnpm build:editor
|
||||
```
|
||||
|
||||
### 4. 启动编辑器
|
||||
|
||||
```bash
|
||||
cd packages/editor/editor-app
|
||||
pnpm tauri:dev
|
||||
```
|
||||
|
||||
## 可用脚本
|
||||
|
||||
| 脚本 | 说明 |
|
||||
|------|------|
|
||||
| `pnpm build:rapier2d` | 构建 Rapier2D WASM(首次构建必须执行)|
|
||||
| `pnpm build:editor` | 构建编辑器及所有依赖 |
|
||||
| `pnpm tauri:dev` | 开发模式运行编辑器(支持热重载)|
|
||||
| `pnpm tauri:build` | 构建生产版本应用 |
|
||||
| `pnpm build:sdk` | 构建 editor-runtime SDK |
|
||||
|
||||
## 项目结构
|
||||
|
||||
```
|
||||
editor-app/
|
||||
├── src/ # React 应用源码
|
||||
│ ├── components/ # UI 组件
|
||||
│ ├── panels/ # 编辑器面板
|
||||
│ └── services/ # 核心服务
|
||||
├── src-tauri/ # Tauri (Rust) 后端
|
||||
├── public/ # 静态资源
|
||||
└── scripts/ # 构建脚本
|
||||
```
|
||||
|
||||
## 常见问题
|
||||
|
||||
### Rapier2D WASM 构建失败
|
||||
|
||||
**错误**: `Could not resolve "../pkg/rapier_wasm2d"`
|
||||
|
||||
**原因**: 缺少 Rapier2D 的 WASM 产物。
|
||||
|
||||
**解决方案**:
|
||||
1. 确保已安装 `wasm-pack`:`cargo install wasm-pack`
|
||||
2. 执行 `pnpm build:rapier2d`
|
||||
3. 确认 `packages/physics/rapier2d/pkg/` 目录存在且包含 `rapier_wasm2d_bg.wasm` 文件
|
||||
|
||||
### 构建错误
|
||||
|
||||
```bash
|
||||
pnpm clean
|
||||
pnpm install
|
||||
pnpm build:editor
|
||||
```
|
||||
|
||||
### Rust/Tauri 错误
|
||||
|
||||
```bash
|
||||
rustup update
|
||||
```
|
||||
|
||||
### Windows 用户构建 WASM
|
||||
|
||||
`pnpm build:rapier2d` 脚本在 Windows 上可以直接运行。如果遇到问题:
|
||||
1. 使用 Git Bash 或 WSL
|
||||
2. 或从 [Releases](https://github.com/esengine/esengine/releases) 下载预编译的 WASM 产物
|
||||
|
||||
## 文档
|
||||
|
||||
- [ESEngine 文档](https://esengine.cn/)
|
||||
- [Tauri 文档](https://tauri.app/)
|
||||
|
||||
## 许可证
|
||||
|
||||
MIT License
|
||||
@@ -9,7 +9,7 @@
|
||||
"build": "npm run build:sdk && tsc && vite build",
|
||||
"build:watch": "vite build --watch",
|
||||
"tauri": "tauri",
|
||||
"copy-modules": "node ../../scripts/copy-engine-modules.mjs",
|
||||
"copy-modules": "node ../../../scripts/copy-engine-modules.mjs",
|
||||
"tauri:dev": "npm run build:sdk && npm run copy-modules && tauri dev",
|
||||
"bundle:runtime": "node scripts/bundle-runtime.mjs",
|
||||
"tauri:build": "npm run build:sdk && npm run copy-modules && npm run bundle:runtime && tauri build",
|
||||
|
||||
1010
packages/editor/editor-app/src-tauri/Cargo.lock
generated
1010
packages/editor/editor-app/src-tauri/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -10,16 +10,16 @@ name = "ecs_editor_lib"
|
||||
crate-type = ["staticlib", "cdylib", "rlib"]
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "2.0", features = [] }
|
||||
tauri-build = { version = "2", features = [] }
|
||||
|
||||
[dependencies]
|
||||
tauri = { version = "2.0", features = ["protocol-asset"] }
|
||||
tauri-plugin-shell = "2.0"
|
||||
tauri-plugin-dialog = "2.0"
|
||||
tauri-plugin-fs = "2.0"
|
||||
tauri = { version = "2", features = ["protocol-asset"] }
|
||||
tauri-plugin-shell = "2"
|
||||
tauri-plugin-dialog = "2"
|
||||
tauri-plugin-fs = "2"
|
||||
tauri-plugin-updater = "2"
|
||||
tauri-plugin-http = "2.0"
|
||||
tauri-plugin-cli = "2.0"
|
||||
tauri-plugin-http = "2"
|
||||
tauri-plugin-cli = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
glob = "0.3"
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
"devDependencies": {
|
||||
"@esengine/ecs-framework": "workspace:*",
|
||||
"@esengine/engine-core": "workspace:*",
|
||||
"@esengine/asset-system": "workspace:*",
|
||||
"@esengine/editor-core": "workspace:*",
|
||||
"@esengine/editor-runtime": "workspace:*",
|
||||
"@esengine/node-editor": "workspace:*",
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* @zh ESEngine 行为树运行时模块
|
||||
* @en ESEngine Behavior Tree Runtime Module
|
||||
*
|
||||
* @zh 纯运行时模块,不依赖 asset-system。资产加载由编辑器在 install 时注册。
|
||||
* @en Pure runtime module, no asset-system dependency. Asset loading is registered by editor during install.
|
||||
*/
|
||||
|
||||
import type { IScene, ServiceContainer, IComponentRegistry } from '@esengine/ecs-framework';
|
||||
import type { IRuntimeModule, SystemContext } from '@esengine/engine-core';
|
||||
|
||||
import {
|
||||
BehaviorTreeRuntimeComponent,
|
||||
BehaviorTreeExecutionSystem,
|
||||
BehaviorTreeAssetManager,
|
||||
GlobalBlackboardService,
|
||||
BehaviorTreeSystemToken
|
||||
} from '@esengine/behavior-tree';
|
||||
|
||||
export class BehaviorTreeRuntimeModule implements IRuntimeModule {
|
||||
registerComponents(registry: IComponentRegistry): void {
|
||||
registry.register(BehaviorTreeRuntimeComponent);
|
||||
}
|
||||
|
||||
registerServices(services: ServiceContainer): void {
|
||||
if (!services.isRegistered(GlobalBlackboardService)) {
|
||||
services.registerSingleton(GlobalBlackboardService);
|
||||
}
|
||||
if (!services.isRegistered(BehaviorTreeAssetManager)) {
|
||||
services.registerSingleton(BehaviorTreeAssetManager);
|
||||
}
|
||||
}
|
||||
|
||||
createSystems(scene: IScene, context: SystemContext): void {
|
||||
const ecsServices = (context as { ecsServices?: ServiceContainer }).ecsServices;
|
||||
const behaviorTreeSystem = new BehaviorTreeExecutionSystem(ecsServices);
|
||||
|
||||
if (context.isEditor) {
|
||||
behaviorTreeSystem.enabled = false;
|
||||
}
|
||||
|
||||
scene.addSystem(behaviorTreeSystem);
|
||||
context.services.register(BehaviorTreeSystemToken, behaviorTreeSystem);
|
||||
}
|
||||
}
|
||||
@@ -30,8 +30,11 @@ import {
|
||||
LocaleService,
|
||||
} from '@esengine/editor-runtime';
|
||||
|
||||
// Runtime imports from @esengine/behavior-tree package
|
||||
import { BehaviorTreeRuntimeComponent, BehaviorTreeRuntimeModule } from '@esengine/behavior-tree';
|
||||
// Runtime imports
|
||||
import { BehaviorTreeRuntimeComponent, BehaviorTreeAssetType } from '@esengine/behavior-tree';
|
||||
import { AssetManagerToken } from '@esengine/asset-system';
|
||||
import { BehaviorTreeRuntimeModule } from './BehaviorTreeRuntimeModule';
|
||||
import { BehaviorTreeLoader } from './runtime/BehaviorTreeLoader';
|
||||
|
||||
// Editor components and services
|
||||
import { BehaviorTreeService } from './services/BehaviorTreeService';
|
||||
@@ -71,6 +74,10 @@ export class BehaviorTreeEditorModule implements IEditorModuleLoader {
|
||||
// 设置插件上下文
|
||||
PluginContext.setServices(services);
|
||||
|
||||
// 注册行为树资产加载器到 AssetManager
|
||||
// Register behavior tree asset loader to AssetManager
|
||||
this.registerAssetLoader();
|
||||
|
||||
// 注册服务
|
||||
this.registerServices(services);
|
||||
|
||||
@@ -92,6 +99,22 @@ export class BehaviorTreeEditorModule implements IEditorModuleLoader {
|
||||
logger.info('BehaviorTree editor module installed');
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册行为树资产加载器
|
||||
* Register behavior tree asset loader
|
||||
*/
|
||||
private registerAssetLoader(): void {
|
||||
try {
|
||||
const assetManager = PluginAPI.resolve(AssetManagerToken);
|
||||
if (assetManager) {
|
||||
assetManager.registerLoader(BehaviorTreeAssetType, new BehaviorTreeLoader());
|
||||
logger.info('BehaviorTree asset loader registered');
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn('Failed to register asset loader:', error);
|
||||
}
|
||||
}
|
||||
|
||||
private registerAssetCreationMappings(services: ServiceContainer): void {
|
||||
try {
|
||||
const fileActionRegistry = services.resolve<FileActionRegistry>(IFileActionRegistry);
|
||||
@@ -376,7 +399,7 @@ export const BehaviorTreePlugin: IEditorPlugin = {
|
||||
editorModule: new BehaviorTreeEditorModule(),
|
||||
};
|
||||
|
||||
export { BehaviorTreeRuntimeModule };
|
||||
// BehaviorTreeRuntimeModule is internal, not re-exported
|
||||
|
||||
// Re-exports for editor functionality
|
||||
export { PluginContext } from './PluginContext';
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* @zh ESEngine 资产加载器
|
||||
* @en ESEngine asset loader
|
||||
* @internal
|
||||
*/
|
||||
|
||||
import { Core } from '@esengine/ecs-framework';
|
||||
import {
|
||||
BehaviorTreeAssetManager,
|
||||
EditorToBehaviorTreeDataConverter,
|
||||
BehaviorTreeAssetType,
|
||||
type BehaviorTreeData
|
||||
} from '@esengine/behavior-tree';
|
||||
|
||||
/**
|
||||
* @zh 行为树资产接口
|
||||
* @en Behavior tree asset interface
|
||||
* @internal
|
||||
*/
|
||||
export interface IBehaviorTreeAsset {
|
||||
data: BehaviorTreeData;
|
||||
path: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 行为树加载器
|
||||
* @en Behavior tree loader implementing IAssetLoader interface
|
||||
* @internal
|
||||
*/
|
||||
export class BehaviorTreeLoader {
|
||||
readonly supportedType = BehaviorTreeAssetType;
|
||||
readonly supportedExtensions = ['.btree'];
|
||||
readonly contentType = 'text' as const;
|
||||
|
||||
async parse(content: { text?: string }, context: { metadata: { path: string } }): Promise<IBehaviorTreeAsset> {
|
||||
if (!content.text) {
|
||||
throw new Error('Behavior tree content is empty');
|
||||
}
|
||||
|
||||
const treeData = EditorToBehaviorTreeDataConverter.fromEditorJSON(content.text);
|
||||
const assetPath = context.metadata.path;
|
||||
treeData.id = assetPath;
|
||||
|
||||
const btAssetManager = Core.services.tryResolve(BehaviorTreeAssetManager);
|
||||
if (btAssetManager) {
|
||||
btAssetManager.loadAsset(treeData);
|
||||
}
|
||||
|
||||
return {
|
||||
data: treeData,
|
||||
path: assetPath
|
||||
};
|
||||
}
|
||||
|
||||
dispose(asset: IBehaviorTreeAsset): void {
|
||||
const btAssetManager = Core.services.tryResolve(BehaviorTreeAssetManager);
|
||||
if (btAssetManager && asset.data) {
|
||||
btAssetManager.unloadAsset(asset.data.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,18 @@
|
||||
{
|
||||
"extends": "../../../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"module": "ES2020",
|
||||
"moduleResolution": "bundler",
|
||||
"lib": ["ES2020", "DOM"],
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"composite": false,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"jsx": "react-jsx",
|
||||
"resolveJsonModule": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "bundler",
|
||||
"paths": {
|
||||
"@esengine/asset-system": ["../../../engine/asset-system/src"]
|
||||
}
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { defineConfig } from 'tsup';
|
||||
import { editorOnlyPreset } from '../../../tools/build-config/src/presets/plugin-tsup';
|
||||
|
||||
export default defineConfig({
|
||||
...editorOnlyPreset(),
|
||||
tsconfig: 'tsconfig.build.json'
|
||||
...editorOnlyPreset({}),
|
||||
tsconfig: 'tsconfig.build.json',
|
||||
noExternal: ['@esengine/asset-system']
|
||||
});
|
||||
|
||||
13
packages/editor/plugins/fairygui-editor/tsconfig.build.json
Normal file
13
packages/editor/plugins/fairygui-editor/tsconfig.build.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"extends": "../../../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"composite": false,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"extends": "../build-config/tsconfig.json",
|
||||
"extends": "../../../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"jsx": "react-jsx",
|
||||
|
||||
@@ -5,6 +5,7 @@ export default defineConfig({
|
||||
format: ['esm'],
|
||||
dts: true,
|
||||
clean: true,
|
||||
tsconfig: 'tsconfig.build.json',
|
||||
external: [
|
||||
'react',
|
||||
'react-dom',
|
||||
|
||||
@@ -1,5 +1,121 @@
|
||||
# @esengine/behavior-tree
|
||||
|
||||
## 4.2.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#408](https://github.com/esengine/esengine/pull/408) [`b9ea8d1`](https://github.com/esengine/esengine/commit/b9ea8d14cf38e1480f638c229f9ee150b65f0c60) Thanks [@esengine](https://github.com/esengine)! - feat: add action() and condition() methods to BehaviorTreeBuilder
|
||||
|
||||
Added new methods to support custom executor types directly in the builder:
|
||||
- `action(implementationType, name?, config?)` - Use custom action executors registered via `@NodeExecutorMetadata`
|
||||
- `condition(implementationType, name?, config?)` - Use custom condition executors
|
||||
|
||||
This provides a cleaner API for using custom node executors compared to the existing `executeAction()` which only supports blackboard functions.
|
||||
|
||||
Example:
|
||||
|
||||
```typescript
|
||||
// Define custom executor
|
||||
@NodeExecutorMetadata({
|
||||
implementationType: 'AttackAction',
|
||||
nodeType: NodeType.Action,
|
||||
displayName: 'Attack',
|
||||
category: 'Combat'
|
||||
})
|
||||
class AttackAction implements INodeExecutor {
|
||||
execute(context: NodeExecutionContext): TaskStatus {
|
||||
return TaskStatus.Success;
|
||||
}
|
||||
}
|
||||
|
||||
// Use in builder
|
||||
const tree = BehaviorTreeBuilder.create('AI')
|
||||
.selector('Root')
|
||||
.action('AttackAction', 'Attack', { damage: 50 })
|
||||
.end()
|
||||
.build();
|
||||
```
|
||||
|
||||
## 4.1.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- [#406](https://github.com/esengine/esengine/pull/406) [`0de4527`](https://github.com/esengine/esengine/commit/0de45279e612c04ae9be7fbd65ce496e4797a43c) Thanks [@esengine](https://github.com/esengine)! - fix(behavior-tree): export NodeExecutorMetadata as value instead of type
|
||||
|
||||
Fixed the export of `NodeExecutorMetadata` decorator in `execution/index.ts`.
|
||||
Previously it was exported as `export type { NodeExecutorMetadata }` which only
|
||||
exported the type signature, not the actual function. This caused runtime errors
|
||||
in Cocos Creator: "TypeError: (intermediate value) is not a function".
|
||||
|
||||
Changed to `export { NodeExecutorMetadata }` to properly export the decorator function.
|
||||
|
||||
## 4.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`3e5b778`](https://github.com/esengine/esengine/commit/3e5b7783beec08e247f7525184935401923ecde8)]:
|
||||
- @esengine/ecs-framework@2.7.1
|
||||
|
||||
## 4.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#400](https://github.com/esengine/esengine/pull/400) [`d2af9ca`](https://github.com/esengine/esengine/commit/d2af9caae9d5620c5f690272ab80dc246e9b7e10) Thanks [@esengine](https://github.com/esengine)! - feat(behavior-tree): add pure BehaviorTreePlugin class for Cocos/Laya integration
|
||||
- Added `BehaviorTreePlugin` class that only depends on `@esengine/ecs-framework`
|
||||
- Implements `IPlugin` interface with `install()`, `uninstall()`, and `setupScene()` methods
|
||||
- Removed `esengine/` subdirectory that incorrectly depended on `@esengine/engine-core`
|
||||
- Updated package documentation with correct usage examples
|
||||
|
||||
Usage:
|
||||
|
||||
```typescript
|
||||
import { Core, Scene } from '@esengine/ecs-framework';
|
||||
import { BehaviorTreePlugin, BehaviorTreeBuilder, BehaviorTreeStarter } from '@esengine/behavior-tree';
|
||||
|
||||
Core.create();
|
||||
const plugin = new BehaviorTreePlugin();
|
||||
await Core.installPlugin(plugin);
|
||||
|
||||
const scene = new Scene();
|
||||
plugin.setupScene(scene);
|
||||
Core.setScene(scene);
|
||||
```
|
||||
|
||||
## 4.0.0
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`1f3a76a`](https://github.com/esengine/esengine/commit/1f3a76aabea2d3eb8a5eb8b73e29127da57e2028)]:
|
||||
- @esengine/ecs-framework@2.7.0
|
||||
|
||||
## 3.0.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`04b08f3`](https://github.com/esengine/esengine/commit/04b08f3f073d69beb8f4be399c774bea0acb612e)]:
|
||||
- @esengine/ecs-framework@2.6.1
|
||||
|
||||
## 3.0.0
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies []:
|
||||
- @esengine/ecs-framework@2.6.0
|
||||
|
||||
## 2.0.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`a08a84b`](https://github.com/esengine/esengine/commit/a08a84b7db28e1140cbc637d442552747ad81c76)]:
|
||||
- @esengine/ecs-framework@2.5.1
|
||||
|
||||
## 2.0.0
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`1f297ac`](https://github.com/esengine/esengine/commit/1f297ac769e37700f72fb4425639af7090898256)]:
|
||||
- @esengine/ecs-framework@2.5.0
|
||||
|
||||
## 1.0.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@esengine/behavior-tree",
|
||||
"version": "1.0.3",
|
||||
"version": "4.2.0",
|
||||
"description": "ECS-based AI behavior tree system - works with any ECS framework (ESEngine, Cocos, Laya, etc.)",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.js",
|
||||
@@ -29,7 +29,8 @@
|
||||
"clean": "rimraf dist tsconfig.tsbuildinfo",
|
||||
"build": "tsup",
|
||||
"build:watch": "tsup --watch",
|
||||
"type-check": "tsc --noEmit"
|
||||
"type-check": "tsc --noEmit",
|
||||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
||||
},
|
||||
"author": "yhh",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -181,12 +181,73 @@ export class BehaviorTreeBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加执行动作
|
||||
* 添加执行动作(通过黑板函数)
|
||||
*
|
||||
* @zh 使用黑板中的 action_{actionName} 函数执行动作
|
||||
* @en Execute action using action_{actionName} function from blackboard
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* BehaviorTreeBuilder.create("AI")
|
||||
* .defineBlackboardVariable("action_Attack", (entity) => TaskStatus.Success)
|
||||
* .selector("Root")
|
||||
* .executeAction("Attack")
|
||||
* .end()
|
||||
* .build();
|
||||
* ```
|
||||
*/
|
||||
executeAction(actionName: string, name?: string): BehaviorTreeBuilder {
|
||||
return this.addActionNode('ExecuteAction', name || 'ExecuteAction', { actionName });
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加自定义动作节点
|
||||
*
|
||||
* @zh 直接使用注册的执行器类型(通过 @NodeExecutorMetadata 装饰器注册的类)
|
||||
* @en Use a registered executor type directly (class registered via @NodeExecutorMetadata decorator)
|
||||
*
|
||||
* @param implementationType - 执行器类型名称(@NodeExecutorMetadata 中的 implementationType)
|
||||
* @param name - 节点显示名称
|
||||
* @param config - 节点配置参数
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 1. 定义自定义执行器
|
||||
* @NodeExecutorMetadata({
|
||||
* implementationType: 'AttackAction',
|
||||
* nodeType: NodeType.Action,
|
||||
* displayName: '攻击动作',
|
||||
* category: 'Action'
|
||||
* })
|
||||
* class AttackAction implements INodeExecutor {
|
||||
* execute(context: NodeExecutionContext): TaskStatus {
|
||||
* console.log("执行攻击!");
|
||||
* return TaskStatus.Success;
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* // 2. 在行为树中使用
|
||||
* BehaviorTreeBuilder.create("AI")
|
||||
* .selector("Root")
|
||||
* .action("AttackAction", "Attack")
|
||||
* .end()
|
||||
* .build();
|
||||
* ```
|
||||
*/
|
||||
action(implementationType: string, name?: string, config?: Record<string, any>): BehaviorTreeBuilder {
|
||||
return this.addActionNode(implementationType, name || implementationType, config || {});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加自定义条件节点
|
||||
*
|
||||
* @zh 直接使用注册的条件执行器类型
|
||||
* @en Use a registered condition executor type directly
|
||||
*/
|
||||
condition(implementationType: string, name?: string, config?: Record<string, any>): BehaviorTreeBuilder {
|
||||
return this.addConditionNode(implementationType, name || implementationType, config || {});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加黑板比较条件
|
||||
*/
|
||||
|
||||
118
packages/framework/behavior-tree/src/BehaviorTreePlugin.ts
Normal file
118
packages/framework/behavior-tree/src/BehaviorTreePlugin.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import type { Core, ServiceContainer, IPlugin, IScene } from '@esengine/ecs-framework';
|
||||
import { BehaviorTreeExecutionSystem } from './execution/BehaviorTreeExecutionSystem';
|
||||
import { BehaviorTreeAssetManager } from './execution/BehaviorTreeAssetManager';
|
||||
import { GlobalBlackboardService } from './Services/GlobalBlackboardService';
|
||||
|
||||
/**
|
||||
* @zh 行为树插件
|
||||
* @en Behavior Tree Plugin
|
||||
*
|
||||
* @zh 为 ECS 框架提供行为树支持的插件。
|
||||
* 可与任何基于 @esengine/ecs-framework 的引擎集成(Cocos、Laya、Node.js 等)。
|
||||
*
|
||||
* @en Plugin that provides behavior tree support for ECS framework.
|
||||
* Can be integrated with any engine based on @esengine/ecs-framework (Cocos, Laya, Node.js, etc.).
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { Core, Scene } from '@esengine/ecs-framework';
|
||||
* import { BehaviorTreePlugin, BehaviorTreeBuilder, BehaviorTreeStarter } from '@esengine/behavior-tree';
|
||||
*
|
||||
* // Initialize
|
||||
* Core.create();
|
||||
* const plugin = new BehaviorTreePlugin();
|
||||
* await Core.installPlugin(plugin);
|
||||
*
|
||||
* // Setup scene
|
||||
* const scene = new Scene();
|
||||
* plugin.setupScene(scene);
|
||||
* Core.setScene(scene);
|
||||
*
|
||||
* // Create and start behavior tree
|
||||
* const tree = BehaviorTreeBuilder.create('MyAI')
|
||||
* .selector('Root')
|
||||
* .log('Hello from behavior tree!')
|
||||
* .end()
|
||||
* .build();
|
||||
*
|
||||
* const entity = scene.createEntity('AIEntity');
|
||||
* BehaviorTreeStarter.start(entity, tree);
|
||||
* ```
|
||||
*/
|
||||
export class BehaviorTreePlugin implements IPlugin {
|
||||
/**
|
||||
* @zh 插件名称
|
||||
* @en Plugin name
|
||||
*/
|
||||
readonly name = '@esengine/behavior-tree';
|
||||
|
||||
/**
|
||||
* @zh 插件版本
|
||||
* @en Plugin version
|
||||
*/
|
||||
readonly version = '1.0.0';
|
||||
|
||||
/**
|
||||
* @zh 插件依赖
|
||||
* @en Plugin dependencies
|
||||
*/
|
||||
readonly dependencies: readonly string[] = [];
|
||||
|
||||
private _services: ServiceContainer | null = null;
|
||||
|
||||
/**
|
||||
* @zh 安装插件
|
||||
* @en Install plugin
|
||||
*
|
||||
* @param _core - Core 实例
|
||||
* @param services - 服务容器
|
||||
*/
|
||||
install(_core: Core, services: ServiceContainer): void {
|
||||
this._services = services;
|
||||
|
||||
// Register services
|
||||
if (!services.isRegistered(GlobalBlackboardService)) {
|
||||
services.registerSingleton(GlobalBlackboardService);
|
||||
}
|
||||
if (!services.isRegistered(BehaviorTreeAssetManager)) {
|
||||
services.registerSingleton(BehaviorTreeAssetManager);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 卸载插件
|
||||
* @en Uninstall plugin
|
||||
*/
|
||||
uninstall(): void {
|
||||
if (this._services) {
|
||||
const assetManager = this._services.tryResolve(BehaviorTreeAssetManager);
|
||||
if (assetManager) {
|
||||
assetManager.dispose();
|
||||
}
|
||||
|
||||
const blackboardService = this._services.tryResolve(GlobalBlackboardService);
|
||||
if (blackboardService) {
|
||||
blackboardService.dispose();
|
||||
}
|
||||
}
|
||||
this._services = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 设置场景,添加行为树执行系统
|
||||
* @en Setup scene, add behavior tree execution system
|
||||
*
|
||||
* @param scene - 要设置的场景
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const scene = new Scene();
|
||||
* plugin.setupScene(scene);
|
||||
* Core.setScene(scene);
|
||||
* ```
|
||||
*/
|
||||
setupScene(scene: IScene): void {
|
||||
const system = new BehaviorTreeExecutionSystem(this._services ?? undefined);
|
||||
scene.addSystem(system);
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
/**
|
||||
* @zh ESEngine 资产加载器
|
||||
* @en ESEngine asset loader
|
||||
*
|
||||
* @zh 实现 IAssetLoader 接口,用于通过 AssetManager 加载行为树文件。
|
||||
* 此文件仅在使用 ESEngine 时需要。
|
||||
*
|
||||
* @en Implements IAssetLoader interface for loading behavior tree files via AssetManager.
|
||||
* This file is only needed when using ESEngine.
|
||||
*/
|
||||
|
||||
import type {
|
||||
IAssetLoader,
|
||||
IAssetParseContext,
|
||||
IAssetContent,
|
||||
AssetContentType
|
||||
} from '@esengine/asset-system';
|
||||
import { Core } from '@esengine/ecs-framework';
|
||||
import { BehaviorTreeData } from '../execution/BehaviorTreeData';
|
||||
import { BehaviorTreeAssetManager } from '../execution/BehaviorTreeAssetManager';
|
||||
import { EditorToBehaviorTreeDataConverter } from '../Serialization/EditorToBehaviorTreeDataConverter';
|
||||
import { BehaviorTreeAssetType } from '../constants';
|
||||
|
||||
/**
|
||||
* @zh 行为树资产接口
|
||||
* @en Behavior tree asset interface
|
||||
*/
|
||||
export interface IBehaviorTreeAsset {
|
||||
/** @zh 行为树数据 @en Behavior tree data */
|
||||
data: BehaviorTreeData;
|
||||
/** @zh 文件路径 @en File path */
|
||||
path: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 行为树加载器
|
||||
* @en Behavior tree loader implementing IAssetLoader interface
|
||||
*/
|
||||
export class BehaviorTreeLoader implements IAssetLoader<IBehaviorTreeAsset> {
|
||||
readonly supportedType = BehaviorTreeAssetType;
|
||||
readonly supportedExtensions = ['.btree'];
|
||||
readonly contentType: AssetContentType = 'text';
|
||||
|
||||
/**
|
||||
* @zh 从内容解析行为树资产
|
||||
* @en Parse behavior tree asset from content
|
||||
*/
|
||||
async parse(content: IAssetContent, context: IAssetParseContext): Promise<IBehaviorTreeAsset> {
|
||||
if (!content.text) {
|
||||
throw new Error('Behavior tree content is empty');
|
||||
}
|
||||
|
||||
// Convert to runtime data
|
||||
const treeData = EditorToBehaviorTreeDataConverter.fromEditorJSON(content.text);
|
||||
|
||||
// Use file path as ID
|
||||
const assetPath = context.metadata.path;
|
||||
treeData.id = assetPath;
|
||||
|
||||
// Also register to BehaviorTreeAssetManager for legacy code
|
||||
const btAssetManager = Core.services.tryResolve(BehaviorTreeAssetManager);
|
||||
if (btAssetManager) {
|
||||
btAssetManager.loadAsset(treeData);
|
||||
}
|
||||
|
||||
return {
|
||||
data: treeData,
|
||||
path: assetPath
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 释放资产
|
||||
* @en Dispose asset
|
||||
*/
|
||||
dispose(asset: IBehaviorTreeAsset): void {
|
||||
const btAssetManager = Core.services.tryResolve(BehaviorTreeAssetManager);
|
||||
if (btAssetManager && asset.data) {
|
||||
btAssetManager.unloadAsset(asset.data.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
/**
|
||||
* @zh ESEngine 集成模块
|
||||
* @en ESEngine integration module
|
||||
*
|
||||
* @zh 此文件包含与 ESEngine 引擎核心集成的代码。
|
||||
* 使用 Cocos/Laya 等其他引擎时不需要此文件。
|
||||
*
|
||||
* @en This file contains code for integrating with ESEngine engine-core.
|
||||
* Not needed when using other engines like Cocos/Laya.
|
||||
*/
|
||||
|
||||
import type { IScene, ServiceContainer, IComponentRegistry } from '@esengine/ecs-framework';
|
||||
import type { IRuntimeModule, IRuntimePlugin, ModuleManifest, SystemContext } from '@esengine/engine-core';
|
||||
import { AssetManagerToken } from '@esengine/asset-system';
|
||||
|
||||
import { BehaviorTreeRuntimeComponent } from '../execution/BehaviorTreeRuntimeComponent';
|
||||
import { BehaviorTreeExecutionSystem } from '../execution/BehaviorTreeExecutionSystem';
|
||||
import { BehaviorTreeAssetManager } from '../execution/BehaviorTreeAssetManager';
|
||||
import { GlobalBlackboardService } from '../Services/GlobalBlackboardService';
|
||||
import { BehaviorTreeLoader } from './BehaviorTreeLoader';
|
||||
import { BehaviorTreeAssetType } from '../constants';
|
||||
import { BehaviorTreeSystemToken } from '../tokens';
|
||||
|
||||
// Re-export tokens for ESEngine users
|
||||
export { BehaviorTreeSystemToken } from '../tokens';
|
||||
|
||||
class BehaviorTreeRuntimeModule implements IRuntimeModule {
|
||||
private _loaderRegistered = false;
|
||||
|
||||
registerComponents(registry: IComponentRegistry): void {
|
||||
registry.register(BehaviorTreeRuntimeComponent);
|
||||
}
|
||||
|
||||
registerServices(services: ServiceContainer): void {
|
||||
if (!services.isRegistered(GlobalBlackboardService)) {
|
||||
services.registerSingleton(GlobalBlackboardService);
|
||||
}
|
||||
if (!services.isRegistered(BehaviorTreeAssetManager)) {
|
||||
services.registerSingleton(BehaviorTreeAssetManager);
|
||||
}
|
||||
}
|
||||
|
||||
createSystems(scene: IScene, context: SystemContext): void {
|
||||
// Get dependencies from service registry
|
||||
const assetManager = context.services.get(AssetManagerToken);
|
||||
|
||||
if (!this._loaderRegistered && assetManager) {
|
||||
assetManager.registerLoader(BehaviorTreeAssetType, new BehaviorTreeLoader());
|
||||
this._loaderRegistered = true;
|
||||
}
|
||||
|
||||
// Use ECS service container from context.services
|
||||
const ecsServices = (context as { ecsServices?: ServiceContainer }).ecsServices;
|
||||
const behaviorTreeSystem = new BehaviorTreeExecutionSystem(ecsServices);
|
||||
|
||||
if (assetManager) {
|
||||
behaviorTreeSystem.setAssetManager(assetManager);
|
||||
}
|
||||
|
||||
if (context.isEditor) {
|
||||
behaviorTreeSystem.enabled = false;
|
||||
}
|
||||
|
||||
scene.addSystem(behaviorTreeSystem);
|
||||
|
||||
// Register service to service registry
|
||||
context.services.register(BehaviorTreeSystemToken, behaviorTreeSystem);
|
||||
}
|
||||
}
|
||||
|
||||
const manifest: ModuleManifest = {
|
||||
id: 'behavior-tree',
|
||||
name: '@esengine/behavior-tree',
|
||||
displayName: 'Behavior Tree',
|
||||
version: '1.0.0',
|
||||
description: 'AI behavior tree system',
|
||||
category: 'AI',
|
||||
icon: 'GitBranch',
|
||||
isCore: false,
|
||||
defaultEnabled: false,
|
||||
isEngineModule: true,
|
||||
canContainContent: true,
|
||||
dependencies: ['core'],
|
||||
exports: { components: ['BehaviorTreeComponent'] },
|
||||
editorPackage: '@esengine/behavior-tree-editor'
|
||||
};
|
||||
|
||||
export const BehaviorTreePlugin: IRuntimePlugin = {
|
||||
manifest,
|
||||
runtimeModule: new BehaviorTreeRuntimeModule()
|
||||
};
|
||||
|
||||
export { BehaviorTreeRuntimeModule };
|
||||
@@ -1,39 +0,0 @@
|
||||
/**
|
||||
* @zh ESEngine 集成入口
|
||||
* @en ESEngine integration entry point
|
||||
*
|
||||
* @zh 此模块包含与 ESEngine 引擎核心集成所需的所有代码。
|
||||
* 使用 Cocos/Laya 等其他引擎时,只需导入主模块即可。
|
||||
*
|
||||
* @en This module contains all code required for ESEngine engine-core integration.
|
||||
* When using other engines like Cocos/Laya, just import the main module.
|
||||
*
|
||||
* @example ESEngine 使用方式 / ESEngine usage:
|
||||
* ```typescript
|
||||
* import { BehaviorTreePlugin } from '@esengine/behavior-tree/esengine';
|
||||
*
|
||||
* // Register with ESEngine plugin system
|
||||
* engine.registerPlugin(BehaviorTreePlugin);
|
||||
* ```
|
||||
*
|
||||
* @example Cocos/Laya 使用方式 / Cocos/Laya usage:
|
||||
* ```typescript
|
||||
* import {
|
||||
* BehaviorTreeAssetManager,
|
||||
* BehaviorTreeExecutionSystem
|
||||
* } from '@esengine/behavior-tree';
|
||||
*
|
||||
* // Load behavior tree from JSON
|
||||
* const assetManager = new BehaviorTreeAssetManager();
|
||||
* assetManager.loadFromEditorJSON(jsonContent);
|
||||
*
|
||||
* // Add system to your ECS world
|
||||
* world.addSystem(new BehaviorTreeExecutionSystem());
|
||||
* ```
|
||||
*/
|
||||
|
||||
// Runtime module and plugin
|
||||
export { BehaviorTreeRuntimeModule, BehaviorTreePlugin, BehaviorTreeSystemToken } from './BehaviorTreeRuntimeModule';
|
||||
|
||||
// Asset loader for ESEngine asset-system
|
||||
export { BehaviorTreeLoader, type IBehaviorTreeAsset } from './BehaviorTreeLoader';
|
||||
@@ -5,7 +5,7 @@ export { BehaviorTreeAssetManager } from './BehaviorTreeAssetManager';
|
||||
export type { INodeExecutor, NodeExecutionContext } from './NodeExecutor';
|
||||
export { NodeExecutorRegistry, BindingHelper } from './NodeExecutor';
|
||||
export { BehaviorTreeExecutionSystem } from './BehaviorTreeExecutionSystem';
|
||||
export type { NodeMetadata, ConfigFieldDefinition, NodeExecutorMetadata } from './NodeMetadata';
|
||||
export { NodeMetadataRegistry } from './NodeMetadata';
|
||||
export type { NodeMetadata, ConfigFieldDefinition } from './NodeMetadata';
|
||||
export { NodeMetadataRegistry, NodeExecutorMetadata } from './NodeMetadata';
|
||||
|
||||
export * from './Executors';
|
||||
|
||||
@@ -4,32 +4,44 @@
|
||||
* @zh AI 行为树系统,支持运行时执行和可视化编辑
|
||||
* @en AI Behavior Tree System with runtime execution and visual editor support
|
||||
*
|
||||
* @zh 此包是通用的行为树实现,可以与任何 ECS 框架配合使用。
|
||||
* 对于 ESEngine 集成,请从 '@esengine/behavior-tree/esengine' 导入插件。
|
||||
* @zh 此包是通用的行为树实现,可以与任何基于 @esengine/ecs-framework 的引擎集成
|
||||
* (Cocos Creator、LayaAir、Node.js 等)。
|
||||
*
|
||||
* @en This package is a generic behavior tree implementation that works with any ECS framework.
|
||||
* For ESEngine integration, import the plugin from '@esengine/behavior-tree/esengine'.
|
||||
* @en This package is a generic behavior tree implementation that works with any engine
|
||||
* based on @esengine/ecs-framework (Cocos Creator, LayaAir, Node.js, etc.).
|
||||
*
|
||||
* @example Cocos/Laya/通用 ECS 使用方式:
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { Core, Scene } from '@esengine/ecs-framework';
|
||||
* import {
|
||||
* BehaviorTreeAssetManager,
|
||||
* BehaviorTreeExecutionSystem,
|
||||
* BehaviorTreeRuntimeComponent
|
||||
* BehaviorTreePlugin,
|
||||
* BehaviorTreeBuilder,
|
||||
* BehaviorTreeStarter
|
||||
* } from '@esengine/behavior-tree';
|
||||
*
|
||||
* // 1. Register service
|
||||
* Core.services.registerSingleton(BehaviorTreeAssetManager);
|
||||
* // 1. Initialize Core and install plugin
|
||||
* Core.create();
|
||||
* const plugin = new BehaviorTreePlugin();
|
||||
* await Core.installPlugin(plugin);
|
||||
*
|
||||
* // 2. Load behavior tree from JSON
|
||||
* const assetManager = Core.services.resolve(BehaviorTreeAssetManager);
|
||||
* assetManager.loadFromEditorJSON(jsonContent);
|
||||
* // 2. Create scene and setup behavior tree system
|
||||
* const scene = new Scene();
|
||||
* plugin.setupScene(scene);
|
||||
* Core.setScene(scene);
|
||||
*
|
||||
* // 3. Add component to entity
|
||||
* entity.addComponent(new BehaviorTreeRuntimeComponent());
|
||||
* // 3. Build behavior tree
|
||||
* const tree = BehaviorTreeBuilder.create('MyAI')
|
||||
* .selector('Root')
|
||||
* .log('Hello!')
|
||||
* .end()
|
||||
* .build();
|
||||
*
|
||||
* // 4. Add system to scene
|
||||
* scene.addSystem(new BehaviorTreeExecutionSystem());
|
||||
* // 4. Start behavior tree on entity
|
||||
* const entity = scene.createEntity('AIEntity');
|
||||
* BehaviorTreeStarter.start(entity, tree);
|
||||
*
|
||||
* // 5. Run game loop
|
||||
* setInterval(() => Core.update(0.016), 16);
|
||||
* ```
|
||||
*
|
||||
* @packageDocumentation
|
||||
@@ -65,3 +77,6 @@ export { BlackboardTypes } from './Blackboard/BlackboardTypes';
|
||||
|
||||
// Service tokens (using ecs-framework's createServiceToken, not engine-core)
|
||||
export { BehaviorTreeSystemToken } from './tokens';
|
||||
|
||||
// Plugin
|
||||
export { BehaviorTreePlugin } from './BehaviorTreePlugin';
|
||||
|
||||
@@ -1,5 +1,47 @@
|
||||
# @esengine/blueprint
|
||||
|
||||
## 4.0.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`3e5b778`](https://github.com/esengine/esengine/commit/3e5b7783beec08e247f7525184935401923ecde8)]:
|
||||
- @esengine/ecs-framework@2.7.1
|
||||
|
||||
## 4.0.0
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`1f3a76a`](https://github.com/esengine/esengine/commit/1f3a76aabea2d3eb8a5eb8b73e29127da57e2028)]:
|
||||
- @esengine/ecs-framework@2.7.0
|
||||
|
||||
## 3.0.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`04b08f3`](https://github.com/esengine/esengine/commit/04b08f3f073d69beb8f4be399c774bea0acb612e)]:
|
||||
- @esengine/ecs-framework@2.6.1
|
||||
|
||||
## 3.0.0
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies []:
|
||||
- @esengine/ecs-framework@2.6.0
|
||||
|
||||
## 2.0.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`a08a84b`](https://github.com/esengine/esengine/commit/a08a84b7db28e1140cbc637d442552747ad81c76)]:
|
||||
- @esengine/ecs-framework@2.5.1
|
||||
|
||||
## 2.0.0
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [[`1f297ac`](https://github.com/esengine/esengine/commit/1f297ac769e37700f72fb4425639af7090898256)]:
|
||||
- @esengine/ecs-framework@2.5.0
|
||||
|
||||
## 1.0.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@esengine/blueprint",
|
||||
"version": "1.0.2",
|
||||
"version": "4.0.1",
|
||||
"description": "Visual scripting system - works with any ECS framework (ESEngine, Cocos, Laya, etc.)",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.js",
|
||||
|
||||
@@ -1,5 +1,217 @@
|
||||
# @esengine/ecs-framework
|
||||
|
||||
## 2.7.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- [#402](https://github.com/esengine/esengine/pull/402) [`3e5b778`](https://github.com/esengine/esengine/commit/3e5b7783beec08e247f7525184935401923ecde8) Thanks [@esengine](https://github.com/esengine)! - fix(ecs): 修复 ESM 环境下 require 不存在的问题
|
||||
- 新增 `RuntimeConfig` 模块,作为运行时环境配置的独立存储
|
||||
- `Core.runtimeEnvironment` 和 `Scene.runtimeEnvironment` 现在都从 `RuntimeConfig` 读取
|
||||
- 移除 `Scene.ts` 中的 `require()` 调用,解决 Node.js ESM 环境下的兼容性问题
|
||||
|
||||
此修复解决了在 Node.js ESM 环境(如游戏服务端)中使用 `scene.isServer` 时报错 `ReferenceError: require is not defined` 的问题。
|
||||
|
||||
## 2.7.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#398](https://github.com/esengine/esengine/pull/398) [`1f3a76a`](https://github.com/esengine/esengine/commit/1f3a76aabea2d3eb8a5eb8b73e29127da57e2028) Thanks [@esengine](https://github.com/esengine)! - feat(ecs): 添加运行时环境区分机制 | add runtime environment detection
|
||||
|
||||
新增功能:
|
||||
- `Core` 新增静态属性 `runtimeEnvironment`,支持 `'server' | 'client' | 'standalone'`
|
||||
- `Core` 新增 `isServer` / `isClient` 静态只读属性
|
||||
- `ICoreConfig` 新增 `runtimeEnvironment` 配置项
|
||||
- `Scene` 新增 `isServer` / `isClient` 只读属性(默认从 Core 继承,可通过 config 覆盖)
|
||||
- 新增 `@ServerOnly()` / `@ClientOnly()` / `@NotServer()` / `@NotClient()` 方法装饰器
|
||||
|
||||
用于网络游戏中区分服务端权威逻辑和客户端逻辑:
|
||||
|
||||
```typescript
|
||||
// 方式1: 全局设置(推荐)
|
||||
Core.create({ runtimeEnvironment: 'server' });
|
||||
// 或直接设置静态属性
|
||||
Core.runtimeEnvironment = 'server';
|
||||
|
||||
// 所有场景自动继承
|
||||
const scene = new Scene();
|
||||
console.log(scene.isServer); // true
|
||||
|
||||
// 方式2: 单个场景覆盖(可选)
|
||||
const clientScene = new Scene({ runtimeEnvironment: 'client' });
|
||||
|
||||
// 在系统中检查环境
|
||||
class CollectibleSpawnSystem extends EntitySystem {
|
||||
private checkCollections(): void {
|
||||
if (!this.scene.isServer) return; // 客户端跳过
|
||||
// ... 服务端权威逻辑
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2.6.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- [#396](https://github.com/esengine/esengine/pull/396) [`04b08f3`](https://github.com/esengine/esengine/commit/04b08f3f073d69beb8f4be399c774bea0acb612e) Thanks [@esengine](https://github.com/esengine)! - fix(ecs): COMPONENT_ADDED 事件添加 entity 字段
|
||||
|
||||
修复 `ECSEventType.COMPONENT_ADDED` 事件缺少 `entity` 字段的问题,导致 ECSRoom 的 `@NetworkEntity` 自动广播功能报错。
|
||||
|
||||
## 2.6.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- feat(ecs): 添加 @NetworkEntity 装饰器,支持自动广播实体生成/销毁
|
||||
|
||||
### 新功能
|
||||
|
||||
**@NetworkEntity 装饰器**
|
||||
- 标记组件为网络实体,自动广播 spawn/despawn 消息
|
||||
- 支持 `autoSpawn` 和 `autoDespawn` 配置选项
|
||||
- 通过事件系统(`ECSEventType.COMPONENT_ADDED` / `ECSEventType.ENTITY_DESTROYED`)实现
|
||||
|
||||
**ECSRoom 增强**
|
||||
- 新增 `enableAutoNetworkEntity` 配置选项(默认启用)
|
||||
- 自动监听组件添加和实体销毁事件
|
||||
- 简化 GameRoom 实现,无需手动回调
|
||||
|
||||
### 改进
|
||||
|
||||
**Entity 事件**
|
||||
- `Entity.destroy()` 现在发出 `entity:destroyed` 事件
|
||||
- `Entity.active` 变化时发出 `entity:enabled` / `entity:disabled` 事件
|
||||
- 使用 `ECSEventType` 常量替代硬编码字符串
|
||||
|
||||
### 使用示例
|
||||
|
||||
```typescript
|
||||
import { Component, ECSComponent, sync, NetworkEntity } from '@esengine/ecs-framework';
|
||||
|
||||
@ECSComponent('Enemy')
|
||||
@NetworkEntity('Enemy')
|
||||
class EnemyComponent extends Component {
|
||||
@sync('float32') x: number = 0;
|
||||
@sync('float32') y: number = 0;
|
||||
}
|
||||
|
||||
// 服务端
|
||||
const entity = scene.createEntity('Enemy');
|
||||
entity.addComponent(new EnemyComponent()); // 自动广播 spawn
|
||||
entity.destroy(); // 自动广播 despawn
|
||||
```
|
||||
|
||||
## 2.5.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- [#392](https://github.com/esengine/esengine/pull/392) [`a08a84b`](https://github.com/esengine/esengine/commit/a08a84b7db28e1140cbc637d442552747ad81c76) Thanks [@esengine](https://github.com/esengine)! - fix(sync): Decoder 现在使用 GlobalComponentRegistry 查找组件 | Decoder now uses GlobalComponentRegistry for component lookup
|
||||
|
||||
**问题 | Problem:**
|
||||
1. `Decoder.ts` 有自己独立的 `componentRegistry` Map,与 `GlobalComponentRegistry` 完全分离。这导致通过 `@ECSComponent` 装饰器注册的组件在网络反序列化时找不到,产生 "Unknown component type" 错误。
|
||||
2. `@sync` 装饰器使用 `constructor.name` 作为 `typeId`,而不是 `@ECSComponent` 装饰器指定的名称,导致编码和解码使用不同的类型 ID。
|
||||
3. `Decoder.ts` had its own local `componentRegistry` Map that was completely separate from `GlobalComponentRegistry`. This caused components registered via `@ECSComponent` decorator to not be found during network deserialization, resulting in "Unknown component type" errors.
|
||||
4. `@sync` decorator used `constructor.name` as `typeId` instead of the name specified by `@ECSComponent` decorator, causing encoding and decoding to use different type IDs.
|
||||
|
||||
**修改 | Changes:**
|
||||
- 从 Decoder.ts 中移除本地 `componentRegistry`
|
||||
- 更新 `decodeEntity` 和 `decodeSpawn` 使用 `GlobalComponentRegistry.getComponentType()`
|
||||
- 移除已废弃的 `registerSyncComponent` 和 `autoRegisterSyncComponent` 函数
|
||||
- 更新 `@sync` 装饰器使用 `getComponentTypeName()` 获取组件类型名称
|
||||
- 更新 `@ECSComponent` 装饰器同步更新 `SYNC_METADATA.typeId`
|
||||
- Removed local `componentRegistry` from Decoder.ts
|
||||
- Updated `decodeEntity` and `decodeSpawn` to use `GlobalComponentRegistry.getComponentType()`
|
||||
- Removed deprecated `registerSyncComponent` and `autoRegisterSyncComponent` functions
|
||||
- Updated `@sync` decorator to use `getComponentTypeName()` for component type name
|
||||
- Updated `@ECSComponent` decorator to sync update `SYNC_METADATA.typeId`
|
||||
|
||||
现在使用 `@ECSComponent` 装饰器的组件会自动可用于网络同步解码,无需手动注册。
|
||||
|
||||
Now `@ECSComponent` decorated components are automatically available for network sync decoding without any manual registration.
|
||||
|
||||
## 2.5.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#390](https://github.com/esengine/esengine/pull/390) [`1f297ac`](https://github.com/esengine/esengine/commit/1f297ac769e37700f72fb4425639af7090898256) Thanks [@esengine](https://github.com/esengine)! - feat: ECS 网络状态同步系统
|
||||
|
||||
## @esengine/ecs-framework
|
||||
|
||||
新增 `@sync` 装饰器和二进制编解码器,支持基于 Component 的网络状态同步:
|
||||
|
||||
```typescript
|
||||
import { Component, ECSComponent, sync } from '@esengine/ecs-framework';
|
||||
|
||||
@ECSComponent('Player')
|
||||
class PlayerComponent extends Component {
|
||||
@sync('string') name: string = '';
|
||||
@sync('uint16') score: number = 0;
|
||||
@sync('float32') x: number = 0;
|
||||
@sync('float32') y: number = 0;
|
||||
}
|
||||
```
|
||||
|
||||
### 新增导出
|
||||
- `sync` - 标记需要同步的字段装饰器
|
||||
- `SyncType` - 支持的同步类型
|
||||
- `SyncOperation` - 同步操作类型(FULL/DELTA/SPAWN/DESPAWN)
|
||||
- `encodeSnapshot` / `decodeSnapshot` - 批量编解码
|
||||
- `encodeSpawn` / `decodeSpawn` - 实体生成编解码
|
||||
- `encodeDespawn` / `processDespawn` - 实体销毁编解码
|
||||
- `ChangeTracker` - 字段级变更追踪
|
||||
- `initChangeTracker` / `clearChanges` / `hasChanges` - 变更追踪工具函数
|
||||
|
||||
### 内部方法标记
|
||||
|
||||
将以下方法标记为 `@internal`,用户应通过 `Core.update()` 驱动更新:
|
||||
- `Scene.update()`
|
||||
- `SceneManager.update()`
|
||||
- `WorldManager.updateAll()`
|
||||
|
||||
## @esengine/network
|
||||
|
||||
新增 `ComponentSyncSystem`,基于 `@sync` 装饰器自动同步组件状态:
|
||||
|
||||
```typescript
|
||||
import { ComponentSyncSystem } from '@esengine/network';
|
||||
|
||||
// 服务端:编码状态
|
||||
const data = syncSystem.encodeAllEntities(false);
|
||||
|
||||
// 客户端:解码状态
|
||||
syncSystem.applySnapshot(data);
|
||||
```
|
||||
|
||||
### 修复
|
||||
- 将 `@esengine/ecs-framework` 从 devDependencies 移到 peerDependencies
|
||||
|
||||
## @esengine/server
|
||||
|
||||
新增 `ECSRoom`,带有 ECS World 支持的房间基类:
|
||||
|
||||
```typescript
|
||||
import { ECSRoom } from '@esengine/server/ecs';
|
||||
|
||||
// 服务端启动
|
||||
Core.create();
|
||||
setInterval(() => Core.update(1 / 60), 16);
|
||||
|
||||
// 定义房间
|
||||
class GameRoom extends ECSRoom {
|
||||
onCreate() {
|
||||
this.addSystem(new PhysicsSystem());
|
||||
}
|
||||
|
||||
onJoin(player: Player) {
|
||||
const entity = this.createPlayerEntity(player.id);
|
||||
entity.addComponent(new PlayerComponent());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 设计
|
||||
- 每个 `ECSRoom` 在 `Core.worldManager` 中创建独立的 World
|
||||
- `Core.update()` 统一更新 Time 和所有 World
|
||||
- `onTick()` 只处理状态同步逻辑
|
||||
|
||||
## 2.4.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
{
|
||||
"name": "@esengine/ecs-framework",
|
||||
"version": "2.4.4",
|
||||
"version": "2.7.1",
|
||||
"description": "用于Laya、Cocos Creator等JavaScript游戏引擎的高性能ECS框架",
|
||||
"main": "dist/index.cjs",
|
||||
"module": "dist/index.mjs",
|
||||
"types": "dist/index.d.ts",
|
||||
"unpkg": "dist/index.umd.js",
|
||||
"sideEffects": false,
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.mjs",
|
||||
"require": "./dist/index.cjs"
|
||||
"require": "./dist/index.cjs",
|
||||
"source": "./src/index.ts"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
@@ -50,23 +52,24 @@
|
||||
"@babel/plugin-transform-nullish-coalescing-operator": "^7.27.1",
|
||||
"@babel/plugin-transform-optional-chaining": "^7.27.1",
|
||||
"@babel/preset-env": "^7.28.3",
|
||||
"@eslint/js": "^9.37.0",
|
||||
"@jest/globals": "^29.7.0",
|
||||
"@rollup/plugin-babel": "^6.0.4",
|
||||
"@rollup/plugin-commonjs": "^28.0.3",
|
||||
"@rollup/plugin-node-resolve": "^16.0.1",
|
||||
"@rollup/plugin-terser": "^0.4.4",
|
||||
"@jest/globals": "^29.7.0",
|
||||
"@types/jest": "^29.5.14",
|
||||
"@types/node": "^20.19.17",
|
||||
"@eslint/js": "^9.37.0",
|
||||
"eslint": "^9.37.0",
|
||||
"typescript-eslint": "^8.46.1",
|
||||
"jest": "^29.7.0",
|
||||
"jest-environment-jsdom": "^29.7.0",
|
||||
"rimraf": "^5.0.0",
|
||||
"rollup": "^4.42.0",
|
||||
"rollup-plugin-dts": "^6.2.1",
|
||||
"rollup-plugin-sourcemaps": "^0.6.3",
|
||||
"ts-jest": "^29.4.0",
|
||||
"typescript": "^5.8.3"
|
||||
"typescript": "^5.8.3",
|
||||
"typescript-eslint": "^8.46.1"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Time } from './Utils/Time';
|
||||
import { PerformanceMonitor } from './Utils/PerformanceMonitor';
|
||||
import { PoolManager } from './Utils/Pool/PoolManager';
|
||||
import { DebugManager } from './Utils/Debug';
|
||||
import { ICoreConfig, IECSDebugConfig } from './Types';
|
||||
import { ICoreConfig, IECSDebugConfig, RuntimeEnvironment } from './Types';
|
||||
import { createLogger } from './Utils/Logger';
|
||||
import { SceneManager } from './ECS/SceneManager';
|
||||
import { IScene } from './ECS/IScene';
|
||||
@@ -16,6 +16,7 @@ import { IPlugin } from './Core/Plugin';
|
||||
import { WorldManager } from './ECS/WorldManager';
|
||||
import { DebugConfigService } from './Utils/Debug/DebugConfigService';
|
||||
import { createInstance } from './Core/DI/Decorators';
|
||||
import { RuntimeConfig } from './RuntimeConfig';
|
||||
|
||||
/**
|
||||
* @zh 游戏引擎核心类
|
||||
@@ -63,6 +64,53 @@ export class Core {
|
||||
*/
|
||||
public static paused = false;
|
||||
|
||||
/**
|
||||
* @zh 运行时环境
|
||||
* @en Runtime environment
|
||||
*
|
||||
* @zh 全局运行时环境设置。所有 Scene 默认继承此值。
|
||||
* 服务端框架(如 @esengine/server)应在启动时设置为 'server'。
|
||||
* 客户端应用应设置为 'client'。
|
||||
* 单机游戏使用默认值 'standalone'。
|
||||
*
|
||||
* @en Global runtime environment setting. All Scenes inherit this value by default.
|
||||
* Server frameworks (like @esengine/server) should set this to 'server' at startup.
|
||||
* Client apps should set this to 'client'.
|
||||
* Standalone games use the default 'standalone'.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // @zh 服务端启动时设置 | @en Set at server startup
|
||||
* Core.runtimeEnvironment = 'server';
|
||||
*
|
||||
* // @zh 或在 Core.create 时配置 | @en Or configure in Core.create
|
||||
* Core.create({ runtimeEnvironment: 'server' });
|
||||
* ```
|
||||
*/
|
||||
public static get runtimeEnvironment(): RuntimeEnvironment {
|
||||
return RuntimeConfig.runtimeEnvironment;
|
||||
}
|
||||
|
||||
public static set runtimeEnvironment(value: RuntimeEnvironment) {
|
||||
RuntimeConfig.runtimeEnvironment = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 是否在服务端运行
|
||||
* @en Whether running on server
|
||||
*/
|
||||
public static get isServer(): boolean {
|
||||
return RuntimeConfig.isServer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 是否在客户端运行
|
||||
* @en Whether running on client
|
||||
*/
|
||||
public static get isClient(): boolean {
|
||||
return RuntimeConfig.isClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 全局核心实例,可能为null表示Core尚未初始化或已被销毁
|
||||
* @en Global core instance, null means Core is not initialized or destroyed
|
||||
@@ -133,6 +181,11 @@ export class Core {
|
||||
this._config = { debug: true, ...config };
|
||||
this._serviceContainer = new ServiceContainer();
|
||||
|
||||
// 设置全局运行时环境
|
||||
if (config.runtimeEnvironment) {
|
||||
Core.runtimeEnvironment = config.runtimeEnvironment;
|
||||
}
|
||||
|
||||
this._timerManager = new TimerManager();
|
||||
this._serviceContainer.registerInstance(TimerManager, this._timerManager);
|
||||
|
||||
|
||||
@@ -10,10 +10,16 @@ import { Int32 } from './Core/SoAStorage';
|
||||
* @en Components in ECS architecture should be pure data containers.
|
||||
* All game logic should be implemented in EntitySystem, not inside components.
|
||||
*
|
||||
* @zh **重要:所有 Component 子类都必须使用 @ECSComponent 装饰器!**
|
||||
* @zh 该装饰器用于注册组件类型名称,是序列化、网络同步等功能正常工作的前提。
|
||||
* @en **IMPORTANT: All Component subclasses MUST use the @ECSComponent decorator!**
|
||||
* @en This decorator registers the component type name, which is required for serialization, network sync, etc.
|
||||
*
|
||||
* @example
|
||||
* @zh 推荐做法:纯数据组件
|
||||
* @en Recommended: Pure data component
|
||||
* @zh 正确做法:使用 @ECSComponent 装饰器
|
||||
* @en Correct: Use @ECSComponent decorator
|
||||
* ```typescript
|
||||
* @ECSComponent('HealthComponent')
|
||||
* class HealthComponent extends Component {
|
||||
* public health: number = 100;
|
||||
* public maxHealth: number = 100;
|
||||
|
||||
188
packages/framework/core/src/ECS/Decorators/RuntimeEnvironment.ts
Normal file
188
packages/framework/core/src/ECS/Decorators/RuntimeEnvironment.ts
Normal file
@@ -0,0 +1,188 @@
|
||||
/**
|
||||
* @zh 运行时环境装饰器
|
||||
* @en Runtime Environment Decorators
|
||||
*
|
||||
* @zh 提供 @ServerOnly 和 @ClientOnly 装饰器,用于标记只在特定环境执行的方法
|
||||
* @en Provides @ServerOnly and @ClientOnly decorators to mark methods that only execute in specific environments
|
||||
*/
|
||||
|
||||
import type { EntitySystem } from '../Systems/EntitySystem';
|
||||
|
||||
/**
|
||||
* @zh 服务端专用方法装饰器
|
||||
* @en Server-only method decorator
|
||||
*
|
||||
* @zh 被装饰的方法只会在服务端环境执行(scene.isServer === true)。
|
||||
* 在客户端或单机模式下,方法调用会被静默跳过。
|
||||
*
|
||||
* @en Decorated methods only execute in server environment (scene.isServer === true).
|
||||
* In client or standalone mode, method calls are silently skipped.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* class CollectibleSpawnSystem extends EntitySystem {
|
||||
* @ServerOnly()
|
||||
* private checkCollections(players: readonly Entity[]): void {
|
||||
* // 只在服务端执行收集检测
|
||||
* // Only check collections on server
|
||||
* for (const entity of this.scene.entities.buffer) {
|
||||
* // ...
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function ServerOnly(): MethodDecorator {
|
||||
return function <T>(
|
||||
_target: object,
|
||||
_propertyKey: string | symbol,
|
||||
descriptor: TypedPropertyDescriptor<T>
|
||||
): TypedPropertyDescriptor<T> | void {
|
||||
const originalMethod = descriptor.value as unknown as (...args: unknown[]) => unknown;
|
||||
|
||||
if (typeof originalMethod !== 'function') {
|
||||
throw new Error(`@ServerOnly can only be applied to methods, not ${typeof originalMethod}`);
|
||||
}
|
||||
|
||||
descriptor.value = function (this: EntitySystem, ...args: unknown[]): unknown {
|
||||
if (!this.scene?.isServer) {
|
||||
return undefined;
|
||||
}
|
||||
return originalMethod.apply(this, args);
|
||||
} as unknown as T;
|
||||
|
||||
return descriptor;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 客户端专用方法装饰器
|
||||
* @en Client-only method decorator
|
||||
*
|
||||
* @zh 被装饰的方法只会在客户端环境执行(scene.isClient === true)。
|
||||
* 在服务端或单机模式下,方法调用会被静默跳过。
|
||||
*
|
||||
* @en Decorated methods only execute in client environment (scene.isClient === true).
|
||||
* In server or standalone mode, method calls are silently skipped.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* class RenderSystem extends EntitySystem {
|
||||
* @ClientOnly()
|
||||
* private updateVisuals(): void {
|
||||
* // 只在客户端执行渲染逻辑
|
||||
* // Only update visuals on client
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function ClientOnly(): MethodDecorator {
|
||||
return function <T>(
|
||||
_target: object,
|
||||
_propertyKey: string | symbol,
|
||||
descriptor: TypedPropertyDescriptor<T>
|
||||
): TypedPropertyDescriptor<T> | void {
|
||||
const originalMethod = descriptor.value as unknown as (...args: unknown[]) => unknown;
|
||||
|
||||
if (typeof originalMethod !== 'function') {
|
||||
throw new Error(`@ClientOnly can only be applied to methods, not ${typeof originalMethod}`);
|
||||
}
|
||||
|
||||
descriptor.value = function (this: EntitySystem, ...args: unknown[]): unknown {
|
||||
if (!this.scene?.isClient) {
|
||||
return undefined;
|
||||
}
|
||||
return originalMethod.apply(this, args);
|
||||
} as unknown as T;
|
||||
|
||||
return descriptor;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 非客户端环境方法装饰器
|
||||
* @en Non-client method decorator
|
||||
*
|
||||
* @zh 被装饰的方法在服务端和单机模式下执行,但不在客户端执行。
|
||||
* 用于需要在服务端和单机都运行,但客户端跳过的逻辑。
|
||||
*
|
||||
* @en Decorated methods execute in server and standalone mode, but not on client.
|
||||
* Used for logic that should run on server and standalone, but skip on client.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* class SpawnSystem extends EntitySystem {
|
||||
* @NotClient()
|
||||
* private spawnEntities(): void {
|
||||
* // 服务端和单机模式执行,客户端跳过
|
||||
* // Execute on server and standalone, skip on client
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function NotClient(): MethodDecorator {
|
||||
return function <T>(
|
||||
_target: object,
|
||||
_propertyKey: string | symbol,
|
||||
descriptor: TypedPropertyDescriptor<T>
|
||||
): TypedPropertyDescriptor<T> | void {
|
||||
const originalMethod = descriptor.value as unknown as (...args: unknown[]) => unknown;
|
||||
|
||||
if (typeof originalMethod !== 'function') {
|
||||
throw new Error(`@NotClient can only be applied to methods, not ${typeof originalMethod}`);
|
||||
}
|
||||
|
||||
descriptor.value = function (this: EntitySystem, ...args: unknown[]): unknown {
|
||||
if (this.scene?.isClient) {
|
||||
return undefined;
|
||||
}
|
||||
return originalMethod.apply(this, args);
|
||||
} as unknown as T;
|
||||
|
||||
return descriptor;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 非服务端环境方法装饰器
|
||||
* @en Non-server method decorator
|
||||
*
|
||||
* @zh 被装饰的方法在客户端和单机模式下执行,但不在服务端执行。
|
||||
* 用于需要在客户端和单机都运行,但服务端跳过的逻辑(如渲染、音效)。
|
||||
*
|
||||
* @en Decorated methods execute in client and standalone mode, but not on server.
|
||||
* Used for logic that should run on client and standalone, but skip on server (like rendering, audio).
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* class AudioSystem extends EntitySystem {
|
||||
* @NotServer()
|
||||
* private playSound(): void {
|
||||
* // 客户端和单机模式执行,服务端跳过
|
||||
* // Execute on client and standalone, skip on server
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function NotServer(): MethodDecorator {
|
||||
return function <T>(
|
||||
_target: object,
|
||||
_propertyKey: string | symbol,
|
||||
descriptor: TypedPropertyDescriptor<T>
|
||||
): TypedPropertyDescriptor<T> | void {
|
||||
const originalMethod = descriptor.value as unknown as (...args: unknown[]) => unknown;
|
||||
|
||||
if (typeof originalMethod !== 'function') {
|
||||
throw new Error(`@NotServer can only be applied to methods, not ${typeof originalMethod}`);
|
||||
}
|
||||
|
||||
descriptor.value = function (this: EntitySystem, ...args: unknown[]): unknown {
|
||||
if (this.scene?.isServer) {
|
||||
return undefined;
|
||||
}
|
||||
return originalMethod.apply(this, args);
|
||||
} as unknown as T;
|
||||
|
||||
return descriptor;
|
||||
};
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
type ComponentEditorOptions,
|
||||
type ComponentType
|
||||
} from '../Core/ComponentStorage/ComponentTypeUtils';
|
||||
import { SYNC_METADATA, type SyncMetadata } from '../Sync/types';
|
||||
|
||||
/**
|
||||
* 存储系统类型名称的Symbol键
|
||||
@@ -138,6 +139,14 @@ export function ECSComponent(typeName: string, options?: ComponentOptions) {
|
||||
metadata[COMPONENT_EDITOR_OPTIONS] = options.editor;
|
||||
}
|
||||
|
||||
// 更新 @sync 装饰器创建的 SYNC_METADATA.typeId(如果存在)
|
||||
// Update SYNC_METADATA.typeId created by @sync decorator (if exists)
|
||||
// Property decorators execute before class decorators, so @sync may have used constructor.name
|
||||
const syncMeta = (target as any)[SYNC_METADATA] as SyncMetadata | undefined;
|
||||
if (syncMeta) {
|
||||
syncMeta.typeId = typeName;
|
||||
}
|
||||
|
||||
// 自动注册到全局 ComponentRegistry,使组件可以通过名称查找
|
||||
// Auto-register to GlobalComponentRegistry, enabling lookup by name
|
||||
GlobalComponentRegistry.register(target);
|
||||
|
||||
@@ -82,3 +82,14 @@ export {
|
||||
hasSchedulingMetadata,
|
||||
SCHEDULING_METADATA
|
||||
} from './SystemScheduling';
|
||||
|
||||
// ============================================================================
|
||||
// Runtime Environment Decorators
|
||||
// 运行时环境装饰器
|
||||
// ============================================================================
|
||||
export {
|
||||
ServerOnly,
|
||||
ClientOnly,
|
||||
NotServer,
|
||||
NotClient
|
||||
} from './RuntimeEnvironment';
|
||||
|
||||
@@ -7,14 +7,7 @@ import { getComponentInstanceTypeName, getComponentTypeName } from './Decorators
|
||||
import { generateGUID } from '../Utils/GUID';
|
||||
import type { IScene } from './IScene';
|
||||
import { EntityHandle, NULL_HANDLE } from './Core/EntityHandle';
|
||||
|
||||
/**
|
||||
* @zh 组件活跃状态变化接口
|
||||
* @en Interface for component active state change
|
||||
*/
|
||||
interface IActiveChangeable {
|
||||
onActiveChanged(): void;
|
||||
}
|
||||
import { ECSEventType } from './CoreEvents';
|
||||
|
||||
/**
|
||||
* @zh 比较两个实体的优先级
|
||||
@@ -482,9 +475,10 @@ export class Entity {
|
||||
}
|
||||
|
||||
if (this.scene.eventSystem) {
|
||||
this.scene.eventSystem.emitSync('component:added', {
|
||||
this.scene.eventSystem.emitSync(ECSEventType.COMPONENT_ADDED, {
|
||||
timestamp: Date.now(),
|
||||
source: 'Entity',
|
||||
entity: this,
|
||||
entityId: this.id,
|
||||
entityName: this.name,
|
||||
entityTag: this.tag?.toString(),
|
||||
@@ -639,7 +633,7 @@ export class Entity {
|
||||
component.entityId = null;
|
||||
|
||||
if (this.scene?.eventSystem) {
|
||||
this.scene.eventSystem.emitSync('component:removed', {
|
||||
this.scene.eventSystem.emitSync(ECSEventType.COMPONENT_REMOVED, {
|
||||
timestamp: Date.now(),
|
||||
source: 'Entity',
|
||||
entityId: this.id,
|
||||
@@ -770,19 +764,23 @@ export class Entity {
|
||||
}
|
||||
|
||||
/**
|
||||
* 活跃状态改变时的回调
|
||||
* @zh 活跃状态改变时的回调
|
||||
* @en Callback when active state changes
|
||||
*
|
||||
* @zh 通过事件系统发出 ENTITY_ENABLED 或 ENTITY_DISABLED 事件,
|
||||
* 组件可以通过监听这些事件来响应实体状态变化。
|
||||
* @en Emits ENTITY_ENABLED or ENTITY_DISABLED event through the event system.
|
||||
* Components can listen to these events to respond to entity state changes.
|
||||
*/
|
||||
private onActiveChanged(): void {
|
||||
for (const component of this.components) {
|
||||
if ('onActiveChanged' in component && typeof component.onActiveChanged === 'function') {
|
||||
(component as IActiveChangeable).onActiveChanged();
|
||||
}
|
||||
}
|
||||
if (this.scene?.eventSystem) {
|
||||
const eventType = this._active
|
||||
? ECSEventType.ENTITY_ENABLED
|
||||
: ECSEventType.ENTITY_DISABLED;
|
||||
|
||||
if (this.scene && this.scene.eventSystem) {
|
||||
this.scene.eventSystem.emitSync('entity:activeChanged', {
|
||||
this.scene.eventSystem.emitSync(eventType, {
|
||||
entity: this,
|
||||
active: this._active
|
||||
scene: this.scene,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -801,6 +799,15 @@ export class Entity {
|
||||
|
||||
this._isDestroyed = true;
|
||||
|
||||
// 在清理之前发出销毁事件(组件仍然可访问)
|
||||
if (this.scene?.eventSystem) {
|
||||
this.scene.eventSystem.emitSync(ECSEventType.ENTITY_DESTROYED, {
|
||||
entity: this,
|
||||
entityId: this.id,
|
||||
scene: this.scene,
|
||||
});
|
||||
}
|
||||
|
||||
if (this.scene && this.scene.referenceTracker) {
|
||||
this.scene.referenceTracker.clearReferencesTo(this.id);
|
||||
this.scene.referenceTracker.unregisterEntityScene(this.id);
|
||||
|
||||
@@ -12,6 +12,10 @@ import type { ServiceContainer, ServiceType } from '../Core/ServiceContainer';
|
||||
import type { TypedQueryBuilder } from './Core/Query/TypedQuery';
|
||||
import type { SceneSerializationOptions, SceneDeserializationOptions } from './Serialization/SceneSerializer';
|
||||
import type { IncrementalSnapshot, IncrementalSerializationOptions } from './Serialization/IncrementalSerializer';
|
||||
import type { RuntimeEnvironment } from '../Types';
|
||||
|
||||
// Re-export for convenience
|
||||
export type { RuntimeEnvironment };
|
||||
|
||||
/**
|
||||
* 场景接口定义
|
||||
@@ -113,6 +117,27 @@ export type IScene = {
|
||||
*/
|
||||
isEditorMode: boolean;
|
||||
|
||||
/**
|
||||
* @zh 运行时环境
|
||||
* @en Runtime environment
|
||||
*
|
||||
* @zh 标识场景运行在服务端、客户端还是单机模式
|
||||
* @en Indicates whether scene runs on server, client, or standalone mode
|
||||
*/
|
||||
readonly runtimeEnvironment: RuntimeEnvironment;
|
||||
|
||||
/**
|
||||
* @zh 是否在服务端运行
|
||||
* @en Whether running on server
|
||||
*/
|
||||
readonly isServer: boolean;
|
||||
|
||||
/**
|
||||
* @zh 是否在客户端运行
|
||||
* @en Whether running on client
|
||||
*/
|
||||
readonly isClient: boolean;
|
||||
|
||||
/**
|
||||
* 获取系统列表
|
||||
*/
|
||||
@@ -395,4 +420,18 @@ export type ISceneConfig = {
|
||||
* @default 10
|
||||
*/
|
||||
maxSystemErrorCount?: number;
|
||||
|
||||
/**
|
||||
* @zh 运行时环境
|
||||
* @en Runtime environment
|
||||
*
|
||||
* @zh 用于区分场景运行在服务端、客户端还是单机模式。
|
||||
* 配合 @ServerOnly / @ClientOnly 装饰器使用,可以让系统方法只在特定环境执行。
|
||||
*
|
||||
* @en Used to distinguish whether scene runs on server, client, or standalone mode.
|
||||
* Works with @ServerOnly / @ClientOnly decorators to make system methods execute only in specific environments.
|
||||
*
|
||||
* @default 'standalone'
|
||||
*/
|
||||
runtimeEnvironment?: RuntimeEnvironment;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ import type { IComponentRegistry } from './Core/ComponentStorage';
|
||||
import { QuerySystem } from './Core/QuerySystem';
|
||||
import { TypeSafeEventSystem } from './Core/EventSystem';
|
||||
import { ReferenceTracker } from './Core/ReferenceTracker';
|
||||
import { IScene, ISceneConfig } from './IScene';
|
||||
import { IScene, ISceneConfig, RuntimeEnvironment } from './IScene';
|
||||
import { RuntimeConfig } from '../RuntimeConfig';
|
||||
import { getComponentInstanceTypeName, getSystemInstanceTypeName, getSystemMetadata, getSystemInstanceMetadata } from './Decorators';
|
||||
import { TypedQueryBuilder } from './Core/Query/TypedQuery';
|
||||
import {
|
||||
@@ -180,6 +181,45 @@ export class Scene implements IScene {
|
||||
*/
|
||||
public isEditorMode: boolean = false;
|
||||
|
||||
/**
|
||||
* @zh 场景级别的运行时环境覆盖
|
||||
* @en Scene-level runtime environment override
|
||||
*
|
||||
* @zh 如果未设置,则从 Core.runtimeEnvironment 读取
|
||||
* @en If not set, reads from Core.runtimeEnvironment
|
||||
*/
|
||||
private _runtimeEnvironmentOverride: RuntimeEnvironment | undefined;
|
||||
|
||||
/**
|
||||
* @zh 获取运行时环境
|
||||
* @en Get runtime environment
|
||||
*
|
||||
* @zh 优先返回场景级别设置,否则返回 Core 全局设置
|
||||
* @en Returns scene-level setting if set, otherwise returns Core global setting
|
||||
*/
|
||||
public get runtimeEnvironment(): RuntimeEnvironment {
|
||||
if (this._runtimeEnvironmentOverride) {
|
||||
return this._runtimeEnvironmentOverride;
|
||||
}
|
||||
return RuntimeConfig.runtimeEnvironment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 是否在服务端运行
|
||||
* @en Whether running on server
|
||||
*/
|
||||
public get isServer(): boolean {
|
||||
return this.runtimeEnvironment === 'server';
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 是否在客户端运行
|
||||
* @en Whether running on client
|
||||
*/
|
||||
public get isClient(): boolean {
|
||||
return this.runtimeEnvironment === 'client';
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟的组件生命周期回调队列
|
||||
*
|
||||
@@ -398,6 +438,11 @@ export class Scene implements IScene {
|
||||
this._logger = createLogger('Scene');
|
||||
this._maxErrorCount = config?.maxSystemErrorCount ?? 10;
|
||||
|
||||
// 只有显式指定时才覆盖,否则从 Core 读取
|
||||
if (config?.runtimeEnvironment) {
|
||||
this._runtimeEnvironmentOverride = config.runtimeEnvironment;
|
||||
}
|
||||
|
||||
if (config?.name) {
|
||||
this.name = config.name;
|
||||
}
|
||||
@@ -508,7 +553,9 @@ export class Scene implements IScene {
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新场景
|
||||
* @zh 更新场景
|
||||
* @en Update scene
|
||||
* @internal 由 SceneManager 或 World 调用,用户不应直接调用
|
||||
*/
|
||||
public update() {
|
||||
this.epochManager.increment();
|
||||
|
||||
@@ -240,18 +240,9 @@ export class SceneManager implements IService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新场景
|
||||
*
|
||||
* 应该在每帧的游戏循环中调用。
|
||||
* 会自动处理延迟场景切换。
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* function gameLoop(deltaTime: number) {
|
||||
* Core.update(deltaTime);
|
||||
* sceneManager.update(); // 每帧调用
|
||||
* }
|
||||
* ```
|
||||
* @zh 更新场景
|
||||
* @en Update scene
|
||||
* @internal 由 Core.update() 调用,用户不应直接调用
|
||||
*/
|
||||
public update(): void {
|
||||
// 处理延迟场景切换
|
||||
|
||||
125
packages/framework/core/src/ECS/Sync/ChangeTracker.ts
Normal file
125
packages/framework/core/src/ECS/Sync/ChangeTracker.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* @zh 组件变更追踪器
|
||||
* @en Component change tracker
|
||||
*
|
||||
* @zh 用于追踪 @sync 标记字段的变更,支持增量同步
|
||||
* @en Tracks changes to @sync marked fields for delta synchronization
|
||||
*/
|
||||
export class ChangeTracker {
|
||||
/**
|
||||
* @zh 脏字段索引集合
|
||||
* @en Set of dirty field indices
|
||||
*/
|
||||
private _dirtyFields: Set<number> = new Set();
|
||||
|
||||
/**
|
||||
* @zh 是否有任何变更
|
||||
* @en Whether there are any changes
|
||||
*/
|
||||
private _hasChanges: boolean = false;
|
||||
|
||||
/**
|
||||
* @zh 上次同步的时间戳
|
||||
* @en Last sync timestamp
|
||||
*/
|
||||
private _lastSyncTime: number = 0;
|
||||
|
||||
/**
|
||||
* @zh 标记字段为脏
|
||||
* @en Mark field as dirty
|
||||
*
|
||||
* @param fieldIndex - @zh 字段索引 @en Field index
|
||||
*/
|
||||
public setDirty(fieldIndex: number): void {
|
||||
this._dirtyFields.add(fieldIndex);
|
||||
this._hasChanges = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查是否有变更
|
||||
* @en Check if there are any changes
|
||||
*/
|
||||
public hasChanges(): boolean {
|
||||
return this._hasChanges;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查特定字段是否脏
|
||||
* @en Check if a specific field is dirty
|
||||
*
|
||||
* @param fieldIndex - @zh 字段索引 @en Field index
|
||||
*/
|
||||
public isDirty(fieldIndex: number): boolean {
|
||||
return this._dirtyFields.has(fieldIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取所有脏字段索引
|
||||
* @en Get all dirty field indices
|
||||
*/
|
||||
public getDirtyFields(): number[] {
|
||||
return Array.from(this._dirtyFields);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取脏字段数量
|
||||
* @en Get number of dirty fields
|
||||
*/
|
||||
public getDirtyCount(): number {
|
||||
return this._dirtyFields.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 清除所有变更标记
|
||||
* @en Clear all change marks
|
||||
*/
|
||||
public clear(): void {
|
||||
this._dirtyFields.clear();
|
||||
this._hasChanges = false;
|
||||
this._lastSyncTime = Date.now();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 清除特定字段的变更标记
|
||||
* @en Clear change mark for a specific field
|
||||
*
|
||||
* @param fieldIndex - @zh 字段索引 @en Field index
|
||||
*/
|
||||
public clearField(fieldIndex: number): void {
|
||||
this._dirtyFields.delete(fieldIndex);
|
||||
if (this._dirtyFields.size === 0) {
|
||||
this._hasChanges = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取上次同步时间
|
||||
* @en Get last sync time
|
||||
*/
|
||||
public get lastSyncTime(): number {
|
||||
return this._lastSyncTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 标记所有字段为脏(用于首次同步)
|
||||
* @en Mark all fields as dirty (for initial sync)
|
||||
*
|
||||
* @param fieldCount - @zh 字段数量 @en Field count
|
||||
*/
|
||||
public markAllDirty(fieldCount: number): void {
|
||||
for (let i = 0; i < fieldCount; i++) {
|
||||
this._dirtyFields.add(i);
|
||||
}
|
||||
this._hasChanges = fieldCount > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 重置追踪器
|
||||
* @en Reset tracker
|
||||
*/
|
||||
public reset(): void {
|
||||
this._dirtyFields.clear();
|
||||
this._hasChanges = false;
|
||||
this._lastSyncTime = 0;
|
||||
}
|
||||
}
|
||||
147
packages/framework/core/src/ECS/Sync/NetworkEntityDecorator.ts
Normal file
147
packages/framework/core/src/ECS/Sync/NetworkEntityDecorator.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* @zh 网络实体装饰器
|
||||
* @en Network entity decorator
|
||||
*
|
||||
* @zh 提供 @NetworkEntity 装饰器,用于标记需要自动广播生成/销毁的组件
|
||||
* @en Provides @NetworkEntity decorator to mark components for automatic spawn/despawn broadcasting
|
||||
*/
|
||||
|
||||
/**
|
||||
* @zh 网络实体元数据的 Symbol 键
|
||||
* @en Symbol key for network entity metadata
|
||||
*/
|
||||
export const NETWORK_ENTITY_METADATA = Symbol('NetworkEntityMetadata');
|
||||
|
||||
/**
|
||||
* @zh 网络实体元数据
|
||||
* @en Network entity metadata
|
||||
*/
|
||||
export interface NetworkEntityMetadata {
|
||||
/**
|
||||
* @zh 预制体类型名称(用于客户端重建实体)
|
||||
* @en Prefab type name (used by client to reconstruct entity)
|
||||
*/
|
||||
prefabType: string;
|
||||
|
||||
/**
|
||||
* @zh 是否自动广播生成
|
||||
* @en Whether to auto-broadcast spawn
|
||||
* @default true
|
||||
*/
|
||||
autoSpawn: boolean;
|
||||
|
||||
/**
|
||||
* @zh 是否自动广播销毁
|
||||
* @en Whether to auto-broadcast despawn
|
||||
* @default true
|
||||
*/
|
||||
autoDespawn: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 网络实体装饰器配置选项
|
||||
* @en Network entity decorator options
|
||||
*/
|
||||
export interface NetworkEntityOptions {
|
||||
/**
|
||||
* @zh 是否自动广播生成
|
||||
* @en Whether to auto-broadcast spawn
|
||||
* @default true
|
||||
*/
|
||||
autoSpawn?: boolean;
|
||||
|
||||
/**
|
||||
* @zh 是否自动广播销毁
|
||||
* @en Whether to auto-broadcast despawn
|
||||
* @default true
|
||||
*/
|
||||
autoDespawn?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 网络实体装饰器
|
||||
* @en Network entity decorator
|
||||
*
|
||||
* @zh 标记组件类为网络实体。当包含此组件的实体被创建或销毁时,
|
||||
* ECSRoom 会自动广播相应的 spawn/despawn 消息给所有客户端。
|
||||
* @en Marks a component class as a network entity. When an entity containing
|
||||
* this component is created or destroyed, ECSRoom will automatically broadcast
|
||||
* the corresponding spawn/despawn messages to all clients.
|
||||
*
|
||||
* @param prefabType - @zh 预制体类型名称 @en Prefab type name
|
||||
* @param options - @zh 可选配置 @en Optional configuration
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { Component, ECSComponent, NetworkEntity, sync } from '@esengine/ecs-framework';
|
||||
*
|
||||
* @ECSComponent('Enemy')
|
||||
* @NetworkEntity('Enemy')
|
||||
* class EnemyComponent extends Component {
|
||||
* @sync('float32') x: number = 0;
|
||||
* @sync('float32') y: number = 0;
|
||||
* @sync('uint16') health: number = 100;
|
||||
* }
|
||||
*
|
||||
* // 当添加此组件到实体时,ECSRoom 会自动广播 spawn
|
||||
* const enemy = scene.createEntity('Enemy');
|
||||
* enemy.addComponent(new EnemyComponent()); // 自动广播给所有客户端
|
||||
*
|
||||
* // 当实体销毁时,自动广播 despawn
|
||||
* enemy.destroy(); // 自动广播给所有客户端
|
||||
* ```
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // 只自动广播生成,销毁由手动控制
|
||||
* @ECSComponent('Bullet')
|
||||
* @NetworkEntity('Bullet', { autoDespawn: false })
|
||||
* class BulletComponent extends Component {
|
||||
* @sync('float32') x: number = 0;
|
||||
* @sync('float32') y: number = 0;
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function NetworkEntity(prefabType: string, options?: NetworkEntityOptions) {
|
||||
return function <T extends new (...args: any[]) => any>(target: T): T {
|
||||
const metadata: NetworkEntityMetadata = {
|
||||
prefabType,
|
||||
autoSpawn: options?.autoSpawn ?? true,
|
||||
autoDespawn: options?.autoDespawn ?? true,
|
||||
};
|
||||
|
||||
(target as any)[NETWORK_ENTITY_METADATA] = metadata;
|
||||
|
||||
return target;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取组件类的网络实体元数据
|
||||
* @en Get network entity metadata for a component class
|
||||
*
|
||||
* @param componentClass - @zh 组件类或组件实例 @en Component class or instance
|
||||
* @returns @zh 网络实体元数据,如果不存在则返回 null @en Network entity metadata, or null if not exists
|
||||
*/
|
||||
export function getNetworkEntityMetadata(componentClass: any): NetworkEntityMetadata | null {
|
||||
if (!componentClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const constructor = typeof componentClass === 'function'
|
||||
? componentClass
|
||||
: componentClass.constructor;
|
||||
|
||||
return constructor[NETWORK_ENTITY_METADATA] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查组件是否标记为网络实体
|
||||
* @en Check if a component is marked as a network entity
|
||||
*
|
||||
* @param component - @zh 组件类或组件实例 @en Component class or instance
|
||||
* @returns @zh 如果是网络实体返回 true @en Returns true if is a network entity
|
||||
*/
|
||||
export function isNetworkEntity(component: any): boolean {
|
||||
return getNetworkEntityMetadata(component) !== null;
|
||||
}
|
||||
219
packages/framework/core/src/ECS/Sync/decorators.ts
Normal file
219
packages/framework/core/src/ECS/Sync/decorators.ts
Normal file
@@ -0,0 +1,219 @@
|
||||
/**
|
||||
* @zh 网络同步装饰器
|
||||
* @en Network synchronization decorators
|
||||
*
|
||||
* @zh 提供 @sync 装饰器,用于标记需要网络同步的 Component 字段
|
||||
* @en Provides @sync decorator to mark Component fields for network synchronization
|
||||
*/
|
||||
|
||||
import type { SyncType, SyncFieldMetadata, SyncMetadata } from './types';
|
||||
import { SYNC_METADATA, CHANGE_TRACKER } from './types';
|
||||
import { ChangeTracker } from './ChangeTracker';
|
||||
import { getComponentTypeName } from '../Core/ComponentStorage/ComponentTypeUtils';
|
||||
|
||||
/**
|
||||
* @zh 获取或创建组件的同步元数据
|
||||
* @en Get or create sync metadata for a component class
|
||||
*
|
||||
* @param target - @zh 组件类的原型 @en Component class prototype
|
||||
* @returns @zh 同步元数据 @en Sync metadata
|
||||
*/
|
||||
function getOrCreateSyncMetadata(target: any): SyncMetadata {
|
||||
const constructor = target.constructor;
|
||||
|
||||
// Check if has own metadata (not inherited)
|
||||
const hasOwnMetadata = Object.prototype.hasOwnProperty.call(constructor, SYNC_METADATA);
|
||||
|
||||
if (hasOwnMetadata) {
|
||||
return constructor[SYNC_METADATA];
|
||||
}
|
||||
|
||||
// Check for inherited metadata
|
||||
const inheritedMetadata: SyncMetadata | undefined = constructor[SYNC_METADATA];
|
||||
|
||||
// Create new metadata (copy from inherited if exists)
|
||||
// Use getComponentTypeName to get @ECSComponent decorator name, or fall back to constructor.name
|
||||
const metadata: SyncMetadata = {
|
||||
typeId: getComponentTypeName(constructor),
|
||||
fields: inheritedMetadata ? [...inheritedMetadata.fields] : [],
|
||||
fieldIndexMap: inheritedMetadata ? new Map(inheritedMetadata.fieldIndexMap) : new Map()
|
||||
};
|
||||
|
||||
constructor[SYNC_METADATA] = metadata;
|
||||
return metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 同步字段装饰器
|
||||
* @en Sync field decorator
|
||||
*
|
||||
* @zh 标记 Component 字段为可网络同步。被标记的字段会自动追踪变更,
|
||||
* 并在值修改时触发变更追踪器。
|
||||
* @en Marks a Component field for network synchronization. Marked fields
|
||||
* automatically track changes and trigger the change tracker on modification.
|
||||
*
|
||||
* @param type - @zh 字段的同步类型 @en Sync type of the field
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { Component, ECSComponent } from '@esengine/ecs-framework';
|
||||
* import { sync } from '@esengine/ecs-framework';
|
||||
*
|
||||
* @ECSComponent('Player')
|
||||
* class PlayerComponent extends Component {
|
||||
* @sync("string") name: string = "";
|
||||
* @sync("uint16") score: number = 0;
|
||||
* @sync("float32") x: number = 0;
|
||||
* @sync("float32") y: number = 0;
|
||||
*
|
||||
* // 不带 @sync 的字段不会同步
|
||||
* // Fields without @sync will not be synchronized
|
||||
* localData: any;
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function sync(type: SyncType) {
|
||||
return function (target: any, propertyKey: string) {
|
||||
const metadata = getOrCreateSyncMetadata(target);
|
||||
|
||||
// Assign field index (auto-increment based on field count)
|
||||
const fieldIndex = metadata.fields.length;
|
||||
|
||||
// Create field metadata
|
||||
const fieldMeta: SyncFieldMetadata = {
|
||||
index: fieldIndex,
|
||||
name: propertyKey,
|
||||
type: type
|
||||
};
|
||||
|
||||
// Register field
|
||||
metadata.fields.push(fieldMeta);
|
||||
metadata.fieldIndexMap.set(propertyKey, fieldIndex);
|
||||
|
||||
// Store original property key for getter/setter
|
||||
const privateKey = `_sync_${propertyKey}`;
|
||||
|
||||
// Define getter/setter to intercept value changes
|
||||
Object.defineProperty(target, propertyKey, {
|
||||
get() {
|
||||
return this[privateKey];
|
||||
},
|
||||
set(value: any) {
|
||||
const oldValue = this[privateKey];
|
||||
if (oldValue !== value) {
|
||||
this[privateKey] = value;
|
||||
// Trigger change tracker if exists
|
||||
const tracker = this[CHANGE_TRACKER] as ChangeTracker | undefined;
|
||||
if (tracker) {
|
||||
tracker.setDirty(fieldIndex);
|
||||
}
|
||||
}
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取组件类的同步元数据
|
||||
* @en Get sync metadata for a component class
|
||||
*
|
||||
* @param componentClass - @zh 组件类或组件实例 @en Component class or instance
|
||||
* @returns @zh 同步元数据,如果不存在则返回 null @en Sync metadata, or null if not exists
|
||||
*/
|
||||
export function getSyncMetadata(componentClass: any): SyncMetadata | null {
|
||||
if (!componentClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const constructor = typeof componentClass === 'function'
|
||||
? componentClass
|
||||
: componentClass.constructor;
|
||||
|
||||
return constructor[SYNC_METADATA] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查组件是否有同步字段
|
||||
* @en Check if a component has sync fields
|
||||
*
|
||||
* @param component - @zh 组件类或组件实例 @en Component class or instance
|
||||
* @returns @zh 如果有同步字段返回 true @en Returns true if has sync fields
|
||||
*/
|
||||
export function hasSyncFields(component: any): boolean {
|
||||
const metadata = getSyncMetadata(component);
|
||||
return metadata !== null && metadata.fields.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取组件实例的变更追踪器
|
||||
* @en Get change tracker of a component instance
|
||||
*
|
||||
* @param component - @zh 组件实例 @en Component instance
|
||||
* @returns @zh 变更追踪器,如果不存在则返回 null @en Change tracker, or null if not exists
|
||||
*/
|
||||
export function getChangeTracker(component: any): ChangeTracker | null {
|
||||
if (!component) {
|
||||
return null;
|
||||
}
|
||||
return component[CHANGE_TRACKER] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 为组件实例初始化变更追踪器
|
||||
* @en Initialize change tracker for a component instance
|
||||
*
|
||||
* @zh 这个函数应该在组件首次添加到实体时调用。
|
||||
* 它会创建变更追踪器并标记所有字段为脏(用于首次同步)。
|
||||
* @en This function should be called when a component is first added to an entity.
|
||||
* It creates the change tracker and marks all fields as dirty (for initial sync).
|
||||
*
|
||||
* @param component - @zh 组件实例 @en Component instance
|
||||
* @returns @zh 变更追踪器 @en Change tracker
|
||||
*/
|
||||
export function initChangeTracker(component: any): ChangeTracker {
|
||||
const metadata = getSyncMetadata(component);
|
||||
if (!metadata) {
|
||||
throw new Error('Component does not have sync metadata. Use @sync decorator on fields.');
|
||||
}
|
||||
|
||||
let tracker = component[CHANGE_TRACKER] as ChangeTracker | undefined;
|
||||
if (!tracker) {
|
||||
tracker = new ChangeTracker();
|
||||
component[CHANGE_TRACKER] = tracker;
|
||||
}
|
||||
|
||||
// Mark all fields as dirty for initial sync
|
||||
tracker.markAllDirty(metadata.fields.length);
|
||||
|
||||
return tracker;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 清除组件实例的变更标记
|
||||
* @en Clear change marks for a component instance
|
||||
*
|
||||
* @zh 通常在同步完成后调用,清除所有脏标记
|
||||
* @en Usually called after sync is complete, clears all dirty marks
|
||||
*
|
||||
* @param component - @zh 组件实例 @en Component instance
|
||||
*/
|
||||
export function clearChanges(component: any): void {
|
||||
const tracker = getChangeTracker(component);
|
||||
if (tracker) {
|
||||
tracker.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查组件是否有变更
|
||||
* @en Check if a component has changes
|
||||
*
|
||||
* @param component - @zh 组件实例 @en Component instance
|
||||
* @returns @zh 如果有变更返回 true @en Returns true if has changes
|
||||
*/
|
||||
export function hasChanges(component: any): boolean {
|
||||
const tracker = getChangeTracker(component);
|
||||
return tracker ? tracker.hasChanges() : false;
|
||||
}
|
||||
285
packages/framework/core/src/ECS/Sync/encoding/BinaryReader.ts
Normal file
285
packages/framework/core/src/ECS/Sync/encoding/BinaryReader.ts
Normal file
@@ -0,0 +1,285 @@
|
||||
/**
|
||||
* @zh 二进制读取器
|
||||
* @en Binary Reader
|
||||
*
|
||||
* @zh 提供高效的二进制数据读取功能
|
||||
* @en Provides efficient binary data reading
|
||||
*/
|
||||
|
||||
import { decodeVarint } from './varint';
|
||||
|
||||
/**
|
||||
* @zh 文本解码器(使用浏览器原生 API)
|
||||
* @en Text decoder (using browser native API)
|
||||
*/
|
||||
const textDecoder = typeof TextDecoder !== 'undefined' ? new TextDecoder() : null;
|
||||
|
||||
/**
|
||||
* @zh 二进制读取器
|
||||
* @en Binary reader for decoding data
|
||||
*/
|
||||
export class BinaryReader {
|
||||
/**
|
||||
* @zh 数据缓冲区
|
||||
* @en Data buffer
|
||||
*/
|
||||
private _buffer: Uint8Array;
|
||||
|
||||
/**
|
||||
* @zh DataView 用于读取数值
|
||||
* @en DataView for reading numbers
|
||||
*/
|
||||
private _view: DataView;
|
||||
|
||||
/**
|
||||
* @zh 当前读取位置
|
||||
* @en Current read position
|
||||
*/
|
||||
private _offset: number = 0;
|
||||
|
||||
/**
|
||||
* @zh 创建二进制读取器
|
||||
* @en Create binary reader
|
||||
*
|
||||
* @param buffer - @zh 要读取的数据 @en Data to read
|
||||
*/
|
||||
constructor(buffer: Uint8Array) {
|
||||
this._buffer = buffer;
|
||||
this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取当前读取位置
|
||||
* @en Get current read position
|
||||
*/
|
||||
public get offset(): number {
|
||||
return this._offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 设置读取位置
|
||||
* @en Set read position
|
||||
*/
|
||||
public set offset(value: number) {
|
||||
this._offset = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取剩余可读字节数
|
||||
* @en Get remaining readable bytes
|
||||
*/
|
||||
public get remaining(): number {
|
||||
return this._buffer.length - this._offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查是否有更多数据可读
|
||||
* @en Check if there's more data to read
|
||||
*/
|
||||
public hasMore(): boolean {
|
||||
return this._offset < this._buffer.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取单个字节
|
||||
* @en Read single byte
|
||||
*/
|
||||
public readUint8(): number {
|
||||
this.checkBounds(1);
|
||||
return this._buffer[this._offset++]!;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取有符号字节
|
||||
* @en Read signed byte
|
||||
*/
|
||||
public readInt8(): number {
|
||||
this.checkBounds(1);
|
||||
return this._view.getInt8(this._offset++);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取布尔值
|
||||
* @en Read boolean
|
||||
*/
|
||||
public readBoolean(): boolean {
|
||||
return this.readUint8() !== 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取 16 位无符号整数(小端序)
|
||||
* @en Read 16-bit unsigned integer (little-endian)
|
||||
*/
|
||||
public readUint16(): number {
|
||||
this.checkBounds(2);
|
||||
const value = this._view.getUint16(this._offset, true);
|
||||
this._offset += 2;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取 16 位有符号整数(小端序)
|
||||
* @en Read 16-bit signed integer (little-endian)
|
||||
*/
|
||||
public readInt16(): number {
|
||||
this.checkBounds(2);
|
||||
const value = this._view.getInt16(this._offset, true);
|
||||
this._offset += 2;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取 32 位无符号整数(小端序)
|
||||
* @en Read 32-bit unsigned integer (little-endian)
|
||||
*/
|
||||
public readUint32(): number {
|
||||
this.checkBounds(4);
|
||||
const value = this._view.getUint32(this._offset, true);
|
||||
this._offset += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取 32 位有符号整数(小端序)
|
||||
* @en Read 32-bit signed integer (little-endian)
|
||||
*/
|
||||
public readInt32(): number {
|
||||
this.checkBounds(4);
|
||||
const value = this._view.getInt32(this._offset, true);
|
||||
this._offset += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取 32 位浮点数(小端序)
|
||||
* @en Read 32-bit float (little-endian)
|
||||
*/
|
||||
public readFloat32(): number {
|
||||
this.checkBounds(4);
|
||||
const value = this._view.getFloat32(this._offset, true);
|
||||
this._offset += 4;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取 64 位浮点数(小端序)
|
||||
* @en Read 64-bit float (little-endian)
|
||||
*/
|
||||
public readFloat64(): number {
|
||||
this.checkBounds(8);
|
||||
const value = this._view.getFloat64(this._offset, true);
|
||||
this._offset += 8;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取变长整数
|
||||
* @en Read variable-length integer
|
||||
*/
|
||||
public readVarint(): number {
|
||||
const [value, newOffset] = decodeVarint(this._buffer, this._offset);
|
||||
this._offset = newOffset;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取字符串(UTF-8 编码,带长度前缀)
|
||||
* @en Read string (UTF-8 encoded with length prefix)
|
||||
*/
|
||||
public readString(): string {
|
||||
const length = this.readVarint();
|
||||
this.checkBounds(length);
|
||||
|
||||
const bytes = this._buffer.subarray(this._offset, this._offset + length);
|
||||
this._offset += length;
|
||||
|
||||
if (textDecoder) {
|
||||
return textDecoder.decode(bytes);
|
||||
} else {
|
||||
return this.utf8BytesToString(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 读取原始字节
|
||||
* @en Read raw bytes
|
||||
*
|
||||
* @param length - @zh 要读取的字节数 @en Number of bytes to read
|
||||
*/
|
||||
public readBytes(length: number): Uint8Array {
|
||||
this.checkBounds(length);
|
||||
const bytes = this._buffer.slice(this._offset, this._offset + length);
|
||||
this._offset += length;
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 查看下一个字节但不移动读取位置
|
||||
* @en Peek next byte without advancing read position
|
||||
*/
|
||||
public peekUint8(): number {
|
||||
this.checkBounds(1);
|
||||
return this._buffer[this._offset]!;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 跳过指定字节数
|
||||
* @en Skip specified number of bytes
|
||||
*/
|
||||
public skip(count: number): void {
|
||||
this.checkBounds(count);
|
||||
this._offset += count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 检查边界
|
||||
* @en Check bounds
|
||||
*/
|
||||
private checkBounds(size: number): void {
|
||||
if (this._offset + size > this._buffer.length) {
|
||||
throw new Error(`BinaryReader: buffer overflow (offset=${this._offset}, size=${size}, bufferLength=${this._buffer.length})`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh UTF-8 字节转字符串(后备方案)
|
||||
* @en UTF-8 bytes to string (fallback)
|
||||
*/
|
||||
private utf8BytesToString(bytes: Uint8Array): string {
|
||||
let result = '';
|
||||
let i = 0;
|
||||
|
||||
while (i < bytes.length) {
|
||||
let charCode: number;
|
||||
const byte1 = bytes[i++]!;
|
||||
|
||||
if (byte1 < 0x80) {
|
||||
charCode = byte1;
|
||||
} else if (byte1 < 0xE0) {
|
||||
const byte2 = bytes[i++]!;
|
||||
charCode = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
|
||||
} else if (byte1 < 0xF0) {
|
||||
const byte2 = bytes[i++]!;
|
||||
const byte3 = bytes[i++]!;
|
||||
charCode = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
|
||||
} else {
|
||||
const byte2 = bytes[i++]!;
|
||||
const byte3 = bytes[i++]!;
|
||||
const byte4 = bytes[i++]!;
|
||||
charCode = ((byte1 & 0x07) << 18) | ((byte2 & 0x3F) << 12) |
|
||||
((byte3 & 0x3F) << 6) | (byte4 & 0x3F);
|
||||
|
||||
// Convert to surrogate pair
|
||||
if (charCode > 0xFFFF) {
|
||||
charCode -= 0x10000;
|
||||
result += String.fromCharCode(0xD800 + (charCode >> 10));
|
||||
charCode = 0xDC00 + (charCode & 0x3FF);
|
||||
}
|
||||
}
|
||||
|
||||
result += String.fromCharCode(charCode);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
257
packages/framework/core/src/ECS/Sync/encoding/BinaryWriter.ts
Normal file
257
packages/framework/core/src/ECS/Sync/encoding/BinaryWriter.ts
Normal file
@@ -0,0 +1,257 @@
|
||||
/**
|
||||
* @zh 二进制写入器
|
||||
* @en Binary Writer
|
||||
*
|
||||
* @zh 提供高效的二进制数据写入功能,支持自动扩容
|
||||
* @en Provides efficient binary data writing with auto-expansion
|
||||
*/
|
||||
|
||||
import { encodeVarint, varintSize } from './varint';
|
||||
|
||||
/**
|
||||
* @zh 文本编码器(使用浏览器原生 API)
|
||||
* @en Text encoder (using browser native API)
|
||||
*/
|
||||
const textEncoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : null;
|
||||
|
||||
/**
|
||||
* @zh 二进制写入器
|
||||
* @en Binary writer for encoding data
|
||||
*/
|
||||
export class BinaryWriter {
|
||||
/**
|
||||
* @zh 内部缓冲区
|
||||
* @en Internal buffer
|
||||
*/
|
||||
private _buffer: Uint8Array;
|
||||
|
||||
/**
|
||||
* @zh DataView 用于写入数值
|
||||
* @en DataView for writing numbers
|
||||
*/
|
||||
private _view: DataView;
|
||||
|
||||
/**
|
||||
* @zh 当前写入位置
|
||||
* @en Current write position
|
||||
*/
|
||||
private _offset: number = 0;
|
||||
|
||||
/**
|
||||
* @zh 创建二进制写入器
|
||||
* @en Create binary writer
|
||||
*
|
||||
* @param initialCapacity - @zh 初始容量 @en Initial capacity
|
||||
*/
|
||||
constructor(initialCapacity: number = 256) {
|
||||
this._buffer = new Uint8Array(initialCapacity);
|
||||
this._view = new DataView(this._buffer.buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取当前写入位置
|
||||
* @en Get current write position
|
||||
*/
|
||||
public get offset(): number {
|
||||
return this._offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 获取写入的数据
|
||||
* @en Get written data
|
||||
*
|
||||
* @returns @zh 包含写入数据的 Uint8Array @en Uint8Array containing written data
|
||||
*/
|
||||
public toUint8Array(): Uint8Array {
|
||||
return this._buffer.slice(0, this._offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 重置写入器(清空数据但保留缓冲区)
|
||||
* @en Reset writer (clear data but keep buffer)
|
||||
*/
|
||||
public reset(): void {
|
||||
this._offset = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 确保有足够空间
|
||||
* @en Ensure enough space
|
||||
*
|
||||
* @param size - @zh 需要的额外字节数 @en Extra bytes needed
|
||||
*/
|
||||
private ensureCapacity(size: number): void {
|
||||
const required = this._offset + size;
|
||||
if (required > this._buffer.length) {
|
||||
// Double the buffer size or use required size, whichever is larger
|
||||
const newSize = Math.max(this._buffer.length * 2, required);
|
||||
const newBuffer = new Uint8Array(newSize);
|
||||
newBuffer.set(this._buffer);
|
||||
this._buffer = newBuffer;
|
||||
this._view = new DataView(this._buffer.buffer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入单个字节
|
||||
* @en Write single byte
|
||||
*/
|
||||
public writeUint8(value: number): void {
|
||||
this.ensureCapacity(1);
|
||||
this._buffer[this._offset++] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入有符号字节
|
||||
* @en Write signed byte
|
||||
*/
|
||||
public writeInt8(value: number): void {
|
||||
this.ensureCapacity(1);
|
||||
this._view.setInt8(this._offset++, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入布尔值
|
||||
* @en Write boolean
|
||||
*/
|
||||
public writeBoolean(value: boolean): void {
|
||||
this.writeUint8(value ? 1 : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入 16 位无符号整数(小端序)
|
||||
* @en Write 16-bit unsigned integer (little-endian)
|
||||
*/
|
||||
public writeUint16(value: number): void {
|
||||
this.ensureCapacity(2);
|
||||
this._view.setUint16(this._offset, value, true);
|
||||
this._offset += 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入 16 位有符号整数(小端序)
|
||||
* @en Write 16-bit signed integer (little-endian)
|
||||
*/
|
||||
public writeInt16(value: number): void {
|
||||
this.ensureCapacity(2);
|
||||
this._view.setInt16(this._offset, value, true);
|
||||
this._offset += 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入 32 位无符号整数(小端序)
|
||||
* @en Write 32-bit unsigned integer (little-endian)
|
||||
*/
|
||||
public writeUint32(value: number): void {
|
||||
this.ensureCapacity(4);
|
||||
this._view.setUint32(this._offset, value, true);
|
||||
this._offset += 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入 32 位有符号整数(小端序)
|
||||
* @en Write 32-bit signed integer (little-endian)
|
||||
*/
|
||||
public writeInt32(value: number): void {
|
||||
this.ensureCapacity(4);
|
||||
this._view.setInt32(this._offset, value, true);
|
||||
this._offset += 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入 32 位浮点数(小端序)
|
||||
* @en Write 32-bit float (little-endian)
|
||||
*/
|
||||
public writeFloat32(value: number): void {
|
||||
this.ensureCapacity(4);
|
||||
this._view.setFloat32(this._offset, value, true);
|
||||
this._offset += 4;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入 64 位浮点数(小端序)
|
||||
* @en Write 64-bit float (little-endian)
|
||||
*/
|
||||
public writeFloat64(value: number): void {
|
||||
this.ensureCapacity(8);
|
||||
this._view.setFloat64(this._offset, value, true);
|
||||
this._offset += 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入变长整数
|
||||
* @en Write variable-length integer
|
||||
*/
|
||||
public writeVarint(value: number): void {
|
||||
this.ensureCapacity(varintSize(value));
|
||||
this._offset = encodeVarint(value, this._buffer, this._offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入字符串(UTF-8 编码,带长度前缀)
|
||||
* @en Write string (UTF-8 encoded with length prefix)
|
||||
*/
|
||||
public writeString(value: string): void {
|
||||
if (textEncoder) {
|
||||
const encoded = textEncoder.encode(value);
|
||||
this.writeVarint(encoded.length);
|
||||
this.ensureCapacity(encoded.length);
|
||||
this._buffer.set(encoded, this._offset);
|
||||
this._offset += encoded.length;
|
||||
} else {
|
||||
// Fallback for environments without TextEncoder
|
||||
const bytes = this.stringToUtf8Bytes(value);
|
||||
this.writeVarint(bytes.length);
|
||||
this.ensureCapacity(bytes.length);
|
||||
this._buffer.set(bytes, this._offset);
|
||||
this._offset += bytes.length;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 写入原始字节
|
||||
* @en Write raw bytes
|
||||
*/
|
||||
public writeBytes(data: Uint8Array): void {
|
||||
this.ensureCapacity(data.length);
|
||||
this._buffer.set(data, this._offset);
|
||||
this._offset += data.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 字符串转 UTF-8 字节(后备方案)
|
||||
* @en String to UTF-8 bytes (fallback)
|
||||
*/
|
||||
private stringToUtf8Bytes(str: string): Uint8Array {
|
||||
const bytes: number[] = [];
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
let charCode = str.charCodeAt(i);
|
||||
|
||||
// Handle surrogate pairs
|
||||
if (charCode >= 0xD800 && charCode <= 0xDBFF && i + 1 < str.length) {
|
||||
const next = str.charCodeAt(i + 1);
|
||||
if (next >= 0xDC00 && next <= 0xDFFF) {
|
||||
charCode = 0x10000 + ((charCode - 0xD800) << 10) + (next - 0xDC00);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
if (charCode < 0x80) {
|
||||
bytes.push(charCode);
|
||||
} else if (charCode < 0x800) {
|
||||
bytes.push(0xC0 | (charCode >> 6));
|
||||
bytes.push(0x80 | (charCode & 0x3F));
|
||||
} else if (charCode < 0x10000) {
|
||||
bytes.push(0xE0 | (charCode >> 12));
|
||||
bytes.push(0x80 | ((charCode >> 6) & 0x3F));
|
||||
bytes.push(0x80 | (charCode & 0x3F));
|
||||
} else {
|
||||
bytes.push(0xF0 | (charCode >> 18));
|
||||
bytes.push(0x80 | ((charCode >> 12) & 0x3F));
|
||||
bytes.push(0x80 | ((charCode >> 6) & 0x3F));
|
||||
bytes.push(0x80 | (charCode & 0x3F));
|
||||
}
|
||||
}
|
||||
return new Uint8Array(bytes);
|
||||
}
|
||||
}
|
||||
372
packages/framework/core/src/ECS/Sync/encoding/Decoder.ts
Normal file
372
packages/framework/core/src/ECS/Sync/encoding/Decoder.ts
Normal file
@@ -0,0 +1,372 @@
|
||||
/**
|
||||
* @zh 组件状态解码器
|
||||
* @en Component state decoder
|
||||
*
|
||||
* @zh 从二进制格式解码并应用到 ECS Component
|
||||
* @en Decodes binary format and applies to ECS Components
|
||||
*/
|
||||
|
||||
import type { Entity } from '../../Entity';
|
||||
import type { Component } from '../../Component';
|
||||
import type { Scene } from '../../Scene';
|
||||
import type { SyncType, SyncMetadata } from '../types';
|
||||
import { SyncOperation, SYNC_METADATA } from '../types';
|
||||
import { BinaryReader } from './BinaryReader';
|
||||
import { GlobalComponentRegistry } from '../../Core/ComponentStorage/ComponentRegistry';
|
||||
|
||||
/**
|
||||
* @zh 解码字段值
|
||||
* @en Decode field value
|
||||
*/
|
||||
function decodeFieldValue(reader: BinaryReader, type: SyncType): any {
|
||||
switch (type) {
|
||||
case 'boolean':
|
||||
return reader.readBoolean();
|
||||
case 'int8':
|
||||
return reader.readInt8();
|
||||
case 'uint8':
|
||||
return reader.readUint8();
|
||||
case 'int16':
|
||||
return reader.readInt16();
|
||||
case 'uint16':
|
||||
return reader.readUint16();
|
||||
case 'int32':
|
||||
return reader.readInt32();
|
||||
case 'uint32':
|
||||
return reader.readUint32();
|
||||
case 'float32':
|
||||
return reader.readFloat32();
|
||||
case 'float64':
|
||||
return reader.readFloat64();
|
||||
case 'string':
|
||||
return reader.readString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 解码并应用组件数据
|
||||
* @en Decode and apply component data
|
||||
*
|
||||
* @param component - @zh 组件实例 @en Component instance
|
||||
* @param metadata - @zh 组件同步元数据 @en Component sync metadata
|
||||
* @param reader - @zh 二进制读取器 @en Binary reader
|
||||
*/
|
||||
export function decodeComponent(
|
||||
component: Component,
|
||||
metadata: SyncMetadata,
|
||||
reader: BinaryReader
|
||||
): void {
|
||||
const fieldCount = reader.readVarint();
|
||||
|
||||
for (let i = 0; i < fieldCount; i++) {
|
||||
const fieldIndex = reader.readUint8();
|
||||
const field = metadata.fields[fieldIndex];
|
||||
|
||||
if (field) {
|
||||
const value = decodeFieldValue(reader, field.type);
|
||||
// Directly set the private backing field to avoid triggering change tracking
|
||||
(component as any)[`_sync_${field.name}`] = value;
|
||||
} else {
|
||||
// Unknown field, skip based on type info in metadata
|
||||
console.warn(`Unknown sync field index: ${fieldIndex}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 解码实体快照结果
|
||||
* @en Decode entity snapshot result
|
||||
*/
|
||||
export interface DecodeEntityResult {
|
||||
/**
|
||||
* @zh 实体 ID
|
||||
* @en Entity ID
|
||||
*/
|
||||
entityId: number;
|
||||
|
||||
/**
|
||||
* @zh 是否为新实体
|
||||
* @en Whether it's a new entity
|
||||
*/
|
||||
isNew: boolean;
|
||||
|
||||
/**
|
||||
* @zh 解码的组件类型列表
|
||||
* @en List of decoded component types
|
||||
*/
|
||||
componentTypes: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 解码并应用实体数据
|
||||
* @en Decode and apply entity data
|
||||
*
|
||||
* @param scene - @zh 场景 @en Scene
|
||||
* @param reader - @zh 二进制读取器 @en Binary reader
|
||||
* @param entityMap - @zh 实体 ID 映射(可选)@en Entity ID mapping (optional)
|
||||
* @returns @zh 解码结果 @en Decode result
|
||||
*/
|
||||
export function decodeEntity(
|
||||
scene: Scene,
|
||||
reader: BinaryReader,
|
||||
entityMap?: Map<number, Entity>
|
||||
): DecodeEntityResult {
|
||||
const entityId = reader.readUint32();
|
||||
const componentCount = reader.readVarint();
|
||||
const componentTypes: string[] = [];
|
||||
|
||||
// Find or create entity
|
||||
let entity: Entity | null | undefined = entityMap?.get(entityId);
|
||||
let isNew = false;
|
||||
|
||||
if (!entity) {
|
||||
entity = scene.findEntityById(entityId);
|
||||
}
|
||||
|
||||
if (!entity) {
|
||||
// Entity doesn't exist, create it
|
||||
entity = scene.createEntity(`entity_${entityId}`);
|
||||
isNew = true;
|
||||
entityMap?.set(entityId, entity);
|
||||
}
|
||||
|
||||
for (let i = 0; i < componentCount; i++) {
|
||||
const typeId = reader.readString();
|
||||
componentTypes.push(typeId);
|
||||
|
||||
// Find component class from GlobalComponentRegistry
|
||||
const componentClass = GlobalComponentRegistry.getComponentType(typeId) as (new () => Component) | null;
|
||||
if (!componentClass) {
|
||||
console.warn(`Unknown component type: ${typeId}`);
|
||||
// Skip component data - we need to read it to advance the reader
|
||||
const fieldCount = reader.readVarint();
|
||||
for (let j = 0; j < fieldCount; j++) {
|
||||
reader.readUint8(); // fieldIndex
|
||||
// We can't skip properly without knowing the type, so this is a problem
|
||||
// For now, log error and break
|
||||
console.error(`Cannot skip unknown component type: ${typeId}`);
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const metadata: SyncMetadata | undefined = (componentClass as any)[SYNC_METADATA];
|
||||
if (!metadata) {
|
||||
console.warn(`Component ${typeId} has no sync metadata`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find or add component
|
||||
let component = entity.getComponent(componentClass);
|
||||
if (!component) {
|
||||
component = entity.addComponent(new componentClass());
|
||||
}
|
||||
|
||||
// Decode component data
|
||||
decodeComponent(component, metadata, reader);
|
||||
}
|
||||
|
||||
return { entityId, isNew, componentTypes };
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 解码快照结果
|
||||
* @en Decode snapshot result
|
||||
*/
|
||||
export interface DecodeSnapshotResult {
|
||||
/**
|
||||
* @zh 操作类型
|
||||
* @en Operation type
|
||||
*/
|
||||
operation: SyncOperation;
|
||||
|
||||
/**
|
||||
* @zh 解码的实体列表
|
||||
* @en List of decoded entities
|
||||
*/
|
||||
entities: DecodeEntityResult[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 解码状态快照
|
||||
* @en Decode state snapshot
|
||||
*
|
||||
* @param scene - @zh 场景 @en Scene
|
||||
* @param data - @zh 二进制数据 @en Binary data
|
||||
* @param entityMap - @zh 实体 ID 映射(可选)@en Entity ID mapping (optional)
|
||||
* @returns @zh 解码结果 @en Decode result
|
||||
*/
|
||||
export function decodeSnapshot(
|
||||
scene: Scene,
|
||||
data: Uint8Array,
|
||||
entityMap?: Map<number, Entity>
|
||||
): DecodeSnapshotResult {
|
||||
const reader = new BinaryReader(data);
|
||||
const operation = reader.readUint8() as SyncOperation;
|
||||
const entityCount = reader.readVarint();
|
||||
const entities: DecodeEntityResult[] = [];
|
||||
|
||||
const map = entityMap || new Map<number, Entity>();
|
||||
|
||||
for (let i = 0; i < entityCount; i++) {
|
||||
const result = decodeEntity(scene, reader, map);
|
||||
entities.push(result);
|
||||
}
|
||||
|
||||
return { operation, entities };
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 解码生成消息结果
|
||||
* @en Decode spawn message result
|
||||
*/
|
||||
export interface DecodeSpawnResult {
|
||||
/**
|
||||
* @zh 实体
|
||||
* @en Entity
|
||||
*/
|
||||
entity: Entity;
|
||||
|
||||
/**
|
||||
* @zh 预制体类型
|
||||
* @en Prefab type
|
||||
*/
|
||||
prefabType: string;
|
||||
|
||||
/**
|
||||
* @zh 解码的组件类型列表
|
||||
* @en List of decoded component types
|
||||
*/
|
||||
componentTypes: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 解码实体生成消息
|
||||
* @en Decode entity spawn message
|
||||
*
|
||||
* @param scene - @zh 场景 @en Scene
|
||||
* @param data - @zh 二进制数据 @en Binary data
|
||||
* @param entityMap - @zh 实体 ID 映射(可选)@en Entity ID mapping (optional)
|
||||
* @returns @zh 解码结果,如果不是 SPAWN 消息则返回 null @en Decode result, or null if not a SPAWN message
|
||||
*/
|
||||
export function decodeSpawn(
|
||||
scene: Scene,
|
||||
data: Uint8Array,
|
||||
entityMap?: Map<number, Entity>
|
||||
): DecodeSpawnResult | null {
|
||||
const reader = new BinaryReader(data);
|
||||
const operation = reader.readUint8();
|
||||
|
||||
if (operation !== SyncOperation.SPAWN) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const entityId = reader.readUint32();
|
||||
const prefabType = reader.readString();
|
||||
const componentCount = reader.readVarint();
|
||||
const componentTypes: string[] = [];
|
||||
|
||||
// Create entity
|
||||
const entity = scene.createEntity(`entity_${entityId}`);
|
||||
entityMap?.set(entityId, entity);
|
||||
|
||||
for (let i = 0; i < componentCount; i++) {
|
||||
const typeId = reader.readString();
|
||||
componentTypes.push(typeId);
|
||||
|
||||
const componentClass = GlobalComponentRegistry.getComponentType(typeId) as (new () => Component) | null;
|
||||
if (!componentClass) {
|
||||
console.warn(`Unknown component type: ${typeId}`);
|
||||
// Try to skip
|
||||
const fieldCount = reader.readVarint();
|
||||
for (let j = 0; j < fieldCount; j++) {
|
||||
reader.readUint8();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const metadata: SyncMetadata | undefined = (componentClass as any)[SYNC_METADATA];
|
||||
if (!metadata) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const component = entity.addComponent(new (componentClass as new () => Component)());
|
||||
decodeComponent(component, metadata, reader);
|
||||
}
|
||||
|
||||
return { entity, prefabType, componentTypes };
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 解码销毁消息结果
|
||||
* @en Decode despawn message result
|
||||
*/
|
||||
export interface DecodeDespawnResult {
|
||||
/**
|
||||
* @zh 销毁的实体 ID 列表
|
||||
* @en List of despawned entity IDs
|
||||
*/
|
||||
entityIds: number[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 解码实体销毁消息
|
||||
* @en Decode entity despawn message
|
||||
*
|
||||
* @param data - @zh 二进制数据 @en Binary data
|
||||
* @returns @zh 解码结果,如果不是 DESPAWN 消息则返回 null @en Decode result, or null if not a DESPAWN message
|
||||
*/
|
||||
export function decodeDespawn(data: Uint8Array): DecodeDespawnResult | null {
|
||||
const reader = new BinaryReader(data);
|
||||
const operation = reader.readUint8();
|
||||
|
||||
if (operation !== SyncOperation.DESPAWN) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const entityIds: number[] = [];
|
||||
|
||||
// Check if it's a single entity or batch
|
||||
if (reader.remaining === 4) {
|
||||
// Single entity
|
||||
entityIds.push(reader.readUint32());
|
||||
} else {
|
||||
// Batch
|
||||
const count = reader.readVarint();
|
||||
for (let i = 0; i < count; i++) {
|
||||
entityIds.push(reader.readUint32());
|
||||
}
|
||||
}
|
||||
|
||||
return { entityIds };
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 处理销毁消息(从场景中移除实体)
|
||||
* @en Process despawn message (remove entities from scene)
|
||||
*
|
||||
* @param scene - @zh 场景 @en Scene
|
||||
* @param data - @zh 二进制数据 @en Binary data
|
||||
* @param entityMap - @zh 实体 ID 映射(可选)@en Entity ID mapping (optional)
|
||||
* @returns @zh 移除的实体 ID 列表 @en List of removed entity IDs
|
||||
*/
|
||||
export function processDespawn(
|
||||
scene: Scene,
|
||||
data: Uint8Array,
|
||||
entityMap?: Map<number, Entity>
|
||||
): number[] {
|
||||
const result = decodeDespawn(data);
|
||||
if (!result) {
|
||||
return [];
|
||||
}
|
||||
|
||||
for (const entityId of result.entityIds) {
|
||||
const entity = entityMap?.get(entityId) || scene.findEntityById(entityId);
|
||||
if (entity) {
|
||||
entity.destroy();
|
||||
entityMap?.delete(entityId);
|
||||
}
|
||||
}
|
||||
|
||||
return result.entityIds;
|
||||
}
|
||||
291
packages/framework/core/src/ECS/Sync/encoding/Encoder.ts
Normal file
291
packages/framework/core/src/ECS/Sync/encoding/Encoder.ts
Normal file
@@ -0,0 +1,291 @@
|
||||
/**
|
||||
* @zh 组件状态编码器
|
||||
* @en Component state encoder
|
||||
*
|
||||
* @zh 将 ECS Component 的 @sync 字段编码为二进制格式
|
||||
* @en Encodes @sync fields of ECS Components to binary format
|
||||
*/
|
||||
|
||||
import type { Entity } from '../../Entity';
|
||||
import type { Component } from '../../Component';
|
||||
import type { SyncType, SyncMetadata } from '../types';
|
||||
import { SyncOperation, SYNC_METADATA, CHANGE_TRACKER } from '../types';
|
||||
import type { ChangeTracker } from '../ChangeTracker';
|
||||
import { BinaryWriter } from './BinaryWriter';
|
||||
|
||||
/**
|
||||
* @zh 编码单个字段值
|
||||
* @en Encode a single field value
|
||||
*/
|
||||
function encodeFieldValue(writer: BinaryWriter, value: any, type: SyncType): void {
|
||||
switch (type) {
|
||||
case 'boolean':
|
||||
writer.writeBoolean(value);
|
||||
break;
|
||||
case 'int8':
|
||||
writer.writeInt8(value);
|
||||
break;
|
||||
case 'uint8':
|
||||
writer.writeUint8(value);
|
||||
break;
|
||||
case 'int16':
|
||||
writer.writeInt16(value);
|
||||
break;
|
||||
case 'uint16':
|
||||
writer.writeUint16(value);
|
||||
break;
|
||||
case 'int32':
|
||||
writer.writeInt32(value);
|
||||
break;
|
||||
case 'uint32':
|
||||
writer.writeUint32(value);
|
||||
break;
|
||||
case 'float32':
|
||||
writer.writeFloat32(value);
|
||||
break;
|
||||
case 'float64':
|
||||
writer.writeFloat64(value);
|
||||
break;
|
||||
case 'string':
|
||||
writer.writeString(value ?? '');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 编码组件的完整状态
|
||||
* @en Encode full state of a component
|
||||
*
|
||||
* @zh 格式: [fieldCount: varint] ([fieldIndex: uint8] [value])...
|
||||
* @en Format: [fieldCount: varint] ([fieldIndex: uint8] [value])...
|
||||
*
|
||||
* @param component - @zh 组件实例 @en Component instance
|
||||
* @param metadata - @zh 组件同步元数据 @en Component sync metadata
|
||||
* @param writer - @zh 二进制写入器 @en Binary writer
|
||||
*/
|
||||
export function encodeComponentFull(
|
||||
component: Component,
|
||||
metadata: SyncMetadata,
|
||||
writer: BinaryWriter
|
||||
): void {
|
||||
const fields = metadata.fields;
|
||||
writer.writeVarint(fields.length);
|
||||
|
||||
for (const field of fields) {
|
||||
writer.writeUint8(field.index);
|
||||
const value = (component as any)[field.name];
|
||||
encodeFieldValue(writer, value, field.type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 编码组件的增量状态(只编码脏字段)
|
||||
* @en Encode delta state of a component (only dirty fields)
|
||||
*
|
||||
* @zh 格式: [dirtyCount: varint] ([fieldIndex: uint8] [value])...
|
||||
* @en Format: [dirtyCount: varint] ([fieldIndex: uint8] [value])...
|
||||
*
|
||||
* @param component - @zh 组件实例 @en Component instance
|
||||
* @param metadata - @zh 组件同步元数据 @en Component sync metadata
|
||||
* @param tracker - @zh 变更追踪器 @en Change tracker
|
||||
* @param writer - @zh 二进制写入器 @en Binary writer
|
||||
* @returns @zh 是否有数据编码 @en Whether any data was encoded
|
||||
*/
|
||||
export function encodeComponentDelta(
|
||||
component: Component,
|
||||
metadata: SyncMetadata,
|
||||
tracker: ChangeTracker,
|
||||
writer: BinaryWriter
|
||||
): boolean {
|
||||
if (!tracker.hasChanges()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const dirtyFields = tracker.getDirtyFields();
|
||||
writer.writeVarint(dirtyFields.length);
|
||||
|
||||
for (const fieldIndex of dirtyFields) {
|
||||
const field = metadata.fields[fieldIndex];
|
||||
if (field) {
|
||||
writer.writeUint8(field.index);
|
||||
const value = (component as any)[field.name];
|
||||
encodeFieldValue(writer, value, field.type);
|
||||
}
|
||||
}
|
||||
|
||||
return dirtyFields.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 编码实体的所有同步组件
|
||||
* @en Encode all sync components of an entity
|
||||
*
|
||||
* @zh 格式:
|
||||
* [entityId: uint32]
|
||||
* [componentCount: varint]
|
||||
* ([typeIdLength: varint] [typeId: string] [componentData])...
|
||||
*
|
||||
* @en Format:
|
||||
* [entityId: uint32]
|
||||
* [componentCount: varint]
|
||||
* ([typeIdLength: varint] [typeId: string] [componentData])...
|
||||
*
|
||||
* @param entity - @zh 实体 @en Entity
|
||||
* @param writer - @zh 二进制写入器 @en Binary writer
|
||||
* @param deltaOnly - @zh 只编码增量 @en Only encode delta
|
||||
* @returns @zh 编码的组件数量 @en Number of components encoded
|
||||
*/
|
||||
export function encodeEntity(
|
||||
entity: Entity,
|
||||
writer: BinaryWriter,
|
||||
deltaOnly: boolean = false
|
||||
): number {
|
||||
writer.writeUint32(entity.id);
|
||||
|
||||
const components = entity.components;
|
||||
const syncComponents: Array<{
|
||||
component: Component;
|
||||
metadata: SyncMetadata;
|
||||
tracker: ChangeTracker | undefined;
|
||||
}> = [];
|
||||
|
||||
// Collect components with sync metadata
|
||||
for (const component of components) {
|
||||
const constructor = component.constructor as any;
|
||||
const metadata: SyncMetadata | undefined = constructor[SYNC_METADATA];
|
||||
if (metadata && metadata.fields.length > 0) {
|
||||
const tracker = (component as any)[CHANGE_TRACKER] as ChangeTracker | undefined;
|
||||
|
||||
// For delta encoding, only include components with changes
|
||||
if (deltaOnly && tracker && !tracker.hasChanges()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
syncComponents.push({ component, metadata, tracker });
|
||||
}
|
||||
}
|
||||
|
||||
writer.writeVarint(syncComponents.length);
|
||||
|
||||
for (const { component, metadata, tracker } of syncComponents) {
|
||||
// Write component type ID
|
||||
writer.writeString(metadata.typeId);
|
||||
|
||||
if (deltaOnly && tracker) {
|
||||
encodeComponentDelta(component, metadata, tracker, writer);
|
||||
} else {
|
||||
encodeComponentFull(component, metadata, writer);
|
||||
}
|
||||
}
|
||||
|
||||
return syncComponents.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 编码状态快照(多个实体)
|
||||
* @en Encode state snapshot (multiple entities)
|
||||
*
|
||||
* @zh 格式:
|
||||
* [operation: uint8] (FULL=0, DELTA=1, SPAWN=2, DESPAWN=3)
|
||||
* [entityCount: varint]
|
||||
* (entityData)...
|
||||
*
|
||||
* @en Format:
|
||||
* [operation: uint8] (FULL=0, DELTA=1, SPAWN=2, DESPAWN=3)
|
||||
* [entityCount: varint]
|
||||
* (entityData)...
|
||||
*
|
||||
* @param entities - @zh 要编码的实体数组 @en Entities to encode
|
||||
* @param operation - @zh 同步操作类型 @en Sync operation type
|
||||
* @returns @zh 编码后的二进制数据 @en Encoded binary data
|
||||
*/
|
||||
export function encodeSnapshot(
|
||||
entities: Entity[],
|
||||
operation: SyncOperation = SyncOperation.FULL
|
||||
): Uint8Array {
|
||||
const writer = new BinaryWriter(1024);
|
||||
|
||||
writer.writeUint8(operation);
|
||||
writer.writeVarint(entities.length);
|
||||
|
||||
const deltaOnly = operation === SyncOperation.DELTA;
|
||||
|
||||
for (const entity of entities) {
|
||||
encodeEntity(entity, writer, deltaOnly);
|
||||
}
|
||||
|
||||
return writer.toUint8Array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 编码实体生成消息
|
||||
* @en Encode entity spawn message
|
||||
*
|
||||
* @param entity - @zh 生成的实体 @en Spawned entity
|
||||
* @param prefabType - @zh 预制体类型(可选)@en Prefab type (optional)
|
||||
* @returns @zh 编码后的二进制数据 @en Encoded binary data
|
||||
*/
|
||||
export function encodeSpawn(entity: Entity, prefabType?: string): Uint8Array {
|
||||
const writer = new BinaryWriter(256);
|
||||
|
||||
writer.writeUint8(SyncOperation.SPAWN);
|
||||
writer.writeUint32(entity.id);
|
||||
writer.writeString(prefabType || '');
|
||||
|
||||
// Encode all sync components for initial state
|
||||
const components = entity.components;
|
||||
const syncComponents: Array<{ component: Component; metadata: SyncMetadata }> = [];
|
||||
|
||||
for (const component of components) {
|
||||
const constructor = component.constructor as any;
|
||||
const metadata: SyncMetadata | undefined = constructor[SYNC_METADATA];
|
||||
if (metadata && metadata.fields.length > 0) {
|
||||
syncComponents.push({ component, metadata });
|
||||
}
|
||||
}
|
||||
|
||||
writer.writeVarint(syncComponents.length);
|
||||
|
||||
for (const { component, metadata } of syncComponents) {
|
||||
writer.writeString(metadata.typeId);
|
||||
encodeComponentFull(component, metadata, writer);
|
||||
}
|
||||
|
||||
return writer.toUint8Array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 编码实体销毁消息
|
||||
* @en Encode entity despawn message
|
||||
*
|
||||
* @param entityId - @zh 销毁的实体 ID @en Despawned entity ID
|
||||
* @returns @zh 编码后的二进制数据 @en Encoded binary data
|
||||
*/
|
||||
export function encodeDespawn(entityId: number): Uint8Array {
|
||||
const writer = new BinaryWriter(8);
|
||||
|
||||
writer.writeUint8(SyncOperation.DESPAWN);
|
||||
writer.writeUint32(entityId);
|
||||
|
||||
return writer.toUint8Array();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 编码批量实体销毁消息
|
||||
* @en Encode batch entity despawn message
|
||||
*
|
||||
* @param entityIds - @zh 销毁的实体 ID 数组 @en Despawned entity IDs
|
||||
* @returns @zh 编码后的二进制数据 @en Encoded binary data
|
||||
*/
|
||||
export function encodeDespawnBatch(entityIds: number[]): Uint8Array {
|
||||
const writer = new BinaryWriter(8 + entityIds.length * 4);
|
||||
|
||||
writer.writeUint8(SyncOperation.DESPAWN);
|
||||
writer.writeVarint(entityIds.length);
|
||||
|
||||
for (const id of entityIds) {
|
||||
writer.writeUint32(id);
|
||||
}
|
||||
|
||||
return writer.toUint8Array();
|
||||
}
|
||||
50
packages/framework/core/src/ECS/Sync/encoding/index.ts
Normal file
50
packages/framework/core/src/ECS/Sync/encoding/index.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* @zh 二进制编解码模块
|
||||
* @en Binary encoding/decoding module
|
||||
*
|
||||
* @zh 提供 ECS Component 状态的二进制序列化和反序列化功能
|
||||
* @en Provides binary serialization and deserialization for ECS Component state
|
||||
*/
|
||||
|
||||
// Variable-length integer encoding
|
||||
export {
|
||||
varintSize,
|
||||
encodeVarint,
|
||||
decodeVarint,
|
||||
zigzagEncode,
|
||||
zigzagDecode,
|
||||
encodeSignedVarint,
|
||||
decodeSignedVarint
|
||||
} from './varint';
|
||||
|
||||
// Binary writer/reader
|
||||
export { BinaryWriter } from './BinaryWriter';
|
||||
export { BinaryReader } from './BinaryReader';
|
||||
|
||||
// Encoder
|
||||
export {
|
||||
encodeComponentFull,
|
||||
encodeComponentDelta,
|
||||
encodeEntity,
|
||||
encodeSnapshot,
|
||||
encodeSpawn,
|
||||
encodeDespawn,
|
||||
encodeDespawnBatch
|
||||
} from './Encoder';
|
||||
|
||||
// Decoder
|
||||
export {
|
||||
decodeComponent,
|
||||
decodeEntity,
|
||||
decodeSnapshot,
|
||||
decodeSpawn,
|
||||
decodeDespawn,
|
||||
processDespawn
|
||||
} from './Decoder';
|
||||
|
||||
export type {
|
||||
DecodeEntityResult,
|
||||
DecodeSnapshotResult,
|
||||
DecodeSpawnResult,
|
||||
DecodeDespawnResult
|
||||
} from './Decoder';
|
||||
137
packages/framework/core/src/ECS/Sync/encoding/varint.ts
Normal file
137
packages/framework/core/src/ECS/Sync/encoding/varint.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
/**
|
||||
* @zh 变长整数编解码
|
||||
* @en Variable-length integer encoding/decoding
|
||||
*
|
||||
* @zh 使用 LEB128 编码方式,可变长度编码正整数。
|
||||
* 小数值使用更少字节,大数值使用更多字节。
|
||||
* @en Uses LEB128 encoding for variable-length integer encoding.
|
||||
* Small values use fewer bytes, large values use more bytes.
|
||||
*
|
||||
* | 值范围 | 字节数 |
|
||||
* |--------|--------|
|
||||
* | 0-127 | 1 |
|
||||
* | 128-16383 | 2 |
|
||||
* | 16384-2097151 | 3 |
|
||||
* | 2097152-268435455 | 4 |
|
||||
* | 268435456+ | 5 |
|
||||
*/
|
||||
|
||||
/**
|
||||
* @zh 计算变长整数所需的字节数
|
||||
* @en Calculate bytes needed for a varint
|
||||
*
|
||||
* @param value - @zh 整数值 @en Integer value
|
||||
* @returns @zh 所需字节数 @en Bytes needed
|
||||
*/
|
||||
export function varintSize(value: number): number {
|
||||
if (value < 0) {
|
||||
throw new Error('Varint only supports non-negative integers');
|
||||
}
|
||||
if (value < 128) return 1;
|
||||
if (value < 16384) return 2;
|
||||
if (value < 2097152) return 3;
|
||||
if (value < 268435456) return 4;
|
||||
return 5;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 编码变长整数到字节数组
|
||||
* @en Encode varint to byte array
|
||||
*
|
||||
* @param value - @zh 要编码的整数 @en Integer to encode
|
||||
* @param buffer - @zh 目标缓冲区 @en Target buffer
|
||||
* @param offset - @zh 写入偏移 @en Write offset
|
||||
* @returns @zh 写入后的新偏移 @en New offset after writing
|
||||
*/
|
||||
export function encodeVarint(value: number, buffer: Uint8Array, offset: number): number {
|
||||
if (value < 0) {
|
||||
throw new Error('Varint only supports non-negative integers');
|
||||
}
|
||||
|
||||
while (value >= 0x80) {
|
||||
buffer[offset++] = (value & 0x7F) | 0x80;
|
||||
value >>>= 7;
|
||||
}
|
||||
buffer[offset++] = value;
|
||||
return offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 从字节数组解码变长整数
|
||||
* @en Decode varint from byte array
|
||||
*
|
||||
* @param buffer - @zh 源缓冲区 @en Source buffer
|
||||
* @param offset - @zh 读取偏移 @en Read offset
|
||||
* @returns @zh [解码值, 新偏移] @en [decoded value, new offset]
|
||||
*/
|
||||
export function decodeVarint(buffer: Uint8Array, offset: number): [number, number] {
|
||||
let result = 0;
|
||||
let shift = 0;
|
||||
let byte: number;
|
||||
|
||||
do {
|
||||
if (offset >= buffer.length) {
|
||||
throw new Error('Varint decode: buffer overflow');
|
||||
}
|
||||
byte = buffer[offset++]!;
|
||||
result |= (byte & 0x7F) << shift;
|
||||
shift += 7;
|
||||
} while (byte >= 0x80);
|
||||
|
||||
return [result, offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 编码有符号整数(ZigZag 编码)
|
||||
* @en Encode signed integer (ZigZag encoding)
|
||||
*
|
||||
* @zh ZigZag 编码将有符号整数映射到无符号整数:
|
||||
* 0 → 0, -1 → 1, 1 → 2, -2 → 3, 2 → 4, ...
|
||||
* 这样小的负数也能用较少字节表示。
|
||||
* @en ZigZag encoding maps signed integers to unsigned:
|
||||
* 0 → 0, -1 → 1, 1 → 2, -2 → 3, 2 → 4, ...
|
||||
* This allows small negative numbers to use fewer bytes.
|
||||
*
|
||||
* @param value - @zh 有符号整数 @en Signed integer
|
||||
* @returns @zh ZigZag 编码后的值 @en ZigZag encoded value
|
||||
*/
|
||||
export function zigzagEncode(value: number): number {
|
||||
return (value << 1) ^ (value >> 31);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 解码有符号整数(ZigZag 解码)
|
||||
* @en Decode signed integer (ZigZag decoding)
|
||||
*
|
||||
* @param value - @zh ZigZag 编码的值 @en ZigZag encoded value
|
||||
* @returns @zh 原始有符号整数 @en Original signed integer
|
||||
*/
|
||||
export function zigzagDecode(value: number): number {
|
||||
return (value >>> 1) ^ -(value & 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 编码有符号变长整数
|
||||
* @en Encode signed varint
|
||||
*
|
||||
* @param value - @zh 有符号整数 @en Signed integer
|
||||
* @param buffer - @zh 目标缓冲区 @en Target buffer
|
||||
* @param offset - @zh 写入偏移 @en Write offset
|
||||
* @returns @zh 写入后的新偏移 @en New offset after writing
|
||||
*/
|
||||
export function encodeSignedVarint(value: number, buffer: Uint8Array, offset: number): number {
|
||||
return encodeVarint(zigzagEncode(value), buffer, offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 解码有符号变长整数
|
||||
* @en Decode signed varint
|
||||
*
|
||||
* @param buffer - @zh 源缓冲区 @en Source buffer
|
||||
* @param offset - @zh 读取偏移 @en Read offset
|
||||
* @returns @zh [解码值, 新偏移] @en [decoded value, new offset]
|
||||
*/
|
||||
export function decodeSignedVarint(buffer: Uint8Array, offset: number): [number, number] {
|
||||
const [encoded, newOffset] = decodeVarint(buffer, offset);
|
||||
return [zigzagDecode(encoded), newOffset];
|
||||
}
|
||||
65
packages/framework/core/src/ECS/Sync/index.ts
Normal file
65
packages/framework/core/src/ECS/Sync/index.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* @zh ECS 网络同步模块
|
||||
* @en ECS Network Synchronization Module
|
||||
*
|
||||
* @zh 提供基于 ECS Component 的网络状态同步功能:
|
||||
* - @sync 装饰器:标记需要同步的字段
|
||||
* - ChangeTracker:追踪字段级变更
|
||||
* - 二进制编解码器:高效的网络序列化
|
||||
*
|
||||
* @en Provides network state synchronization based on ECS Components:
|
||||
* - @sync decorator: Mark fields for synchronization
|
||||
* - ChangeTracker: Track field-level changes
|
||||
* - Binary encoder/decoder: Efficient network serialization
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { Component, ECSComponent, sync } from '@esengine/ecs-framework';
|
||||
*
|
||||
* @ECSComponent('Player')
|
||||
* class PlayerComponent extends Component {
|
||||
* @sync("string") name: string = "";
|
||||
* @sync("uint16") score: number = 0;
|
||||
* @sync("float32") x: number = 0;
|
||||
* @sync("float32") y: number = 0;
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
|
||||
// Types
|
||||
export {
|
||||
SyncType,
|
||||
SyncFieldMetadata,
|
||||
SyncMetadata,
|
||||
SyncOperation,
|
||||
TYPE_SIZES,
|
||||
SYNC_METADATA,
|
||||
CHANGE_TRACKER
|
||||
} from './types';
|
||||
|
||||
// Change Tracker
|
||||
export { ChangeTracker } from './ChangeTracker';
|
||||
|
||||
// Decorators
|
||||
export {
|
||||
sync,
|
||||
getSyncMetadata,
|
||||
hasSyncFields,
|
||||
getChangeTracker,
|
||||
initChangeTracker,
|
||||
clearChanges,
|
||||
hasChanges
|
||||
} from './decorators';
|
||||
|
||||
// Network Entity Decorator
|
||||
export {
|
||||
NetworkEntity,
|
||||
getNetworkEntityMetadata,
|
||||
isNetworkEntity,
|
||||
NETWORK_ENTITY_METADATA,
|
||||
type NetworkEntityMetadata,
|
||||
type NetworkEntityOptions
|
||||
} from './NetworkEntityDecorator';
|
||||
|
||||
// Encoding
|
||||
export * from './encoding';
|
||||
127
packages/framework/core/src/ECS/Sync/types.ts
Normal file
127
packages/framework/core/src/ECS/Sync/types.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* @zh 网络同步类型定义
|
||||
* @en Network synchronization type definitions
|
||||
*/
|
||||
|
||||
/**
|
||||
* @zh 支持的同步数据类型
|
||||
* @en Supported sync data types
|
||||
*/
|
||||
export type SyncType =
|
||||
| 'boolean'
|
||||
| 'int8'
|
||||
| 'uint8'
|
||||
| 'int16'
|
||||
| 'uint16'
|
||||
| 'int32'
|
||||
| 'uint32'
|
||||
| 'float32'
|
||||
| 'float64'
|
||||
| 'string';
|
||||
|
||||
/**
|
||||
* @zh 同步字段元数据
|
||||
* @en Sync field metadata
|
||||
*/
|
||||
export interface SyncFieldMetadata {
|
||||
/**
|
||||
* @zh 字段索引(用于二进制编码)
|
||||
* @en Field index (for binary encoding)
|
||||
*/
|
||||
index: number;
|
||||
|
||||
/**
|
||||
* @zh 字段名称
|
||||
* @en Field name
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* @zh 字段类型
|
||||
* @en Field type
|
||||
*/
|
||||
type: SyncType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 组件同步元数据
|
||||
* @en Component sync metadata
|
||||
*/
|
||||
export interface SyncMetadata {
|
||||
/**
|
||||
* @zh 组件类型 ID
|
||||
* @en Component type ID
|
||||
*/
|
||||
typeId: string;
|
||||
|
||||
/**
|
||||
* @zh 同步字段列表(按索引排序)
|
||||
* @en Sync fields list (sorted by index)
|
||||
*/
|
||||
fields: SyncFieldMetadata[];
|
||||
|
||||
/**
|
||||
* @zh 字段名到索引的映射
|
||||
* @en Field name to index mapping
|
||||
*/
|
||||
fieldIndexMap: Map<string, number>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 同步操作类型
|
||||
* @en Sync operation type
|
||||
*/
|
||||
export enum SyncOperation {
|
||||
/**
|
||||
* @zh 完整快照
|
||||
* @en Full snapshot
|
||||
*/
|
||||
FULL = 0,
|
||||
|
||||
/**
|
||||
* @zh 增量更新
|
||||
* @en Delta update
|
||||
*/
|
||||
DELTA = 1,
|
||||
|
||||
/**
|
||||
* @zh 实体生成
|
||||
* @en Entity spawn
|
||||
*/
|
||||
SPAWN = 2,
|
||||
|
||||
/**
|
||||
* @zh 实体销毁
|
||||
* @en Entity despawn
|
||||
*/
|
||||
DESPAWN = 3,
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 各类型的字节大小
|
||||
* @en Byte size for each type
|
||||
*/
|
||||
export const TYPE_SIZES: Record<SyncType, number> = {
|
||||
boolean: 1,
|
||||
int8: 1,
|
||||
uint8: 1,
|
||||
int16: 2,
|
||||
uint16: 2,
|
||||
int32: 4,
|
||||
uint32: 4,
|
||||
float32: 4,
|
||||
float64: 8,
|
||||
string: -1, // 动态长度 | dynamic length
|
||||
};
|
||||
|
||||
/**
|
||||
* @zh 同步元数据的 Symbol 键
|
||||
* @en Symbol key for sync metadata
|
||||
*/
|
||||
export const SYNC_METADATA = Symbol('SyncMetadata');
|
||||
|
||||
/**
|
||||
* @zh 变更追踪器的 Symbol 键
|
||||
* @en Symbol key for change tracker
|
||||
*/
|
||||
export const CHANGE_TRACKER = Symbol('ChangeTracker');
|
||||
@@ -317,9 +317,7 @@ export class WorldManager implements IService {
|
||||
/**
|
||||
* @zh 更新所有活跃的World
|
||||
* @en Update all active Worlds
|
||||
*
|
||||
* @zh 应该在每帧的游戏循环中调用
|
||||
* @en Should be called in each frame of game loop
|
||||
* @internal 由 Core.update() 调用,用户不应直接调用
|
||||
*/
|
||||
public updateAll(): void {
|
||||
if (!this._isRunning) return;
|
||||
|
||||
@@ -7,7 +7,7 @@ export * from './Utils';
|
||||
export * from './Decorators';
|
||||
export * from './Components';
|
||||
export { Scene } from './Scene';
|
||||
export type { IScene, ISceneFactory, ISceneConfig } from './IScene';
|
||||
export type { IScene, ISceneFactory, ISceneConfig, RuntimeEnvironment } from './IScene';
|
||||
export { SceneManager } from './SceneManager';
|
||||
export { World } from './World';
|
||||
export type { IWorldConfig } from './World';
|
||||
@@ -57,3 +57,6 @@ export { EpochManager } from './Core/EpochManager';
|
||||
// Compiled Query
|
||||
export { CompiledQuery } from './Core/Query/CompiledQuery';
|
||||
export type { InstanceTypes } from './Core/Query/CompiledQuery';
|
||||
|
||||
// Network Synchronization
|
||||
export * from './Sync';
|
||||
|
||||
50
packages/framework/core/src/RuntimeConfig.ts
Normal file
50
packages/framework/core/src/RuntimeConfig.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import type { RuntimeEnvironment } from './Types';
|
||||
|
||||
/**
|
||||
* @zh 全局运行时配置
|
||||
* @en Global runtime configuration
|
||||
*
|
||||
* @zh 独立模块,避免 Core 和 Scene 之间的循环依赖
|
||||
* @en Standalone module to avoid circular dependency between Core and Scene
|
||||
*/
|
||||
class RuntimeConfigClass {
|
||||
private _runtimeEnvironment: RuntimeEnvironment = 'standalone';
|
||||
|
||||
/**
|
||||
* @zh 获取运行时环境
|
||||
* @en Get runtime environment
|
||||
*/
|
||||
get runtimeEnvironment(): RuntimeEnvironment {
|
||||
return this._runtimeEnvironment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 设置运行时环境
|
||||
* @en Set runtime environment
|
||||
*/
|
||||
set runtimeEnvironment(value: RuntimeEnvironment) {
|
||||
this._runtimeEnvironment = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 是否在服务端运行
|
||||
* @en Whether running on server
|
||||
*/
|
||||
get isServer(): boolean {
|
||||
return this._runtimeEnvironment === 'server';
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 是否在客户端运行
|
||||
* @en Whether running on client
|
||||
*/
|
||||
get isClient(): boolean {
|
||||
return this._runtimeEnvironment === 'client';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 全局运行时配置单例
|
||||
* @en Global runtime configuration singleton
|
||||
*/
|
||||
export const RuntimeConfig = new RuntimeConfigClass();
|
||||
@@ -267,6 +267,12 @@ export type IECSDebugConfig = {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 运行时环境类型
|
||||
* @en Runtime environment type
|
||||
*/
|
||||
export type RuntimeEnvironment = 'server' | 'client' | 'standalone';
|
||||
|
||||
/**
|
||||
* Core配置接口
|
||||
*/
|
||||
@@ -277,6 +283,16 @@ export type ICoreConfig = {
|
||||
debugConfig?: IECSDebugConfig;
|
||||
/** WorldManager配置 */
|
||||
worldManagerConfig?: IWorldManagerConfig;
|
||||
/**
|
||||
* @zh 运行时环境
|
||||
* @en Runtime environment
|
||||
*
|
||||
* @zh 设置后所有 Scene 默认继承此环境。服务端框架应设置为 'server',客户端应用设置为 'client'。
|
||||
* @en All Scenes inherit this environment by default. Server frameworks should set 'server', client apps should set 'client'.
|
||||
*
|
||||
* @default 'standalone'
|
||||
*/
|
||||
runtimeEnvironment?: RuntimeEnvironment;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
// 核心模块
|
||||
export { Core } from './Core';
|
||||
export { RuntimeConfig } from './RuntimeConfig';
|
||||
export { ServiceContainer, ServiceLifetime } from './Core/ServiceContainer';
|
||||
export type { IService, ServiceType, ServiceIdentifier } from './Core/ServiceContainer';
|
||||
|
||||
|
||||
172
packages/framework/core/tests/ECS/Sync/ChangeTracker.test.ts
Normal file
172
packages/framework/core/tests/ECS/Sync/ChangeTracker.test.ts
Normal file
@@ -0,0 +1,172 @@
|
||||
import { ChangeTracker } from '../../../src/ECS/Sync/ChangeTracker';
|
||||
|
||||
describe('ChangeTracker - 变更追踪器测试', () => {
|
||||
let tracker: ChangeTracker;
|
||||
|
||||
beforeEach(() => {
|
||||
tracker = new ChangeTracker();
|
||||
});
|
||||
|
||||
describe('基本功能', () => {
|
||||
test('初始状态应该没有变更', () => {
|
||||
expect(tracker.hasChanges()).toBe(false);
|
||||
expect(tracker.getDirtyCount()).toBe(0);
|
||||
expect(tracker.getDirtyFields()).toEqual([]);
|
||||
});
|
||||
|
||||
test('setDirty 应该标记字段为脏', () => {
|
||||
tracker.setDirty(0);
|
||||
|
||||
expect(tracker.hasChanges()).toBe(true);
|
||||
expect(tracker.isDirty(0)).toBe(true);
|
||||
expect(tracker.getDirtyCount()).toBe(1);
|
||||
expect(tracker.getDirtyFields()).toEqual([0]);
|
||||
});
|
||||
|
||||
test('多次 setDirty 同一字段应该只记录一次', () => {
|
||||
tracker.setDirty(0);
|
||||
tracker.setDirty(0);
|
||||
tracker.setDirty(0);
|
||||
|
||||
expect(tracker.getDirtyCount()).toBe(1);
|
||||
expect(tracker.getDirtyFields()).toEqual([0]);
|
||||
});
|
||||
|
||||
test('setDirty 不同字段应该都被记录', () => {
|
||||
tracker.setDirty(0);
|
||||
tracker.setDirty(1);
|
||||
tracker.setDirty(2);
|
||||
|
||||
expect(tracker.getDirtyCount()).toBe(3);
|
||||
expect(tracker.getDirtyFields().sort()).toEqual([0, 1, 2]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isDirty 方法', () => {
|
||||
test('未标记的字段应该返回 false', () => {
|
||||
expect(tracker.isDirty(0)).toBe(false);
|
||||
expect(tracker.isDirty(5)).toBe(false);
|
||||
});
|
||||
|
||||
test('已标记的字段应该返回 true', () => {
|
||||
tracker.setDirty(3);
|
||||
|
||||
expect(tracker.isDirty(3)).toBe(true);
|
||||
expect(tracker.isDirty(0)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('clear 方法', () => {
|
||||
test('clear 应该清除所有变更', () => {
|
||||
tracker.setDirty(0);
|
||||
tracker.setDirty(1);
|
||||
tracker.setDirty(2);
|
||||
|
||||
expect(tracker.hasChanges()).toBe(true);
|
||||
|
||||
tracker.clear();
|
||||
|
||||
expect(tracker.hasChanges()).toBe(false);
|
||||
expect(tracker.getDirtyCount()).toBe(0);
|
||||
expect(tracker.getDirtyFields()).toEqual([]);
|
||||
});
|
||||
|
||||
test('clear 应该更新 lastSyncTime', () => {
|
||||
const before = tracker.lastSyncTime;
|
||||
tracker.setDirty(0);
|
||||
tracker.clear();
|
||||
|
||||
expect(tracker.lastSyncTime).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('clearField 方法', () => {
|
||||
test('clearField 应该只清除指定字段', () => {
|
||||
tracker.setDirty(0);
|
||||
tracker.setDirty(1);
|
||||
tracker.setDirty(2);
|
||||
|
||||
tracker.clearField(1);
|
||||
|
||||
expect(tracker.isDirty(0)).toBe(true);
|
||||
expect(tracker.isDirty(1)).toBe(false);
|
||||
expect(tracker.isDirty(2)).toBe(true);
|
||||
expect(tracker.getDirtyCount()).toBe(2);
|
||||
});
|
||||
|
||||
test('清除最后一个字段应该使 hasChanges 返回 false', () => {
|
||||
tracker.setDirty(0);
|
||||
expect(tracker.hasChanges()).toBe(true);
|
||||
|
||||
tracker.clearField(0);
|
||||
|
||||
expect(tracker.hasChanges()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('markAllDirty 方法', () => {
|
||||
test('markAllDirty 应该标记所有字段', () => {
|
||||
tracker.markAllDirty(5);
|
||||
|
||||
expect(tracker.hasChanges()).toBe(true);
|
||||
expect(tracker.getDirtyCount()).toBe(5);
|
||||
expect(tracker.getDirtyFields().sort()).toEqual([0, 1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
test('markAllDirty(0) 应该没有变更', () => {
|
||||
tracker.markAllDirty(0);
|
||||
|
||||
expect(tracker.hasChanges()).toBe(false);
|
||||
expect(tracker.getDirtyCount()).toBe(0);
|
||||
});
|
||||
|
||||
test('markAllDirty 用于首次同步', () => {
|
||||
tracker.markAllDirty(3);
|
||||
|
||||
expect(tracker.isDirty(0)).toBe(true);
|
||||
expect(tracker.isDirty(1)).toBe(true);
|
||||
expect(tracker.isDirty(2)).toBe(true);
|
||||
expect(tracker.isDirty(3)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('reset 方法', () => {
|
||||
test('reset 应该重置所有状态', () => {
|
||||
tracker.setDirty(0);
|
||||
tracker.setDirty(1);
|
||||
tracker.clear();
|
||||
|
||||
tracker.reset();
|
||||
|
||||
expect(tracker.hasChanges()).toBe(false);
|
||||
expect(tracker.getDirtyCount()).toBe(0);
|
||||
expect(tracker.lastSyncTime).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('边界情况', () => {
|
||||
test('大量字段标记应该正常工作', () => {
|
||||
const fieldCount = 1000;
|
||||
|
||||
for (let i = 0; i < fieldCount; i++) {
|
||||
tracker.setDirty(i);
|
||||
}
|
||||
|
||||
expect(tracker.getDirtyCount()).toBe(fieldCount);
|
||||
expect(tracker.hasChanges()).toBe(true);
|
||||
});
|
||||
|
||||
test('交替设置和清除应该正常工作', () => {
|
||||
tracker.setDirty(0);
|
||||
tracker.setDirty(1);
|
||||
tracker.clearField(0);
|
||||
tracker.setDirty(2);
|
||||
tracker.clearField(1);
|
||||
|
||||
expect(tracker.isDirty(0)).toBe(false);
|
||||
expect(tracker.isDirty(1)).toBe(false);
|
||||
expect(tracker.isDirty(2)).toBe(true);
|
||||
expect(tracker.getDirtyCount()).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
327
packages/framework/core/tests/ECS/Sync/decorators.test.ts
Normal file
327
packages/framework/core/tests/ECS/Sync/decorators.test.ts
Normal file
@@ -0,0 +1,327 @@
|
||||
import { Component } from '../../../src/ECS/Component';
|
||||
import { ECSComponent } from '../../../src/ECS/Decorators';
|
||||
import { Scene } from '../../../src/ECS/Scene';
|
||||
import {
|
||||
sync,
|
||||
getSyncMetadata,
|
||||
hasSyncFields,
|
||||
getChangeTracker,
|
||||
initChangeTracker,
|
||||
clearChanges,
|
||||
hasChanges
|
||||
} from '../../../src/ECS/Sync/decorators';
|
||||
import { SYNC_METADATA, CHANGE_TRACKER } from '../../../src/ECS/Sync/types';
|
||||
|
||||
@ECSComponent('SyncTest_PlayerComponent')
|
||||
class PlayerComponent extends Component {
|
||||
@sync("string") name: string = "";
|
||||
@sync("uint16") score: number = 0;
|
||||
@sync("float32") x: number = 0;
|
||||
@sync("float32") y: number = 0;
|
||||
|
||||
localData: string = "not synced";
|
||||
}
|
||||
|
||||
@ECSComponent('SyncTest_SimpleComponent')
|
||||
class SimpleComponent extends Component {
|
||||
@sync("boolean") active: boolean = true;
|
||||
@sync("int32") value: number = 100;
|
||||
}
|
||||
|
||||
@ECSComponent('SyncTest_NoSyncComponent')
|
||||
class NoSyncComponent extends Component {
|
||||
localValue: number = 0;
|
||||
}
|
||||
|
||||
@ECSComponent('SyncTest_AllTypesComponent')
|
||||
class AllTypesComponent extends Component {
|
||||
@sync("boolean") boolField: boolean = false;
|
||||
@sync("int8") int8Field: number = 0;
|
||||
@sync("uint8") uint8Field: number = 0;
|
||||
@sync("int16") int16Field: number = 0;
|
||||
@sync("uint16") uint16Field: number = 0;
|
||||
@sync("int32") int32Field: number = 0;
|
||||
@sync("uint32") uint32Field: number = 0;
|
||||
@sync("float32") float32Field: number = 0;
|
||||
@sync("float64") float64Field: number = 0;
|
||||
@sync("string") stringField: string = "";
|
||||
}
|
||||
|
||||
describe('@sync 装饰器测试', () => {
|
||||
describe('getSyncMetadata', () => {
|
||||
test('应该返回带 @sync 字段的组件元数据', () => {
|
||||
const metadata = getSyncMetadata(PlayerComponent);
|
||||
|
||||
expect(metadata).not.toBeNull();
|
||||
expect(metadata!.typeId).toBe('SyncTest_PlayerComponent');
|
||||
expect(metadata!.fields.length).toBe(4);
|
||||
});
|
||||
|
||||
test('应该正确记录字段信息', () => {
|
||||
const metadata = getSyncMetadata(PlayerComponent);
|
||||
|
||||
const nameField = metadata!.fields.find(f => f.name === 'name');
|
||||
expect(nameField).toBeDefined();
|
||||
expect(nameField!.type).toBe('string');
|
||||
expect(nameField!.index).toBe(0);
|
||||
|
||||
const scoreField = metadata!.fields.find(f => f.name === 'score');
|
||||
expect(scoreField).toBeDefined();
|
||||
expect(scoreField!.type).toBe('uint16');
|
||||
|
||||
const xField = metadata!.fields.find(f => f.name === 'x');
|
||||
expect(xField).toBeDefined();
|
||||
expect(xField!.type).toBe('float32');
|
||||
});
|
||||
|
||||
test('没有 @sync 字段的组件应该返回 null', () => {
|
||||
const metadata = getSyncMetadata(NoSyncComponent);
|
||||
|
||||
expect(metadata).toBeNull();
|
||||
});
|
||||
|
||||
test('可以从实例获取元数据', () => {
|
||||
const component = new PlayerComponent();
|
||||
const metadata = getSyncMetadata(component);
|
||||
|
||||
expect(metadata).not.toBeNull();
|
||||
expect(metadata!.fields.length).toBe(4);
|
||||
});
|
||||
|
||||
test('fieldIndexMap 应该正确映射字段名到索引', () => {
|
||||
const metadata = getSyncMetadata(PlayerComponent);
|
||||
|
||||
expect(metadata!.fieldIndexMap.get('name')).toBe(0);
|
||||
expect(metadata!.fieldIndexMap.get('score')).toBe(1);
|
||||
expect(metadata!.fieldIndexMap.get('x')).toBe(2);
|
||||
expect(metadata!.fieldIndexMap.get('y')).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasSyncFields', () => {
|
||||
test('有 @sync 字段应该返回 true', () => {
|
||||
expect(hasSyncFields(PlayerComponent)).toBe(true);
|
||||
expect(hasSyncFields(new PlayerComponent())).toBe(true);
|
||||
});
|
||||
|
||||
test('没有 @sync 字段应该返回 false', () => {
|
||||
expect(hasSyncFields(NoSyncComponent)).toBe(false);
|
||||
expect(hasSyncFields(new NoSyncComponent())).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('支持所有同步类型', () => {
|
||||
test('AllTypesComponent 应该有所有类型的字段', () => {
|
||||
const metadata = getSyncMetadata(AllTypesComponent);
|
||||
|
||||
expect(metadata).not.toBeNull();
|
||||
expect(metadata!.fields.length).toBe(10);
|
||||
|
||||
const types = metadata!.fields.map(f => f.type);
|
||||
expect(types).toContain('boolean');
|
||||
expect(types).toContain('int8');
|
||||
expect(types).toContain('uint8');
|
||||
expect(types).toContain('int16');
|
||||
expect(types).toContain('uint16');
|
||||
expect(types).toContain('int32');
|
||||
expect(types).toContain('uint32');
|
||||
expect(types).toContain('float32');
|
||||
expect(types).toContain('float64');
|
||||
expect(types).toContain('string');
|
||||
});
|
||||
});
|
||||
|
||||
describe('字段值拦截', () => {
|
||||
test('修改 @sync 字段应该触发变更追踪', () => {
|
||||
const component = new PlayerComponent();
|
||||
initChangeTracker(component);
|
||||
|
||||
const tracker = getChangeTracker(component);
|
||||
expect(tracker).not.toBeNull();
|
||||
tracker!.clear();
|
||||
|
||||
component.name = "TestPlayer";
|
||||
|
||||
expect(tracker!.hasChanges()).toBe(true);
|
||||
expect(tracker!.isDirty(0)).toBe(true);
|
||||
});
|
||||
|
||||
test('设置相同值不应该触发变更', () => {
|
||||
const component = new PlayerComponent();
|
||||
component.name = "Test";
|
||||
initChangeTracker(component);
|
||||
|
||||
const tracker = getChangeTracker(component);
|
||||
tracker!.clear();
|
||||
|
||||
component.name = "Test";
|
||||
|
||||
expect(tracker!.hasChanges()).toBe(false);
|
||||
});
|
||||
|
||||
test('修改非 @sync 字段不应该触发变更', () => {
|
||||
const component = new PlayerComponent();
|
||||
initChangeTracker(component);
|
||||
|
||||
const tracker = getChangeTracker(component);
|
||||
tracker!.clear();
|
||||
|
||||
component.localData = "new value";
|
||||
|
||||
expect(tracker!.hasChanges()).toBe(false);
|
||||
});
|
||||
|
||||
test('多个字段变更应该都被追踪', () => {
|
||||
const component = new PlayerComponent();
|
||||
initChangeTracker(component);
|
||||
|
||||
const tracker = getChangeTracker(component);
|
||||
tracker!.clear();
|
||||
|
||||
component.name = "NewName";
|
||||
component.score = 100;
|
||||
component.x = 1.5;
|
||||
|
||||
expect(tracker!.getDirtyCount()).toBe(3);
|
||||
expect(tracker!.isDirty(0)).toBe(true);
|
||||
expect(tracker!.isDirty(1)).toBe(true);
|
||||
expect(tracker!.isDirty(2)).toBe(true);
|
||||
expect(tracker!.isDirty(3)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('initChangeTracker', () => {
|
||||
test('应该创建变更追踪器', () => {
|
||||
const component = new PlayerComponent();
|
||||
|
||||
expect(getChangeTracker(component)).toBeNull();
|
||||
|
||||
initChangeTracker(component);
|
||||
|
||||
expect(getChangeTracker(component)).not.toBeNull();
|
||||
});
|
||||
|
||||
test('应该标记所有字段为脏(用于首次同步)', () => {
|
||||
const component = new PlayerComponent();
|
||||
initChangeTracker(component);
|
||||
|
||||
const tracker = getChangeTracker(component);
|
||||
expect(tracker!.hasChanges()).toBe(true);
|
||||
expect(tracker!.getDirtyCount()).toBe(4);
|
||||
});
|
||||
|
||||
test('对没有 @sync 字段的组件应该抛出错误', () => {
|
||||
const component = new NoSyncComponent();
|
||||
|
||||
expect(() => {
|
||||
initChangeTracker(component);
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
test('重复初始化应该重新标记所有字段', () => {
|
||||
const component = new PlayerComponent();
|
||||
initChangeTracker(component);
|
||||
|
||||
const tracker = getChangeTracker(component);
|
||||
tracker!.clear();
|
||||
|
||||
expect(tracker!.hasChanges()).toBe(false);
|
||||
|
||||
initChangeTracker(component);
|
||||
|
||||
expect(tracker!.hasChanges()).toBe(true);
|
||||
expect(tracker!.getDirtyCount()).toBe(4);
|
||||
});
|
||||
});
|
||||
|
||||
describe('clearChanges', () => {
|
||||
test('应该清除所有变更标记', () => {
|
||||
const component = new PlayerComponent();
|
||||
initChangeTracker(component);
|
||||
|
||||
expect(hasChanges(component)).toBe(true);
|
||||
|
||||
clearChanges(component);
|
||||
|
||||
expect(hasChanges(component)).toBe(false);
|
||||
});
|
||||
|
||||
test('对没有追踪器的组件应该安全执行', () => {
|
||||
const component = new PlayerComponent();
|
||||
|
||||
expect(() => {
|
||||
clearChanges(component);
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasChanges', () => {
|
||||
test('初始化后应该有变更', () => {
|
||||
const component = new PlayerComponent();
|
||||
initChangeTracker(component);
|
||||
|
||||
expect(hasChanges(component)).toBe(true);
|
||||
});
|
||||
|
||||
test('清除后应该没有变更', () => {
|
||||
const component = new PlayerComponent();
|
||||
initChangeTracker(component);
|
||||
clearChanges(component);
|
||||
|
||||
expect(hasChanges(component)).toBe(false);
|
||||
});
|
||||
|
||||
test('修改字段后应该有变更', () => {
|
||||
const component = new PlayerComponent();
|
||||
initChangeTracker(component);
|
||||
clearChanges(component);
|
||||
|
||||
component.score = 999;
|
||||
|
||||
expect(hasChanges(component)).toBe(true);
|
||||
});
|
||||
|
||||
test('没有追踪器应该返回 false', () => {
|
||||
const component = new PlayerComponent();
|
||||
|
||||
expect(hasChanges(component)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('与实体集成', () => {
|
||||
let scene: Scene;
|
||||
|
||||
beforeEach(() => {
|
||||
scene = new Scene();
|
||||
});
|
||||
|
||||
test('添加到实体的组件应该能正常工作', () => {
|
||||
const entity = scene.createEntity('TestEntity');
|
||||
const component = new PlayerComponent();
|
||||
|
||||
entity.addComponent(component);
|
||||
initChangeTracker(component);
|
||||
|
||||
component.name = "EntityPlayer";
|
||||
component.x = 100;
|
||||
|
||||
const tracker = getChangeTracker(component);
|
||||
expect(tracker!.hasChanges()).toBe(true);
|
||||
});
|
||||
|
||||
test('从实体获取的组件应该保持追踪状态', () => {
|
||||
const entity = scene.createEntity('TestEntity');
|
||||
const component = new PlayerComponent();
|
||||
|
||||
entity.addComponent(component);
|
||||
initChangeTracker(component);
|
||||
clearChanges(component);
|
||||
|
||||
const retrieved = entity.getComponent(PlayerComponent);
|
||||
retrieved!.score = 50;
|
||||
|
||||
expect(hasChanges(component)).toBe(true);
|
||||
expect(hasChanges(retrieved!)).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
530
packages/framework/core/tests/ECS/Sync/encoding.test.ts
Normal file
530
packages/framework/core/tests/ECS/Sync/encoding.test.ts
Normal file
@@ -0,0 +1,530 @@
|
||||
import { BinaryWriter } from '../../../src/ECS/Sync/encoding/BinaryWriter';
|
||||
import { BinaryReader } from '../../../src/ECS/Sync/encoding/BinaryReader';
|
||||
import { Component } from '../../../src/ECS/Component';
|
||||
import { ECSComponent } from '../../../src/ECS/Decorators';
|
||||
import { Scene } from '../../../src/ECS/Scene';
|
||||
import { sync, initChangeTracker, clearChanges } from '../../../src/ECS/Sync/decorators';
|
||||
import {
|
||||
encodeSnapshot,
|
||||
encodeSpawn,
|
||||
encodeDespawn,
|
||||
encodeDespawnBatch
|
||||
} from '../../../src/ECS/Sync/encoding/Encoder';
|
||||
import {
|
||||
decodeSnapshot,
|
||||
decodeSpawn,
|
||||
processDespawn
|
||||
} from '../../../src/ECS/Sync/encoding/Decoder';
|
||||
import { SyncOperation } from '../../../src/ECS/Sync/types';
|
||||
|
||||
@ECSComponent('EncodingTest_PlayerComponent')
|
||||
class PlayerComponent extends Component {
|
||||
@sync("string") name: string = "";
|
||||
@sync("uint16") score: number = 0;
|
||||
@sync("float32") x: number = 0;
|
||||
@sync("float32") y: number = 0;
|
||||
}
|
||||
|
||||
@ECSComponent('EncodingTest_AllTypesComponent')
|
||||
class AllTypesComponent extends Component {
|
||||
@sync("boolean") boolField: boolean = false;
|
||||
@sync("int8") int8Field: number = 0;
|
||||
@sync("uint8") uint8Field: number = 0;
|
||||
@sync("int16") int16Field: number = 0;
|
||||
@sync("uint16") uint16Field: number = 0;
|
||||
@sync("int32") int32Field: number = 0;
|
||||
@sync("uint32") uint32Field: number = 0;
|
||||
@sync("float32") float32Field: number = 0;
|
||||
@sync("float64") float64Field: number = 0;
|
||||
@sync("string") stringField: string = "";
|
||||
}
|
||||
|
||||
describe('BinaryWriter/BinaryReader - 二进制读写器测试', () => {
|
||||
describe('基本数值类型', () => {
|
||||
test('writeUint8/readUint8', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeUint8(0);
|
||||
writer.writeUint8(127);
|
||||
writer.writeUint8(255);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readUint8()).toBe(0);
|
||||
expect(reader.readUint8()).toBe(127);
|
||||
expect(reader.readUint8()).toBe(255);
|
||||
});
|
||||
|
||||
test('writeInt8/readInt8', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeInt8(-128);
|
||||
writer.writeInt8(0);
|
||||
writer.writeInt8(127);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readInt8()).toBe(-128);
|
||||
expect(reader.readInt8()).toBe(0);
|
||||
expect(reader.readInt8()).toBe(127);
|
||||
});
|
||||
|
||||
test('writeBoolean/readBoolean', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeBoolean(true);
|
||||
writer.writeBoolean(false);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readBoolean()).toBe(true);
|
||||
expect(reader.readBoolean()).toBe(false);
|
||||
});
|
||||
|
||||
test('writeUint16/readUint16', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeUint16(0);
|
||||
writer.writeUint16(32767);
|
||||
writer.writeUint16(65535);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readUint16()).toBe(0);
|
||||
expect(reader.readUint16()).toBe(32767);
|
||||
expect(reader.readUint16()).toBe(65535);
|
||||
});
|
||||
|
||||
test('writeInt16/readInt16', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeInt16(-32768);
|
||||
writer.writeInt16(0);
|
||||
writer.writeInt16(32767);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readInt16()).toBe(-32768);
|
||||
expect(reader.readInt16()).toBe(0);
|
||||
expect(reader.readInt16()).toBe(32767);
|
||||
});
|
||||
|
||||
test('writeUint32/readUint32', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeUint32(0);
|
||||
writer.writeUint32(2147483647);
|
||||
writer.writeUint32(4294967295);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readUint32()).toBe(0);
|
||||
expect(reader.readUint32()).toBe(2147483647);
|
||||
expect(reader.readUint32()).toBe(4294967295);
|
||||
});
|
||||
|
||||
test('writeInt32/readInt32', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeInt32(-2147483648);
|
||||
writer.writeInt32(0);
|
||||
writer.writeInt32(2147483647);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readInt32()).toBe(-2147483648);
|
||||
expect(reader.readInt32()).toBe(0);
|
||||
expect(reader.readInt32()).toBe(2147483647);
|
||||
});
|
||||
|
||||
test('writeFloat32/readFloat32', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeFloat32(0);
|
||||
writer.writeFloat32(3.14);
|
||||
writer.writeFloat32(-100.5);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readFloat32()).toBe(0);
|
||||
expect(reader.readFloat32()).toBeCloseTo(3.14, 5);
|
||||
expect(reader.readFloat32()).toBeCloseTo(-100.5, 5);
|
||||
});
|
||||
|
||||
test('writeFloat64/readFloat64', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeFloat64(0);
|
||||
writer.writeFloat64(Math.PI);
|
||||
writer.writeFloat64(-1e100);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readFloat64()).toBe(0);
|
||||
expect(reader.readFloat64()).toBe(Math.PI);
|
||||
expect(reader.readFloat64()).toBe(-1e100);
|
||||
});
|
||||
});
|
||||
|
||||
describe('变长整数 (Varint)', () => {
|
||||
test('小值 (1字节)', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeVarint(0);
|
||||
writer.writeVarint(127);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readVarint()).toBe(0);
|
||||
expect(reader.readVarint()).toBe(127);
|
||||
});
|
||||
|
||||
test('中等值 (2字节)', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeVarint(128);
|
||||
writer.writeVarint(16383);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readVarint()).toBe(128);
|
||||
expect(reader.readVarint()).toBe(16383);
|
||||
});
|
||||
|
||||
test('大值 (多字节)', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeVarint(16384);
|
||||
writer.writeVarint(1000000);
|
||||
writer.writeVarint(2147483647);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readVarint()).toBe(16384);
|
||||
expect(reader.readVarint()).toBe(1000000);
|
||||
expect(reader.readVarint()).toBe(2147483647);
|
||||
});
|
||||
});
|
||||
|
||||
describe('字符串', () => {
|
||||
test('空字符串', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeString("");
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readString()).toBe("");
|
||||
});
|
||||
|
||||
test('ASCII 字符串', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeString("Hello, World!");
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readString()).toBe("Hello, World!");
|
||||
});
|
||||
|
||||
test('Unicode 字符串', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeString("你好世界");
|
||||
writer.writeString("日本語テスト");
|
||||
writer.writeString("emoji: 🎮🎯");
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readString()).toBe("你好世界");
|
||||
expect(reader.readString()).toBe("日本語テスト");
|
||||
expect(reader.readString()).toBe("emoji: 🎮🎯");
|
||||
});
|
||||
|
||||
test('混合字符串', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeString("Player_玩家_プレイヤー");
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.readString()).toBe("Player_玩家_プレイヤー");
|
||||
});
|
||||
});
|
||||
|
||||
describe('字节数组', () => {
|
||||
test('writeBytes/readBytes', () => {
|
||||
const writer = new BinaryWriter();
|
||||
const data = new Uint8Array([1, 2, 3, 4, 5]);
|
||||
writer.writeBytes(data);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
const result = reader.readBytes(5);
|
||||
expect(Array.from(result)).toEqual([1, 2, 3, 4, 5]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('BinaryReader 辅助方法', () => {
|
||||
test('remaining 应该返回剩余字节数', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeUint32(100);
|
||||
writer.writeUint32(200);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.remaining).toBe(8);
|
||||
|
||||
reader.readUint32();
|
||||
expect(reader.remaining).toBe(4);
|
||||
});
|
||||
|
||||
test('hasMore 应该正确判断', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeUint8(1);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.hasMore()).toBe(true);
|
||||
|
||||
reader.readUint8();
|
||||
expect(reader.hasMore()).toBe(false);
|
||||
});
|
||||
|
||||
test('peekUint8 不应该移动读取位置', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeUint8(42);
|
||||
writer.writeUint8(99);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
expect(reader.peekUint8()).toBe(42);
|
||||
expect(reader.peekUint8()).toBe(42);
|
||||
expect(reader.offset).toBe(0);
|
||||
});
|
||||
|
||||
test('skip 应该跳过指定字节', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeUint8(1);
|
||||
writer.writeUint8(2);
|
||||
writer.writeUint8(3);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
reader.skip(2);
|
||||
expect(reader.readUint8()).toBe(3);
|
||||
});
|
||||
|
||||
test('读取超出范围应该抛出错误', () => {
|
||||
const reader = new BinaryReader(new Uint8Array([1, 2]));
|
||||
|
||||
expect(() => reader.readUint32()).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('BinaryWriter 自动扩容', () => {
|
||||
test('应该自动扩容', () => {
|
||||
const writer = new BinaryWriter(4);
|
||||
|
||||
for (let i = 0; i < 100; i++) {
|
||||
writer.writeUint32(i);
|
||||
}
|
||||
|
||||
expect(writer.offset).toBe(400);
|
||||
|
||||
const reader = new BinaryReader(writer.toUint8Array());
|
||||
for (let i = 0; i < 100; i++) {
|
||||
expect(reader.readUint32()).toBe(i);
|
||||
}
|
||||
});
|
||||
|
||||
test('reset 应该清空数据但保留缓冲区', () => {
|
||||
const writer = new BinaryWriter();
|
||||
writer.writeUint32(100);
|
||||
writer.writeUint32(200);
|
||||
|
||||
expect(writer.offset).toBe(8);
|
||||
|
||||
writer.reset();
|
||||
|
||||
expect(writer.offset).toBe(0);
|
||||
expect(writer.toUint8Array().length).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Encoder/Decoder - 实体编解码测试', () => {
|
||||
let scene: Scene;
|
||||
|
||||
// Components are auto-registered via @ECSComponent decorator
|
||||
|
||||
beforeEach(() => {
|
||||
scene = new Scene();
|
||||
});
|
||||
|
||||
describe('encodeSnapshot/decodeSnapshot', () => {
|
||||
test('应该编码和解码单个实体', () => {
|
||||
const entity = scene.createEntity('Player1');
|
||||
const comp = entity.addComponent(new PlayerComponent());
|
||||
comp.name = "TestPlayer";
|
||||
comp.score = 100;
|
||||
comp.x = 10.5;
|
||||
comp.y = 20.5;
|
||||
initChangeTracker(comp);
|
||||
|
||||
const data = encodeSnapshot([entity], SyncOperation.FULL);
|
||||
|
||||
const targetScene = new Scene();
|
||||
const result = decodeSnapshot(targetScene, data);
|
||||
|
||||
expect(result.operation).toBe(SyncOperation.FULL);
|
||||
expect(result.entities.length).toBe(1);
|
||||
expect(result.entities[0].isNew).toBe(true);
|
||||
|
||||
const decodedEntity = targetScene.entities.buffer[0];
|
||||
expect(decodedEntity).toBeDefined();
|
||||
|
||||
const decodedComp = decodedEntity!.getComponent(PlayerComponent);
|
||||
expect(decodedComp).not.toBeNull();
|
||||
expect(decodedComp!.name).toBe("TestPlayer");
|
||||
expect(decodedComp!.score).toBe(100);
|
||||
expect(decodedComp!.x).toBeCloseTo(10.5, 5);
|
||||
expect(decodedComp!.y).toBeCloseTo(20.5, 5);
|
||||
});
|
||||
|
||||
test('应该编码和解码多个实体', () => {
|
||||
const entity1 = scene.createEntity('Player1');
|
||||
const comp1 = entity1.addComponent(new PlayerComponent());
|
||||
comp1.name = "Player1";
|
||||
comp1.score = 50;
|
||||
initChangeTracker(comp1);
|
||||
|
||||
const entity2 = scene.createEntity('Player2');
|
||||
const comp2 = entity2.addComponent(new PlayerComponent());
|
||||
comp2.name = "Player2";
|
||||
comp2.score = 100;
|
||||
initChangeTracker(comp2);
|
||||
|
||||
const data = encodeSnapshot([entity1, entity2], SyncOperation.FULL);
|
||||
|
||||
const targetScene = new Scene();
|
||||
const result = decodeSnapshot(targetScene, data);
|
||||
|
||||
expect(result.entities.length).toBe(2);
|
||||
});
|
||||
|
||||
test('DELTA 操作应该只编码变更的字段', () => {
|
||||
const entity = scene.createEntity('Player1');
|
||||
const comp = entity.addComponent(new PlayerComponent());
|
||||
comp.name = "TestPlayer";
|
||||
comp.score = 0;
|
||||
initChangeTracker(comp);
|
||||
clearChanges(comp);
|
||||
|
||||
comp.score = 200;
|
||||
|
||||
const deltaData = encodeSnapshot([entity], SyncOperation.DELTA);
|
||||
|
||||
expect(deltaData[0]).toBe(SyncOperation.DELTA);
|
||||
expect(deltaData.length).toBeLessThan(50);
|
||||
});
|
||||
});
|
||||
|
||||
describe('encodeSpawn/decodeSpawn', () => {
|
||||
test('应该编码和解码实体生成', () => {
|
||||
const entity = scene.createEntity('SpawnedEntity');
|
||||
const comp = entity.addComponent(new PlayerComponent());
|
||||
comp.name = "SpawnedPlayer";
|
||||
comp.score = 50;
|
||||
comp.x = 100;
|
||||
comp.y = 200;
|
||||
initChangeTracker(comp);
|
||||
|
||||
const data = encodeSpawn(entity, 'Player');
|
||||
|
||||
const targetScene = new Scene();
|
||||
const result = decodeSpawn(targetScene, data);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.prefabType).toBe('Player');
|
||||
expect(result!.componentTypes).toContain('EncodingTest_PlayerComponent');
|
||||
|
||||
const decodedComp = result!.entity.getComponent(PlayerComponent);
|
||||
expect(decodedComp!.name).toBe("SpawnedPlayer");
|
||||
expect(decodedComp!.score).toBe(50);
|
||||
});
|
||||
|
||||
test('没有 prefabType 应该也能工作', () => {
|
||||
const entity = scene.createEntity('Entity');
|
||||
const comp = entity.addComponent(new PlayerComponent());
|
||||
initChangeTracker(comp);
|
||||
|
||||
const data = encodeSpawn(entity);
|
||||
|
||||
const targetScene = new Scene();
|
||||
const result = decodeSpawn(targetScene, data);
|
||||
|
||||
expect(result!.prefabType).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('encodeDespawn/processDespawn', () => {
|
||||
test('应该编码和处理单个实体销毁', () => {
|
||||
const targetScene = new Scene();
|
||||
const entity = targetScene.createEntity('ToBeDestroyed');
|
||||
const entityId = entity.id;
|
||||
|
||||
const data = encodeDespawn(entityId);
|
||||
|
||||
expect(data[0]).toBe(SyncOperation.DESPAWN);
|
||||
|
||||
const removedIds = processDespawn(targetScene, data);
|
||||
|
||||
expect(removedIds).toContain(entityId);
|
||||
});
|
||||
|
||||
test('应该编码和处理批量实体销毁', () => {
|
||||
const targetScene = new Scene();
|
||||
const entity1 = targetScene.createEntity('Entity1');
|
||||
const entity2 = targetScene.createEntity('Entity2');
|
||||
const entity3 = targetScene.createEntity('Entity3');
|
||||
|
||||
const data = encodeDespawnBatch([entity1.id, entity2.id, entity3.id]);
|
||||
|
||||
expect(data[0]).toBe(SyncOperation.DESPAWN);
|
||||
|
||||
const removedIds = processDespawn(targetScene, data);
|
||||
|
||||
expect(removedIds.length).toBe(3);
|
||||
expect(removedIds).toContain(entity1.id);
|
||||
expect(removedIds).toContain(entity2.id);
|
||||
expect(removedIds).toContain(entity3.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe('所有同步类型编解码', () => {
|
||||
test('应该正确编解码所有类型', () => {
|
||||
const entity = scene.createEntity('AllTypes');
|
||||
const comp = entity.addComponent(new AllTypesComponent());
|
||||
comp.boolField = true;
|
||||
comp.int8Field = -100;
|
||||
comp.uint8Field = 200;
|
||||
comp.int16Field = -30000;
|
||||
comp.uint16Field = 60000;
|
||||
comp.int32Field = -2000000000;
|
||||
comp.uint32Field = 4000000000;
|
||||
comp.float32Field = 3.14159;
|
||||
comp.float64Field = Math.PI;
|
||||
comp.stringField = "测试字符串";
|
||||
initChangeTracker(comp);
|
||||
|
||||
const data = encodeSnapshot([entity], SyncOperation.FULL);
|
||||
|
||||
const targetScene = new Scene();
|
||||
decodeSnapshot(targetScene, data);
|
||||
|
||||
const decodedEntity = targetScene.entities.buffer[0];
|
||||
const decodedComp = decodedEntity!.getComponent(AllTypesComponent);
|
||||
|
||||
expect(decodedComp!.boolField).toBe(true);
|
||||
expect(decodedComp!.int8Field).toBe(-100);
|
||||
expect(decodedComp!.uint8Field).toBe(200);
|
||||
expect(decodedComp!.int16Field).toBe(-30000);
|
||||
expect(decodedComp!.uint16Field).toBe(60000);
|
||||
expect(decodedComp!.int32Field).toBe(-2000000000);
|
||||
expect(decodedComp!.uint32Field).toBe(4000000000);
|
||||
expect(decodedComp!.float32Field).toBeCloseTo(3.14159, 4);
|
||||
expect(decodedComp!.float64Field).toBe(Math.PI);
|
||||
expect(decodedComp!.stringField).toBe("测试字符串");
|
||||
});
|
||||
});
|
||||
|
||||
describe('边界情况', () => {
|
||||
test('空实体列表应该能编码', () => {
|
||||
const data = encodeSnapshot([], SyncOperation.FULL);
|
||||
|
||||
const targetScene = new Scene();
|
||||
const result = decodeSnapshot(targetScene, data);
|
||||
|
||||
expect(result.entities.length).toBe(0);
|
||||
});
|
||||
|
||||
test('entityMap 应该正确跟踪实体', () => {
|
||||
const entity = scene.createEntity('Tracked');
|
||||
const comp = entity.addComponent(new PlayerComponent());
|
||||
comp.name = "TrackedPlayer";
|
||||
initChangeTracker(comp);
|
||||
|
||||
const data = encodeSnapshot([entity], SyncOperation.FULL);
|
||||
|
||||
const targetScene = new Scene();
|
||||
const entityMap = new Map();
|
||||
decodeSnapshot(targetScene, data, entityMap);
|
||||
|
||||
expect(entityMap.size).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
49
packages/framework/database-drivers/CHANGELOG.md
Normal file
49
packages/framework/database-drivers/CHANGELOG.md
Normal file
@@ -0,0 +1,49 @@
|
||||
# @esengine/database-drivers
|
||||
|
||||
## 1.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#410](https://github.com/esengine/esengine/pull/410) [`71022ab`](https://github.com/esengine/esengine/commit/71022abc99ad4a1b349f19f4ccf1e0a2a0923dfa) Thanks [@esengine](https://github.com/esengine)! - feat: add database layer architecture
|
||||
|
||||
Added new database packages with layered architecture:
|
||||
|
||||
**@esengine/database-drivers (Layer 1)**
|
||||
- MongoDB connection with pool management, auto-reconnect, events
|
||||
- Redis connection with auto-reconnect, key prefix
|
||||
- Type-safe `IMongoCollection<T>` interface decoupled from mongodb types
|
||||
- Service tokens for dependency injection (`MongoConnectionToken`, `RedisConnectionToken`)
|
||||
|
||||
**@esengine/database (Layer 2)**
|
||||
- Generic `Repository<T>` with CRUD, pagination, soft delete
|
||||
- `UserRepository` with registration, authentication, role management
|
||||
- Password hashing utilities using scrypt
|
||||
- Query operators: `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`, `$in`, `$nin`, `$like`, `$regex`
|
||||
|
||||
**@esengine/transaction**
|
||||
- Refactored `MongoStorage` to use shared connection from `@esengine/database-drivers`
|
||||
- Removed factory pattern in favor of shared connection (breaking change)
|
||||
- Simplified API: `createMongoStorage(connection, options?)`
|
||||
|
||||
Example usage:
|
||||
|
||||
```typescript
|
||||
import { createMongoConnection } from '@esengine/database-drivers';
|
||||
import { UserRepository } from '@esengine/database';
|
||||
import { createMongoStorage, TransactionManager } from '@esengine/transaction';
|
||||
|
||||
// Create shared connection
|
||||
const mongo = createMongoConnection({
|
||||
uri: 'mongodb://localhost:27017',
|
||||
database: 'game'
|
||||
});
|
||||
await mongo.connect();
|
||||
|
||||
// Use for database operations
|
||||
const userRepo = new UserRepository(mongo);
|
||||
await userRepo.register({ username: 'john', password: '123456' });
|
||||
|
||||
// Use for transactions (same connection)
|
||||
const storage = createMongoStorage(mongo);
|
||||
const txManager = new TransactionManager({ storage });
|
||||
```
|
||||
23
packages/framework/database-drivers/module.json
Normal file
23
packages/framework/database-drivers/module.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"id": "database-drivers",
|
||||
"name": "@esengine/database-drivers",
|
||||
"globalKey": "database-drivers",
|
||||
"displayName": "Database Drivers",
|
||||
"description": "数据库连接驱动,提供 MongoDB、Redis 等数据库的连接管理 | Database connection drivers with connection pooling for MongoDB, Redis, etc.",
|
||||
"version": "1.0.0",
|
||||
"category": "Infrastructure",
|
||||
"icon": "Database",
|
||||
"tags": ["database", "mongodb", "redis", "connection"],
|
||||
"isCore": false,
|
||||
"defaultEnabled": true,
|
||||
"isEngineModule": false,
|
||||
"canContainContent": false,
|
||||
"platforms": ["server"],
|
||||
"dependencies": [],
|
||||
"exports": {
|
||||
"components": [],
|
||||
"systems": []
|
||||
},
|
||||
"requiresWasm": false,
|
||||
"outputPath": "dist/index.js"
|
||||
}
|
||||
48
packages/framework/database-drivers/package.json
Normal file
48
packages/framework/database-drivers/package.json
Normal file
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "@esengine/database-drivers",
|
||||
"version": "1.1.0",
|
||||
"description": "Database connection drivers for ESEngine | ESEngine 数据库连接驱动",
|
||||
"type": "module",
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"module.json"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "tsup",
|
||||
"build:watch": "tsup --watch",
|
||||
"type-check": "tsc --noEmit",
|
||||
"clean": "rimraf dist"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"mongodb": ">=6.0.0",
|
||||
"ioredis": ">=5.0.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"mongodb": {
|
||||
"optional": true
|
||||
},
|
||||
"ioredis": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.0.0",
|
||||
"mongodb": "^6.12.0",
|
||||
"ioredis": "^5.3.0",
|
||||
"tsup": "^8.0.0",
|
||||
"typescript": "^5.8.0",
|
||||
"rimraf": "^5.0.0"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
/**
|
||||
* @zh MongoDB 集合适配器
|
||||
* @en MongoDB collection adapter
|
||||
*
|
||||
* @zh 将 MongoDB 原生 Collection 适配为简化接口
|
||||
* @en Adapts native MongoDB Collection to simplified interface
|
||||
*/
|
||||
|
||||
import type { Collection, Db } from 'mongodb'
|
||||
import type {
|
||||
DeleteResult,
|
||||
FindOneAndUpdateOptions,
|
||||
FindOptions,
|
||||
IMongoCollection,
|
||||
IMongoDatabase,
|
||||
IndexOptions,
|
||||
InsertManyResult,
|
||||
InsertOneResult,
|
||||
UpdateResult
|
||||
} from '../interfaces/IMongoCollection.js'
|
||||
|
||||
/**
|
||||
* @zh MongoDB 集合适配器
|
||||
* @en MongoDB collection adapter
|
||||
*/
|
||||
export class MongoCollectionAdapter<T extends object> implements IMongoCollection<T> {
|
||||
readonly name: string
|
||||
|
||||
constructor(private readonly _collection: Collection<T>) {
|
||||
this.name = _collection.collectionName
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 查询 | Query
|
||||
// =========================================================================
|
||||
|
||||
async findOne(filter: object, options?: FindOptions): Promise<T | null> {
|
||||
const doc = await this._collection.findOne(
|
||||
filter as Parameters<typeof this._collection.findOne>[0],
|
||||
{
|
||||
sort: options?.sort as Parameters<typeof this._collection.findOne>[1] extends { sort?: infer S } ? S : never,
|
||||
projection: options?.projection
|
||||
}
|
||||
)
|
||||
return doc ? this._stripId(doc) : null
|
||||
}
|
||||
|
||||
async find(filter: object, options?: FindOptions): Promise<T[]> {
|
||||
let cursor = this._collection.find(
|
||||
filter as Parameters<typeof this._collection.find>[0]
|
||||
)
|
||||
|
||||
if (options?.sort) {
|
||||
cursor = cursor.sort(options.sort as Parameters<typeof cursor.sort>[0])
|
||||
}
|
||||
|
||||
if (options?.skip) {
|
||||
cursor = cursor.skip(options.skip)
|
||||
}
|
||||
|
||||
if (options?.limit) {
|
||||
cursor = cursor.limit(options.limit)
|
||||
}
|
||||
|
||||
if (options?.projection) {
|
||||
cursor = cursor.project(options.projection)
|
||||
}
|
||||
|
||||
const docs = await cursor.toArray()
|
||||
return docs.map(doc => this._stripId(doc))
|
||||
}
|
||||
|
||||
async countDocuments(filter?: object): Promise<number> {
|
||||
return this._collection.countDocuments(
|
||||
(filter ?? {}) as Parameters<typeof this._collection.countDocuments>[0]
|
||||
)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 创建 | Create
|
||||
// =========================================================================
|
||||
|
||||
async insertOne(doc: T): Promise<InsertOneResult> {
|
||||
const result = await this._collection.insertOne(
|
||||
doc as Parameters<typeof this._collection.insertOne>[0]
|
||||
)
|
||||
return {
|
||||
insertedId: result.insertedId,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
async insertMany(docs: T[]): Promise<InsertManyResult> {
|
||||
const result = await this._collection.insertMany(
|
||||
docs as Parameters<typeof this._collection.insertMany>[0]
|
||||
)
|
||||
return {
|
||||
insertedCount: result.insertedCount,
|
||||
insertedIds: result.insertedIds as Record<number, unknown>,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 更新 | Update
|
||||
// =========================================================================
|
||||
|
||||
async updateOne(filter: object, update: object): Promise<UpdateResult> {
|
||||
const result = await this._collection.updateOne(
|
||||
filter as Parameters<typeof this._collection.updateOne>[0],
|
||||
update as Parameters<typeof this._collection.updateOne>[1]
|
||||
)
|
||||
return {
|
||||
matchedCount: result.matchedCount,
|
||||
modifiedCount: result.modifiedCount,
|
||||
upsertedCount: result.upsertedCount,
|
||||
upsertedId: result.upsertedId,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
async updateMany(filter: object, update: object): Promise<UpdateResult> {
|
||||
const result = await this._collection.updateMany(
|
||||
filter as Parameters<typeof this._collection.updateMany>[0],
|
||||
update as Parameters<typeof this._collection.updateMany>[1]
|
||||
)
|
||||
return {
|
||||
matchedCount: result.matchedCount,
|
||||
modifiedCount: result.modifiedCount,
|
||||
upsertedCount: result.upsertedCount,
|
||||
upsertedId: result.upsertedId,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
async findOneAndUpdate(
|
||||
filter: object,
|
||||
update: object,
|
||||
options?: FindOneAndUpdateOptions
|
||||
): Promise<T | null> {
|
||||
const result = await this._collection.findOneAndUpdate(
|
||||
filter as Parameters<typeof this._collection.findOneAndUpdate>[0],
|
||||
update as Parameters<typeof this._collection.findOneAndUpdate>[1],
|
||||
{
|
||||
returnDocument: options?.returnDocument ?? 'after',
|
||||
upsert: options?.upsert
|
||||
}
|
||||
)
|
||||
return result ? this._stripId(result) : null
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 删除 | Delete
|
||||
// =========================================================================
|
||||
|
||||
async deleteOne(filter: object): Promise<DeleteResult> {
|
||||
const result = await this._collection.deleteOne(
|
||||
filter as Parameters<typeof this._collection.deleteOne>[0]
|
||||
)
|
||||
return {
|
||||
deletedCount: result.deletedCount,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
async deleteMany(filter: object): Promise<DeleteResult> {
|
||||
const result = await this._collection.deleteMany(
|
||||
filter as Parameters<typeof this._collection.deleteMany>[0]
|
||||
)
|
||||
return {
|
||||
deletedCount: result.deletedCount,
|
||||
acknowledged: result.acknowledged
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 索引 | Index
|
||||
// =========================================================================
|
||||
|
||||
async createIndex(
|
||||
spec: Record<string, 1 | -1>,
|
||||
options?: IndexOptions
|
||||
): Promise<string> {
|
||||
return this._collection.createIndex(spec, options)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 内部方法 | Internal Methods
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* @zh 移除 MongoDB 的 _id 字段
|
||||
* @en Remove MongoDB's _id field
|
||||
*/
|
||||
private _stripId<D extends object>(doc: D): D {
|
||||
const { _id, ...rest } = doc as { _id?: unknown } & Record<string, unknown>
|
||||
return rest as D
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh MongoDB 数据库适配器
|
||||
* @en MongoDB database adapter
|
||||
*/
|
||||
export class MongoDatabaseAdapter implements IMongoDatabase {
|
||||
readonly name: string
|
||||
private _collections = new Map<string, MongoCollectionAdapter<object>>()
|
||||
|
||||
constructor(private readonly _db: Db) {
|
||||
this.name = _db.databaseName
|
||||
}
|
||||
|
||||
collection<T extends object = object>(name: string): IMongoCollection<T> {
|
||||
if (!this._collections.has(name)) {
|
||||
const nativeCollection = this._db.collection<T>(name)
|
||||
this._collections.set(
|
||||
name,
|
||||
new MongoCollectionAdapter(nativeCollection) as MongoCollectionAdapter<object>
|
||||
)
|
||||
}
|
||||
return this._collections.get(name) as IMongoCollection<T>
|
||||
}
|
||||
|
||||
async listCollections(): Promise<string[]> {
|
||||
const collections = await this._db.listCollections().toArray()
|
||||
return collections.map(c => c.name)
|
||||
}
|
||||
|
||||
async dropCollection(name: string): Promise<boolean> {
|
||||
try {
|
||||
await this._db.dropCollection(name)
|
||||
this._collections.delete(name)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
/**
|
||||
* @zh MongoDB 连接驱动
|
||||
* @en MongoDB connection driver
|
||||
*
|
||||
* @zh 提供 MongoDB 数据库的连接管理、自动重连和事件通知
|
||||
* @en Provides MongoDB connection management, auto-reconnect, and event notification
|
||||
*/
|
||||
|
||||
import type { Db, MongoClient as MongoClientType, MongoClientOptions } from 'mongodb'
|
||||
import { randomUUID } from 'crypto'
|
||||
import {
|
||||
ConnectionError,
|
||||
type ConnectionEvent,
|
||||
type ConnectionEventListener,
|
||||
type ConnectionEventType,
|
||||
type ConnectionState,
|
||||
type IEventableConnection,
|
||||
type MongoConnectionConfig
|
||||
} from '../types.js'
|
||||
import type { IMongoCollection, IMongoDatabase } from '../interfaces/IMongoCollection.js'
|
||||
import { MongoDatabaseAdapter } from '../adapters/MongoCollectionAdapter.js'
|
||||
|
||||
/**
|
||||
* @zh MongoDB 连接接口
|
||||
* @en MongoDB connection interface
|
||||
*/
|
||||
export interface IMongoConnection extends IEventableConnection {
|
||||
/**
|
||||
* @zh 获取数据库接口
|
||||
* @en Get database interface
|
||||
*/
|
||||
getDatabase(): IMongoDatabase
|
||||
|
||||
/**
|
||||
* @zh 获取原生客户端(高级用法)
|
||||
* @en Get native client (advanced usage)
|
||||
*/
|
||||
getNativeClient(): MongoClientType
|
||||
|
||||
/**
|
||||
* @zh 获取原生数据库(高级用法)
|
||||
* @en Get native database (advanced usage)
|
||||
*/
|
||||
getNativeDatabase(): Db
|
||||
|
||||
/**
|
||||
* @zh 获取集合
|
||||
* @en Get collection
|
||||
*/
|
||||
collection<T extends object = object>(name: string): IMongoCollection<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh MongoDB 连接实现
|
||||
* @en MongoDB connection implementation
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const mongo = new MongoConnection({
|
||||
* uri: 'mongodb://localhost:27017',
|
||||
* database: 'game',
|
||||
* autoReconnect: true,
|
||||
* })
|
||||
*
|
||||
* mongo.on('connected', () => console.log('Connected!'))
|
||||
* mongo.on('error', (e) => console.error('Error:', e.error))
|
||||
*
|
||||
* await mongo.connect()
|
||||
*
|
||||
* const users = mongo.collection('users')
|
||||
* await users.insertOne({ name: 'test' })
|
||||
*
|
||||
* await mongo.disconnect()
|
||||
* ```
|
||||
*/
|
||||
export class MongoConnection implements IMongoConnection {
|
||||
readonly id: string
|
||||
private _state: ConnectionState = 'disconnected'
|
||||
private _client: MongoClientType | null = null
|
||||
private _db: Db | null = null
|
||||
private _config: MongoConnectionConfig
|
||||
private _listeners = new Map<ConnectionEventType, Set<ConnectionEventListener>>()
|
||||
private _reconnectAttempts = 0
|
||||
private _reconnectTimer: ReturnType<typeof setTimeout> | null = null
|
||||
|
||||
constructor(config: MongoConnectionConfig) {
|
||||
this.id = randomUUID()
|
||||
this._config = {
|
||||
autoReconnect: true,
|
||||
reconnectInterval: 5000,
|
||||
maxReconnectAttempts: 10,
|
||||
...config
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 状态 | State
|
||||
// =========================================================================
|
||||
|
||||
get state(): ConnectionState {
|
||||
return this._state
|
||||
}
|
||||
|
||||
isConnected(): boolean {
|
||||
return this._state === 'connected' && this._client !== null
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 连接管理 | Connection Management
|
||||
// =========================================================================
|
||||
|
||||
async connect(): Promise<void> {
|
||||
if (this._state === 'connected') {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._state === 'connecting') {
|
||||
throw new ConnectionError('Connection already in progress')
|
||||
}
|
||||
|
||||
this._state = 'connecting'
|
||||
|
||||
try {
|
||||
const { MongoClient } = await import('mongodb')
|
||||
|
||||
const options: MongoClientOptions = {}
|
||||
if (this._config.pool) {
|
||||
if (this._config.pool.minSize) {
|
||||
options.minPoolSize = this._config.pool.minSize
|
||||
}
|
||||
if (this._config.pool.maxSize) {
|
||||
options.maxPoolSize = this._config.pool.maxSize
|
||||
}
|
||||
if (this._config.pool.acquireTimeout) {
|
||||
options.waitQueueTimeoutMS = this._config.pool.acquireTimeout
|
||||
}
|
||||
if (this._config.pool.maxLifetime) {
|
||||
options.maxIdleTimeMS = this._config.pool.maxLifetime
|
||||
}
|
||||
}
|
||||
|
||||
this._client = new MongoClient(this._config.uri, options)
|
||||
await this._client.connect()
|
||||
this._db = this._client.db(this._config.database)
|
||||
|
||||
this._state = 'connected'
|
||||
this._reconnectAttempts = 0
|
||||
this._emit('connected')
|
||||
|
||||
this._setupClientEvents()
|
||||
} catch (error) {
|
||||
this._state = 'error'
|
||||
const connError = new ConnectionError(
|
||||
`Failed to connect to MongoDB: ${(error as Error).message}`,
|
||||
'CONNECTION_FAILED',
|
||||
error as Error
|
||||
)
|
||||
this._emit('error', connError)
|
||||
throw connError
|
||||
}
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
if (this._state === 'disconnected') {
|
||||
return
|
||||
}
|
||||
|
||||
this._clearReconnectTimer()
|
||||
this._state = 'disconnecting'
|
||||
|
||||
try {
|
||||
if (this._client) {
|
||||
await this._client.close()
|
||||
this._client = null
|
||||
this._db = null
|
||||
}
|
||||
|
||||
this._state = 'disconnected'
|
||||
this._emit('disconnected')
|
||||
} catch (error) {
|
||||
this._state = 'error'
|
||||
throw new ConnectionError(
|
||||
`Failed to disconnect: ${(error as Error).message}`,
|
||||
'CONNECTION_FAILED',
|
||||
error as Error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
async ping(): Promise<boolean> {
|
||||
if (!this._db) {
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
await this._db.command({ ping: 1 })
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 数据库访问 | Database Access
|
||||
// =========================================================================
|
||||
|
||||
private _dbAdapter: MongoDatabaseAdapter | null = null
|
||||
|
||||
getDatabase(): IMongoDatabase {
|
||||
if (!this._db) {
|
||||
throw new ConnectionError('Not connected to database', 'CONNECTION_CLOSED')
|
||||
}
|
||||
if (!this._dbAdapter) {
|
||||
this._dbAdapter = new MongoDatabaseAdapter(this._db)
|
||||
}
|
||||
return this._dbAdapter
|
||||
}
|
||||
|
||||
getNativeDatabase(): Db {
|
||||
if (!this._db) {
|
||||
throw new ConnectionError('Not connected to database', 'CONNECTION_CLOSED')
|
||||
}
|
||||
return this._db
|
||||
}
|
||||
|
||||
getNativeClient(): MongoClientType {
|
||||
if (!this._client) {
|
||||
throw new ConnectionError('Not connected to database', 'CONNECTION_CLOSED')
|
||||
}
|
||||
return this._client
|
||||
}
|
||||
|
||||
collection<T extends object = object>(name: string): IMongoCollection<T> {
|
||||
return this.getDatabase().collection<T>(name)
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 事件 | Events
|
||||
// =========================================================================
|
||||
|
||||
on(event: ConnectionEventType, listener: ConnectionEventListener): void {
|
||||
if (!this._listeners.has(event)) {
|
||||
this._listeners.set(event, new Set())
|
||||
}
|
||||
this._listeners.get(event)!.add(listener)
|
||||
}
|
||||
|
||||
off(event: ConnectionEventType, listener: ConnectionEventListener): void {
|
||||
this._listeners.get(event)?.delete(listener)
|
||||
}
|
||||
|
||||
once(event: ConnectionEventType, listener: ConnectionEventListener): void {
|
||||
const wrapper: ConnectionEventListener = (e) => {
|
||||
this.off(event, wrapper)
|
||||
listener(e)
|
||||
}
|
||||
this.on(event, wrapper)
|
||||
}
|
||||
|
||||
private _emit(type: ConnectionEventType, error?: Error): void {
|
||||
const event: ConnectionEvent = {
|
||||
type,
|
||||
connectionId: this.id,
|
||||
timestamp: Date.now(),
|
||||
error
|
||||
}
|
||||
|
||||
const listeners = this._listeners.get(type)
|
||||
if (listeners) {
|
||||
for (const listener of listeners) {
|
||||
try {
|
||||
listener(event)
|
||||
} catch {
|
||||
// Ignore listener errors
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 内部方法 | Internal Methods
|
||||
// =========================================================================
|
||||
|
||||
private _setupClientEvents(): void {
|
||||
if (!this._client) return
|
||||
|
||||
this._client.on('close', () => {
|
||||
if (this._state === 'connected') {
|
||||
this._state = 'disconnected'
|
||||
this._emit('disconnected')
|
||||
this._scheduleReconnect()
|
||||
}
|
||||
})
|
||||
|
||||
this._client.on('error', (error) => {
|
||||
this._emit('error', error)
|
||||
})
|
||||
}
|
||||
|
||||
private _scheduleReconnect(): void {
|
||||
if (!this._config.autoReconnect) return
|
||||
if (this._reconnectAttempts >= (this._config.maxReconnectAttempts ?? 10)) {
|
||||
return
|
||||
}
|
||||
|
||||
this._clearReconnectTimer()
|
||||
this._emit('reconnecting')
|
||||
|
||||
this._reconnectTimer = setTimeout(async () => {
|
||||
this._reconnectAttempts++
|
||||
try {
|
||||
await this.connect()
|
||||
this._emit('reconnected')
|
||||
} catch {
|
||||
this._scheduleReconnect()
|
||||
}
|
||||
}, this._config.reconnectInterval ?? 5000)
|
||||
}
|
||||
|
||||
private _clearReconnectTimer(): void {
|
||||
if (this._reconnectTimer) {
|
||||
clearTimeout(this._reconnectTimer)
|
||||
this._reconnectTimer = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 创建 MongoDB 连接
|
||||
* @en Create MongoDB connection
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const mongo = createMongoConnection({
|
||||
* uri: process.env.MONGODB_URI!,
|
||||
* database: 'game',
|
||||
* })
|
||||
* await mongo.connect()
|
||||
* ```
|
||||
*/
|
||||
export function createMongoConnection(config: MongoConnectionConfig): MongoConnection {
|
||||
return new MongoConnection(config)
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user