From ad686526eb7c8c72e9242b1645a2cadca149bd1b Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Thu, 9 May 2024 00:02:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E4=B8=80=E4=B8=8Bapi?= =?UTF-8?q?=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- branch/latest/md5.js | 41 ++++++++++-- branch/latest/test.ts | 150 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 176 insertions(+), 15 deletions(-) diff --git a/branch/latest/md5.js b/branch/latest/md5.js index 9900273..463e0d0 100644 --- a/branch/latest/md5.js +++ b/branch/latest/md5.js @@ -21798,33 +21798,60 @@ const runner = { return new Promise((resolve, reject) => { let win_datas = []; finish_trigger.on("win_rate", (run_round, win_count) => { - // 先把数据存起来 - win_datas.push({round: run_round, win_count: win_count}); + win_datas.push({ round: run_round, win_count: win_count }); // 如果数据长度等于 round,说明数据已经全部返回 if (run_round >= target_round) { stop_bomb = true; - resolve({score: win_count, raw_data: win_datas}); - } + resolve({ score: win_count, raw_data: win_datas }); + } }); main(names); }); }, + win_rate_callback: (names, callback) => { + return new Promise((resolve, reject) => { + let win_datas = []; + finish_trigger.on("win_rate", (run_round, win_count) => { + win_datas.push({ round: run_round, win_count: win_count }); + // 调用 callback + let result = callback(run_round, win_count); + if (!result) { + stop_bomb = true; + resolve({ score: win_count, raw_data: win_datas }); + } + }); + main(names); + }); + }, score: (names, target_round) => { return new Promise((resolve, reject) => { let score_datas = []; finish_trigger.on("score_report", (run_round, score) => { - // 先把数据存起来 - score_datas.push({round: run_round, score: score}); + score_datas.push({ round: run_round, score: score }); // 如果数据长度等于 round,说明数据已经全部返回 if (run_round >= target_round) { stop_bomb = true; - resolve({score: score, raw_data: score_datas}); + resolve({ score: score, raw_data: score_datas }); }; }); main(names); }); }, + score_callback: (names, callback) => { + return new Promise((resolve, reject) => { + let score_datas = []; + finish_trigger.on("score_report", (run_round, score) => { + score_datas.push({ round: run_round, score: score }); + // 调用 callback + let result = callback(run_round, score); + if (!result) { + stop_bomb = true; + resolve({ score: score, raw_data: score_datas }); + } + }); + }); + }, }; if (run_env.from_code) { diff --git a/branch/latest/test.ts b/branch/latest/test.ts index 0f238bc..41646b3 100644 --- a/branch/latest/test.ts +++ b/branch/latest/test.ts @@ -1,18 +1,152 @@ -const md5_module = require('./md5.js'); +const md5_module = require("./md5.js"); + +/** + * 对战结果的数据结构 + * 其实只有 source_plr 是有用的, 是赢家之一 + */ +type FightResult = { + message: string; + source_plr: string; + target_plr: string; + affect: string | number; +}; + +/** + * 每一行具体的胜率结果 + */ +type WinRate = { + round: number; + win_count: number; +}; + +/** + * 胜率的数据结构 + */ +type WinRateResult = { + souce: number; + raw_data: WinRate[]; +}; + +/** + * 用于接收胜率的回调函数 + * 返回一个 bool, true 表示继续, false 表示停止 + */ +type WinRateCallback = (run_round: number, win_count: number) => boolean; + +/** + * 分数的数据结构 + */ +type Score = { + round: number; + score: number; +}; + +/** + * 分数的数据结构 + */ +type ScoreResult = { + source: number; + raw_data: Score[]; +}; + +/** + * 用于接收分数的回调函数 + * 返回一个 bool, true 表示继续, false 表示停止 + */ +type ScoreCallback = (run_round: number, score: number) => boolean; + +/** + * + * @param names 原始的输入框输入 + * @returns 对战结果 + */ +async function fight(names: string): Promise { + // 检查一下输入是否合法 + // 比如里面有没有 !test! + if (names.startsWith("!test!")) { + throw new Error("你怎么在对战输入里加 !test!(恼)\n${names}"); + } + return await md5_module.fight(names); +} + +/** + * 对于胜率/评分的输入检查 + * @param names + * @returns + */ +function test_check(names: string): boolean { + const have_test = names.startsWith("!test!"); + + return have_test; +} + +/** + * 测量胜率 + * @param names 原始的输入框输入 + * @param round 战斗的回合数 + * @returns 胜率结果 + */ +async function win_rate(names: string, round: number): Promise { + // 检查 round 是否合法 + if (round <= 0) { + throw new Error("round 必须大于 0"); + } + if (!test_check(names)) { + throw new Error("你怎么在胜率输入里加 !test!(恼)\n${names}"); + } + return await md5_module.win_rate(names, round); +} + +/** + * + * @param names 原始的输入框输入 + * @param callback 用于接收胜率的回调函数 + * @returns 胜率结果 + */ +async function win_rate_callback( + names: string, + callback: WinRateCallback, +): Promise { + if (!test_check(names)) { + throw new Error("你怎么在胜率输入里加 !test!(恼)\n${names}"); + } + return await md5_module.win_rate_callback(names, callback); +} + +async function score(names: string, round: number): Promise { + // 检查 round 是否合法 + if (round <= 0) { + throw new Error("round 必须大于 0"); + } + if (!test_check(names)) { + throw new Error("你怎么在分数输入里加 !test!(恼)\n${names}"); + } + return await md5_module.score(names, round); +} + +async function score_callback( + names: string, + callback: ScoreCallback, +): Promise { + if (!test_check(names)) { + throw new Error("你怎么在分数输入里加 !test!(恼)\n${names}"); + } + return await md5_module.score_callback(names, callback); +} // 启动一个 async 函数 async function main() { - let result = await md5_module.fight("aaaaaa\nbbbbb"); + const result = await md5_module.fight("aaaaaa\nbbbbb"); - console.log("对战结果: ", result); + console.log("对战结果: ", result); - let win_rate = await md5_module.win_rate("!test!\n\naaaaaa\n\nbbbbb", 1000); + const win_rate = await md5_module.win_rate("!test!\n\naaaaaa\n\nbbbbb", 1000); - console.log("胜率: ", win_rate); + console.log("胜率: ", win_rate); - let score = await md5_module.score("!test!\n\naaaaaabbbb", 1000); + const score = await md5_module.score("!test!\n\naaaaaabbbb", 1000); - console.log("分数: ", score); -}; + console.log("分数: ", score); +} main();