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

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

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

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

--------------------------------------------------------------------------------
-- Builds a link to any playable character's skill.
--
-- @TODO: handle enemy skills?!
--------------------------------------------------------------------------------
function p._link(alias, display)
    local skill = links[alias]
    local fmt = "[[%s#%s|%s]]"

    if not skill then
        error("the skill '" .. alias .. "' does not exist", 0)
    end

    return fmt:format(skill.target, skill.name, display or skill.name)
end

function p.link(frame)
    local args = getArgs(frame)
    local success, response = pcall(p._link, args[1], args[2])

    if success then
        return response
    end

    return userError(response, "Improper MARDEK Skill Link Usage")
end

--------------------------------------------------------------------------------
-- Use item data to generate a list of available skills for each playable
-- character.
--
-- @see: [[Template:Reactions]]
--
-- @TODO:
-- Rewrite, preferably so we don't need to manually spell out which items teach
-- every skill.
--------------------------------------------------------------------------------
function p.limitations(frame)
    local args = getArgs(frame)
    local container = mw.html.create("div")
        :css("display", "flex")
        :css("flex-wrap", "wrap")
        :css("margin", "0.5em -0.5em")

    -- Add a table for each kind of skill
    for _, skillType in ipairs(data) do
        local header = ("[[%s|link=%s|20px]] [[%s]]"):format(
            skillType.image,
            skillType.name,
            skillType.name
        )
        local skillTable = container:tag("div")
            :css("margin", "0.5em")
            :css("width", "calc(50% - 1em)")
            :tag("table")
                :addClass("article-table")
                :css("margin", "0")
                :css("table-layout", "fixed")
                :css("width", "100%")
                :tag("caption")
                    :wikitext(header)
                    :done()
                :tag("tr")
                    :tag("th")
                        :attr("scope", "col")
                        :wikitext("Skill name")
                        :done()
                    :tag("th")
                        :attr("scope", "col")
                        :wikitext("Learned from")
                        :done()
                    :done()

        -- Add a row for each skill
        for _, skill in ipairs(skillType.skills) do
            local itemList = "N/A"

            if args[skill] then
                itemList = {}

                -- Add a link for each item listed
                for itemName in mw.text.gsplit(args[skill], "%s*,%s*") do
                    local item = items[itemName]
                    local link = userError(
                        "that item does not exist",
                        "Improper MItemLink Usage"
                    )

                    if item then
                        local fmt = "[[%s|%s|link=%s]]"
                        local altText = (item.type == "PLOT" and "★ " or "") .. item.name

                        link = fmt:format(item.sprite, altText, item.article)
                    end

                    table.insert(itemList, link)
                end

                itemList = table.concat(itemList, " ")
            end

            skillTable:tag("tr")
                :tag("td")
                    :wikitext(p._link(skill))
                    :done()
                :tag("td")
                    :wikitext(itemList)
                    :done()
                :done()
        end

        skillTable:done():done()
    end

    return tostring(container)
end

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