Module:Challenges

From Idle Wizard Wiki
Jump to navigation Jump to search

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

--<nowiki>
--Note that the syntax highlighting in the Module page can be broken, you can click on 'Edit' to see the code with proper highlighting.
local p = {}

local challenges = require("Module:Data/Challenges").challenges


function list2Text(list)
    if list == nil then
        return ""
    else
        return table.concat(list, "\n\n")
    end
end

function findNext(challenge_name)
    for name, c in pairs(challenges) do
        if c.Previous == challenge_name then
            return "[[" .. name .. "]]"
        end
    end
    return ""
end

function formatMinutes(minutes)
    if minutes == nil then
        return ""
    elseif minutes >= 60 then
        h = minutes / 60
        m = minutes % 60
        if m == 0 then
            return h .. "h"
        else
            return string.format("%dh %02dm", h, m)
        end
    else
        return minutes .. "m"
    end
end

function linkIfNotRandom(str)
    if str == nil then
        return "Any"
    elseif str == "Random" or str == "Unknown" then
        return str
    else
        return "[[" .. str .. "]]"
    end
end


local challenge_template = [=[<infobox>
    <title><default>%s</default></title>
    <data>
        <label>Requirements</label>
        <default>%s</default>
    </data>
    <data>
        <label>Class</label>
        <default>%s</default>
    </data>
    <data>
        <label>Pet</label>
        <default>%s</default>
    </data>
    <data>
        <label>Time Limit</label>
        <default>%s</default>
    </data>
    <data>
        <label><abbr title="Can you use items?">Items</abbr></label>
        <default>%s</default>
    </data>
    <data>
        <label><abbr title="Are VIP, buffs and market available?">Market</abbr></label>
        <default>%s</default>
    </data>
    <data>
        <label><abbr title="Do current mysteries increase production?">Mysteries</abbr></label>
        <default>%s</default>
    </data>
    <data>
        <label><abbr title="Can you earn achievements and mysteries, increase total stats and persistent spell casts?">Track Stats</abbr></label>
        <default>%s</default>
    </data>
    <data>
        <label>Rules</label>
        <default>%s</default>
    </data>
    <data>
        <label>Objectives</label>
        <default>%s</default>
    </data>
    <data>
        <label>Reward</label>
        <default>%s</default>
    </data>
    <group layout="horizontal" show="incomplete">
        <data>
            <label>Previous</label>
            <default>%s</default>
        </data>
        <data>
            <label>Next</label>
            <default>%s</default>
        </data>
    </group>
</infobox>
{{#if:{{NAMESPACE}} | |[[Category:Challenges]]}}
]=]
function p.GetChallengeTemplate(frame)
    local challenge_name = frame.args[1] or mw.title.getCurrentTitle().text
    local challenge = challenges[challenge_name]
    if challenge == nil then
        mw.log(challenge_name)
        return "''Challenge infobox data not found for \"" .. challenge_name .. "\". Check the parameter or add the data to [[Module:Data/Challenges]].''"
    end

    return frame:preprocess(string.format(challenge_template,
        challenge_name,
        list2Text(challenge.Requirements),
        linkIfNotRandom(challenge.Class),
        linkIfNotRandom(challenge.Pet),
        formatMinutes(challenge.TimeLimit),
        challenge.Items and "Yes" or "No",
        challenge.Market and "Yes" or "No",
        challenge.Mysteries and "Yes" or "No",
        challenge.Stats and "Yes" or "No",
        list2Text(challenge.Rules),
        list2Text(challenge.Objectives),
        challenge.Reward,
        challenge.Previous and ("[[" .. challenge.Previous .. "]]") or "",
        findNext(challenge_name)
    ))
end

local table_head = [=[{| class="mw-collapsible sortable wikitable" style="width:100%" id="Challenges" border="0" cellpadding="10"
! colspan="4" |Challenges
|-
! scope="col" data-sort-type="number"|Id
! scope="col" data-sort-type="text"|Name
! scope="col" data-sort-type="text"|Class
! scope="col" data-sort-type="text"|Reward
]=]
local table_row = [=[|-
|%s
|[[%s]]
|%s
|%s
]=]
function p.GetChallengeList(frame)
    local all_challenges = {}
    for challenge_name, challenge in pairs(challenges) do
        challenge.Name = challenge_name
        table.insert(all_challenges, challenge)
    end
    table.sort(all_challenges, function(a, b) return a.Id < b.Id end)

    local table = table_head
    for _, challenge in pairs(all_challenges) do
        table = table .. string.format(table_row,
            challenge.Id,
            challenge.Name,
            challenge.Class or "",
            challenge.Reward
        )
    end
    table = table .. '|}'

    return table
end

return p
--</nowiki>
--[[Category:Lua]]