FANDOM


--<nowiki>
 
--------------------------------------------------
-- private
 
local assert = assert
local ipairs = ipairs
local require = require
local table = table
local mw = mw
local lang = mw.language.getContentLanguage()
local data = mw.loadData("Module:Talent/data")
local getArgs = require("Dev:Arguments").getArgs
 
--- Returns the talent tha has the target symbol or nil.
local function getTalentFromSymbol(symbol)
    for _, talent in ipairs(data) do
        if talent.symbol == symbol then
            return talent
        end
    end
    return nil
end
 
--- Returns an iterator of talents.
-- The category is optional.
-- The talents are ordered by not needing gold, then by lowest level.
local function getTalentsIterator(category)
    if category then
        category = lang:lc(category)
        if category == "adventures" then
            category = "adventure"
        elseif category == "swindle" then
            category = "hoodwink"
        end
    end
    local talents = {}
    for _, talent in ipairs(data) do
        if not category or talent.display.category == category then
            local ispremium = (talent.requires.gold or 0) > 0
            local level = talent.requires.level or 0
            local i = 1
            while talents[i] do
                local i_ispremium = (talents[i].requires.gold or 0) > 0
                local i_level = talents[i].requires.level or 0
                if ispremium == i_ispremium then
                    if level < i_level then
                        break
                    end
                else
                    if not ispremium then
                        break
                    end
                end
                i = i + 1
            end
            table.insert(talents, i, talent)
        end
    end
    local i = 1
    return function ()
        while talents[i] do
            local talent = talents[i]
            i = i + 1
            return talent
        end
        return nil -- stop iteration
    end
end
 
--- Non-standard images. Map image symbol to image file.
local _talentImages = {
    spy_counterintelligence = "Talent Spy Counter-intelligence.png", -- no space after "-"
    fight_guerilla_tactics = "Talent Fight Guerilla Tactics.png", -- one "r" is missing
    stark_winter_is_coming = "Talent Stark Winter Is Coming.png", -- not lowercase "is"
    stark_descendants_of_first_men = "Talent Stark Descendents of the First Men.png", -- extra "the"
    martell_desert_trek = "Talent Martell Desert Trek.png", -- not "Nomad's Strongbox"
    martell_unbowed_unbroken = "Talent Martell Unbowed Unbroken.png", -- no ", Unbent,"
    martell_nymeria = "Talent Martell Nymeria.png", -- no "Queen"
    tyrell_kings_of_the_reach = "Talent_Tyrell_Kings_of_the_Reach.png", -- not "Stewards"
    tyrell_gardens_thorns = "Talent Tyrell Gardens Thorns.png", -- not "Garden's"
    tully_family_duty_honor = "Talent Tully Family Duty Honour.png", -- no ","
}
 
--- Returns the name of the image for the talent
local function getTalentImage(talent)
    assert(talent)
    local category = talent.display.category
    local name = talent.name
    local image = talent.image
    if _talentImages[image] then
        return _talentImages[image]
    end
    if category == "adventure" then
        category = "adventures"
    elseif category == "hoodwink" then
        category = "swindle"
    end
    return "Talent " .. lang:ucfirst(category) .. " " .. talent.name .. ".png"
end
 
--- Returns what the talent requires
local function getTalentRequires(talent)
    assert(talent)
    local requires = talent.requires
    local arr = {}
    if requires.level then
        table.insert(arr, "player level " .. requires.level)
    end
    if requires.talent then
        local required_talent = getTalentFromSymbol(requires.talent)
        table.insert(arr, "'''" .. required_talent.name .. "'''")
    end
    if table.getn(arr) == 0 then
        return "nothing"
    else
        return mw.text.listToText(arr)
    end
end
 
local _formatText = {}
local function formatText(text)
    assert(text ~= nil)
    if #_formatText == 0 then
        local frame = mw.getCurrentFrame()
        if frame then
            -- cache items
            _formatText.silver = frame:expandTemplate{title = "Silver"}
            _formatText.gold = frame:expandTemplate{title = "Gold"}
        end
    end
    local silver = _formatText.silver or "silver"
    local gold = _formatText.gold or "gold"
    text = " " .. text .. " " -- add extra spaces
    text = text:gsub(" [Ss]ilver ", " " .. silver .. " ")
    text = text:gsub(" gold ", " " .. gold .. " ")
    return text:sub(2, -2) -- remove extra spaces
end
 
--------------------------------------------------
-- public
 
local p = {}
 
--- Renders a table for the [[Talents]] page.
-- The table frame argument is optional.
-- The category frame argument is optional.
function p.renderTalentsTable(frame)
    local args = getArgs(frame)
    local arr = {}
    table.insert(arr, "{|" .. (args.table or ""))
    for talent in getTalentsIterator(args.category) do
        table.insert(arr, "|-")
        table.insert(arr, "|[[File:" .. getTalentImage(talent) .. "|100px|link=|link=]]")
        table.insert(arr, "|'''" .. talent.name .. "'''")
        table.insert(arr, formatText(talent.description))
        table.insert(arr, "* Requires: " .. getTalentRequires(talent))
        table.insert(arr, "* Max talent level: " .. talent.max_level)
        if talent.requires.gold then
            table.insert(arr, "* [[Premium Talents|Premium Talent]]: " .. talent.requires.gold .. " " .. formatText("gold"))
        end
    end
    table.insert(arr, "|}")
    return table.concat(arr, "\n")
end
 
--- Renders a table for each "House XXX/Fealty Talents" page
-- The table frame argument is optional.
-- The house frame argument is required.
function p.renderHouseTalentsTable(frame)
    local args = getArgs(frame)
    if not args.house then
        return error("house argument is required")
    end
    local arr = {}
    table.insert(arr, "{|" .. (args.table or ""))
    for talent in getTalentsIterator(args.house) do
        table.insert(arr, "|-")
        table.insert(arr, "|[[File:" .. getTalentImage(talent) .. "|100px|link=|link=]]")
        local line = {}
        table.insert(line, "|'''" .. talent.name .. "'''")
        if talent.requires.gold then
            table.insert(line, "[[File:Locked_Icon.png|right]]" .. formatText(talent.description))
        else
            table.insert(line, formatText(talent.description))
        end
        table.insert(line, "Requires: " .. getTalentRequires(talent))
        table.insert(line, "Max talent level: " .. talent.max_level)
        if talent.requires.gold then
            table.insert(line, "'''[[Premium Talents|Premium Talent]]: Unlocked for " .. talent.requires.gold .. " " .. formatText("gold") .. "'''")
        end
        table.insert(arr, table.concat(line, "<br />"))
    end
    table.insert(arr, "|}")
    return table.concat(arr, "\n")
end
 
return p
 
--</nowiki>
-- [[Category:Modules]]

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.