Skip to content

Commit

Permalink
add: implement warnings in monster search script
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitsunee committed Oct 17, 2024
1 parent bc62dc8 commit efa3706
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/search-monster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ options:
--hide-lv Disables showing monster levels
--show-rank Enables showing monster ranks
--hide-rank Disables showing monster ranks
--show-warn Enables showing monster warnings
--hide-warn Disables showing monster warnings
Options can be entered at any point and in any order.
All arguments starting with a - (dash) are parsed as an argument, all other arguments are parsed as search queries.
Expand Down Expand Up @@ -58,6 +60,12 @@ async function main() {
case "--hide-rank":
opts.showRank = false;
continue;
case "--show-warn":
opts.showWarnings = true;
continue;
case "--hide-warn":
opts.showWarnings = false;
continue;
}

// handle unknown arg
Expand All @@ -76,7 +84,7 @@ async function main() {
const results = await searchMonsterByName(list, query);
for (const result of results) {
console.log(
`- ${describeMonster(result.data, { ...opts, lang: result.lang })}`
`- ${await describeMonster(result.data, { ...opts, lang: result.lang })}`
);
}
console.log(""); // force newline
Expand Down
17 changes: 16 additions & 1 deletion src/utils/describe-monster.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getMonsterWarnings } from "./get-monster-warnings";

type LangOpt = "always" | "never" | "not en only";

export interface DescribeMonsterOpts {
Expand All @@ -6,17 +8,22 @@ export interface DescribeMonsterOpts {
showLevel?: boolean;
lang?: GameLanguage;
showLang?: LangOpt;
showWarnings?: boolean;
}

/**
* Describes a monster in text
* @param data MonsterData Object
*/
export function describeMonster(data: MonsterData, opts?: DescribeMonsterOpts) {
export async function describeMonster(
data: MonsterData,
opts?: DescribeMonsterOpts
) {
const showLevel = opts?.showLevel ?? true;
const showRank = opts?.showRank ?? false;
const lang: GameLanguage = opts?.lang ?? "en";
const showLang = opts?.showLang ?? "not en only";
const showWarnings = opts?.showWarnings ?? false;
let description = "";

// Handle showing ID
Expand Down Expand Up @@ -46,5 +53,13 @@ export function describeMonster(data: MonsterData, opts?: DescribeMonsterOpts) {
description += ` (${lang})`;
}

// Handle warnings
if (showWarnings) {
const warnings = await getMonsterWarnings(data);
if (warnings.length > 0) {
description += ` (WARN: ${warnings.join(", ")})`;
}
}

return description;
}

0 comments on commit efa3706

Please sign in to comment.