koishi-plugin-game-mini 0.2.1 → 0.2.2

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.d.ts CHANGED
@@ -19,12 +19,10 @@ export interface Config {
19
19
  showRank: boolean;
20
20
  };
21
21
  wzHero: {
22
- botParticipateInGroup: boolean;
23
22
  totalRounds: number;
24
23
  showRank: boolean;
25
24
  };
26
25
  chengyuImage: {
27
- botParticipateInGroup: boolean;
28
26
  totalRounds: number;
29
27
  showRank: boolean;
30
28
  };
package/lib/index.js CHANGED
@@ -57,9 +57,6 @@ exports.Config = koishi_1.Schema.object({
57
57
  }).description('猜数字游戏配置')
58
58
  .i18n({ 'zh-CN': '猜数字游戏配置' }),
59
59
  wzHero: koishi_1.Schema.object({
60
- botParticipateInGroup: koishi_1.Schema.boolean().default(false)
61
- .description('群聊中机器人是否参与王者英雄猜谜')
62
- .i18n({ 'zh-CN': '机器人参与群聊游戏' }),
63
60
  totalRounds: koishi_1.Schema.number().min(1).default(10)
64
61
  .description('王者英雄猜谜总回合数')
65
62
  .i18n({ 'zh-CN': '总回合数' }),
@@ -69,9 +66,6 @@ exports.Config = koishi_1.Schema.object({
69
66
  }).description('王者英雄猜谜游戏配置')
70
67
  .i18n({ 'zh-CN': '王者英雄猜谜配置' }),
71
68
  chengyuImage: koishi_1.Schema.object({
72
- botParticipateInGroup: koishi_1.Schema.boolean().default(false)
73
- .description('群聊中机器人是否参与看图猜成语')
74
- .i18n({ 'zh-CN': '机器人参与群聊游戏' }),
75
69
  totalRounds: koishi_1.Schema.number().min(1).default(10)
76
70
  .description('看图猜成语总回合数')
77
71
  .i18n({ 'zh-CN': '总回合数' }),
@@ -115,11 +109,9 @@ exports.Config = koishi_1.Schema.object({
115
109
  'guessNumber.totalRounds': '总回合数',
116
110
  'guessNumber.showRank': '显示排行榜',
117
111
  'wzHero': '王者英雄猜谜配置',
118
- 'wzHero.botParticipateInGroup': '机器人参与群聊游戏',
119
112
  'wzHero.totalRounds': '总回合数',
120
113
  'wzHero.showRank': '显示排行榜',
121
114
  'chengyuImage': '看图猜成语配置',
122
- 'chengyuImage.botParticipateInGroup': '机器人参与群聊游戏',
123
115
  'chengyuImage.totalRounds': '总回合数',
124
116
  'chengyuImage.showRank': '显示排行榜',
125
117
  'chengyuJielong': '成语接龙配置',
@@ -159,11 +151,17 @@ const checkGameRunning = (s, t) => {
159
151
  return true;
160
152
  return false;
161
153
  };
154
+ const clearGameTimer = (state) => {
155
+ if (state.stopTimer)
156
+ clearTimeout(state.stopTimer);
157
+ };
162
158
  const clearAllTimers = (st) => {
159
+ clearGameTimer(st.guessNumber);
160
+ clearGameTimer(st.wzHero);
161
+ clearGameTimer(st.chengyuImage);
162
+ clearGameTimer(st.chengyuJielong);
163
163
  if (st.chengyuJielong.autoPlayTimer)
164
164
  clearTimeout(st.chengyuJielong.autoPlayTimer);
165
- if (st.chengyuJielong.stopTimer)
166
- clearTimeout(st.chengyuJielong.stopTimer);
167
165
  };
168
166
  const initState = (key, c) => {
169
167
  const s = {
@@ -229,7 +227,7 @@ const generateRankText = (players) => {
229
227
  const sortedPlayers = playerList.sort((a, b) => b.score - a.score);
230
228
  let rankText = '🏆 排行榜 🏆\n';
231
229
  sortedPlayers.forEach((player, index) => {
232
- rankText += `${index + 1}. ${player.name}(${player.platform}-${player.userId}) - 得分:${player.score}分(答对${player.correctCount}题)\n`;
230
+ rankText += `${index + 1}. ${player.name} - 得分:${player.score}分(答对${player.correctCount}题)\n`;
233
231
  });
234
232
  return rankText.trim();
235
233
  };
@@ -238,6 +236,7 @@ const clearGameData = (gameState) => {
238
236
  gameState.participants = [];
239
237
  gameState.currentRound = 0;
240
238
  gameState.started = false;
239
+ clearGameTimer(gameState);
241
240
  };
242
241
  const addScoreToOthers = (players, excludePlayerKey) => {
243
242
  Object.keys(players).forEach(playerKey => {
@@ -246,6 +245,18 @@ const addScoreToOthers = (players, excludePlayerKey) => {
246
245
  }
247
246
  });
248
247
  };
248
+ const setupAutoStop = (session, state, cfg, endMsg, showRank) => {
249
+ clearGameTimer(state);
250
+ if (cfg.autoStopInactiveTime <= 0)
251
+ return;
252
+ state.stopTimer = setTimeout(() => {
253
+ clearGameData(state);
254
+ session.send(endMsg);
255
+ if (showRank && session.channelId) {
256
+ session.send(generateRankText(state.players));
257
+ }
258
+ }, cfg.autoStopInactiveTime * 1000);
259
+ };
249
260
  function apply(ctx, cfg) {
250
261
  const d = {
251
262
  guessNumber: {
@@ -261,6 +272,7 @@ function apply(ctx, cfg) {
261
272
  rankTitle: "🏆 排行榜 🏆",
262
273
  rankEmpty: "暂无参与记录~",
263
274
  disabled: "猜数字功能已关闭,请联系管理员开启",
275
+ autoStop: "游戏因长时间无人操作,已自动结束"
264
276
  },
265
277
  wzHero: {
266
278
  start: "王者英雄猜谜开始!本次共 {0} 回合~",
@@ -276,6 +288,7 @@ function apply(ctx, cfg) {
276
288
  rankTitle: "🏆 排行榜 🏆",
277
289
  rankEmpty: "暂无参与记录~",
278
290
  disabled: "王者英雄猜谜功能已关闭,请联系管理员开启",
291
+ autoStop: "游戏因长时间无人操作,已自动结束"
279
292
  },
280
293
  chengyuImage: {
281
294
  start: "看图猜成语比赛开始!本次共 {0} 回合~",
@@ -293,6 +306,7 @@ function apply(ctx, cfg) {
293
306
  rankTitle: "🏆 排行榜 🏆",
294
307
  rankEmpty: "暂无参与记录~",
295
308
  disabled: "看图猜成语功能已关闭,请联系管理员开启",
309
+ autoStop: "游戏因长时间无人操作,已自动结束"
296
310
  },
297
311
  chengyuJielong: {
298
312
  start: "成语接龙比赛开始!本次共 {0} 回合~我先来~",
@@ -312,6 +326,7 @@ function apply(ctx, cfg) {
312
326
  rankTitle: "🏆 排行榜 🏆",
313
327
  rankEmpty: "暂无参与记录~",
314
328
  disabled: "成语接龙功能已关闭,请联系管理员开启",
329
+ autoStop: "游戏因长时间无人操作,已自动结束"
315
330
  },
316
331
  common: {
317
332
  gameRunning: "当前已有游戏在运行中,请先结束当前游戏后再开始新游戏!",
@@ -348,20 +363,6 @@ function apply(ctx, cfg) {
348
363
  doAutoPlay(session, key, st);
349
364
  }, delay);
350
365
  };
351
- const scheduleStop = (session, key, st) => {
352
- clearTimeout(st.chengyuJielong.stopTimer);
353
- st.chengyuJielong.stopTimer = setTimeout(() => {
354
- st.chengyuJielong.started = false;
355
- clearAllTimers(st);
356
- session.send(d.common.gameStoppedByInactive);
357
- if (cfg.chengyuJielong.showRank && !isPrivate(session)) {
358
- const rankText = generateRankText(st.chengyuJielong.players);
359
- session.send(rankText);
360
- }
361
- clearGameData(st.chengyuJielong);
362
- gameStates.set(key, st);
363
- }, cfg.autoStopInactiveTime * 1000);
364
- };
365
366
  const doAutoPlay = async (session, key, st) => {
366
367
  if (!st.chengyuJielong.started)
367
368
  return;
@@ -371,7 +372,7 @@ function apply(ctx, cfg) {
371
372
  session.send(d.chengyuJielong.autoPlayTooMany.replace('{0}', String(cfg.autoPlayMaxCount)));
372
373
  st.chengyuJielong.hasTriggeredAutoPlay = true;
373
374
  gameStates.set(key, st);
374
- scheduleStop(session, key, st);
375
+ setupAutoStop(session, st.chengyuJielong, cfg, d.chengyuJielong.autoStop, cfg.chengyuJielong.showRank);
375
376
  return;
376
377
  }
377
378
  st.chengyuJielong.hasTriggeredAutoPlay = true;
@@ -422,7 +423,7 @@ function apply(ctx, cfg) {
422
423
  await session.send(d.chengyuJielong.apiError);
423
424
  }
424
425
  gameStates.set(key, st);
425
- scheduleStop(session, key, st);
426
+ setupAutoStop(session, st.chengyuJielong, cfg, d.chengyuJielong.autoStop, cfg.chengyuJielong.showRank);
426
427
  };
427
428
  ctx.middleware(async (session, next) => {
428
429
  if (!session.content)
@@ -435,11 +436,13 @@ function apply(ctx, cfg) {
435
436
  const playerKey = `${session.platform}_${session.userId || 'unknown'}`;
436
437
  if (cfg.enableChengyuJielong && st.chengyuJielong.started) {
437
438
  refreshActive(st);
439
+ clearGameTimer(st.chengyuJielong);
438
440
  if (!st.chengyuJielong.players[playerKey]) {
439
441
  st.chengyuJielong.players[playerKey] = getPlayerData(session);
440
442
  st.chengyuJielong.participants.push(playerKey);
441
443
  }
442
444
  if (!isFourCharIdiom(content)) {
445
+ setupAutoStop(session, st.chengyuJielong, cfg, d.chengyuJielong.autoStop, cfg.chengyuJielong.showRank);
443
446
  scheduleAutoPlay(session, key, st);
444
447
  return next();
445
448
  }
@@ -489,6 +492,7 @@ function apply(ctx, cfg) {
489
492
  m += '\n' + d.chengyuJielong.roundEnd.replace('{0}', st.chengyuJielong.currentRound.toString()).replace('{1}', (st.chengyuJielong.totalRounds - st.chengyuJielong.currentRound).toString());
490
493
  await session.send(m);
491
494
  st.chengyuJielong.hasTriggeredAutoPlay = false;
495
+ setupAutoStop(session, st.chengyuJielong, cfg, d.chengyuJielong.autoStop, cfg.chengyuJielong.showRank);
492
496
  scheduleAutoPlay(session, key, st);
493
497
  }
494
498
  }
@@ -501,12 +505,14 @@ function apply(ctx, cfg) {
501
505
  }
502
506
  catch (error) {
503
507
  await session.send(d.chengyuJielong.apiError);
508
+ setupAutoStop(session, st.chengyuJielong, cfg, d.chengyuJielong.autoStop, cfg.chengyuJielong.showRank);
504
509
  scheduleAutoPlay(session, key, st);
505
510
  }
506
511
  gameStates.set(key, st);
507
512
  return;
508
513
  }
509
514
  if (cfg.enableChengyuImage && st.chengyuImage.started) {
515
+ clearGameTimer(st.chengyuImage);
510
516
  if (!st.chengyuImage.players[playerKey]) {
511
517
  st.chengyuImage.players[playerKey] = getPlayerData(session);
512
518
  st.chengyuImage.participants.push(playerKey);
@@ -531,10 +537,13 @@ function apply(ctx, cfg) {
531
537
  catch (error) {
532
538
  await session.send(d.chengyuImage.hintError);
533
539
  }
540
+ setupAutoStop(session, st.chengyuImage, cfg, d.chengyuImage.autoStop, cfg.chengyuImage.showRank);
534
541
  return;
535
542
  }
536
- if (!isFourCharIdiom(content))
543
+ if (!isFourCharIdiom(content)) {
544
+ setupAutoStop(session, st.chengyuImage, cfg, d.chengyuImage.autoStop, cfg.chengyuImage.showRank);
537
545
  return next();
546
+ }
538
547
  try {
539
548
  const r = await axios_1.default.get('https://jcy.lvlong.xyz/api/api/chengyu.php', {
540
549
  params: {
@@ -554,6 +563,7 @@ function apply(ctx, cfg) {
554
563
  st.chengyuImage.currentRound += 1;
555
564
  if (st.chengyuImage.currentRound > st.chengyuImage.totalRounds) {
556
565
  st.chengyuImage.started = false;
566
+ clearGameTimer(st.chengyuImage);
557
567
  await session.send(reply);
558
568
  await session.send(d.chengyuImage.gameEnd);
559
569
  if (cfg.chengyuImage.showRank && !isPrivate(session)) {
@@ -585,6 +595,7 @@ function apply(ctx, cfg) {
585
595
  else {
586
596
  await session.send('获取下一题失败,比赛结束!');
587
597
  st.chengyuImage.started = false;
598
+ clearGameTimer(st.chengyuImage);
588
599
  if (cfg.chengyuImage.showRank && !isPrivate(session)) {
589
600
  const rankText = generateRankText(st.chengyuImage.players);
590
601
  await session.send(rankText);
@@ -605,10 +616,12 @@ function apply(ctx, cfg) {
605
616
  catch (error) {
606
617
  await session.send(d.chengyuImage.apiError);
607
618
  }
619
+ setupAutoStop(session, st.chengyuImage, cfg, d.chengyuImage.autoStop, cfg.chengyuImage.showRank);
608
620
  gameStates.set(key, st);
609
621
  return;
610
622
  }
611
623
  if (cfg.enableWzHero && st.wzHero.started) {
624
+ clearGameTimer(st.wzHero);
612
625
  if (!st.wzHero.players[playerKey]) {
613
626
  st.wzHero.players[playerKey] = getPlayerData(session);
614
627
  st.wzHero.participants.push(playerKey);
@@ -628,6 +641,7 @@ function apply(ctx, cfg) {
628
641
  st.wzHero.currentRound += 1;
629
642
  if (st.wzHero.currentRound > st.wzHero.totalRounds) {
630
643
  st.wzHero.started = false;
644
+ clearGameTimer(st.wzHero);
631
645
  await session.send(text);
632
646
  await session.send(d.wzHero.gameEnd);
633
647
  if (cfg.wzHero.showRank && !isPrivate(session)) {
@@ -664,23 +678,28 @@ function apply(ctx, cfg) {
664
678
  catch (error) {
665
679
  await session.send(d.wzHero.apiError);
666
680
  }
681
+ setupAutoStop(session, st.wzHero, cfg, d.wzHero.autoStop, cfg.wzHero.showRank);
667
682
  gameStates.set(key, st);
668
683
  return;
669
684
  }
670
685
  if (cfg.enableNumberGuess && st.guessNumber.started) {
686
+ clearGameTimer(st.guessNumber);
671
687
  if (!st.guessNumber.players[playerKey]) {
672
688
  st.guessNumber.players[playerKey] = getPlayerData(session);
673
689
  st.guessNumber.participants.push(playerKey);
674
690
  }
675
691
  const num = parseInt(content);
676
- if (isNaN(num))
692
+ if (isNaN(num)) {
693
+ setupAutoStop(session, st.guessNumber, cfg, d.guessNumber.autoStop, cfg.guessNumber.showRank);
677
694
  return next();
695
+ }
678
696
  if (num === st.guessNumber.target) {
679
697
  await session.send(d.guessNumber.correct);
680
698
  addScoreToOthers(st.guessNumber.players, playerKey);
681
699
  st.guessNumber.currentRound += 1;
682
700
  if (st.guessNumber.currentRound > st.guessNumber.totalRounds) {
683
701
  st.guessNumber.started = false;
702
+ clearGameTimer(st.guessNumber);
684
703
  await session.send(d.guessNumber.gameEnd);
685
704
  if (cfg.guessNumber.showRank && !isPrivate(session)) {
686
705
  const rankText = generateRankText(st.guessNumber.players);
@@ -700,6 +719,7 @@ function apply(ctx, cfg) {
700
719
  else {
701
720
  await session.send(d.guessNumber.tooBig.replace('{0}', num.toString()).replace('{1}', st.guessNumber.currentMin.toString()));
702
721
  }
722
+ setupAutoStop(session, st.guessNumber, cfg, d.guessNumber.autoStop, cfg.guessNumber.showRank);
703
723
  gameStates.set(key, st);
704
724
  return;
705
725
  }
@@ -741,6 +761,7 @@ function apply(ctx, cfg) {
741
761
  t = d.wzHero.start.replace('{0}', cfg.wzHero.totalRounds.toString()) + '\n' + t;
742
762
  st.wzHero.currentAnswer = dt.hint || '';
743
763
  await s.send(t);
764
+ setupAutoStop(s, st.wzHero, cfg, d.wzHero.autoStop, cfg.wzHero.showRank);
744
765
  }
745
766
  else {
746
767
  await s.send(dt.message || '开始失败');
@@ -753,6 +774,7 @@ function apply(ctx, cfg) {
753
774
  }
754
775
  else if (action === 'stop') {
755
776
  st.wzHero.started = false;
777
+ clearGameTimer(st.wzHero);
756
778
  await s.send(d.wzHero.stop);
757
779
  if (cfg.wzHero.showRank && !isPrivate(s)) {
758
780
  const rankText = generateRankText(st.wzHero.players);
@@ -799,6 +821,7 @@ function apply(ctx, cfg) {
799
821
  await s.send(d.chengyuImage.start.replace('{0}', cfg.chengyuImage.totalRounds.toString()));
800
822
  await s.send(`第 1 题来啦!\n请从选项中选择4个字组成成语\n选项:${dt.data.question.options.join('、')}`);
801
823
  await s.send(koishi_1.segment.image(dt.data.question.image_url));
824
+ setupAutoStop(s, st.chengyuImage, cfg, d.chengyuImage.autoStop, cfg.chengyuImage.showRank);
802
825
  }
803
826
  else {
804
827
  await s.send('获取题目失败,请稍后再试');
@@ -812,6 +835,7 @@ function apply(ctx, cfg) {
812
835
  }
813
836
  else if (action === 'stop') {
814
837
  st.chengyuImage.started = false;
838
+ clearGameTimer(st.chengyuImage);
815
839
  await s.send(d.chengyuImage.stop);
816
840
  if (cfg.chengyuImage.showRank && !isPrivate(s)) {
817
841
  const rankText = generateRankText(st.chengyuImage.players);
@@ -875,6 +899,7 @@ function apply(ctx, cfg) {
875
899
  catch (error) {
876
900
  await s.send(d.chengyuJielong.apiError);
877
901
  }
902
+ setupAutoStop(s, st.chengyuJielong, cfg, d.chengyuJielong.autoStop, cfg.chengyuJielong.showRank);
878
903
  gameStates.set(key, st);
879
904
  scheduleAutoPlay(s, key, st);
880
905
  }
@@ -917,9 +942,11 @@ function apply(ctx, cfg) {
917
942
  gameStates.set(key, st);
918
943
  await s.send(d.guessNumber.start.replace('{0}', cfg.guessNumber.totalRounds.toString()));
919
944
  await s.send(`猜数字范围:${st.guessNumber.currentMin} - ${st.guessNumber.currentMax},开始你的猜测吧!`);
945
+ setupAutoStop(s, st.guessNumber, cfg, d.guessNumber.autoStop, cfg.guessNumber.showRank);
920
946
  }
921
947
  else if (action === 'stop') {
922
948
  st.guessNumber.started = false;
949
+ clearGameTimer(st.guessNumber);
923
950
  await s.send(d.guessNumber.stop);
924
951
  if (cfg.guessNumber.showRank && !isPrivate(s)) {
925
952
  const rankText = generateRankText(st.guessNumber.players);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-game-mini",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "一款包含猜数字、猜王者英雄、看图猜成语、成语接龙的多功能小游戏插件",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",