tunic-tracker-redux/tunictracker/tracker/static/tracker/assets/translate-hints.js

179 lines
4.4 KiB
JavaScript

const lookup = {
ah: "aa",
R: "ax",
aw: "a",
A: "ei",
eh: "e",
E: "i",
Er: "ix",
uh: "uu",
Ar: "ex",
i: "ii",
I: "ai",
ur: "x",
O: "o",
oi: "oi",
oo: "u",
ou: "oo",
ow: "au",
or: "ox",
b: "b",
J: "ch",
d: "d",
f: "f",
g: "g",
h: "h",
j: "j",
k: "k",
l: "l",
m: "m",
n: "n",
"^": "ng",
p: "p",
r: "r",
s: "s",
$: "sh",
t: "t",
"%": "th",
"#": "tz",
v: "v",
w: "w",
y: "y",
z: "z",
"&": "zh",
};
const skip = [" ", ",", "."];
const re =
/(\\"[\w\s\.\-']*\\")|\<(#[a-fA-F0-9]*)\>(.*)\<#[a-fA-F0-9]*\>|(\[[\w]*\])/gm;
async function parse_hints(hint) {
let new_hint = hint;
const color_tags = [
...hint.matchAll(/\<[^\>]+\>(.*)\<[^\>]+\>/gm),
...hint.matchAll(/\<([^\>]+)\>/gm),
];
let color_tag_value = "";
if (color_tags) {
if (color_tags[1]) {
if (color_tag_value == "") {
color_tag_value = color_tags[1][1];
}
const color_tags_text = `<span style="color: ${color_tags[1][1]}">${color_tags[0][1]}</span>`;
new_hint = new_hint.replace(
/\<[^\>]+\>[^\<]+\<[^\>]+\>/gm,
color_tags_text
);
}
}
const image_text = [...new_hint.matchAll(/\[[^\]]+\]/gm)];
if (image_text && image_text[0]) {
let translated_image = image_text[0][0].replace(/\[|\]/g, "");
if (translated_image == "hexagram") {
translated_image = `${color_tag_value
.substring(1)
.toUpperCase()}_${translated_image}`;
}
new_hint = new_hint.replace(
/\[[^\]]+\]/gm,
`<img class="inline-block w-8 h-8 my-auto align-middle drop-shadow-[2px_2px_0px_rgba(36,36,36,0.8)]" src="/static/tracker/images/sprites/${translated_image}.png">`
);
}
let trunic_hint = new_hint;
const tag_matches = [...new_hint.matchAll(/\<[^\>]*\>/gm)].map((x) => x[0]);
for (const matched in tag_matches) {
trunic_hint = trunic_hint.replace(
tag_matches[matched],
"*".repeat(tag_matches[matched].length)
);
}
const quote_matches = [...trunic_hint.matchAll(/\"[^\"]*\"/gm)].map(
(x) => x[0]
);
for (const matched in quote_matches) {
trunic_hint = trunic_hint.replace(
quote_matches[matched],
"*".repeat(quote_matches[matched].length)
);
}
let current_index = 0;
new_hint = [...trunic_hint.matchAll(/\*+/gm)]
.map((x, index) => {
let points = [];
if (index != 0) {
points = [current_index, x.index];
} else {
points = [0, x.index];
}
current_index = x.index + x[0].length;
const to_translate = trunic_hint.substring(points[0], points[1]).trim();
if (to_translate.length > 0) {
return (
translate(to_translate) +
"&nbsp;" +
new_hint.substring(x.index, current_index).trim()
);
} else {
return new_hint.substring(x.index, current_index).trim();
}
})
.join(" ");
return new_hint;
}
const translate = (input) => {
let payload = "";
let inQuote = false;
let cursor = 0;
// // remove [text in square brackets]
// // remove extra whitespace (eg., "I saw A [hourglass] "HOURGLASS"" becomes "I saw A "HOURGLASS"")
// input = input.replace(/\s+\[.+?\]\s+/gm, " ");
// // remove <text in carats>
// // no whitespace concerns (eg., "sehz <#FF00FF>sohm%i^" becomes "sehz sohm%i^")
// input = input.replace(/<.+?>/gm, "");
// input = input.trim();
while (cursor < input.length) {
// get what one character and two characters ahead would be
const one = input[cursor];
const two = input.slice(cursor, cursor + 2);
// things in between quotes are in english, ignore it and set span accordingly
if (one == '"') {
payload += `</span><span class="${inQuote ? "font-trunic" : "english"}">`;
inQuote = !inQuote;
cursor++;
continue;
}
// if we're in an english quote, add it in and move on
if (inQuote) {
payload += one;
cursor++;
continue;
}
// check if this is a skipped character
if (skip.includes(one)) {
payload += one;
// check if we have a translation for this character
} else if (lookup.hasOwnProperty(one)) {
payload += lookup[one];
// check if we have a translation for two characters ahead
// if so, advance the cursor head extra to make up for using two
} else if (lookup.hasOwnProperty(two)) {
payload += lookup[two];
cursor++;
// uh oh
} else {
// console.log(input[cursor], "UNKNOWN", input[cursor].charCodeAt(0));
}
// advance the cursor head to the next character
cursor++;
}
return `<span class="inline-block pt-0.5 font-trunic text-md sm:text-xl h-full align-bottom">${payload}</span>`;
};
export default { parse_hints };