@lzpenguin/server 1.0.8 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +79 -42
  2. package/index.d.ts +67 -6
  3. package/index.js +107 -31
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -50,6 +50,82 @@ new RiffleServer({
50
50
  })
51
51
  ```
52
52
 
53
+ ## API 方法
54
+
55
+ ### 细粒度操作 API(推荐)
56
+
57
+ 从 v1.1.0 起,提供了更细粒度的操作 API,使代码更加语义化和易读。
58
+
59
+ #### World 数据操作
60
+
61
+ ```javascript
62
+ // 添加/设置单个键值对
63
+ server.world.add('score', 100);
64
+ server.world.update('level', 2);
65
+
66
+ // 删除键(设置为 null)
67
+ server.world.delete('oldKey');
68
+
69
+ // 批量设置
70
+ server.world.set({
71
+ score: 100,
72
+ level: 2,
73
+ players: 10
74
+ });
75
+ ```
76
+
77
+ #### Self 数据操作
78
+
79
+ ```javascript
80
+ // 添加/设置单个键值对
81
+ server.self.add('x', 150);
82
+ server.self.update('y', 250);
83
+
84
+ // 删除键(设置为 null)
85
+ server.self.delete('oldProp');
86
+
87
+ // 批量设置
88
+ server.self.set({
89
+ x: 150,
90
+ y: 250,
91
+ rotation: 90
92
+ });
93
+ ```
94
+
95
+ #### 完整示例
96
+
97
+ ```javascript
98
+ import { RiffleServer } from '@lzpenguin/server';
99
+
100
+ const server = new RiffleServer({
101
+ url: 'wss://api.riffle.app',
102
+ postId: 123456,
103
+ token: 'your-token-here'
104
+ });
105
+
106
+ // 监听数据更新
107
+ server.onData((data) => {
108
+ console.log('World:', data.world);
109
+ console.log('Self:', data.self);
110
+ console.log('Players:', data.players);
111
+ });
112
+
113
+ // 初始化
114
+ server.init({
115
+ hash: 'v1.0.0',
116
+ world: { score: 0, level: 1 },
117
+ self: { public: { name: 'Player1', x: 0, y: 0 } }
118
+ });
119
+
120
+ // 使用细粒度 API 更新数据
121
+ server.world.update('score', 100); // 更新分数
122
+ server.self.update('x', 150); // 更新 X 坐标
123
+ server.self.update('y', 250); // 更新 Y 坐标
124
+
125
+ // 批量更新
126
+ server.self.set({ x: 200, y: 300, rotation: 45 });
127
+ ```
128
+
53
129
  ## 三种消息类型
54
130
 
55
131
  ### 1. init() - 初始化服务器
@@ -86,40 +162,7 @@ server.init({
86
162
 
87
163
  **注意:** init 成功后服务器每 0.2 秒自动推送最新数据
88
164
 
89
- ### 2. update() - 更新数据
90
-
91
- 部分更新数据(合并到现有数据,不替换)。
92
-
93
- ```javascript
94
- // 更新世界数据
95
- server.update({ world: { score: 200 } });
96
-
97
- // 更新玩家数据
98
- server.update({
99
- self: {
100
- public: { x: 15, y: 25, rotation: 90 }
101
- }
102
- });
103
-
104
- // 同时更新世界和玩家数据
105
- server.update({
106
- world: { score: 200 },
107
- self: { public: { x: 15, y: 25 } }
108
- });
109
- ```
110
-
111
- **响应数据(通过 onData 接收):**
112
- ```json
113
- {
114
- "world": { "score": 200, "level": 1 },
115
- "self": { "name": "Player1", "x": 15, "y": 25, "rotation": 90 },
116
- "players": [{ "name": "Player2", "x": 10, "y": 20 }]
117
- }
118
- ```
119
-
120
- **注意:** 只更新提供的字段,未提供的字段保持不变,更新后立即推送最新数据
121
-
122
- ### 3. onData() - 监听数据推送
165
+ ### 2. onData() - 监听数据推送
123
166
 
124
167
  监听服务器推送的最新数据。init 成功后每 0.2 秒自动推送,调用 init/update 后立即推送。
125
168
 
@@ -191,18 +234,12 @@ server.init({
191
234
 
192
235
  // 更新玩家位置
193
236
  function movePlayer(x, y) {
194
- server.update({
195
- self: {
196
- public: { x, y }
197
- }
198
- });
237
+ server.self.set({ x, y });
199
238
  }
200
239
 
201
240
  // 更新分数
202
241
  function updateScore(score) {
203
- server.update({
204
- world: { score }
205
- });
242
+ server.world.update('score', score);
206
243
  }
207
244
  ```
208
245
 
package/index.d.ts CHANGED
@@ -63,6 +63,68 @@ export interface ServerData {
63
63
  players: Array<Record<string, any>>;
64
64
  }
65
65
 
66
+ /**
67
+ * WorldManager - 管理 world 数据的细粒度操作
68
+ */
69
+ export class WorldManager {
70
+ /**
71
+ * 添加或设置 world 中的键值对
72
+ * @param key 键名
73
+ * @param value 值
74
+ */
75
+ add(key: string, value: any): void;
76
+
77
+ /**
78
+ * 更新 world 中的键值对(与 add 相同,提供语义化接口)
79
+ * @param key 键名
80
+ * @param value 值
81
+ */
82
+ update(key: string, value: any): void;
83
+
84
+ /**
85
+ * 删除 world 中的键(通过设置为 null)
86
+ * @param key 键名
87
+ */
88
+ delete(key: string): void;
89
+
90
+ /**
91
+ * 批量设置多个键值对
92
+ * @param data 键值对对象
93
+ */
94
+ set(data: Record<string, any>): void;
95
+ }
96
+
97
+ /**
98
+ * SelfManager - 管理 self 数据的细粒度操作
99
+ */
100
+ export class SelfManager {
101
+ /**
102
+ * 添加或设置 self.public 中的键值对
103
+ * @param key 键名
104
+ * @param value 值
105
+ */
106
+ add(key: string, value: any): void;
107
+
108
+ /**
109
+ * 更新 self.public 中的键值对(与 add 相同,提供语义化接口)
110
+ * @param key 键名
111
+ * @param value 值
112
+ */
113
+ update(key: string, value: any): void;
114
+
115
+ /**
116
+ * 删除 self.public 中的键(通过设置为 null)
117
+ * @param key 键名
118
+ */
119
+ delete(key: string): void;
120
+
121
+ /**
122
+ * 批量设置多个键值对
123
+ * @param data 键值对对象
124
+ */
125
+ set(data: Record<string, any>): void;
126
+ }
127
+
66
128
  /**
67
129
  * RiffleServer - 游戏服务器 WebSocket 客户端
68
130
  */
@@ -80,18 +142,17 @@ export class RiffleServer {
80
142
  /** 是否已初始化 */
81
143
  readonly isInitialized: boolean;
82
144
 
145
+ /** World 数据管理器 */
146
+ readonly world: WorldManager;
147
+ /** Self 数据管理器 */
148
+ readonly self: SelfManager;
149
+
83
150
  /**
84
151
  * 初始化服务器(必需,连接后必须先调用)
85
152
  * @param initData 初始化数据
86
153
  */
87
154
  init(initData: InitData): void;
88
155
 
89
- /**
90
- * 更新数据(部分更新,合并到现有数据)
91
- * @param updateData 更新数据
92
- */
93
- update(updateData: UpdateData): void;
94
-
95
156
  /**
96
157
  * 监听服务器推送的最新数据
97
158
  * @param callback 回调函数,接收服务器推送的数据
package/index.js CHANGED
@@ -28,12 +28,98 @@ if (typeof window !== 'undefined' && window.WebSocket) {
28
28
  }
29
29
  }
30
30
 
31
+ /**
32
+ * WorldManager - 管理 world 数据的细粒度操作
33
+ */
34
+ class WorldManager {
35
+ constructor(server) {
36
+ this._server = server;
37
+ }
38
+
39
+ /**
40
+ * 添加或设置 world 中的键值对
41
+ * @param {string} key - 键名
42
+ * @param {any} value - 值
43
+ */
44
+ add(key, value) {
45
+ this._server.sendUpdate({ world: { [key]: value } });
46
+ }
47
+
48
+ /**
49
+ * 更新 world 中的键值对(与 add 相同,提供语义化接口)
50
+ * @param {string} key - 键名
51
+ * @param {any} value - 值
52
+ */
53
+ update(key, value) {
54
+ this._server.sendUpdate({ world: { [key]: value } });
55
+ }
56
+
57
+ /**
58
+ * 删除 world 中的键(通过设置为 null)
59
+ * @param {string} key - 键名
60
+ */
61
+ delete(key) {
62
+ this._server.sendUpdate({ world: { [key]: null } });
63
+ }
64
+
65
+ /**
66
+ * 批量设置多个键值对
67
+ * @param {Object} data - 键值对对象
68
+ */
69
+ set(data) {
70
+ this._server.sendUpdate({ world: data });
71
+ }
72
+ }
73
+
74
+ /**
75
+ * SelfManager - 管理 self 数据的细粒度操作
76
+ */
77
+ class SelfManager {
78
+ constructor(server) {
79
+ this._server = server;
80
+ }
81
+
82
+ /**
83
+ * 添加或设置 self.public 中的键值对
84
+ * @param {string} key - 键名
85
+ * @param {any} value - 值
86
+ */
87
+ add(key, value) {
88
+ this._server.sendUpdate({ self: { public: { [key]: value } } });
89
+ }
90
+
91
+ /**
92
+ * 更新 self.public 中的键值对(与 add 相同,提供语义化接口)
93
+ * @param {string} key - 键名
94
+ * @param {any} value - 值
95
+ */
96
+ update(key, value) {
97
+ this._server.sendUpdate({ self: { public: { [key]: value } } });
98
+ }
99
+
100
+ /**
101
+ * 删除 self.public 中的键(通过设置为 null)
102
+ * @param {string} key - 键名
103
+ */
104
+ delete(key) {
105
+ this._server.sendUpdate({ self: { public: { [key]: null } } });
106
+ }
107
+
108
+ /**
109
+ * 批量设置多个键值对
110
+ * @param {Object} data - 键值对对象
111
+ */
112
+ set(data) {
113
+ this._server.sendUpdate({ self: { public: data } });
114
+ }
115
+ }
116
+
31
117
  /**
32
118
  * RiffleServer - 游戏服务器 WebSocket 客户端
33
119
  *
34
120
  * @example
35
121
  * ```js
36
- * import { RiffleServer } from '@riffle/server';
122
+ * import { RiffleServer } from '@lzpenguin/server';
37
123
  *
38
124
  * const server = new RiffleServer({
39
125
  * url: 'wss://api.riffle.app',
@@ -44,26 +130,31 @@ if (typeof window !== 'undefined' && window.WebSocket) {
44
130
  * // 监听服务器推送的最新数据
45
131
  * server.onData((data) => {
46
132
  * console.log('World:', data.world);
47
- * console.log('Self:', data.self); // self 现在直接是公开数据,与 players[i] 结构一致
133
+ * console.log('Self:', data.self);
48
134
  * console.log('Players:', data.players);
49
135
  * });
50
136
  *
51
137
  * // 初始化服务器(必需,连接后必须先调用)
52
138
  * server.init({
53
- * hash: 'game-version-hash-123', // 游戏版本哈希值
54
- * world: { score: 0, level: 1 }, // 世界初始数据(可选)
139
+ * hash: 'game-version-hash-123',
140
+ * world: { score: 0, level: 1 },
55
141
  * self: {
56
- * public: { name: 'Player1', x: 100, y: 100 } // 公开数据(可选)
142
+ * public: { name: 'Player1', x: 100, y: 100 }
57
143
  * }
58
144
  * });
59
145
  *
60
- * // 更新数据
61
- * server.update({
62
- * world: { score: 200 },
63
- * self: {
64
- * public: { x: 15, y: 25, rotation: 90 }
65
- * }
66
- * });
146
+ * // 新的细粒度操作 API
147
+ * server.world.add('score', 200); // 添加/更新 world.score
148
+ * server.world.update('level', 2); // 更新 world.level
149
+ * server.world.delete('oldKey'); // 删除 world.oldKey
150
+ *
151
+ * server.self.add('x', 150); // 添加/更新 self.public.x
152
+ * server.self.update('y', 250); // 更新 self.public.y
153
+ * server.self.delete('oldProp'); // 删除 self.public.oldProp
154
+ *
155
+ * // 批量操作
156
+ * server.world.set({ score: 200, level: 2 });
157
+ * server.self.set({ x: 150, y: 250, rotation: 90 });
67
158
  * ```
68
159
  */
69
160
  export class RiffleServer {
@@ -105,6 +196,10 @@ export class RiffleServer {
105
196
  // 当前服务器数据缓存
106
197
  this.currentData = null;
107
198
 
199
+ // 创建细粒度操作管理器
200
+ this.world = new WorldManager(this);
201
+ this.self = new SelfManager(this);
202
+
108
203
  // 连接状态(异步调用,不阻塞构造函数)
109
204
  this.connect().catch(err => {
110
205
  console.error('[RiffleServer] Initial connection failed:', err);
@@ -324,25 +419,6 @@ export class RiffleServer {
324
419
  }
325
420
  }
326
421
 
327
- /**
328
- * 更新数据(部分更新)
329
- * @param {Object} updateData - 更新数据
330
- * @param {Object} [updateData.world] - 世界数据(可选)
331
- * @param {Object} [updateData.self] - 玩家数据(可选)
332
- * @param {Object} [updateData.self.public] - 公开数据(可选)
333
- * @param {Object} [updateData.self.private] - 私有数据(已弃用)
334
- * @example
335
- * server.update({
336
- * world: { score: 200 },
337
- * self: {
338
- * public: { x: 15, y: 25, rotation: 90 }
339
- * }
340
- * });
341
- */
342
- update(updateData) {
343
- this.sendUpdate(updateData);
344
- }
345
-
346
422
  /**
347
423
  * 监听服务器推送的最新数据
348
424
  * @param {Function} callback - 回调函数,接收服务器推送的数据
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lzpenguin/server",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Riffle 游戏服务器 WebSocket 客户端 SDK",
5
5
  "license": "ISC",
6
6
  "author": "lzpenguin",