@azimiao/koishi-plugin-cafe-bot-exp 0.0.8 → 0.0.10

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.
@@ -0,0 +1,3 @@
1
+ import { HTTP } from "koishi";
2
+ declare function downloadFileIfNotExist(http: HTTP, downloadUrl: string, savePath: string, forceUpdate: boolean): Promise<boolean>;
3
+ export default downloadFileIfNotExist;
@@ -4,6 +4,7 @@
4
4
  import { Schema } from "koishi";
5
5
  export interface CafeBotDrawConfig {
6
6
  baseDataUrl: string;
7
+ forceUpdateDataWhenLoad: boolean;
7
8
  ImageServer: string;
8
9
  ImageServerAuth: string;
9
10
  MinCount: number;
package/lib/index.d.ts CHANGED
@@ -5,4 +5,5 @@ import { Context } from 'koishi';
5
5
  import { Config } from './config';
6
6
  export declare const name = "cafe-bot-exp";
7
7
  export * from "./config";
8
+ export declare const inject: string[];
8
9
  export declare function apply(ctx: Context, config: Config): Promise<void>;
package/lib/index.js CHANGED
@@ -32,6 +32,7 @@ var src_exports = {};
32
32
  __export(src_exports, {
33
33
  Config: () => Config4,
34
34
  apply: () => apply4,
35
+ inject: () => inject3,
35
36
  name: () => name4
36
37
  });
37
38
  module.exports = __toCommonJS(src_exports);
@@ -44,9 +45,8 @@ __export(draw_exports, {
44
45
  inject: () => inject,
45
46
  name: () => name
46
47
  });
47
- var fs = __toESM(require("fs/promises"));
48
+ var fs2 = __toESM(require("fs/promises"));
48
49
  var path = __toESM(require("path"));
49
- var import_fs_promises = __toESM(require("fs.promises.exists"));
50
50
 
51
51
  // src/common/DailySeededName.ts
52
52
  function DailySeededName(uid) {
@@ -257,6 +257,7 @@ var html_default = HtmlCreator;
257
257
  var import_koishi = require("koishi");
258
258
  var CafeBotDrawConfig = import_koishi.Schema.object({
259
259
  baseDataUrl: import_koishi.Schema.string().description("数据链接").default("https://www.azimiao.com/kiseki.json"),
260
+ forceUpdateDataWhenLoad: import_koishi.Schema.boolean().description("插件加载时强制刷新数据").default(false),
260
261
  ImageServer: import_koishi.Schema.string().description("图像生成服务器").default("http://127.0.0.1:3000/screenshot"),
261
262
  ImageServerAuth: import_koishi.Schema.string().description("图像生成服务器密码").default(""),
262
263
  MinCount: import_koishi.Schema.number().description("最小抽取次数").default(0),
@@ -281,6 +282,29 @@ function At(argv, newLine = true) {
281
282
  }
282
283
  __name(At, "At");
283
284
 
285
+ // src/common/downloadTool.ts
286
+ var fs = __toESM(require("fs/promises"));
287
+ var import_fs_promises = __toESM(require("fs.promises.exists"));
288
+ async function downloadFileIfNotExist(http, downloadUrl, savePath, forceUpdate) {
289
+ var result = true;
290
+ var exist = await (0, import_fs_promises.default)(savePath);
291
+ console.log(savePath);
292
+ var needDownload = !exist || forceUpdate;
293
+ if (needDownload) {
294
+ console.log(`Data ${savePath} not exist or forceUpdate,try download from ${downloadUrl}`);
295
+ result = await http.get(downloadUrl).then(async (res) => {
296
+ await fs.writeFile(savePath, JSON.stringify(res));
297
+ console.log(`Data ${downloadUrl} download ok,save path ${savePath}`);
298
+ return true;
299
+ }).catch((e) => {
300
+ return false;
301
+ });
302
+ }
303
+ return result;
304
+ }
305
+ __name(downloadFileIfNotExist, "downloadFileIfNotExist");
306
+ var downloadTool_default = downloadFileIfNotExist;
307
+
284
308
  // src/draw/index.ts
285
309
  var name = "cafe-bot-exp.draw";
286
310
  var inject = ["http"];
@@ -298,21 +322,15 @@ function shuffleWithCustomRandom(array, rand) {
298
322
  }
299
323
  __name(shuffleWithCustomRandom, "shuffleWithCustomRandom");
300
324
  var star2Name = null;
301
- async function downloadCardDataIfNotExist(ctx, config) {
302
- if (cafebotCardData != null && cafebotCardData.length > 0) {
325
+ async function downloadCardDataIfNotExist(ctx, config, forceUpdate) {
326
+ if (!forceUpdate && cafebotCardData != null && cafebotCardData.length > 0) {
303
327
  return;
304
328
  }
305
329
  const root = path.join(ctx.baseDir, "data", `${name}-data`);
306
- await fs.mkdir(root, { recursive: true });
330
+ await fs2.mkdir(root, { recursive: true });
307
331
  const cardFilePath = path.join(root, "card.json");
308
- var a = await (0, import_fs_promises.default)(cardFilePath);
309
- if (!a) {
310
- console.log(`card data ${cardFilePath} not exist,try download from ${config.baseDataUrl}`);
311
- await ctx.http.get(config.baseDataUrl).then(async (res) => {
312
- await fs.writeFile(cardFilePath, JSON.stringify(res));
313
- });
314
- }
315
- var fileContent = await fs.readFile(cardFilePath, "utf-8");
332
+ await downloadTool_default(ctx.http, config.baseDataUrl, cardFilePath, forceUpdate);
333
+ var fileContent = await fs2.readFile(cardFilePath, "utf-8");
316
334
  try {
317
335
  cafebotCardData = JSON.parse(fileContent);
318
336
  } catch (error) {
@@ -322,7 +340,7 @@ async function downloadCardDataIfNotExist(ctx, config) {
322
340
  }
323
341
  __name(downloadCardDataIfNotExist, "downloadCardDataIfNotExist");
324
342
  async function getCards(seed, ctx, config) {
325
- await downloadCardDataIfNotExist(ctx, config);
343
+ await downloadCardDataIfNotExist(ctx, config, false);
326
344
  let minCount = config.MinCount;
327
345
  let maxCount = config.MaxCount;
328
346
  let randomer = new PseudoRandom_default(seed);
@@ -377,6 +395,7 @@ async function getCards(seed, ctx, config) {
377
395
  }
378
396
  __name(getCards, "getCards");
379
397
  async function apply(ctx, config) {
398
+ await downloadCardDataIfNotExist(ctx, config, config.forceUpdateDataWhenLoad);
380
399
  ctx.command("轨迹抽卡/给我抽", "进行每日抽卡").action(async (argv, _) => {
381
400
  let seed = DailySeededName(argv.session.userId);
382
401
  console.log(`getcard for ${seed}`);
@@ -448,6 +467,7 @@ async function apply(ctx, config) {
448
467
  await argv.session?.cancelQueued();
449
468
  console.log(`getcard img for ${seed} send ok~`);
450
469
  }).catch(async (e) => {
470
+ console.log(e);
451
471
  delete requestWebCache[seed];
452
472
  await argv.session?.sendQueued(`${At(argv)}图片生成失败`);
453
473
  await argv.session?.cancelQueued();
@@ -536,11 +556,16 @@ __export(quiz_exports, {
536
556
  inject: () => inject2,
537
557
  name: () => name3
538
558
  });
559
+ var import_koishi6 = require("koishi");
560
+ var fs3 = __toESM(require("fs/promises"));
561
+ var path2 = __toESM(require("path"));
539
562
 
540
563
  // src/quiz/config.ts
541
564
  var import_koishi5 = require("koishi");
542
565
  var CafeBotQuizConfig = import_koishi5.Schema.object({
543
- baseQuizUrl: import_koishi5.Schema.string().description("答题数据 Url").default("https://www.azimiao.com/quiz.json"),
566
+ baseQuizUrl: import_koishi5.Schema.string().description("答题列表 Url").default("https://www.azimiao.com/quiz.json"),
567
+ forceUpdateWhenLoad: import_koishi5.Schema.boolean().description("插件加载时强制更新题目列表").default(false),
568
+ answerTimeout: import_koishi5.Schema.number().description("回答超时时间(秒)").default(300),
544
569
  maxQuizPerDay: import_koishi5.Schema.number().description("每日单人最大答题数").default(5),
545
570
  redisServer: import_koishi5.Schema.string().description("redis服务器地址"),
546
571
  // TODO: 计划: 分数存储使用 sqlite, 临时数据(如当前题目)用内存就行了?
@@ -549,7 +574,7 @@ var CafeBotQuizConfig = import_koishi5.Schema.object({
549
574
 
550
575
  // src/quiz/index.ts
551
576
  var name3 = "cafe-bot-exp.quiz";
552
- var inject2 = ["http"];
577
+ var inject2 = ["http", "cache"];
553
578
  var Config3 = CafeBotQuizConfig;
554
579
  var validOptions = {
555
580
  "A": 0,
@@ -565,27 +590,100 @@ var validOptions = {
565
590
  "3": 2,
566
591
  "4": 3
567
592
  };
593
+ var quizDataIds = [];
594
+ var quizDataSet = {};
595
+ async function downloadQuitDataIfNotExist(ctx, config, forceUpdate) {
596
+ if (!forceUpdate && quizDataIds != null && quizDataIds.length > 0) {
597
+ return;
598
+ }
599
+ const root = path2.join(ctx.baseDir, "data", `${name3}-data`);
600
+ await fs3.mkdir(root, { recursive: true });
601
+ const quizFilePath = path2.join(root, "quiz.json");
602
+ await downloadTool_default(ctx.http, config.baseQuizUrl, quizFilePath, forceUpdate);
603
+ var fileContent = await fs3.readFile(quizFilePath, "utf-8");
604
+ try {
605
+ var a = JSON.parse(fileContent);
606
+ var quizData = a.filter((item) => item.question.t === "MCWithTextOnly");
607
+ quizData.forEach((element) => {
608
+ quizDataIds.push(element.question.id);
609
+ quizDataSet[element.question.id] = element;
610
+ });
611
+ } catch (error) {
612
+ throw new Error("quiz read error");
613
+ }
614
+ return;
615
+ }
616
+ __name(downloadQuitDataIfNotExist, "downloadQuitDataIfNotExist");
568
617
  async function apply3(ctx, config) {
618
+ await downloadQuitDataIfNotExist(ctx, config, config.forceUpdateWhenLoad);
569
619
  ctx.command("轨迹答题/出题").action(async (argv, _) => {
570
- await argv.session?.send("开发中");
620
+ await downloadQuitDataIfNotExist(ctx, config, false);
621
+ var lastQuestion = await ctx.cache.get("question", argv.session.userId);
622
+ if (lastQuestion) {
623
+ await argv.session?.send("上一题还没有回答哦~");
624
+ return;
625
+ }
626
+ if (quizDataIds == null || quizDataIds.length <= 0) {
627
+ await argv.session?.send(`${At(argv)}题目数据获取失败~`);
628
+ return;
629
+ }
630
+ var randomId = import_koishi6.Random.pick(quizDataIds);
631
+ var qItem = quizDataSet[randomId];
632
+ var qOptions = import_koishi6.Random.shuffle(qItem.options);
633
+ var answerIndex = -1;
634
+ for (var i = 0; i < qOptions.length; i++) {
635
+ if (qOptions[i].oid === qItem.a) {
636
+ answerIndex = i;
637
+ break;
638
+ }
639
+ }
640
+ await ctx.cache.set("question", argv.session.userId, { question: randomId, answer: answerIndex }, config.answerTimeout * 1e3);
641
+ var messageQuestion = `${qItem.question.img.length > 0 ? `<img src="${qItem.question.img}"/>` : ``}${At(argv)}${qItem.question.s}
642
+ A.${qOptions[0].s}
643
+ B.${qOptions[1].s}
644
+ C.${qOptions[2].s}
645
+ D.${qOptions[3].s}`;
646
+ await argv.session?.send(messageQuestion);
571
647
  return;
572
648
  });
573
- ctx.command("轨迹答题/回答 <answer:text>", `使用"回答+选项"回答问题,如"回答 A"`).action(async (argv, answer) => {
574
- await argv.session?.send("开发中");
575
- return;
649
+ ctx.command("轨迹答题/回答 <answer:text>", `回复"回答+空格+选项"回答问题哦,如"回答 A"`).action(async (argv, answer) => {
650
+ await downloadQuitDataIfNotExist(ctx, config, false);
651
+ var lastQuestion = await ctx.cache.get("question", argv.session.userId);
652
+ if (!lastQuestion) {
653
+ await argv.session?.send("你还未开始答题或已超时哦~");
654
+ return;
655
+ }
576
656
  let selectNumber = -1;
577
657
  if (answer in validOptions) {
578
658
  selectNumber = validOptions[answer];
579
659
  } else {
580
- argv.session?.send(`${At(argv)}回答错误~`);
660
+ await argv.session?.send(`${At(argv)}选项无效,请重新输入~`);
661
+ return;
662
+ }
663
+ await ctx.cache.delete("question", argv.session.userId);
664
+ if (quizDataIds == null || quizDataIds.length <= 0) {
665
+ await argv.session?.send(`${At(argv)}题目数据获取失败,请重新开始答题~`);
666
+ return;
667
+ }
668
+ var qItem = quizDataSet[lastQuestion.question];
669
+ if (qItem && lastQuestion.answer == selectNumber) {
670
+ await argv.session?.send(`${At(argv)}回答正确😊${qItem.explain.length > 0 ? "," + qItem.explain : ""}`);
671
+ return;
672
+ } else {
673
+ await argv.session?.send(`${At(argv)}回答错误😟${qItem.explain2.length > 0 ? "," + qItem.explain2 : ""}`);
674
+ return;
581
675
  }
582
676
  });
677
+ ctx.command("轨迹答题/答题分数").action(async (argv, _) => {
678
+ await argv.session?.send(`${At(argv)}正在开发中哦~`);
679
+ return;
680
+ });
583
681
  }
584
682
  __name(apply3, "apply");
585
683
 
586
684
  // src/config.ts
587
- var import_koishi6 = require("koishi");
588
- var Config4 = import_koishi6.Schema.intersect([
685
+ var import_koishi7 = require("koishi");
686
+ var Config4 = import_koishi7.Schema.intersect([
589
687
  CafeBotDrawConfig.collapse(),
590
688
  CafeBotQuizConfig.collapse(),
591
689
  CafeBotCatConfig.collapse()
@@ -593,6 +691,7 @@ var Config4 = import_koishi6.Schema.intersect([
593
691
 
594
692
  // src/index.ts
595
693
  var name4 = "cafe-bot-exp";
694
+ var inject3 = ["http", "cache"];
596
695
  async function apply4(ctx, config) {
597
696
  ctx.plugin(draw_exports, config);
598
697
  ctx.plugin(quiz_exports, config);
@@ -606,5 +705,6 @@ __name(apply4, "apply");
606
705
  0 && (module.exports = {
607
706
  Config,
608
707
  apply,
708
+ inject,
609
709
  name
610
710
  });
@@ -4,6 +4,8 @@
4
4
  import { Schema } from "koishi";
5
5
  export interface CafeBotQuizConfig {
6
6
  baseQuizUrl: string;
7
+ forceUpdateWhenLoad: boolean;
8
+ answerTimeout: number;
7
9
  maxQuizPerDay: number;
8
10
  redisServer: string;
9
11
  redisAuth: string;
@@ -8,4 +8,14 @@ export declare const inject: string[];
8
8
  export interface Config extends CafeBotQuizConfig {
9
9
  }
10
10
  export declare const Config: Schema<Config>;
11
+ interface QuestionCache {
12
+ question: number;
13
+ answer: number;
14
+ }
15
+ declare module '@koishijs/cache' {
16
+ interface Tables {
17
+ question: QuestionCache;
18
+ }
19
+ }
11
20
  export declare function apply(ctx: Context, config: Config): Promise<void>;
21
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@azimiao/koishi-plugin-cafe-bot-exp",
3
3
  "description": "cafe bot experiment",
4
- "version": "0.0.8",
4
+ "version": "0.0.10",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "repository": {
@@ -32,7 +32,8 @@
32
32
  },
33
33
  "service": {
34
34
  "required": [
35
- "http"
35
+ "http",
36
+ "cache"
36
37
  ]
37
38
  },
38
39
  "locales": [
@@ -44,4 +45,4 @@
44
45
  "@stdlib/random-base-uniform": "^0.2.1",
45
46
  "fs.promises.exists": "^1.1.4"
46
47
  }
47
- }
48
+ }