koishi-plugin-cocoyyy-console 1.1.3-alpha.0 → 1.1.3-alpha.3
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 +203 -21
- package/lib/services/game_func/game_24_service.d.ts +3 -0
- package/lib/services/game_func/game_service.d.ts +6 -7
- package/lib/services/game_func/games/game_24_service.d.ts +3 -0
- package/lib/services/game_func/games/game_turtlesoup_service.d.ts +7 -0
- package/package.json +1 -1
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
|
|
1705
|
-
|
|
1706
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2233
|
+
setGameTimeout(async () => {
|
|
2210
2234
|
logger.info(`[SituationPuzzle Timeout]: 游戏已超时(${GAME_TIMEOUT_MINUTES}分钟),自动结束游戏`);
|
|
2211
2235
|
is_gaming = false;
|
|
2212
2236
|
chatPrompt = [];
|
|
2213
2237
|
chatHistory2 = [];
|
|
2214
|
-
|
|
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,16 @@ async function questSituationPuzzle(content, config) {
|
|
|
2237
2254
|
is_gaming = false;
|
|
2238
2255
|
chatPrompt = [];
|
|
2239
2256
|
chatHistory2 = [];
|
|
2240
|
-
|
|
2257
|
+
clearGameTimeout();
|
|
2258
|
+
} else {
|
|
2259
|
+
clearGameTimeout();
|
|
2260
|
+
setGameTimeout(async () => {
|
|
2261
|
+
logger.info(`[SituationPuzzle Timeout]: 游戏已超时(${GAME_TIMEOUT_MINUTES}分钟),自动结束游戏`);
|
|
2262
|
+
is_gaming = false;
|
|
2263
|
+
chatPrompt = [];
|
|
2264
|
+
chatHistory2 = [];
|
|
2265
|
+
setGameTimeout(null);
|
|
2266
|
+
}, GAME_TIMEOUT_MINUTES * 60 * 1e3);
|
|
2241
2267
|
}
|
|
2242
2268
|
return resp;
|
|
2243
2269
|
}
|
|
@@ -2251,6 +2277,7 @@ async function endSituationPuzzle(config) {
|
|
|
2251
2277
|
logger.error("[endSituationPuzzle Error]: " + err);
|
|
2252
2278
|
return null;
|
|
2253
2279
|
}
|
|
2280
|
+
clearGameTimeout();
|
|
2254
2281
|
logger.info("[endSituationPuzzle Info]: ai api send message success");
|
|
2255
2282
|
return resp;
|
|
2256
2283
|
}
|
|
@@ -2269,11 +2296,120 @@ async function rerollSituationPuzzle(config) {
|
|
|
2269
2296
|
chatPrompt.push({ role: "system", content: [{ type: "text", text: prompt_start }] });
|
|
2270
2297
|
chatPrompt.push({ role: "assistant", content: [{ type: "text", text: resp }] });
|
|
2271
2298
|
is_gaming = true;
|
|
2299
|
+
clearGameTimeout();
|
|
2300
|
+
setGameTimeout(async () => {
|
|
2301
|
+
logger.info(`[SituationPuzzle Timeout]: 游戏已超时(${GAME_TIMEOUT_MINUTES}分钟),自动结束游戏`);
|
|
2302
|
+
is_gaming = false;
|
|
2303
|
+
chatPrompt = [];
|
|
2304
|
+
chatHistory2 = [];
|
|
2305
|
+
setGameTimeout(null);
|
|
2306
|
+
}, GAME_TIMEOUT_MINUTES * 60 * 1e3);
|
|
2272
2307
|
logger.info("[rerollSituationPuzzle Info]: ai api send message success");
|
|
2273
2308
|
return resp;
|
|
2274
2309
|
}
|
|
2275
2310
|
__name(rerollSituationPuzzle, "rerollSituationPuzzle");
|
|
2276
2311
|
|
|
2312
|
+
// src/services/game_func/games/game_24_service.ts
|
|
2313
|
+
var EPSILON = 1e-6;
|
|
2314
|
+
function solve24(nodes) {
|
|
2315
|
+
if (nodes.length === 1) {
|
|
2316
|
+
return Math.abs(nodes[0].value - 24) < EPSILON ? nodes[0].expr : null;
|
|
2317
|
+
}
|
|
2318
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
2319
|
+
for (let j = 0; j < nodes.length; j++) {
|
|
2320
|
+
if (i === j) continue;
|
|
2321
|
+
const rest = [];
|
|
2322
|
+
for (let k = 0; k < nodes.length; k++) {
|
|
2323
|
+
if (k !== i && k !== j) rest.push(nodes[k]);
|
|
2324
|
+
}
|
|
2325
|
+
const a = nodes[i];
|
|
2326
|
+
const b = nodes[j];
|
|
2327
|
+
const candidates = [
|
|
2328
|
+
{ value: a.value + b.value, expr: `(${a.expr} + ${b.expr})` },
|
|
2329
|
+
{ value: a.value - b.value, expr: `(${a.expr} - ${b.expr})` },
|
|
2330
|
+
{ value: a.value * b.value, expr: `(${a.expr} * ${b.expr})` }
|
|
2331
|
+
];
|
|
2332
|
+
if (Math.abs(b.value) > EPSILON) {
|
|
2333
|
+
candidates.push({
|
|
2334
|
+
value: a.value / b.value,
|
|
2335
|
+
expr: `(${a.expr} / ${b.expr})`
|
|
2336
|
+
});
|
|
2337
|
+
}
|
|
2338
|
+
for (const c of candidates) {
|
|
2339
|
+
rest.push(c);
|
|
2340
|
+
const res = solve24(rest);
|
|
2341
|
+
if (res) return res;
|
|
2342
|
+
rest.pop();
|
|
2343
|
+
}
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
return null;
|
|
2347
|
+
}
|
|
2348
|
+
__name(solve24, "solve24");
|
|
2349
|
+
function calcDifficulty(expr) {
|
|
2350
|
+
let score = 0;
|
|
2351
|
+
if (expr.includes("/")) score += 2;
|
|
2352
|
+
if (expr.includes("-")) score += 1;
|
|
2353
|
+
let depth = 0;
|
|
2354
|
+
let maxDepth = 0;
|
|
2355
|
+
for (const ch of expr) {
|
|
2356
|
+
if (ch === "(") {
|
|
2357
|
+
depth++;
|
|
2358
|
+
maxDepth = Math.max(maxDepth, depth);
|
|
2359
|
+
} else if (ch === ")") {
|
|
2360
|
+
depth--;
|
|
2361
|
+
}
|
|
2362
|
+
}
|
|
2363
|
+
score += maxDepth;
|
|
2364
|
+
if (expr.match(/\/\s*[1-9]/)) score += 1;
|
|
2365
|
+
return score;
|
|
2366
|
+
}
|
|
2367
|
+
__name(calcDifficulty, "calcDifficulty");
|
|
2368
|
+
function generate24Puzzle(minScore = 6) {
|
|
2369
|
+
while (true) {
|
|
2370
|
+
const numbers = Array.from(
|
|
2371
|
+
{ length: 4 },
|
|
2372
|
+
() => Math.floor(Math.random() * 13) + 1
|
|
2373
|
+
);
|
|
2374
|
+
if (numbers.includes(1) && numbers.includes(2) && numbers.includes(3)) {
|
|
2375
|
+
continue;
|
|
2376
|
+
}
|
|
2377
|
+
const nodes = numbers.map((n) => ({
|
|
2378
|
+
value: n,
|
|
2379
|
+
expr: n.toString()
|
|
2380
|
+
}));
|
|
2381
|
+
const solution = solve24(nodes);
|
|
2382
|
+
if (!solution) continue;
|
|
2383
|
+
const score = calcDifficulty(solution);
|
|
2384
|
+
if (score >= minScore) {
|
|
2385
|
+
return {
|
|
2386
|
+
numbers,
|
|
2387
|
+
solution: `${solution} = 24`
|
|
2388
|
+
};
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
2391
|
+
}
|
|
2392
|
+
__name(generate24Puzzle, "generate24Puzzle");
|
|
2393
|
+
var currentPuzzle = null;
|
|
2394
|
+
function newPuzzle() {
|
|
2395
|
+
currentPuzzle = generate24Puzzle();
|
|
2396
|
+
clearGameTimeout();
|
|
2397
|
+
setGameTimeout(async () => {
|
|
2398
|
+
logger.info(`[Puzzle24 Timeout]: 24点游戏已超时(${GAME_TIMEOUT_MINUTES}分钟),自动结束游戏`);
|
|
2399
|
+
currentPuzzle = null;
|
|
2400
|
+
setGameTimeout(null);
|
|
2401
|
+
}, GAME_TIMEOUT_MINUTES * 60 * 1e3);
|
|
2402
|
+
return currentPuzzle.numbers;
|
|
2403
|
+
}
|
|
2404
|
+
__name(newPuzzle, "newPuzzle");
|
|
2405
|
+
function getSolution() {
|
|
2406
|
+
if (!currentPuzzle) {
|
|
2407
|
+
throw new Error("尚未生成题目");
|
|
2408
|
+
}
|
|
2409
|
+
return currentPuzzle.solution;
|
|
2410
|
+
}
|
|
2411
|
+
__name(getSolution, "getSolution");
|
|
2412
|
+
|
|
2277
2413
|
// src/services/game_func/game_command.ts
|
|
2278
2414
|
var local_config2 = null;
|
|
2279
2415
|
var is_thinking = false;
|
|
@@ -2283,6 +2419,11 @@ var gameList = [
|
|
|
2283
2419
|
name: "AI海龟汤",
|
|
2284
2420
|
description: "AI海龟汤游戏,通过AI回答问题,猜测答案",
|
|
2285
2421
|
command: "puzzle"
|
|
2422
|
+
},
|
|
2423
|
+
{
|
|
2424
|
+
name: "24点题目生成",
|
|
2425
|
+
description: "24点游戏,生成24点题目,无回复功能",
|
|
2426
|
+
command: "24"
|
|
2286
2427
|
}
|
|
2287
2428
|
];
|
|
2288
2429
|
var commandList = [
|
|
@@ -2300,6 +2441,9 @@ function registerGameCommands(ctx, game_config, ai_config) {
|
|
|
2300
2441
|
} else {
|
|
2301
2442
|
logger.error("[loadYamlConfig Error]: 未配置 Game 配置文件路径");
|
|
2302
2443
|
}
|
|
2444
|
+
if (GAME_TIMEOUT_MINUTES <= 0) {
|
|
2445
|
+
setGameTimeoutMinutes(local_config2?.game?.timeout_minutes || 10);
|
|
2446
|
+
}
|
|
2303
2447
|
ctx.command("gamelist", "查看所有游戏").action(async ({}) => {
|
|
2304
2448
|
return `[所有命令都需要@bot]
|
|
2305
2449
|
游戏列表:
|
|
@@ -2316,12 +2460,20 @@ function registerGameCommands(ctx, game_config, ai_config) {
|
|
|
2316
2460
|
if (!game) return "请提供正确格式,如:@bot startgame [游戏]";
|
|
2317
2461
|
try {
|
|
2318
2462
|
is_thinking = true;
|
|
2463
|
+
let resp_text = "";
|
|
2319
2464
|
switch (game) {
|
|
2465
|
+
// 海龟汤
|
|
2320
2466
|
case gameList[0].command:
|
|
2321
2467
|
await session.send("正在开始游戏...后续问答请参照格式:@bot [问题]\n需要刷新谜题时发送:@bot reroll");
|
|
2322
|
-
|
|
2468
|
+
resp_text = await startSituationPuzzle(ai_config);
|
|
2323
2469
|
now_game = game;
|
|
2324
|
-
return checkResp(
|
|
2470
|
+
return checkResp(resp_text);
|
|
2471
|
+
// 24点生成
|
|
2472
|
+
case gameList[1].command:
|
|
2473
|
+
const numbers = await newPuzzle();
|
|
2474
|
+
resp_text = `题目: ${numbers.join(" ")}`;
|
|
2475
|
+
now_game = game;
|
|
2476
|
+
return checkResp(resp_text);
|
|
2325
2477
|
default:
|
|
2326
2478
|
return `游戏不存在,请输入gamelist查看所有游戏`;
|
|
2327
2479
|
}
|
|
@@ -2329,6 +2481,36 @@ function registerGameCommands(ctx, game_config, ai_config) {
|
|
|
2329
2481
|
is_thinking = false;
|
|
2330
2482
|
}
|
|
2331
2483
|
});
|
|
2484
|
+
ctx.command("endgame", "结束游戏").action(async ({ session }) => {
|
|
2485
|
+
if (!dev_mode) {
|
|
2486
|
+
if (!is_at_bot(session)) return;
|
|
2487
|
+
}
|
|
2488
|
+
if (!now_game) return "未开始游戏";
|
|
2489
|
+
if (now_game == gameList[0].command) return;
|
|
2490
|
+
if (is_thinking) return "正在思考,请耐心等待...";
|
|
2491
|
+
try {
|
|
2492
|
+
is_thinking = true;
|
|
2493
|
+
await session.send("正在结束游戏...");
|
|
2494
|
+
let resp_text = "";
|
|
2495
|
+
switch (now_game) {
|
|
2496
|
+
case gameList[0].command:
|
|
2497
|
+
await session.send("正在结束游戏...");
|
|
2498
|
+
resp_text = await endSituationPuzzle(ai_config);
|
|
2499
|
+
now_game = null;
|
|
2500
|
+
return checkResp(resp_text);
|
|
2501
|
+
case gameList[1].command:
|
|
2502
|
+
resp_text = await getSolution();
|
|
2503
|
+
resp_text = `答案: ${getSolution()}`;
|
|
2504
|
+
now_game = null;
|
|
2505
|
+
clearGameTimeout();
|
|
2506
|
+
return checkResp(resp_text);
|
|
2507
|
+
default:
|
|
2508
|
+
return;
|
|
2509
|
+
}
|
|
2510
|
+
} finally {
|
|
2511
|
+
is_thinking = false;
|
|
2512
|
+
}
|
|
2513
|
+
});
|
|
2332
2514
|
}
|
|
2333
2515
|
__name(registerGameCommands, "registerGameCommands");
|
|
2334
2516
|
function registerGameMiddleware(ctx, ai_config) {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
declare
|
|
3
|
-
declare function
|
|
4
|
-
declare function
|
|
5
|
-
declare function
|
|
6
|
-
|
|
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,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 };
|