Skip to content

Commit

Permalink
feat: Added option to keep the random number (if it exists) when demy…
Browse files Browse the repository at this point in the history
…stifying an npc.

Closes #3
  • Loading branch information
xdy committed Nov 28, 2021
1 parent a9b4ef7 commit 717196d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/module/mystify-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ export async function mystifyToken(token: Token | null, mystified: boolean): Pro
let name = token?.name || "";
if (token) {
if (mystified) {
name = token?.actor?.name || "";
if ((game as Game).settings.get(MODULENAME, "npcMystifierKeepRandomNumberWhenDemystified")) {
name = `${token?.actor?.name} ${name?.match(/\d+$/)?.[0] ?? ""}`;
} else {
name = token?.actor?.name || "";
}
} else {
switch ((game as Game).settings.get(MODULENAME, "npcMystifierMethod")) {
default:
name = generateNameFromTraits(token);
}
if ((game as Game).settings.get(MODULENAME, "npcMystifierAddRandomNumber")) {
name += ` ${Math.floor(Math.random() * 100)}`;
//TODO Check if token exists with this name, if so, reroll.
let rolled = Math.floor(Math.random() * 100) + 1;
//Retry once if the number is already used, can't be bothered to roll until unique or keep track of used numbers
if ((game as Game).scenes?.active?.tokens?.find((t) => t.name.endsWith(rolled.toString()))) {
rolled = Math.floor(Math.random() * 100) + 1;
}
name += ` ${rolled}`;
}
}
}
Expand All @@ -35,29 +43,39 @@ export function preTokenCreateMystification(token: Token) {
(game as Game).keyboard?.isDown(mystifyKey) &&
(!(game as Game).keyboard?.isDown("V") || (game as Game).keyboard?.isDown("Insert"))
) {
mystifyToken(token, isMystified(token));
mystifyToken(token, isTokenNameDifferent(token));
}
}

function isMystified(token: Token | null) {
return token?.data.name !== token?.actor?.name || false;
function isTokenNameDifferent(token: Token | null): boolean {
const tokenName = token?.data.name;
const actorName = token?.actor?.name;
if (
tokenName !== actorName &&
(game as Game).settings.get(MODULENAME, "npcMystifierKeepRandomNumberWhenDemystified")
) {
const tokenNameNoNumber = tokenName?.trim().replace(/\d+$/, "").trim();
const actorNameNoNumber = actorName?.replace(/\d+$/, "").trim();
return tokenNameNoNumber !== actorNameNoNumber;
}
return tokenName !== actorName || false;
}

export function renderNameHud(data: any, html: JQuery<HTMLElement>) {
let token: Token | null;
if ((game as Game).user?.isGM && canvas instanceof Canvas && canvas && canvas.tokens) {
token = canvas.tokens.get(data._id) ?? null;

const title = isMystified(token) ? "Unmystify" : "Mystify";
const title = isTokenNameDifferent(token) ? "Unmystify" : "Mystify";
const toggle = $(
`<div class="control-icon ${
isMystified(token) ? "active" : ""
isTokenNameDifferent(token) ? "active" : ""
}" > <i class="fas fa-eye-slash" title=${title}></i></div>`
);
toggle.on("click", async (e) => {
const hudElement = $(e.currentTarget);
const active = hudElement.hasClass("active");
if (isMystified(token) === active) {
if (isTokenNameDifferent(token) === active) {
await mystifyToken(token, active);
}
hudElement.toggleClass("active");
Expand Down
9 changes: 9 additions & 0 deletions src/module/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ export function registerSettings() {
type: Boolean,
});

settings.register(MODULENAME, "npcMystifierKeepRandomNumberWhenDemystified", {
name: "Keep random number when demystifying.", //(game as Game).i18n.localize(`${MODULENAME}.SETTINGS.npcMystifierKeepRandomNumberWhenDemystified.Name`),
hint: "Keep random number (if any) when demystifying npcs.", //(game as Game).i18n.localize(`${MODULENAME}.settings.npcMystifierKeepRandomNumberWhenDemystified.Hint`),
scope: "world",
config: true,
default: false,
type: Boolean,
});

//TODO These apply only to trait mystification and should be grouped together, maybe on a separate tab?
settings.register(MODULENAME, "npcMystifierFilterRarities", {
name: "No npc rarity in name.", //game.i18n.localize(`${MODULENAME}.settings.npcMystifierFilterRarities.Name`),
Expand Down

0 comments on commit 717196d

Please sign in to comment.