koishi-plugin-game-mini 0.1.9 → 0.2.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.
package/lib/index.js CHANGED
@@ -26,23 +26,23 @@ exports.Config = koishi_1.Schema.object({
26
26
  min: koishi_1.Schema.number().min(0).default(0),
27
27
  max: koishi_1.Schema.number().min(1).default(100),
28
28
  botParticipateInGroup: koishi_1.Schema.boolean().default(true),
29
- totalRounds: koishi_1.Schema.number().min(1).default(10).description('猜数字比赛总回合数'),
30
- showRank: koishi_1.Schema.boolean().default(true).description('是否显示排行榜')
29
+ totalRounds: koishi_1.Schema.number().min(1).default(10),
30
+ showRank: koishi_1.Schema.boolean().default(true),
31
31
  }),
32
32
  wzHero: koishi_1.Schema.object({
33
33
  botParticipateInGroup: koishi_1.Schema.boolean().default(false),
34
- totalRounds: koishi_1.Schema.number().min(1).default(10).description('王者英雄猜谜比赛总回合数'),
35
- showRank: koishi_1.Schema.boolean().default(true).description('是否显示排行榜')
34
+ totalRounds: koishi_1.Schema.number().min(1).default(10),
35
+ showRank: koishi_1.Schema.boolean().default(true),
36
36
  }),
37
37
  chengyuImage: koishi_1.Schema.object({
38
38
  botParticipateInGroup: koishi_1.Schema.boolean().default(false),
39
- totalRounds: koishi_1.Schema.number().min(1).default(10).description('看图猜成语比赛总回合数'),
40
- showRank: koishi_1.Schema.boolean().default(true).description('是否显示排行榜')
39
+ totalRounds: koishi_1.Schema.number().min(1).default(10),
40
+ showRank: koishi_1.Schema.boolean().default(true),
41
41
  }),
42
42
  chengyuJielong: koishi_1.Schema.object({
43
43
  botParticipateInGroup: koishi_1.Schema.boolean().default(true),
44
- totalRounds: koishi_1.Schema.number().min(1).default(10).description('成语接龙比赛总回合数'),
45
- showRank: koishi_1.Schema.boolean().default(true).description('是否显示排行榜')
44
+ totalRounds: koishi_1.Schema.number().min(1).default(10),
45
+ showRank: koishi_1.Schema.boolean().default(true),
46
46
  }),
47
47
  apiConfig: koishi_1.Schema.object({
48
48
  timeout: koishi_1.Schema.number().default(5000),
@@ -51,12 +51,11 @@ exports.Config = koishi_1.Schema.object({
51
51
  const logger = new koishi_1.Logger('game-mini');
52
52
  const gameStates = new Map();
53
53
  const getSessionKey = (s) => s.channelId ? `g:${s.channelId}` : `p:${s.userId}`;
54
- // 修复:处理userId可能为undefined的情况
55
- const getPlayerKey = (platform, userId) => {
56
- const safeUserId = userId || 'unknown_user';
57
- return `${platform}_${safeUserId}`;
54
+ const getIntUserId = (session) => {
55
+ const userIdStr = session.userId || '0';
56
+ const hash = userIdStr.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0);
57
+ return hash || 1000000;
58
58
  };
59
- // 修复:确保返回的PlayerData中所有字段都是string类型
60
59
  const getPlayerData = (session) => {
61
60
  const safeUserId = session.userId || 'unknown_user';
62
61
  return {
@@ -166,7 +165,6 @@ const addScoreToOthers = (players, excludePlayerKey) => {
166
165
  });
167
166
  };
168
167
  function apply(ctx, cfg) {
169
- // 修复:为wzHero添加缺失的start属性
170
168
  const d = {
171
169
  guessNumber: {
172
170
  usage: "用法错误!正确用法:guess start/stop 或 直接输入数字",
@@ -183,7 +181,7 @@ function apply(ctx, cfg) {
183
181
  disabled: "猜数字功能已关闭,请联系管理员开启",
184
182
  },
185
183
  wzHero: {
186
- start: "王者英雄猜谜开始!本次共 {0} 回合~", // 新增缺失的start属性
184
+ start: "王者英雄猜谜开始!本次共 {0} 回合~",
187
185
  startUsage: "用法错误!正确用法:wzhero start [难度] 开始游戏",
188
186
  stop: "王者英雄猜谜比赛已停止",
189
187
  notStarted: "王者英雄猜谜比赛尚未开始,请输入 wzhero start [难度] 开始游戏",
@@ -298,9 +296,16 @@ function apply(ctx, cfg) {
298
296
  st.chengyuJielong.autoPlayCount += 1;
299
297
  gameStates.set(key, st);
300
298
  try {
301
- // 修复:传入可能为undefined的userId,函数内部已处理
299
+ const intUserId = getIntUserId(session);
302
300
  const r = await axios_1.default.get('https://api.suol.cc/v1/game_cyjl.php', {
303
- params: { id: getPlayerKey(session.platform, session.userId), msg: '我接' }, timeout: cfg.apiConfig.timeout
301
+ params: {
302
+ id: intUserId,
303
+ msg: '我接'
304
+ },
305
+ timeout: cfg.apiConfig.timeout,
306
+ headers: {
307
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
308
+ }
304
309
  });
305
310
  const dt = r.data;
306
311
  let m = '';
@@ -310,7 +315,7 @@ function apply(ctx, cfg) {
310
315
  st.chengyuJielong.lastChar = dt.data.current_char || '';
311
316
  }
312
317
  else {
313
- m = d.chengyuJielong.botWin;
318
+ m = dt.msg || d.chengyuJielong.botWin;
314
319
  st.chengyuJielong.currentRound += 1;
315
320
  if (st.chengyuJielong.currentRound > st.chengyuJielong.totalRounds) {
316
321
  st.chengyuJielong.started = false;
@@ -345,8 +350,7 @@ function apply(ctx, cfg) {
345
350
  if (isPrivate(session) && !cfg.enablePrivateGame)
346
351
  return next();
347
352
  const content = session.content.trim();
348
- // 修复:传入可能为undefined的userId,函数内部已处理
349
- const playerKey = getPlayerKey(session.platform, session.userId);
353
+ const playerKey = `${session.platform}_${session.userId || 'unknown'}`;
350
354
  if (cfg.enableChengyuJielong && st.chengyuJielong.started) {
351
355
  refreshActive(st);
352
356
  if (!st.chengyuJielong.players[playerKey]) {
@@ -358,14 +362,22 @@ function apply(ctx, cfg) {
358
362
  return next();
359
363
  }
360
364
  try {
365
+ const intUserId = getIntUserId(session);
361
366
  const r = await axios_1.default.get('https://api.suol.cc/v1/game_cyjl.php', {
362
- params: { id: playerKey, msg: `我接${content}` }, timeout: cfg.apiConfig.timeout
367
+ params: {
368
+ id: intUserId,
369
+ msg: `我接${content}`
370
+ },
371
+ timeout: cfg.apiConfig.timeout,
372
+ headers: {
373
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
374
+ }
363
375
  });
364
376
  const dt = r.data;
365
377
  let m = '';
366
378
  if (dt.code === 200) {
367
379
  m = dt.msg + '\n';
368
- if (dt.data.user) {
380
+ if (dt.data?.user) {
369
381
  st.chengyuJielong.players[playerKey].score += 1;
370
382
  st.chengyuJielong.players[playerKey].correctCount += 1;
371
383
  m += d.chengyuJielong.correct.replace('{0}', st.chengyuJielong.players[playerKey].score.toString()) + '\n';
@@ -375,7 +387,7 @@ function apply(ctx, cfg) {
375
387
  addScoreToOthers(st.chengyuJielong.players, playerKey);
376
388
  m += d.chengyuJielong.wrong.replace('{1}', st.chengyuJielong.players[playerKey].score.toString()) + '\n';
377
389
  }
378
- if (dt.data.system) {
390
+ if (dt.data?.system) {
379
391
  m += `我的接龙:${dt.data.system.idiom}(${dt.data.system.pinyin})\n释义:${dt.data.system.meaning}`;
380
392
  st.chengyuJielong.lastChar = dt.data.current_char || '';
381
393
  }
@@ -435,7 +447,6 @@ function apply(ctx, cfg) {
435
447
  }
436
448
  }
437
449
  catch (error) {
438
- logger.error(error);
439
450
  await session.send(d.chengyuImage.hintError);
440
451
  }
441
452
  return;
@@ -510,7 +521,6 @@ function apply(ctx, cfg) {
510
521
  }
511
522
  }
512
523
  catch (error) {
513
- logger.error(error);
514
524
  await session.send(d.chengyuImage.apiError);
515
525
  }
516
526
  gameStates.set(key, st);
@@ -523,7 +533,7 @@ function apply(ctx, cfg) {
523
533
  }
524
534
  try {
525
535
  const r = await axios_1.default.get('https://api.suol.cc/v1/game_wz.php', {
526
- params: { id: playerKey, msg: content }, timeout: cfg.apiConfig.timeout
536
+ params: { id: getIntUserId(session), msg: content }, timeout: cfg.apiConfig.timeout
527
537
  });
528
538
  const dt = r.data;
529
539
  if (dt.status === 'success') {
@@ -549,7 +559,7 @@ function apply(ctx, cfg) {
549
559
  else {
550
560
  text += '\n' + d.wzHero.roundEnd.replace('{0}', st.wzHero.currentRound.toString()).replace('{1}', (st.wzHero.totalRounds - st.wzHero.currentRound).toString());
551
561
  const nextR = await axios_1.default.get('https://api.suol.cc/v1/game_wz.php', {
552
- params: { id: playerKey, msg: `开始游戏${st.wzHero.difficulty}` }, timeout: cfg.apiConfig.timeout
562
+ params: { id: getIntUserId(session), msg: `开始游戏${st.wzHero.difficulty}` }, timeout: cfg.apiConfig.timeout
553
563
  });
554
564
  const nextDt = nextR.data;
555
565
  if (nextDt.status === 'success') {
@@ -639,15 +649,13 @@ function apply(ctx, cfg) {
639
649
  st.wzHero.participants = [];
640
650
  gameStates.set(key, st);
641
651
  try {
642
- // 修复:传入可能为undefined的userId,函数内部已处理
643
652
  const r = await axios_1.default.get('https://api.suol.cc/v1/game_wz.php', {
644
- params: { id: getPlayerKey(s.platform, s.userId), msg: `开始游戏${fd}` }, timeout: cfg.apiConfig.timeout
653
+ params: { id: getIntUserId(s), msg: `开始游戏${fd}` }, timeout: cfg.apiConfig.timeout
645
654
  });
646
655
  const dt = r.data;
647
656
  if (dt.status === 'success') {
648
657
  let t = `${dt.message}\n${dt.hint}\n剩余提示次数:${dt.remaining_hints}\n${dt.guide}`;
649
658
  t = t.replace(/\\n/g, '\n');
650
- // 现在d.wzHero.start已存在,不会报错
651
659
  t = d.wzHero.start.replace('{0}', cfg.wzHero.totalRounds.toString()) + '\n' + t;
652
660
  st.wzHero.currentAnswer = dt.hint || '';
653
661
  await s.send(t);
@@ -716,7 +724,6 @@ function apply(ctx, cfg) {
716
724
  }
717
725
  }
718
726
  catch (error) {
719
- logger.error(error);
720
727
  await s.send(d.chengyuImage.apiError);
721
728
  st.chengyuImage.started = false;
722
729
  }
@@ -763,9 +770,16 @@ function apply(ctx, cfg) {
763
770
  refreshActive(st);
764
771
  gameStates.set(key, st);
765
772
  try {
766
- // 修复:传入可能为undefined的userId,函数内部已处理
773
+ const intUserId = getIntUserId(s);
767
774
  const r = await axios_1.default.get('https://api.suol.cc/v1/game_cyjl.php', {
768
- params: { id: getPlayerKey(s.platform, s.userId), msg: '开始成语接龙' }, timeout: cfg.apiConfig.timeout
775
+ params: {
776
+ id: intUserId,
777
+ msg: '开始成语接龙'
778
+ },
779
+ timeout: cfg.apiConfig.timeout,
780
+ headers: {
781
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
782
+ }
769
783
  });
770
784
  const dt = r.data;
771
785
  let m = d.chengyuJielong.start.replace('{0}', cfg.chengyuJielong.totalRounds.toString());
@@ -1,3 +1,4 @@
1
+ # zh-CN.yml
1
2
  guessNumber:
2
3
  usage: "用法错误!正确用法:guess start/stop 或 直接输入数字"
3
4
  start: "猜数字比赛开始!本次共 {0} 回合~"
@@ -13,6 +14,7 @@ guessNumber:
13
14
  disabled: "猜数字功能已关闭,请联系管理员开启"
14
15
 
15
16
  wzHero:
17
+ start: "王者英雄猜谜开始!本次共 {0} 回合~"
16
18
  startUsage: "用法错误!正确用法:wzhero start [难度] 开始游戏"
17
19
  stop: "王者英雄猜谜比赛已停止"
18
20
  notStarted: "王者英雄猜谜比赛尚未开始,请输入 wzhero start [难度] 开始游戏"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-game-mini",
3
- "version": "0.1.9",
3
+ "version": "0.2.0",
4
4
  "description": "一款包含猜数字、猜王者英雄、看图猜成语、成语接龙的多功能小游戏插件",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",