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

157 lines
3.7 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 test_parse_hints(hint, translations) {
let new_hint = hint;
const color_tags = [
...hint.matchAll(/\<[^\>]+\>(.*)\<[^\>]+\>/gm),
...hint.matchAll(/\<([^\>]+)\>/gm),
];
if (color_tags) {
if (color_tags[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]) {
console.log(image_text[0][0]);
const translated_image = translations[image_text[0][0]];
console.log(translated_image);
new_hint = new_hint.replace(
/\[[^\]]+\]/gm,
`<img class="inline-block object-contain w-8 h-8 my-auto align-middle" src="/static/tracker/images/sprites/${translated_image}.png">`
);
}
const trunic_hint = new_hint
.split(/\<[^\>]+\>/gm)
.join(' "" ')
.split(/\"[^\"]*\"/gm);
for (const trunic in trunic_hint) {
new_hint = new_hint.replace(
trunic_hint[trunic].trim(),
translate(trunic_hint[trunic].trim())
);
}
return new_hint;
}
async function test_fetch() {
const response = await fetch(`http://localhost:51111/hints`);
const data = await response.json();
return data;
}
async function translation_fetch() {
const response = await fetch(
`http://localhost:8080/static/tracker/data/image_translations.json`
);
const data = await response.json();
return data;
}
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}&nbsp;</span>`;
};
export default { test_parse_hints, translation_fetch };