Jump to content

MediaWiki:Common.js: Difference between revisions

From Emps-World Wiki
No edit summary
No edit summary
 
(73 intermediate revisions by 3 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */


// TODO: link images directly, because the parser has already run at this point
// TODO: Check whether noted icon is in the same file as unnoted icon.
// cannot use wiki commands here anymore.
 
// mapping slot id to a file
// mapping slot id to a file
var slotToImg = [
var slotToImg = [
Line 24: Line 24:


$(document).ready(function() {
$(document).ready(function() {
    function format(x) {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
    function capitalize(x) {
        return x.replace(/\b\w/g, function(l){return l.toUpperCase()});
    }
    $(".item-image").each(function() {
        $(this).find("pre").replaceWith($(this).find("img"));
        var img = $(this).find("img")
        var desc = $(this).attr("class").split("'")[1];
        img.attr("alt", desc);
        img.attr("title", desc);
    });
     $(".item-bonus-table, .item-info-table").each(function() {
     $(".item-bonus-table, .item-info-table").each(function() {
         var table = $(this);
         var table = $(this);
         var itemId = $(this).data("item-id");
         var itemId = $(this).data("item-id");
          
          
         $.get( "../scripts/fetch_item.php", { id: itemId }).done(function(data) {
         $.get( "../scripts/fetch_item.php", { id: itemId }).done(function(json) {
            var json = JSON.parse(data);
             var hc = 0, dc = 0;
             var hc = 0, dc = 0;
           
            // item info table
            if (table.is(".item-info-table")) {
                //console.log("loading item: "+json.id);
                table.find("th").each(function() {
                    if (dc % 2 === 1) {
                        var idx = parseInt(dc / 2);
                        var note = "No";
                        if (!isNaN(json.note)) {
                            note = parseInt(json.note) === parseInt(itemId);
                            if (note)
                                note = "Noted";
                            else
                                note = "Yes";
                        }
                       
                        var mult = 1.0;
                        if (json.id >= 14876 && json.id <= 14892) {
                            mult = 0.1;
                        }


            //console.log("data1: "+JSON.stringify(data));
                        var halch = format(Math.round(json.price * 0.9375 * mult));
            //console.log("data2: "+data);
                        var lalch = format(Math.round(json.price * 0.9375 * 0.85 * mult));
            //console.log("data3: "+json);
                        var shop;
            //console.log("data4: "+json.name);
                        if (json.fp && json.fp !== "") { // fixed prices
           
                            shop = format(json.price * mult);
// item info table
                        } else {
if (table.is(".item-info-table")) {
                            shop = format(Math.round(json.price * 0.6 * mult));
table.find("th").each(function() {
                        }
if (dc % 2 === 1) {
                       
var idx = parseInt(dc / 2);
                        if (idx > 0) {
if (idx > 0) {
                            if (idx == 1) // tradeable
$(this).text("idx: "+idx);
                                $(this).text(capitalize(json.trade));
}
                            else if (idx == 2) // equippable
}
                                $(this).text(json.slot === "6" ? "No" : "Yes");
dc++;
                            else if (idx == 3) // stackable
});
                                $(this).text(capitalize(json.stack));
                            else if (idx == 4) { // noteable
// bonuses table
                                $(this).text(note);
} else {
                                if (note === "Yes") { // add noted icon
// iterate over headers
                                    var iconEl = document.querySelector("table[data-item-id='" + json.id + "'] tbody tr:nth-child(2) th");
table.find("th").each(function() {
                                    var notedEl = iconEl.cloneNode(true);
if (hc === 0)
                                    notedEl = notedEl.firstElementChild;
$(this).text(json.name);
                                    notedEl.innerHTML = notedEl.innerHTML.replace("bottom:" + json.id%1000*64 + "px", "bottom:" + json.note%1000*64 + "px");
hc++;
                                    notedEl.innerHTML = notedEl.innerHTML.replace(/src=\".*\"/, "src=\"../img/itemicons/"+parseInt(json.note/1000)+"-64.png\"");
});
                                    iconEl.append(notedEl);
                                }
// iterate over table data
                            }
table.find("td").each(function() {
                            else if (idx == 5) // destroy
if (dc === 0)
                                $(this).text(json.destroy === "yes" ? "Destroy" : "Drop");
$(this).text(json.mlAcc);
                            else if (idx == 6) // general store
else if (dc === 1)
                                $(this).text(shop+" gp");
$(this).text(json.rnAcc);
                            else if (idx == 7) // low alch
else if (dc === 2)
                                $(this).text(lalch+" gp");
$(this).text(json.mgAcc);
                            else if (idx == 8) // high alch
else if (dc === 3)
                                $(this).text(halch+" gp");
$(this).text(json.mlDef);
                            else if (idx == 9) // weight
else if (dc === 4)
                                $(this).text(json.weight+" kg");
$(this).text(json.rnDef);
                            else if (idx == 10) // examine
else if (dc === 5)
                                $(this).text("You have a "+json.name+".");
$(this).text(json.mgDef);
                        }
else if (dc == 6)
                    }
$(this).text(json.pray);
                    dc++;
else if (dc == 7) {
                });
this.innerHTML = "";
               
var slotFile = slotToImg[json.slot];
            // bonuses table
var slotName = slotFile.substring(5, slotFile.length-8);
            } else {
var iconLink = document.createElement("a");
                // iterate over headers
var iconImg = document.createElement("img");
                table.find("th").each(function() {
$(iconLink).attr("href", "/wiki/File:"+slotName+"slot.png");
                    if (hc === 0)
$(iconLink).attr("class", "image");
                        $(this).text(json.name);
$(iconImg).attr("alt", slotName+"slot.png");
                    hc++;
$(iconImg).attr("src", "/wiki/images/thumb/"+slotFile+"/30px-"+slotName+"slot.png");
                });
$(iconImg).attr("width", "30");
               
$(iconImg).attr("height", "30");
                // iterate over table data
$(iconImg).attr("srcset", "/wiki/images/"+slotFile+" 1.5x, /wiki/images/"+slotFile+" 2x");
                table.find("td").each(function() {
iconLink.appendChild(iconImg);
                    if (dc === 0)
this.appendChild(iconLink);
                        $(this).text(json.mlAcc);
} else if (dc === 8)
                    else if (dc === 1)
$(this).text(json.mlStr);
                        $(this).text(json.rnAcc);
else if (dc === 9)
                    else if (dc === 2)
$(this).text(json.rnStr);
                        $(this).text(json.mgAcc);
else if (dc === 10)
                    else if (dc === 3)
$(this).text(json.mgStr);
                        $(this).text(json.mlDef);
else if (dc === 11)
                    else if (dc === 4)
$(this).text(json.degrade === "1" ? "Yes" : "No");
                        $(this).text(json.rnDef);
else if (dc === 12)
                    else if (dc === 5)
$(this).text(json.repair === "1" ? "Yes" : "No");
                        $(this).text(json.mgDef);
else if (dc === 13) {
                    else if (dc == 6)
if (json.req === "")
                        $(this).text(json.pray);
$(this).text("None");
                    else if (dc == 7)
else
                        $(this).text(json.hp);
$(this).text(json.req);
                    else if (dc == 8) {
} else if (dc === 14) {
                        var bonusBlock = Math.max(json.mlDef, json.rnDef, json.mgDef) * 0.01;
if (json.slot === "3")
                        var bonusText = (+bonusBlock + +json.blk).toFixed(1) + (+json.pblk > 0 ? " ("+json.pblk+"% PvP)" : "");
$(this).text(json.speed);
                        $(this).text(bonusText);
else
                    } else if (dc === 9)
$(this).text("-");
                        $(this).text(json.mlStr);
} else
                    else if (dc === 10)
$(this).text(dc+" data column");
                        $(this).text(json.rnStr);
                    else if (dc === 11)
dc++;
                        $(this).text(json.mgStr);
});
                    else if (dc === 12)
}
                        $(this).text(json.degrade === "1" ? "Yes" : "No");
                    else if (dc === 13)
                        $(this).text(json.repair === "1" ? "Yes" : "No");
                    else if (dc == 14) { // equipment slot icon
                        this.innerHTML = "";
                        var slotFile = slotToImg[json.slot];
                        var slotName = slotFile.substring(5, slotFile.length-8);
                        var iconLink = document.createElement("a");
                        var iconImg = document.createElement("img");
                        $(iconLink).attr("href", "/wiki/File:"+slotName+"slot.png");
                        $(iconLink).attr("class", "image");
                        $(iconImg).attr("alt", slotName+"slot.png");
                        $(iconImg).attr("src", "/wiki/images/thumb/"+slotFile+"/30px-"+slotName+"slot.png");
                        $(iconImg).attr("width", "30");
                        $(iconImg).attr("height", "30");
                        $(iconImg).attr("srcset", "/wiki/images/"+slotFile+" 1.5x, /wiki/images/"+slotFile+" 2x");
                        iconLink.appendChild(iconImg);
                        this.appendChild(iconLink);
                    } else if (dc === 15) {
                        if (json.req === "")
                            $(this).text("None");
                        else
                            $(this).text(json.req);
                    } else if (dc === 16) {
                        if (json.slot === "3")
                            $(this).text(json.speed);
                        else
                            $(this).text("-");
                    } else
                        $(this).text(dc+" data column");
                   
                    dc++;
                });
            }
         });
         });
     });
     });
});
});

Latest revision as of 18:04, 4 February 2025

/* Any JavaScript here will be loaded for all users on every page load. */

// TODO: Check whether noted icon is in the same file as unnoted icon.

// mapping slot id to a file
var slotToImg = [
    "c/ca/Helmetslot.png",
    "d/d7/Capeslot.png",
    "8/85/Amuletslot.png",
    "a/aa/Weaponslot.png",
    "9/94/Bodyslot.png",
    "9/91/Shieldslot.png",
    "Arms",
    "5/5e/Legsslot.png",
    "",
    "8/89/Glovesslot.png",
    "f/f8/Bootsslot.png",
    "",
    "c/ce/Ringslot.png",
    "5/54/Ammoslot.png",
    "7/77/Wingsslot.png",
    "d/d5/Auraslot.png"
];

$(document).ready(function() {
    function format(x) {
        return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
    function capitalize(x) {
        return x.replace(/\b\w/g, function(l){return l.toUpperCase()});
    }

    $(".item-image").each(function() {
        $(this).find("pre").replaceWith($(this).find("img"));
        var img = $(this).find("img")
        var desc = $(this).attr("class").split("'")[1];
        img.attr("alt", desc);
        img.attr("title", desc);
    });

    $(".item-bonus-table, .item-info-table").each(function() {
        var table = $(this);
        var itemId = $(this).data("item-id");
        
        $.get( "../scripts/fetch_item.php", { id: itemId }).done(function(json) {
            var hc = 0, dc = 0;
            
            // item info table
            if (table.is(".item-info-table")) {
                //console.log("loading item: "+json.id);
                table.find("th").each(function() {
                    if (dc % 2 === 1) {
                        var idx = parseInt(dc / 2);
                        var note = "No";
                        if (!isNaN(json.note)) {
                            note = parseInt(json.note) === parseInt(itemId);
                            if (note)
                                note = "Noted";
                            else
                                note = "Yes";
                        }
                        
                        var mult = 1.0;
                        if (json.id >= 14876 && json.id <= 14892) {
                            mult = 0.1;
                        }

                        var halch = format(Math.round(json.price * 0.9375 * mult));
                        var lalch = format(Math.round(json.price * 0.9375 * 0.85 * mult));
                        var shop;
                        if (json.fp && json.fp !== "") { // fixed prices
                            shop = format(json.price * mult);
                        } else {
                            shop = format(Math.round(json.price * 0.6 * mult));
                        }
                        
                        if (idx > 0) {
                            if (idx == 1) // tradeable
                                $(this).text(capitalize(json.trade));
                            else if (idx == 2) // equippable
                                $(this).text(json.slot === "6" ? "No" : "Yes");
                            else if (idx == 3) // stackable
                                $(this).text(capitalize(json.stack));
                            else if (idx == 4) { // noteable
                                $(this).text(note);
                                if (note === "Yes") { // add noted icon
                                    var iconEl = document.querySelector("table[data-item-id='" + json.id + "'] tbody tr:nth-child(2) th");
                                    var notedEl = iconEl.cloneNode(true);
                                    notedEl = notedEl.firstElementChild;
                                    notedEl.innerHTML = notedEl.innerHTML.replace("bottom:" + json.id%1000*64 + "px", "bottom:" + json.note%1000*64 + "px");
                                    notedEl.innerHTML = notedEl.innerHTML.replace(/src=\".*\"/, "src=\"../img/itemicons/"+parseInt(json.note/1000)+"-64.png\"");
                                    iconEl.append(notedEl);
                                }
                            }
                            else if (idx == 5) // destroy
                                $(this).text(json.destroy === "yes" ? "Destroy" : "Drop");
                            else if (idx == 6) // general store
                                $(this).text(shop+" gp");
                            else if (idx == 7) // low alch
                                $(this).text(lalch+" gp");
                            else if (idx == 8) // high alch
                                $(this).text(halch+" gp");
                            else if (idx == 9) // weight
                                $(this).text(json.weight+" kg");
                            else if (idx == 10) // examine
                                $(this).text("You have a "+json.name+".");
                        }
                    }
                    dc++;
                });
                
            // bonuses table
            } else {
                // iterate over headers
                table.find("th").each(function() {
                    if (hc === 0)
                        $(this).text(json.name);
                    hc++;
                });
                
                // iterate over table data
                table.find("td").each(function() {
                    if (dc === 0)
                        $(this).text(json.mlAcc);
                    else if (dc === 1)
                        $(this).text(json.rnAcc);
                    else if (dc === 2)
                        $(this).text(json.mgAcc);
                    else if (dc === 3)
                        $(this).text(json.mlDef);
                    else if (dc === 4)
                        $(this).text(json.rnDef);
                    else if (dc === 5)
                        $(this).text(json.mgDef);
                    else if (dc == 6)
                        $(this).text(json.pray);
                    else if (dc == 7)
                        $(this).text(json.hp);
                    else if (dc == 8) {
                        var bonusBlock = Math.max(json.mlDef, json.rnDef, json.mgDef) * 0.01;
                        var bonusText = (+bonusBlock + +json.blk).toFixed(1) + (+json.pblk > 0 ? " ("+json.pblk+"% PvP)" : "");
                        $(this).text(bonusText);
                    } else if (dc === 9)
                        $(this).text(json.mlStr);
                    else if (dc === 10)
                        $(this).text(json.rnStr);
                    else if (dc === 11)
                        $(this).text(json.mgStr);
                    else if (dc === 12)
                        $(this).text(json.degrade === "1" ? "Yes" : "No");
                    else if (dc === 13)
                        $(this).text(json.repair === "1" ? "Yes" : "No");
                    else if (dc == 14) { // equipment slot icon
                        this.innerHTML = "";
                        var slotFile = slotToImg[json.slot];
                        var slotName = slotFile.substring(5, slotFile.length-8);
                        var iconLink = document.createElement("a");
                        var iconImg = document.createElement("img");
                        $(iconLink).attr("href", "/wiki/File:"+slotName+"slot.png");
                        $(iconLink).attr("class", "image");
                        $(iconImg).attr("alt", slotName+"slot.png");
                        $(iconImg).attr("src", "/wiki/images/thumb/"+slotFile+"/30px-"+slotName+"slot.png");
                        $(iconImg).attr("width", "30");
                        $(iconImg).attr("height", "30");
                        $(iconImg).attr("srcset", "/wiki/images/"+slotFile+" 1.5x, /wiki/images/"+slotFile+" 2x");
                        iconLink.appendChild(iconImg);
                        this.appendChild(iconLink);
                    } else if (dc === 15) {
                        if (json.req === "")
                            $(this).text("None");
                        else
                            $(this).text(json.req);
                    } else if (dc === 16) {
                        if (json.slot === "3")
                            $(this).text(json.speed);
                        else
                            $(this).text("-");
                    } else
                        $(this).text(dc+" data column");
                    
                    dc++;
                });
            }
        });
    });
});