Skip to content

Commit

Permalink
Update nightorder for upstream data change
Browse files Browse the repository at this point in the history
The script tool's nightsheet.json used to contain character names but
now has identifiers. botc-tools now normalizes and lower-cases before
referencing the nightsheet; this is to patch things up for the current
code which calls the nightsheet with names, but it could also all just
use normalized identifiers.

Fixes #20.
  • Loading branch information
tchajed committed Aug 25, 2024
1 parent 1f8b760 commit e53b723
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/js/botc/nightorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,54 @@
*
* Gives a global ordering for all characters, for first night and other nights.
*/
import nightsheet from "../../../assets/data/nightsheet.json";
import nightsheetRaw from "../../../assets/data/nightsheet.json";
import { nameToId } from "./roles";

// Normalize character names to nightsheet keys.
//
// The nightsheet is called in the rest of this code base using capitalized
// names for historical reasons (the nightsheet.json from the website to contain
// character names), but now it contains identifiers like most other things.
function nightsheetNormalize(name: string): string {
if (["DUSK", "DAWN", "MINION", "DEMON"].includes(name)) {
return name;
}
return nameToId(name.toLowerCase());
}

function normalizeNightSheet(): {
firstNight: string[];
otherNight: string[];
} {
return {
firstNight: nightsheetRaw.firstNight.map(nightsheetNormalize),
otherNight: nightsheetRaw.otherNight.map(nightsheetNormalize),
};
}

// the identifiers here are normalized with `nameToId`, except for the special
// DUSK, DAWN, MINION, DEMON night actions.
const nightsheet = normalizeNightSheet();

function getFirstNight(name: string): number | null {
const id = nightsheetNormalize(name);
// implicitly add drunk to nightsheet (since we have a custom night ability for it)
if (name == "Drunk") {
if (id == "drunk") {
return -1;
}
if (name == "Storm Catcher") {
if (id == "stormcatcher") {
return nightsheet.firstNight.indexOf("DEMON");
}
const n = nightsheet.firstNight.indexOf(name);
const n = nightsheet.firstNight.indexOf(id);
if (n < 0) {
return null;
}
return n;
}

function getOtherNights(name: string): number | null {
const n = nightsheet.otherNight.indexOf(name);
const id = nightsheetNormalize(name);
const n = nightsheet.otherNight.indexOf(id);
if (n < 0) {
return null;
}
Expand Down

0 comments on commit e53b723

Please sign in to comment.