Format all upgrade tiers as Roman numerals (#830)

* format all upgrade tiers as Roman numerals

* show upgrade tiers 50+ as Arabic numerals
This commit is contained in:
LeopoldTal 2020-12-07 13:06:54 +01:00 committed by GitHub
parent ca1af5a505
commit f620706ed7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 49 additions and 26 deletions

View File

@ -723,29 +723,8 @@ export function startFileChoose(acceptedType = ".bin") {
});
}
const romanLiterals = [
"0", // NULL
"I",
"II",
"III",
"IV",
"V",
"VI",
"VII",
"VIII",
"IX",
"X",
"XI",
"XII",
"XIII",
"XIV",
"XV",
"XVI",
"XVII",
"XVIII",
"XIX",
"XX",
];
const MAX_ROMAN_NUMBER = 49;
const romanLiteralsCache = ["0"];
/**
*
@ -754,8 +733,52 @@ const romanLiterals = [
*/
export function getRomanNumber(number) {
number = Math.max(0, Math.round(number));
if (number < romanLiterals.length) {
return romanLiterals[number];
if (romanLiteralsCache[number]) {
return romanLiteralsCache[number];
}
return String(number);
if (number > MAX_ROMAN_NUMBER) {
return String(number);
}
function formatDigit(digit, unit, quintuple, decuple) {
switch (digit) {
case 0:
return "";
case 1: // I
return unit;
case 2: // II
return unit + unit;
case 3: // III
return unit + unit + unit;
case 4: // IV
return unit + quintuple;
case 9: // IX
return unit + decuple;
default:
// V, VI, VII, VIII
return quintuple + formatDigit(digit - 5, unit, quintuple, decuple);
}
}
let thousands = Math.floor(number / 1000);
let thousandsPart = "";
while (thousands > 0) {
thousandsPart += "M";
thousands -= 1;
}
const hundreds = Math.floor((number % 1000) / 100);
const hundredsPart = formatDigit(hundreds, "C", "D", "M");
const tens = Math.floor((number % 100) / 10);
const tensPart = formatDigit(tens, "X", "L", "C");
const units = number % 10;
const unitsPart = formatDigit(units, "I", "V", "X");
const formatted = thousandsPart + hundredsPart + tensPart + unitsPart;
romanLiteralsCache[number] = formatted;
return formatted;
}