| 1 | /** |
| 2 | * Dialogue line / choice text placeholders: |
| 3 | * [playername], [playerfullname], [playersurname], and gendered forms ([he]/[Mr], …). |
| 4 | * Gender tokens keep the casing of the placeholder ([He] → She). |
| 5 | */ |
| 6 | |
| 7 | const NAME_TOKENS = Object.freeze({ |
| 8 | playername: "firstName", |
| 9 | playerfullname: "fullName", |
| 10 | playersurname: "lastName" |
| 11 | }); |
| 12 | |
| 13 | /** Male form → [male, female] lowercase pair. Lookup is case-insensitive. */ |
| 14 | const GENDERED_TOKENS = Object.freeze({ |
| 15 | mr: ["mr", "ms"], |
| 16 | he: ["he", "she"], |
| 17 | him: ["him", "her"], |
| 18 | his: ["his", "her"], |
| 19 | hers: ["his", "hers"], |
| 20 | himself: ["himself", "herself"], |
| 21 | man: ["man", "woman"], |
| 22 | men: ["men", "women"], |
| 23 | male: ["male", "female"], |
| 24 | boy: ["boy", "girl"], |
| 25 | lad: ["lad", "lass"], |
| 26 | brother: ["brother", "sister"], |
| 27 | sir: ["sir", "ma'am"], |
| 28 | lord: ["lord", "lady"], |
| 29 | king: ["king", "queen"], |
| 30 | husband: ["husband", "wife"], |
| 31 | son: ["son", "daughter"], |
| 32 | father: ["father", "mother"], |
| 33 | uncle: ["uncle", "aunt"], |
| 34 | nephew: ["nephew", "niece"] |
| 35 | }); |
| 36 | |
| 37 | /** |
| 38 | * Catalog for the editor help modal. |
| 39 | * @returns {{ token: string, meaning: string }[]} |
| 40 | */ |
| 41 | export function dialogueReplacementCatalog () |
| 42 | { |
| 43 | return [ |
| 44 | { token: "[playername]", meaning: "Player’s first name" }, |
| 45 | { token: "[playerfullname]", meaning: "Player’s first and last name" }, |
| 46 | { token: "[playersurname]", meaning: "Player’s last name" }, |
| 47 | ...Object.entries(GENDERED_TOKENS).map(([key, [male, female]]) => ({ |
| 48 | token: `[${key}]`, |
| 49 | meaning: `${male} / ${female} (casing follows the tag, e.g. [He] → She)` |
| 50 | })) |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Apply name + gendered replacements to dialogue text. |
| 56 | * @param {string} text |
| 57 | * @param {{ firstName?: string, lastName?: string, sex?: string }} identity |
| 58 | */ |
| 59 | export function applyDialogueTextReplacements (text, identity = {}) |
| 60 | { |
| 61 | const raw = String(text ?? ""); |
| 62 | if (!raw.includes("[")) |
| 63 | { return raw; } |
| 64 | const firstName = String(identity.firstName ?? "").trim() || "Adventurer"; |
| 65 | const lastName = String(identity.lastName ?? "").trim(); |
| 66 | const fullName = lastName ? `${firstName} ${lastName}` : firstName; |
| 67 | const female = identity.sex === "female"; |
| 68 | const names = { firstName, lastName: lastName || firstName, fullName }; |
| 69 | |
| 70 | return raw.replace(/\[([^\]]+)\]/g, (match, inner) => |
| 71 | { |
| 72 | const key = String(inner).trim().toLowerCase(); |
| 73 | const nameField = NAME_TOKENS[key]; |
| 74 | if (nameField) |
| 75 | { return names[nameField]; } |
| 76 | const pair = GENDERED_TOKENS[key]; |
| 77 | if (!pair) |
| 78 | { return match; } |
| 79 | const word = female ? pair[1] : pair[0]; |
| 80 | return applyTokenCasing(inner.trim(), word); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | /** Copy casing style from the placeholder body onto the replacement word. */ |
| 85 | export function applyTokenCasing (template, word) |
| 86 | { |
| 87 | const src = String(template ?? ""); |
| 88 | const dest = String(word ?? ""); |
| 89 | if (!src || !dest) |
| 90 | { return dest; } |
| 91 | if (src === src.toUpperCase()) |
| 92 | { return dest.toUpperCase(); } |
| 93 | if (src[0] === src[0].toUpperCase() && src.slice(1) === src.slice(1).toLowerCase()) |
| 94 | { |
| 95 | return dest.charAt(0).toUpperCase() + dest.slice(1).toLowerCase(); |
| 96 | } |
| 97 | return dest.toLowerCase(); |
| 98 | } |