Documentation for this module may be created at Module:MARDEK Items/doc

-- <nowiki>
--------------------------------------------------------------------------------
-- This module provides tools for MARDEK item pages.
--------------------------------------------------------------------------------
local p = {}

-- Global modules
local getArgs = require("Dev:Arguments").getArgs
local userError = require("Dev:User error")

-- Local modules
local statusEffect = require("Module:MARDEK Status Effects")._effect

-- Data modules
local data = mw.loadData("Module:MARDEK Items/data")

--------------------------------------------------------------------------------
-- Describe the following function here.
--------------------------------------------------------------------------------
function p.infoIcon(frame)
    local args = getArgs(frame)
    local item = data[args[1]]

    if not item then
        return userError("that item does not exist", "Improper MItemLink Usage")
    end

    return item.infoIcon
end

--------------------------------------------------------------------------------
-- Helper function to build a resistance table (HTML) from a table (Lua).
--------------------------------------------------------------------------------
local function buildResistTable(dataTable)
    local htmlTable = mw.html.create("table")
        :addClass("sortable")
        :css("background-color", "#630")
        :css("border", "1px solid #300")
        :css("border-collapse", "collapse")
        :css("color", "white")
        :css("table-layout", "fixed")
        :css("text-align", "center")
        :css("width", "100%")

    for rowNo, dataRow in ipairs(dataTable) do
        local htmlRow = htmlTable:tag("tr")

        for columnNo, value in ipairs(dataRow) do
            local tag = "td"
            local attrs = {}
            local css = {
                border = "1px solid #300",
                padding = "0.5em 0"
            }
            local wikitext = value

            if rowNo == 1 then
                tag = "th"
                attrs.scope = "col"

                if columnNo == 1 then
                    css.width = "20%"
                end
            elseif type(value) == "number" then
                wikitext = (value > 0 and "+" or "") .. value .. "%"

                local weak = value < 0
                local resist = 0 < value and value <= 100
                local absorb = 100 < value

                if weak then
                    css["background-color"] = "#600"
                    css.color = "#c33"
                elseif resist then
                    css["background-color"] = "#090"
                    css.color = "#6cf"
                elseif absorb then
                    css["background-color"] = "#06c"
                    css.color = "#6f3"
                end
            end

            htmlRow:tag(tag)
                :attr(attrs)
                :css(css)
                :wikitext(wikitext)
                :done()
        end

        htmlRow:done()
    end

    return htmlTable
end

--------------------------------------------------------------------------------
-- Builds a table listing, for a given character, which items and skills they
-- can equip to gain elemental resistance.
--
-- @TODO: Add skills
--------------------------------------------------------------------------------
function p.elemental_resistance(frame)
    local args = getArgs(frame)
    local dataTable = {}
    local elements = {
        "Fire",
        "Water",
        "Air",
        "Earth",
        "Light",
        "Dark",
        "Aether",
        "Fig",
        "Physical",
        "Thauma"
    }

    local function glyph(element, size)
        local fmt = "[[File:Glyph %s.png|%s|link=%s|%ipx]]"

        return fmt:format(element, element, element, size)
    end

    -- Header
    do
        local header = {"Source"}

        for _, element in ipairs(elements) do
            table.insert(header, glyph(element, 20))
        end

        table.insert(dataTable, header)
    end

    -- Base resistance
    do
        local charElement = ({
            Mardek = {"Light", "Dark"},
            Deugan = {"Earth", "Earth"},
            Emela = {"Water", "Water"},
            Vehrn = {"Light", "Light"},
            Zach = {"Air", "Fire"},
            Donovan = {"Fire", "Water"},
            Sharla = {"Air", "Fire"},
            ["Sslen'ck"] = {"Earth", "Air"},
            Solaar = {"Light", "Dark"},
            Elwyen = {"Water", "Earth"},
            Gloria = {"Earth", "Air"},
            Meraeador = {"Fire", "Water"},
            Legion = {"Physical"}
        })[args[1]]

        local link = "[[" .. charElement[1] .. "|Base resistance]]"
        local source = glyph(charElement[1], 36) .. "<br>" .. link
        local row = {source}

        for _, element in ipairs(elements) do
            local resistance = 0

            if element == charElement[1] then
                resistance = 20
            elseif element == charElement[2] then
                resistance = -20
            end

            table.insert(row, resistance)
        end

        table.insert(dataTable, row)
    end

    -- Equipment
    for _, item in ipairs(data) do
        local canEquip = false
        local hasResistances = false
        local row = {}

        for _, character in ipairs(item.canEquip) do
            if character == args[1] then
                canEquip = true

                break
            end
        end

        for i, element in ipairs(elements) do
            local propertyName = "resist-" .. element:lower()
            local resistance = item.properties[propertyName]

            if resistance then
                hasResistances = true
            end

            row[i + 1] = resistance or 0
        end

        if canEquip and hasResistances then
            row[1] = item.infoIcon

            table.insert(dataTable, row)
        end
    end

    -- Skills
    -- @TODO

    return buildResistTable(dataTable)
end

--------------------------------------------------------------------------------
-- Builds a table listing, for a given character, which items and skills they
-- can equip to gain status resistance.
--
-- @TODO: Add skills
--------------------------------------------------------------------------------
function p.status_resistance(frame)
    local args = getArgs(frame)
    local dataTable = {}
    local effects = {
        "poison",
        "paralysis",
        "numbness",
        "silence",
        "curse",
        "confusion",
        "sleep",
        "blindness",
        "zombie",
        "bleed"
    }

    -- Header
    do
        local header = {"Source"}

        for _, effect in ipairs(effects) do
            table.insert(header, statusEffect(effect, true))
        end

        table.insert(dataTable, header)
    end

    -- Equipment
    for _, item in ipairs(data) do
        local canEquip = false
        local hasResistances = false
        local row = {}

        for _, character in ipairs(item.canEquip) do
            if character == args[1] then
                canEquip = true

                break
            end
        end

        for i, effect in ipairs(effects) do
            local resistance = item.properties["resist-" .. effect]

            if resistance then
                hasResistances = true
            end

            row[i + 1] = resistance or 0
        end

        if canEquip and hasResistances then
            row[1] = item.infoIcon

            table.insert(dataTable, row)
        end
    end

    -- Skills
    -- @TODO

    return buildResistTable(dataTable)
end

return p
-- </nowiki>
-- [[Category:Lua Modules]]
Community content is available under CC-BY-SA unless otherwise noted.