icalingua-python-bot/ica-rs/plugins/md5/md5-api.ts

164 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-05-09 00:27:45 +08:00
const md5_module = require("./md5.js");
2024-05-09 22:54:23 +08:00
// import * as md5_module from "./md5.js";
2024-05-09 00:27:45 +08:00
/**
*
* 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<FightResult> {
// 检查一下输入是否合法
// 比如里面有没有 !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<WinRateResult> {
// 检查 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<WinRateResult> {
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<ScoreResult> {
// 检查 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<ScoreResult> {
if (!test_check(names)) {
throw new Error("你怎么在分数输入里加 !test!(恼)\n${names}");
}
return await md5_module.score_callback(names, callback);
}
2024-05-09 00:30:41 +08:00
2024-05-09 22:54:23 +08:00
// export {
// type FightResult,
// type WinRate,
// type WinRateResult,
// type WinRateCallback,
// type Score,
// type ScoreResult,
// type ScoreCallback,
// fight,
// win_rate,
// win_rate_callback,
// score,
// score_callback,
// };
async function main() {
// 从相对位置导入内容
const fs = require("fs");
const path = require("path");
const names = fs.readFileSync(path.resolve(__dirname, "input.txt"), "utf-8");
const result = await fight(names);
2024-05-09 22:58:34 +08:00
console.log(`赢家:|${result.source_plr}|`);
2024-05-09 22:54:23 +08:00
}
main();