@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.
- package/README.md +53 -53
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
1
|
# @lzpenguin/server
|
|
2
2
|
|
|
3
|
-
Riffle
|
|
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, //
|
|
20
|
-
reconnectInterval: 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.
|
|
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); //
|
|
27
|
-
console.log('Players:', data.players); //
|
|
28
|
-
//
|
|
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
|
-
|
|
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
|
-
|
|
63
|
-
- `self.name`
|
|
64
|
-
- `self.avatar`
|
|
65
|
-
- `self.online`
|
|
66
|
-
-
|
|
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
|
-
|
|
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
|
-
//
|
|
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
|
-
|
|
115
|
+
**Push timing:** after init succeeds, auto-pushes every 0.2s; pushes immediately after init/update
|
|
116
116
|
|
|
117
|
-
##
|
|
117
|
+
## About timestamp
|
|
118
118
|
|
|
119
|
-
-
|
|
120
|
-
-
|
|
121
|
-
-
|
|
122
|
-
-
|
|
123
|
-
-
|
|
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
|
|
137
|
-
console.log('
|
|
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('
|
|
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
|
}
|