@seayoo-io/combo-sdk-node 1.6.0 → 1.8.1

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 CHANGED
@@ -14,7 +14,7 @@
14
14
  v1 新增 gm 模块的 [IdempotencyKey](https://docs.seayoo.com/combo/server/gm/#idempotency),主要改动包括:
15
15
 
16
16
  1. 修改了 commandHandler 的参数,插入 idempotencyKey 参数;当此参数为非空字符串时需要处理幂等性;
17
- 2. 新增预设错误 IdempotencyConflict / IdempotencyMismatch
17
+ 2. 新增预设错误 IdempotencyConflict / IdempotencyMismatch
18
18
  3. gm 服务版本号升级到 2.0
19
19
 
20
20
  ## 安装
@@ -32,7 +32,7 @@ pnpm add @seayoo-io/combo-sdk-node
32
32
  请按照工程实际情况选择导入方式,后续示例采用 ES 方式导入。
33
33
 
34
34
  ```js
35
- // ES
35
+ // ES
36
36
  import { ApiClient } from "@seayoo-io/combo-sdk-node"
37
37
 
38
38
  // CommonJS
@@ -47,14 +47,14 @@ const { ApiClient } = require("@seayoo-io/combo-sdk-node")
47
47
  import { ApiClient, Endpoint } from "@seayoo-io/combo-sdk-node"
48
48
 
49
49
  const client = new ApiClient({
50
- game: "<GameId>",
51
- secret: "<SecretKey>",
52
- endpoint: Endpoint.China,
53
- // 以下参数可选
54
- maxRetry: 1, // 失败后自动重试次数,默认 1
55
- retryInterval: 1000, // 重试间隔,默认是 1000,单位 ms,可以传递函数动态设置间隔
56
- logger: function(log) {}, // 请求日志函数,log 类型参见源码类型定义
57
- timeout: 5000, // 超时等待时长,单位 ms,默认 5000
50
+ game: "<GameId>",
51
+ secret: "<SecretKey>",
52
+ endpoint: Endpoint.China,
53
+ // 以下参数可选
54
+ maxRetry: 1, // 失败后自动重试次数,默认 1
55
+ retryInterval: 1000, // 重试间隔,默认是 1000,单位 ms,可以传递函数动态设置间隔
56
+ logger: function (log) {}, // 请求日志函数,log 类型参见源码类型定义
57
+ timeout: 5000, // 超时等待时长,单位 ms,默认 5000
58
58
  })
59
59
  ```
60
60
 
@@ -78,7 +78,7 @@ const createOrderResult = await client.createOrder({
78
78
  */
79
79
  product_id: "<ProductID>",
80
80
  /** 平台,支持类型见源码类型定义 Platform */
81
- platform: Platform.iOS,
81
+ platform: Platform.iOS,
82
82
  /**
83
83
  * 游戏侧接收发货通知的服务端地址
84
84
  * 这个地址对应的服务端应该通过 Notify 模块实现路由处理
@@ -126,11 +126,10 @@ interface CreateOrderResponse {
126
126
 
127
127
  ```js
128
128
  // 进入游戏
129
- await client.enterGame("<ComboID>", "<SessionID>");
129
+ await client.enterGame("<ComboID>", "<SessionID>")
130
130
 
131
131
  // 离开游戏
132
- await client.leaveGame("<ComboID>", "<SessionID>");
133
-
132
+ await client.leaveGame("<ComboID>", "<SessionID>")
134
133
  ```
135
134
 
136
135
  > 注意,SessionID 不可重复使用,可以使用 SDK 提供的工具函数生成 SessionID
@@ -140,7 +139,6 @@ import { genSessionID } from "@seayoo-io/combo-sdk-node"
140
139
 
141
140
  // genSessionID 接受唯一参数 comboId 作为输入,返回一个 32 位固定长度的不重复字符串
142
141
  const userSessionID = genSessionID("<ComboID>")
143
-
144
142
  ```
145
143
 
146
144
  ### 发送/验证短信验证码 OTP
@@ -207,6 +205,33 @@ interface VerifyOtpResponse {
207
205
  > - `sendOtp` 在请求失败时(如触发发送冷却、参数错误、服务端错误)会**抛出异常**,请使用 try/catch 捕获处理。
208
206
  > - `verifyOtp` 仅在请求本身失败时抛出异常。**验证码不正确属于正常业务结果**,此时会返回 `{ valid: false }` 而不会抛出异常,游戏侧需要根据 `valid` 字段判断验证是否通过。
209
207
 
208
+ ### 申请语音审核 VoiceModerationRequest
209
+
210
+ 玩家认为语音房间内某些玩家存在语音违规行为时,可提交语音审核申请。调用前提是:游戏已经在世游开启语音审核服务。
211
+
212
+ ```js
213
+ const ok = await client.voiceModerationRequest({
214
+ /** 房间实例 ID,唯一标识某个语音房间的一次存续(从开启到关闭) */
215
+ room_instance_id: "<RoomInstanceID>",
216
+ /** 游戏服务器 ID */
217
+ server_id: 1001,
218
+ /** 提交审核申请的玩家角色 ID */
219
+ requester_role_id: "<PlayerRoleID>",
220
+ /** 被提交语音审核的玩家角色 ID 列表,一次最多提交 32 个 */
221
+ target_role_ids: ["<TargetRoleID1>", "<TargetRoleID2>"],
222
+ })
223
+
224
+ // ok 为 true 表示提交成功,false 表示提交失败(已自动打印错误日志)
225
+ if (ok) {
226
+ // 提交成功
227
+ }
228
+ ```
229
+
230
+ > ⚠️ 注意事项
231
+ >
232
+ > - `voiceModerationRequest` 不会在失败时抛出异常,而是返回 `false`。游戏侧可根据返回值决定后续处理。
233
+ > - `target_role_ids` 一次最多提交 32 个玩家角色 ID。
234
+
210
235
  ## Notify
211
236
 
212
237
  ### Step 1 准备参数
@@ -250,7 +275,7 @@ interface ShipOrderNotification {
250
275
  */
251
276
  is_sandbox?: boolean
252
277
  }
253
-
278
+
254
279
  interface RefundNotification {
255
280
  // 结构同 ShipOrderNotification
256
281
  }
@@ -269,10 +294,10 @@ const config = {
269
294
  // 使用 http 模块的处理函数
270
295
  import { getNotificationHandler } from "@seayoo-io/combo-sdk-node"
271
296
  const notifyHandler = getNotificationHandler(config, notificationHandler)
272
- http.createServer(async function(req, res){
273
- if(req.path === "<YourNotifyUrl>" && req.method === "POST") {
274
- await notifyHandler(req, res)
275
- }
297
+ http.createServer(async function (req, res) {
298
+ if (req.path === "<YourNotifyUrl>" && req.method === "POST") {
299
+ await notifyHandler(req, res)
300
+ }
276
301
  })
277
302
 
278
303
  // 或,使用 Express 处理函数
@@ -300,15 +325,15 @@ app.post("/path/to/your/notify/url", koaHandler)
300
325
 
301
326
  ```js
302
327
  // 插件需要先于 bodyParse 类似的插件执行,以确保可以获取原始请求的内容
303
- app.use(function(req, res, next){
304
- // 在其他插件处理之前,检查是否为通知请求的 url,方法是否为 POST
305
- if(req.method === "POST" && req.path === "/path/to/your/notify/url") {
306
- // 如果是,则交由插件处理后续响应
307
- notifyMiddleware(req, res)
308
- return
309
- }
310
- // 否则其他一切正常
311
- next()
328
+ app.use(function (req, res, next) {
329
+ // 在其他插件处理之前,检查是否为通知请求的 url,方法是否为 POST
330
+ if (req.method === "POST" && req.path === "/path/to/your/notify/url") {
331
+ // 如果是,则交由插件处理后续响应
332
+ notifyMiddleware(req, res)
333
+ return
334
+ }
335
+ // 否则其他一切正常
336
+ next()
312
337
  })
313
338
  ```
314
339
 
@@ -317,21 +342,13 @@ SDK 提供了两个插件来处理:
317
342
  ```js
318
343
  // express 插件
319
344
  import { getNotificationMiddlewareForExpress } from "@seayoo-io/combo-sdk-node"
320
- const notifyMiddleware = getNotificationMiddlewareForExpress(
321
- "/path/to/your/notify/url",
322
- config,
323
- notificationHandler
324
- );
325
- app.use(notifyMiddleware);
345
+ const notifyMiddleware = getNotificationMiddlewareForExpress("/path/to/your/notify/url", config, notificationHandler)
346
+ app.use(notifyMiddleware)
326
347
 
327
348
  // koa 插件
328
349
  import { getNotificationMiddlewareForKoa } from "@seayoo-io/combo-sdk-node"
329
- const notifyMiddleware = getNotificationMiddlewareForKoa(
330
- "/path/to/your/notify/url",
331
- config,
332
- notificationHandler
333
- );
334
- app.use(notifyMiddleware);
350
+ const notifyMiddleware = getNotificationMiddlewareForKoa("/path/to/your/notify/url", config, notificationHandler)
351
+ app.use(notifyMiddleware)
335
352
  ```
336
353
 
337
354
  ## Verify
@@ -342,9 +359,9 @@ app.use(notifyMiddleware);
342
359
  import { TokenVerifier, Endpoint } from "@seayoo-io/combo-sdk-node"
343
360
 
344
361
  const verifier = new TokenVerifier({
345
- game: "<GameId>",
346
- secret: "<SecretKey>",
347
- endpoint: Endpoint.China
362
+ game: "<GameId>",
363
+ secret: "<SecretKey>",
364
+ endpoint: Endpoint.China,
348
365
  })
349
366
  ```
350
367
 
@@ -358,7 +375,7 @@ const identityPayload = verifier.verifyIdentityToken(token)
358
375
  if(identityPayload instanceof Error) {
359
376
  console.error(result.message)
360
377
  } else {
361
- // WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏
378
+ // WebGL 平台,包括微信、抖音等小游戏,以及 HTML5 网页游戏
362
379
  if(identityPayload.idp === IdP.WebGL) {
363
380
  // do something
364
381
  }
@@ -553,7 +570,7 @@ interface IdempotencyKeyStoreHelper {
553
570
  */
554
571
  setXX: (key: string, value: string) => Promise<void>
555
572
  }
556
-
573
+
557
574
  // SDK 内置实现了两个工具类
558
575
  import { MemoryIdempotencyStore, RedisIdempotencyStore } from "@seayoo-io/combo-sdk-node"
559
576
 
@@ -582,10 +599,10 @@ import { getGMCommandHandler } from "@seayoo-io/combo-sdk-node"
582
599
  // storeHelper 类型为 IdempotencyKeyStoreHelper
583
600
  // 可不传递,如果不提供则不会启用内置的 idempotencyKey 处理逻辑,下同
584
601
  const handler = getGMCommandHandler(config, gmCommandHandler, storeHelper)
585
- http.createServer(async function(req, res){
586
- if(req.path === "<YourNotifyUrl>" && req.method === "POST") {
587
- await handler(req, res)
588
- }
602
+ http.createServer(async function (req, res) {
603
+ if (req.path === "<YourNotifyUrl>" && req.method === "POST") {
604
+ await handler(req, res)
605
+ }
589
606
  })
590
607
 
591
608
  // 方式 2. 使用 express / koa 的 handler
@@ -599,12 +616,10 @@ app.post("/path/to/your/gm/url", koaHandler)
599
616
 
600
617
  // 方式 3. 使用 express / koa 的中间件,推荐
601
618
  import { getGMMiddlewareForExpress } from "@seayoo-io/combo-sdk-node"
602
- const gmMiddleware = getGMMiddlewareForExpress("/path/to/your/gm/url", config, gmCommandHandler, storeHelper);
603
- app.use(gmMiddleware);
619
+ const gmMiddleware = getGMMiddlewareForExpress("/path/to/your/gm/url", config, gmCommandHandler, storeHelper)
620
+ app.use(gmMiddleware)
604
621
 
605
622
  import { getGMMiddlewareForKoa } from "@seayoo-io/combo-sdk-node"
606
- const gmMiddleware = getGMMiddlewareForKoa("/path/to/your/gm/url", config, gmCommandHandler, storeHelper);
607
- app.use(gmMiddleware);
608
-
623
+ const gmMiddleware = getGMMiddlewareForKoa("/path/to/your/gm/url", config, gmCommandHandler, storeHelper)
624
+ app.use(gmMiddleware)
609
625
  ```
610
-