@lzpenguin/server 1.0.9 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +42 -79
  2. package/index.d.ts +6 -67
  3. package/index.js +31 -107
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -50,82 +50,6 @@ 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
-
129
53
  ## 三种消息类型
130
54
 
131
55
  ### 1. init() - 初始化服务器
@@ -162,7 +86,40 @@ server.init({
162
86
 
163
87
  **注意:** init 成功后服务器每 0.2 秒自动推送最新数据
164
88
 
165
- ### 2. onData() - 监听数据推送
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() - 监听数据推送
166
123
 
167
124
  监听服务器推送的最新数据。init 成功后每 0.2 秒自动推送,调用 init/update 后立即推送。
168
125
 
@@ -234,12 +191,18 @@ server.init({
234
191
 
235
192
  // 更新玩家位置
236
193
  function movePlayer(x, y) {
237
- server.self.set({ x, y });
194
+ server.update({
195
+ self: {
196
+ public: { x, y }
197
+ }
198
+ });
238
199
  }
239
200
 
240
201
  // 更新分数
241
202
  function updateScore(score) {
242
- server.world.update('score', score);
203
+ server.update({
204
+ world: { score }
205
+ });
243
206
  }
244
207
  ```
245
208
 
package/index.d.ts CHANGED
@@ -63,68 +63,6 @@ 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
-
128
66
  /**
129
67
  * RiffleServer - 游戏服务器 WebSocket 客户端
130
68
  */
@@ -142,17 +80,18 @@ export class RiffleServer {
142
80
  /** 是否已初始化 */
143
81
  readonly isInitialized: boolean;
144
82
 
145
- /** World 数据管理器 */
146
- readonly world: WorldManager;
147
- /** Self 数据管理器 */
148
- readonly self: SelfManager;
149
-
150
83
  /**
151
84
  * 初始化服务器(必需,连接后必须先调用)
152
85
  * @param initData 初始化数据
153
86
  */
154
87
  init(initData: InitData): void;
155
88
 
89
+ /**
90
+ * 更新数据(部分更新,合并到现有数据)
91
+ * @param updateData 更新数据
92
+ */
93
+ update(updateData: UpdateData): void;
94
+
156
95
  /**
157
96
  * 监听服务器推送的最新数据
158
97
  * @param callback 回调函数,接收服务器推送的数据
package/index.js CHANGED
@@ -28,98 +28,12 @@ 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
-
117
31
  /**
118
32
  * RiffleServer - 游戏服务器 WebSocket 客户端
119
33
  *
120
34
  * @example
121
35
  * ```js
122
- * import { RiffleServer } from '@lzpenguin/server';
36
+ * import { RiffleServer } from '@riffle/server';
123
37
  *
124
38
  * const server = new RiffleServer({
125
39
  * url: 'wss://api.riffle.app',
@@ -130,31 +44,26 @@ class SelfManager {
130
44
  * // 监听服务器推送的最新数据
131
45
  * server.onData((data) => {
132
46
  * console.log('World:', data.world);
133
- * console.log('Self:', data.self);
47
+ * console.log('Self:', data.self); // self 现在直接是公开数据,与 players[i] 结构一致
134
48
  * console.log('Players:', data.players);
135
49
  * });
136
50
  *
137
51
  * // 初始化服务器(必需,连接后必须先调用)
138
52
  * server.init({
139
- * hash: 'game-version-hash-123',
140
- * world: { score: 0, level: 1 },
53
+ * hash: 'game-version-hash-123', // 游戏版本哈希值
54
+ * world: { score: 0, level: 1 }, // 世界初始数据(可选)
141
55
  * self: {
142
- * public: { name: 'Player1', x: 100, y: 100 }
56
+ * public: { name: 'Player1', x: 100, y: 100 } // 公开数据(可选)
143
57
  * }
144
58
  * });
145
59
  *
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 });
60
+ * // 更新数据
61
+ * server.update({
62
+ * world: { score: 200 },
63
+ * self: {
64
+ * public: { x: 15, y: 25, rotation: 90 }
65
+ * }
66
+ * });
158
67
  * ```
159
68
  */
160
69
  export class RiffleServer {
@@ -196,10 +105,6 @@ export class RiffleServer {
196
105
  // 当前服务器数据缓存
197
106
  this.currentData = null;
198
107
 
199
- // 创建细粒度操作管理器
200
- this.world = new WorldManager(this);
201
- this.self = new SelfManager(this);
202
-
203
108
  // 连接状态(异步调用,不阻塞构造函数)
204
109
  this.connect().catch(err => {
205
110
  console.error('[RiffleServer] Initial connection failed:', err);
@@ -419,6 +324,25 @@ export class RiffleServer {
419
324
  }
420
325
  }
421
326
 
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
+
422
346
  /**
423
347
  * 监听服务器推送的最新数据
424
348
  * @param {Function} callback - 回调函数,接收服务器推送的数据
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lzpenguin/server",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
4
4
  "description": "Riffle 游戏服务器 WebSocket 客户端 SDK",
5
5
  "license": "ISC",
6
6
  "author": "lzpenguin",