koishi-plugin-cocoyyy-console 1.1.2 → 1.1.3-alpha.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.js CHANGED
@@ -1701,9 +1701,11 @@ async function randomHImg(keywords, ...args) {
1701
1701
  params.append("excludeAI", "true");
1702
1702
  params.append("size", "regular");
1703
1703
  const random = new import_koishi12.Random(() => Math.random());
1704
- if (r18.toLowerCase() === "r18") {
1705
- params.append("r18", "1");
1706
- tags = keywords?.slice(1);
1704
+ if (r18) {
1705
+ if (r18.toLowerCase() === "r18") {
1706
+ params.append("r18", "1");
1707
+ tags = keywords?.slice(1);
1708
+ }
1707
1709
  }
1708
1710
  if (args[0]) {
1709
1711
  params.append("num", args[0].toString());
@@ -1718,7 +1720,8 @@ async function randomHImg(keywords, ...args) {
1718
1720
  for (let item of res.data?.data) {
1719
1721
  let author = item.author;
1720
1722
  let url = item.urls.regular;
1721
- result_content += "作者: " + author + "\n" + import_koishi12.h.image(url) + "\n";
1723
+ let tags2 = item.tags;
1724
+ result_content += "作者: " + author + "\n标签: \n[" + tags2.join(",") + "]\n" + import_koishi12.h.image(url) + "\n";
1722
1725
  }
1723
1726
  return {
1724
1727
  result: result_content,
@@ -1745,6 +1748,7 @@ function registerHImgCommands(ctx) {
1745
1748
  if (!dev_mode) {
1746
1749
  if (!is_at_bot(session)) return;
1747
1750
  }
1751
+ await session.send("已收到,正在随机...");
1748
1752
  const tag = args;
1749
1753
  if (!tag) return "请提供正确格式,如:@bot setu [是否r-18] [标签1] [标签2]";
1750
1754
  let exec = await randomHImg(tag);
@@ -1756,6 +1760,7 @@ function registerHImgCommands(ctx) {
1756
1760
  if (!dev_mode) {
1757
1761
  if (!is_at_bot(session)) return;
1758
1762
  }
1763
+ await session.send("已收到,正在随机...");
1759
1764
  if (!args) return "请提供正确格式,如:@bot setus [数量(1-10)] [是否r-18] [标签1] [标签2]";
1760
1765
  try {
1761
1766
  const num = parseInt(args[0]);
@@ -2184,11 +2189,33 @@ __name(registerKuroCommands, "registerKuroCommands");
2184
2189
  var import_koishi13 = require("koishi");
2185
2190
 
2186
2191
  // src/services/game_func/game_service.ts
2192
+ var gameTimeout = null;
2193
+ var GAME_TIMEOUT_MINUTES = -1;
2194
+ function setGameTimeoutMinutes(minutes) {
2195
+ GAME_TIMEOUT_MINUTES = minutes;
2196
+ }
2197
+ __name(setGameTimeoutMinutes, "setGameTimeoutMinutes");
2198
+ function setGameTimeout(func, timeoutMs) {
2199
+ if (func === null) {
2200
+ clearGameTimeout();
2201
+ return;
2202
+ }
2203
+ const timeout = timeoutMs !== void 0 ? timeoutMs : GAME_TIMEOUT_MINUTES * 60 * 1e3;
2204
+ gameTimeout = setTimeout(func, timeout);
2205
+ }
2206
+ __name(setGameTimeout, "setGameTimeout");
2207
+ function clearGameTimeout() {
2208
+ if (gameTimeout) {
2209
+ clearTimeout(gameTimeout);
2210
+ gameTimeout = null;
2211
+ }
2212
+ }
2213
+ __name(clearGameTimeout, "clearGameTimeout");
2214
+
2215
+ // src/services/game_func/games/game_turtlesoup_service.ts
2187
2216
  var is_gaming = false;
2188
2217
  var chatPrompt = [];
2189
2218
  var chatHistory2 = [];
2190
- var gameTimeout = null;
2191
- var GAME_TIMEOUT_MINUTES = -1;
2192
2219
  async function startSituationPuzzle(config) {
2193
2220
  chatPrompt = [];
2194
2221
  chatHistory2 = [];
@@ -2202,28 +2229,18 @@ async function startSituationPuzzle(config) {
2202
2229
  }
2203
2230
  chatPrompt.push({ role: "assistant", content: [{ type: "text", text: resp }] });
2204
2231
  is_gaming = true;
2205
- if (GAME_TIMEOUT_MINUTES <= 0) {
2206
- GAME_TIMEOUT_MINUTES = local_config2?.game?.timeout_minutes || 10;
2207
- }
2208
2232
  clearGameTimeout();
2209
- gameTimeout = setTimeout(async () => {
2233
+ setGameTimeout(async () => {
2210
2234
  logger.info(`[SituationPuzzle Timeout]: 游戏已超时(${GAME_TIMEOUT_MINUTES}分钟),自动结束游戏`);
2211
2235
  is_gaming = false;
2212
2236
  chatPrompt = [];
2213
2237
  chatHistory2 = [];
2214
- gameTimeout = null;
2238
+ setGameTimeout(null);
2215
2239
  }, GAME_TIMEOUT_MINUTES * 60 * 1e3);
2216
2240
  logger.info("[startSituationPuzzle Info]: ai api send message success");
2217
2241
  return resp;
2218
2242
  }
2219
2243
  __name(startSituationPuzzle, "startSituationPuzzle");
2220
- function clearGameTimeout() {
2221
- if (gameTimeout) {
2222
- clearTimeout(gameTimeout);
2223
- gameTimeout = null;
2224
- }
2225
- }
2226
- __name(clearGameTimeout, "clearGameTimeout");
2227
2244
  async function questSituationPuzzle(content, config) {
2228
2245
  chatHistory2 = [...chatPrompt];
2229
2246
  chatHistory2.push({ role: "user", content: [{ type: "text", text: content }] });
@@ -2237,7 +2254,7 @@ async function questSituationPuzzle(content, config) {
2237
2254
  is_gaming = false;
2238
2255
  chatPrompt = [];
2239
2256
  chatHistory2 = [];
2240
- gameTimeout = null;
2257
+ clearGameTimeout();
2241
2258
  }
2242
2259
  return resp;
2243
2260
  }
@@ -2251,6 +2268,7 @@ async function endSituationPuzzle(config) {
2251
2268
  logger.error("[endSituationPuzzle Error]: " + err);
2252
2269
  return null;
2253
2270
  }
2271
+ clearGameTimeout();
2254
2272
  logger.info("[endSituationPuzzle Info]: ai api send message success");
2255
2273
  return resp;
2256
2274
  }
@@ -2269,11 +2287,96 @@ async function rerollSituationPuzzle(config) {
2269
2287
  chatPrompt.push({ role: "system", content: [{ type: "text", text: prompt_start }] });
2270
2288
  chatPrompt.push({ role: "assistant", content: [{ type: "text", text: resp }] });
2271
2289
  is_gaming = true;
2290
+ clearGameTimeout();
2291
+ setGameTimeout(async () => {
2292
+ logger.info(`[SituationPuzzle Timeout]: 游戏已超时(${GAME_TIMEOUT_MINUTES}分钟),自动结束游戏`);
2293
+ is_gaming = false;
2294
+ chatPrompt = [];
2295
+ chatHistory2 = [];
2296
+ setGameTimeout(null);
2297
+ }, GAME_TIMEOUT_MINUTES * 60 * 1e3);
2272
2298
  logger.info("[rerollSituationPuzzle Info]: ai api send message success");
2273
2299
  return resp;
2274
2300
  }
2275
2301
  __name(rerollSituationPuzzle, "rerollSituationPuzzle");
2276
2302
 
2303
+ // src/services/game_func/games/game_24_service.ts
2304
+ var EPSILON = 1e-6;
2305
+ function solve24(nodes) {
2306
+ if (nodes.length === 1) {
2307
+ return Math.abs(nodes[0].value - 24) < EPSILON ? nodes[0].expr : null;
2308
+ }
2309
+ for (let i = 0; i < nodes.length; i++) {
2310
+ for (let j = 0; j < nodes.length; j++) {
2311
+ if (i === j) continue;
2312
+ const rest = [];
2313
+ for (let k = 0; k < nodes.length; k++) {
2314
+ if (k !== i && k !== j) rest.push(nodes[k]);
2315
+ }
2316
+ const a = nodes[i];
2317
+ const b = nodes[j];
2318
+ const candidates = [
2319
+ { value: a.value + b.value, expr: `(${a.expr} + ${b.expr})` },
2320
+ { value: a.value - b.value, expr: `(${a.expr} - ${b.expr})` },
2321
+ { value: a.value * b.value, expr: `(${a.expr} * ${b.expr})` }
2322
+ ];
2323
+ if (Math.abs(b.value) > EPSILON) {
2324
+ candidates.push({
2325
+ value: a.value / b.value,
2326
+ expr: `(${a.expr} / ${b.expr})`
2327
+ });
2328
+ }
2329
+ for (const c of candidates) {
2330
+ rest.push(c);
2331
+ const res = solve24(rest);
2332
+ if (res) return res;
2333
+ rest.pop();
2334
+ }
2335
+ }
2336
+ }
2337
+ return null;
2338
+ }
2339
+ __name(solve24, "solve24");
2340
+ function generate24Puzzle() {
2341
+ while (true) {
2342
+ const numbers = Array.from(
2343
+ { length: 4 },
2344
+ () => Math.floor(Math.random() * 13) + 1
2345
+ );
2346
+ const nodes = numbers.map((n) => ({
2347
+ value: n,
2348
+ expr: n.toString()
2349
+ }));
2350
+ const solution = solve24(nodes);
2351
+ if (solution) {
2352
+ return {
2353
+ numbers,
2354
+ solution: `${solution} = 24`
2355
+ };
2356
+ }
2357
+ }
2358
+ }
2359
+ __name(generate24Puzzle, "generate24Puzzle");
2360
+ var currentPuzzle = null;
2361
+ function newPuzzle() {
2362
+ currentPuzzle = generate24Puzzle();
2363
+ clearGameTimeout();
2364
+ setGameTimeout(async () => {
2365
+ logger.info(`[Puzzle24 Timeout]: 24点游戏已超时(${GAME_TIMEOUT_MINUTES}分钟),自动结束游戏`);
2366
+ currentPuzzle = null;
2367
+ setGameTimeout(null);
2368
+ }, GAME_TIMEOUT_MINUTES * 60 * 1e3);
2369
+ return currentPuzzle.numbers;
2370
+ }
2371
+ __name(newPuzzle, "newPuzzle");
2372
+ function getSolution() {
2373
+ if (!currentPuzzle) {
2374
+ throw new Error("尚未生成题目");
2375
+ }
2376
+ return currentPuzzle.solution;
2377
+ }
2378
+ __name(getSolution, "getSolution");
2379
+
2277
2380
  // src/services/game_func/game_command.ts
2278
2381
  var local_config2 = null;
2279
2382
  var is_thinking = false;
@@ -2283,6 +2386,11 @@ var gameList = [
2283
2386
  name: "AI海龟汤",
2284
2387
  description: "AI海龟汤游戏,通过AI回答问题,猜测答案",
2285
2388
  command: "puzzle"
2389
+ },
2390
+ {
2391
+ name: "24点题目生成",
2392
+ description: "24点游戏,生成24点题目,无回复功能",
2393
+ command: "24"
2286
2394
  }
2287
2395
  ];
2288
2396
  var commandList = [
@@ -2300,6 +2408,9 @@ function registerGameCommands(ctx, game_config, ai_config) {
2300
2408
  } else {
2301
2409
  logger.error("[loadYamlConfig Error]: 未配置 Game 配置文件路径");
2302
2410
  }
2411
+ if (GAME_TIMEOUT_MINUTES <= 0) {
2412
+ setGameTimeoutMinutes(local_config2?.game?.timeout_minutes || 10);
2413
+ }
2303
2414
  ctx.command("gamelist", "查看所有游戏").action(async ({}) => {
2304
2415
  return `[所有命令都需要@bot]
2305
2416
  游戏列表:
@@ -2316,12 +2427,20 @@ function registerGameCommands(ctx, game_config, ai_config) {
2316
2427
  if (!game) return "请提供正确格式,如:@bot startgame [游戏]";
2317
2428
  try {
2318
2429
  is_thinking = true;
2430
+ let resp_text = "";
2319
2431
  switch (game) {
2432
+ // 海龟汤
2320
2433
  case gameList[0].command:
2321
2434
  await session.send("正在开始游戏...后续问答请参照格式:@bot [问题]\n需要刷新谜题时发送:@bot reroll");
2322
- const resp = await startSituationPuzzle(ai_config);
2435
+ resp_text = await startSituationPuzzle(ai_config);
2323
2436
  now_game = game;
2324
- return checkResp(resp);
2437
+ return checkResp(resp_text);
2438
+ // 24点生成
2439
+ case gameList[1].command:
2440
+ const numbers = await newPuzzle();
2441
+ resp_text = `题目: ${numbers.join(" ")}`;
2442
+ now_game = game;
2443
+ return checkResp(resp_text);
2325
2444
  default:
2326
2445
  return `游戏不存在,请输入gamelist查看所有游戏`;
2327
2446
  }
@@ -2329,6 +2448,31 @@ function registerGameCommands(ctx, game_config, ai_config) {
2329
2448
  is_thinking = false;
2330
2449
  }
2331
2450
  });
2451
+ ctx.command("endgame", "结束游戏").action(async ({ session }) => {
2452
+ if (!dev_mode) {
2453
+ if (!is_at_bot(session)) return;
2454
+ }
2455
+ if (!now_game) return "未开始游戏";
2456
+ if (now_game == gameList[0].command) return;
2457
+ if (is_thinking) return "正在思考,请耐心等待...";
2458
+ try {
2459
+ is_thinking = true;
2460
+ await session.send("正在结束游戏...");
2461
+ let resp_text = "";
2462
+ switch (now_game) {
2463
+ case gameList[1].command:
2464
+ const resp = await getSolution();
2465
+ resp_text = `答案: ${getSolution()}`;
2466
+ now_game = null;
2467
+ clearGameTimeout();
2468
+ return checkResp(resp);
2469
+ default:
2470
+ return;
2471
+ }
2472
+ } finally {
2473
+ is_thinking = false;
2474
+ }
2475
+ });
2332
2476
  }
2333
2477
  __name(registerGameCommands, "registerGameCommands");
2334
2478
  function registerGameMiddleware(ctx, ai_config) {
@@ -0,0 +1,3 @@
1
+ declare const EPSILON = 0.000001;
2
+ declare function canMake24(nums: number[]): boolean;
3
+ declare function compute(a: number, b: number): number[];
@@ -1,7 +1,6 @@
1
- import { AiAPIConfig } from "../../utils/config";
2
- declare let is_gaming: boolean;
3
- declare function startSituationPuzzle(config: AiAPIConfig): Promise<string>;
4
- declare function questSituationPuzzle(content: string, config: AiAPIConfig): Promise<string>;
5
- declare function endSituationPuzzle(config: AiAPIConfig): Promise<string>;
6
- declare function rerollSituationPuzzle(config: AiAPIConfig): Promise<string>;
7
- export { is_gaming, startSituationPuzzle, questSituationPuzzle, endSituationPuzzle, rerollSituationPuzzle };
1
+ declare let gameTimeout: NodeJS.Timeout | null;
2
+ declare var GAME_TIMEOUT_MINUTES: number;
3
+ declare function setGameTimeoutMinutes(minutes: number): void;
4
+ declare function setGameTimeout(func: (() => void) | null, timeoutMs?: number): void;
5
+ declare function clearGameTimeout(): void;
6
+ export { gameTimeout, GAME_TIMEOUT_MINUTES, setGameTimeoutMinutes, setGameTimeout, clearGameTimeout };
@@ -0,0 +1,3 @@
1
+ declare function newPuzzle(): number[];
2
+ declare function getSolution(): string;
3
+ export { newPuzzle, getSolution };
@@ -0,0 +1,7 @@
1
+ import { AiAPIConfig } from "../../../utils/config";
2
+ declare let is_gaming: boolean;
3
+ declare function startSituationPuzzle(config: AiAPIConfig): Promise<string>;
4
+ declare function questSituationPuzzle(content: string, config: AiAPIConfig): Promise<string>;
5
+ declare function endSituationPuzzle(config: AiAPIConfig): Promise<string>;
6
+ declare function rerollSituationPuzzle(config: AiAPIConfig): Promise<string>;
7
+ export { is_gaming, startSituationPuzzle, questSituationPuzzle, endSituationPuzzle, rerollSituationPuzzle };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-cocoyyy-console",
3
3
  "description": "自用koishi插件,功能包含复读,记录黑历史,*人等",
4
- "version": "1.1.2",
4
+ "version": "1.1.3-alpha.2",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [