MediaWiki:Cycle11.js: Difference between revisions

miraheze>CodeTriangle
this code should give players the percentage of total shares owned using a tooltip.
m 10 revisions imported: Import Miraheze archive 2022-10-29
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
// DOM objects
// DOM objects
let st = document.querySelector("#sharetable tbody");
let st = document.querySelector("#sharetable tbody");
var vt = document.querySelector("#valuetable tbody");


// Only do the rest if the sharetable exists
// Only do the rest if the sharetable exists
Line 20: Line 21:
// Code to generate a hover-over indication of percentages.
// Code to generate a hover-over indication of percentages.
for (let row of st.children) {
for (let row of st.children) {
if (!isNaN(row.children[i].innerText)) {
// The cell that contains the entity's name.
row.children[i].setAttribute(
let ent = row.children[0];
    let ch = row.children[i];
if (!isNaN(ch.innerText)) {
        let percent = parseInt(ch.innerText) / total;
        let majority = percent > 0.5;
        let minority = majority ? false : percent >= 0.1;
       
        // But the BoB cannot be a shareholder at all.
        if (ent.innerText.match(/Bureau/)) {
        majority = minority = false;
        }
       
ch.setAttribute(
         "title",
         "title",
         // hacky way to round to two decimal points
         // hacky way to round to two decimal points
         // because the right way is worse
         // because the right way is worse
         ((parseInt(row.children[i].innerText) / total * 10000) | 0) / 100 + "%"
         ((percent * 10000) | 0) / 100 + "%" + (majority ? " (majority)" : minority ? " (minority)" : "")
     );
     );
       
        if (majority) {
        ch.style.fontWeight = "bold";
        ch.style.textDecoration = "underline";
        }
       
        if (minority) {
        ch.style.fontWeight = "bold";
        }
}
}
}
}
Line 44: Line 66:
// Append this row to the share table.
// Append this row to the share table.
st.appendChild(newRow);
st.appendChild(newRow);
}
if (vt) {
let restIdx = -1;
let lastIdx = -1;
let currentIdx = -1;
let rollIdx = -1;
for (let j = 1; j < vt.children.length; j++) {
let text = vt.children[j].children[0].innerText;
if (/rest/i.test(text)) {
restIdx = j;
} else if (/last/i.test(text)) {
lastIdx = j;
} else if (/current/i.test(text)) {
currentIdx = j;
} else if (/roll/i.test(text) || /dice/i.test(text)) {
rollIdx = j;
}
}
var nextValues = [];
var nextShareValues = [];
if (restIdx != -1 &&
    lastIdx != -1 &&
    currentIdx != -1 &&
    rollIdx != -1) {
for (let i = 1; i < vt.children[0].children.length; i++) {
function getFromTable(j) {
return parseInt(vt.children[j].children[i].innerText);
}
let rest = getFromTable(restIdx);
let last = getFromTable(lastIdx);
let current = getFromTable(currentIdx);
let roll = getFromTable(rollIdx);
let val = Math.floor(
  (roll-50) + current + (rest-current)/5 + (current-last)/4
);
nextValues.push(val);
nextShareValues.push(Math.floor(val / totals[i - 1]));
}
vt.children[currentIdx].children[0].innerText = vt.children[currentIdx].children[0].innerText.replaceAll(/current/ig, "Last");
vt.children[lastIdx].remove();
// Construct a new row to contain totals for each rule.
let valuesRow = document.createElement("tr");
let shareValuesRow = document.createElement("tr");
let valuesLabel = document.createElement("th");
valuesLabel.innerText = "Current Rule Value";
valuesRow.appendChild(valuesLabel);
let shareValuesLabel = document.createElement("th");
shareValuesLabel.innerText = "Current Share Value";
shareValuesRow.appendChild(shareValuesLabel);
for (let value of nextValues) {
let valuesCell = document.createElement("td");
valuesCell.innerText = value;
valuesCell.style = "font-weight: bold;";
valuesRow.appendChild(valuesCell);
}
for (let value of nextShareValues) {
let shareValuesCell = document.createElement("td");
shareValuesCell.innerText = value;
shareValuesCell.style = "font-weight: bold;";
shareValuesRow.appendChild(shareValuesCell);
}
// Append this row to the share table.
vt.appendChild(valuesRow);
vt.appendChild(shareValuesRow);
}
}
}