@lzpenguin/server 1.1.11 → 1.1.12

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 (2) hide show
  1. package/README.md +53 -53
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,52 +1,52 @@
1
1
  # @lzpenguin/server
2
2
 
3
- Riffle 游戏服务器 WebSocket 客户端 SDK
3
+ Riffle game server WebSocket client SDK
4
4
 
5
- ## 安装
5
+ ## Installation
6
6
 
7
7
  ```bash
8
8
  npm install @lzpenguin/server
9
9
  ```
10
10
 
11
- ## 快速开始
11
+ ## Quick Start
12
12
 
13
13
  ```javascript
14
14
  import { RiffleServer } from '@lzpenguin/server';
15
15
 
16
- // 创建服务器实例
16
+ // Create a server instance
17
17
  const server = new RiffleServer({
18
- timestamp: 1234567890, // 必需:游戏时间戳(数字类型),直接使用上下文中给出的值
19
- autoReconnect: true, // 可选:自动重连,默认 true
20
- reconnectInterval: 3000 // 可选:重连间隔(毫秒),默认 3000
18
+ timestamp: 1234567890, // Required: game timestamp (number). Use the value provided in the context
19
+ autoReconnect: true, // Optional: auto reconnect, default true
20
+ reconnectInterval: 3000 // Optional: reconnect interval (ms), default 3000
21
21
  });
22
22
 
23
- // 1. 监听数据推送(init 成功后每 0.2 秒自动推送,调用 init/update 后立即推送)
23
+ // 1. Listen to data push (after init succeeds, auto-pushes every 0.2s; pushes immediately after init/update)
24
24
  server.onData((data) => {
25
- console.log('World:', data.world); // 世界数据
26
- console.log('Self:', data.self); // 自己的公开数据(包含 name / avatar / online 字段)
27
- console.log('Players:', data.players); // 其他玩家的公开数据(包含 name / avatar / online 字段)
28
- // 注意:self players[i] 结构相同,都只包含公开数据
25
+ console.log('World:', data.world); // World data
26
+ console.log('Self:', data.self); // Your public data (includes name / avatar / online fields)
27
+ console.log('Players:', data.players); // Other players' public data (includes name / avatar / online fields)
28
+ // Note: self and players[i] have the same structure and contain only public data
29
29
  });
30
30
 
31
- // 2. 初始化服务器(必需,连接后必须先调用)
31
+ // 2. Initialize the server (required, must be called after connecting)
32
32
  server.init({
33
33
  world: { score: 0, level: 1 },
34
34
  self: {
35
35
  public: { x: 100, y: 100 }
36
- // name 字段不需要设置,服务器返回数据会包含
36
+ // The name field does not need to be set; it is included in the server response
37
37
  }
38
38
  });
39
39
 
40
- // 3. 更新数据(部分更新,合并到现有数据)
40
+ // 3. Update data (partial update, merged into existing data)
41
41
  server.update({
42
42
  world: { score: 200 },
43
43
  self: { public: { x: 150, y: 150 } }
44
44
  });
45
45
  ```
46
46
 
47
- ## 数据格式
47
+ ## Data Format
48
48
 
49
- 服务器推送的数据格式(通过 `onData` 接收):
49
+ Data format pushed by the server (received via `onData`):
50
50
 
51
51
  ```json
52
52
  {
@@ -59,70 +59,70 @@ server.update({
59
59
  }
60
60
  ```
61
61
 
62
- **关于 name / avatar / online 字段:**
63
- - `self.name` `players[i].name` 字段**一定会存在**,包含用户的昵称或用户名
64
- - `self.avatar` `players[i].avatar` 字段由服务器根据用户头像覆盖
65
- - `self.online` `players[i].online` 字段存储在玩家数据中,`/api/v1/server/ws` 连接建立时自动为 true,断开时自动为 false
66
- - 你不需要在 `init()` `update()` 时设置 name / avatar / online 字段,服务器会直接返回
62
+ **About the name / avatar / online fields:**
63
+ - `self.name` and `players[i].name` **always exist** and contain the user's nickname or username
64
+ - `self.avatar` and `players[i].avatar` are overridden by the server based on the user's avatar
65
+ - `self.online` and `players[i].online` are stored in player data and are set to true when `/api/v1/server/ws` connects, and false when it disconnects
66
+ - You do not need to set name / avatar / online in `init()` or `update()`; the server returns them directly
67
67
 
68
- ## API 说明
68
+ ## API Reference
69
69
 
70
- ### init(data) - 初始化服务器
70
+ ### init(data) - Initialize Server
71
71
 
72
- 必需,连接后必须先调用。根据 timestamp 判断是否重新初始化。
72
+ Required. Must be called after connecting. Uses timestamp to determine whether to re-initialize.
73
73
 
74
74
  ```javascript
75
75
  server.init({
76
- world: { score: 0, level: 1 }, // 可选:世界初始数据
76
+ world: { score: 0, level: 1 }, // Optional: initial world data
77
77
  self: {
78
- public: { x: 100, y: 100 } // 可选:玩家公开数据
79
- // 注意:name 字段不需要设置,服务器返回的数据会包含该字段
78
+ public: { x: 100, y: 100 } // Optional: player public data
79
+ // Note: the name field does not need to be set; it is included in the server response
80
80
  }
81
81
  });
82
82
  ```
83
83
 
84
- ### update(data) - 更新数据
84
+ ### update(data) - Update Data
85
85
 
86
- 部分更新数据(合并,不替换)。只更新提供的字段,未提供的字段保持不变。
86
+ Partially updates data (merged, not replaced). Only provided fields are updated; missing fields remain unchanged.
87
87
 
88
88
  ```javascript
89
- // 更新世界数据
89
+ // Update world data
90
90
  server.update({ world: { score: 200 } });
91
91
 
92
- // 更新玩家数据
92
+ // Update player data
93
93
  server.update({ self: { public: { x: 15, y: 25 } } });
94
94
 
95
- // 同时更新
95
+ // Update both
96
96
  server.update({
97
97
  world: { score: 200 },
98
98
  self: { public: { x: 15, y: 25 } }
99
99
  });
100
100
  ```
101
101
 
102
- ### onData(callback) - 监听数据推送
102
+ ### onData(callback) - Listen to Data Pushes
103
103
 
104
- 监听服务器推送的数据。返回取消监听函数。
104
+ Listens to data pushed by the server. Returns an unsubscribe function.
105
105
 
106
106
  ```javascript
107
107
  const unsubscribe = server.onData((data) => {
108
- // 处理数据
108
+ // Handle data
109
109
  });
110
110
 
111
- // 取消监听
111
+ // Unsubscribe
112
112
  unsubscribe();
113
113
  ```
114
114
 
115
- **推送时机:** init 成功后每 0.2 秒自动推送,调用 init/update 后立即推送
115
+ **Push timing:** after init succeeds, auto-pushes every 0.2s; pushes immediately after init/update
116
116
 
117
- ## 关于 timestamp
117
+ ## About timestamp
118
118
 
119
- - **类型要求**:必须是数字类型(number/int),不能是字符串
120
- - **如何获取**:直接使用上下文中给出的 timestamp
121
- - **较小或相等**:不生效,返回现有数据
122
- - **较大**:删除旧服务器并重新创建
123
- - **何时更新**:改动角色或世界字段设计(新增/删除/改变类型)时,需使用更大的 timestamp 重新初始化
119
+ - **Type requirement**: must be a number (number/int), not a string
120
+ - **How to get it**: use the timestamp value provided in the context directly
121
+ - **Smaller or equal**: no effect; returns existing data
122
+ - **Larger**: deletes the old server and recreates it
123
+ - **When to update**: when changing character or world field design (add/remove/change type), use a larger timestamp to re-initialize
124
124
 
125
- ## 完整示例
125
+ ## Complete Example
126
126
 
127
127
  ```javascript
128
128
  import { RiffleServer } from '@lzpenguin/server';
@@ -131,33 +131,33 @@ const server = new RiffleServer({
131
131
  timestamp: 1234567890
132
132
  });
133
133
 
134
- // 监听推送
134
+ // Listen to pushes
135
135
  server.onData((data) => {
136
- // data.self.name data.players[i].name 一定会存在
137
- console.log('我的名字:', data.self.name);
136
+ // data.self.name and data.players[i].name always exist
137
+ console.log('My name:', data.self.name);
138
138
 
139
139
  const myPosition = { x: data.self.x, y: data.self.y };
140
140
  const otherPlayers = data.players || [];
141
141
  renderPlayers(myPosition, otherPlayers);
142
142
 
143
143
  if (data.world?.gameOver) {
144
- console.log('游戏结束!分数:', data.world.score);
144
+ console.log('Game over! Score:', data.world.score);
145
145
  }
146
146
  });
147
147
 
148
- // 初始化
148
+ // Initialize
149
149
  server.init({
150
150
  world: { score: 0, level: 1 },
151
151
  self: { public: { x: 0, y: 0 } }
152
- // name 字段不需要设置,返回的数据会包含
152
+ // The name field does not need to be set; it is included in the response
153
153
  });
154
154
 
155
- // 更新玩家位置
155
+ // Update player position
156
156
  function movePlayer(x, y) {
157
157
  server.update({ self: { public: { x, y } } });
158
158
  }
159
159
 
160
- // 更新分数
160
+ // Update score
161
161
  function updateScore(score) {
162
162
  server.update({ world: { score } });
163
163
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lzpenguin/server",
3
- "version": "1.1.11",
3
+ "version": "1.1.12",
4
4
  "description": "Riffle 游戏服务器 WebSocket 客户端 SDK",
5
5
  "license": "ISC",
6
6
  "author": "lzpenguin",