Module:ContractChainTiles

From Dune: Awakening Community Wiki
Jump to navigation Jump to search

Module:ContractChainTiles

Renders the Contract Chains belonging to a Contract Group as clickable ItemTiles, for use by Template:ContractChainTiles.

This module should not be invoked directly. To list Contract Chains, use Template:ContractChainTiles.

This module provides the following function(s):

  • main - queries DPL3 for every Contract Chain in a Contract Group and renders each as an ItemTile inside a flex container.

Usage

Render Contract Chains for a group

{{#invoke:ContractChainTiles|main|Tradepost - The Anvil}}

Parameters

main

1
string
The Contract Group name, matching the group field of the Contract Chains' Template:QuestInfobox/ContractChain infoboxes. Queried via the category Contract Chains (<GROUP>).
Returns an error message if not specified or if no Contract Chains are found.

Notes

  • Each tile's image is taken from the Contract Chain's image field. If the page has no Contract Chain image, the first Contract image found is used instead.
  • If a Contract Chain contains both the Atreides and Harkonnen icons, the hybrid faction icon (Icon - Contract Faction.png) is used in place of either.
  • Results are passed through Template:ItemTile/Quest and wrapped in Template:FlexContainer.

local p = {}

local common = require("Module:Common")

-- ---------- Data ----------

local ATREIDES_ICON = "T_UI_IconContracts_Atre_D.png"
local HARKONNEN_ICON = "T_UI_IconContracts_Hark_D.png"
local HYBRID_ICON = "Icon - Contract Faction.png"

local FIELD_SEPARATOR = "~"
local DPL_QUERY = '{{ #dpl: category = Contract Chains (%s) | include = {QuestInfobox/ContractChain}:image, {QuestInfobox/Contract}:image | format = ,%%TITLE%%' .. FIELD_SEPARATOR .. ',\\n, | redirects = include }}'

-- ---------- Processing ----------

-- splits a concatenated string of filenames into an array, e.g. "A.pngB.png" -> { "A.png", "B.png" }
local function splitImages(s)
    local images = {}
    for image in s:gmatch("(.-%.[Pp][Nn][Gg])") do
        image = common.trim(image)
        if image ~= "" then images[#images + 1] = image end
    end
    return images
end

-- decides which image to use if the contract chain page has more than 1 infobox
local function resolveImage(images)
    if #images == 0 then return nil end

    if #images > 1
        and common.listContains(images, ATREIDES_ICON)
        and common.listContains(images, HARKONNEN_ICON) then
        return HYBRID_ICON
    end

    return images[1]
end

-- ---------- Interface ----------

function p.main(frame)
    local args = frame.args
    local group = common.trim(args[1] or "")
    if group == "" then return common.renderError("No contract group specified") end

    local raw = common.trim(frame:preprocess(string.format(DPL_QUERY, group)))
    if raw == "" then return common.renderError("No results for group: <code>" .. group .. "</code>") end

    local tiles = {}
    for line in raw:gmatch("[^\r\n]+") do
        line = common.trim(line)
        if line ~= "" then
            local pos = line:find(FIELD_SEPARATOR, 1, true)
            if pos then
                local title = common.trim(line:sub(1, pos - 1))
                local images = splitImages(line:sub(pos + 1))

                if title ~= "" then
                    tiles[#tiles + 1] = frame:expandTemplate{
                        title = "ItemTile/Quest",
                        args = { image = resolveImage(images), title = title },
                    }
                end
            end
        end
    end

    return frame:expandTemplate{
        title = "FlexContainer",
        args = { table.concat(tiles, "\n") },
    }
end

return p