Module:DLC

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

Module:DLC

Renders a DLC infobox using Template:DLCInfobox, sourced from Module:DLC/data with translatable display strings from MessageBundle:DLC.

This module provides the following function(s):

  • get - renders an infobox for a single DLC.
  • latest - renders an infobox for the newest DLC.

Usage

Render infobox for current DLC page

On a DLC page, where the page title matches (via slugification) a DLC key in the dataset:

{{#invoke:DLC|get|{{BASEPAGENAME}}}}

Render infobox for a specific DLC

A specific DLC name can also be provided manually:

{{#invoke:DLC|get|Wildlife of Arrakis}}

Render infobox for the latest DLC

Renders whichever DLC currently has the latest release date in the dataset:

{{#invoke:DLC|latest}}

Include contents list

An optional contents parameter can be passed through to the infobox, typically a bulleted list:

{{#invoke:DLC|get|Wildlife of Arrakis
|contents = 
* [[Wildlife]]-themed Decorations
}}

Parameters

get

1 (required)
string
The DLC name. This is slugified before lookup, so it does not need to exactly match the key casing/formatting used in Module:DLC/data.
Returns an error message if no DLC is specified, or if the slugified name has no match in the dataset.
contents (optional)
string
Wikitext passed through to the infobox's Contents field. Omitted entirely if not provided.

latest

<No parameters>

Notes

get

  • Display strings are resolved via MessageBundle:DLC using the page's current language, with the English source also resolved separately for use as internal/category-facing values.

latest

  • The latest DLC item is determined by the latest date value in the dataset.
  • If multiple entries share that release date, one is selected at random, and this choice may change whenever the page is reparsed (e.g. edit / purge / cache expiry).

local p = {}

local common = require("Module:Common")
local data = require("Module:DLC/data")

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

local function parseDate(dateStr)
    local year, month, day = dateStr:match("^(%d+)-(%d+)-(%d+)$")
    return os.time({ year = tonumber(year), month = tonumber(month), day = tonumber(day) })
end

local FLAT_DATA = {}
local ORDERED = {}

for dlcType, items in pairs(data) do
    for id, item in pairs(items) do
        item.type = dlcType
        item.timestamp = parseDate(item.date)

        FLAT_DATA[id] = item
        ORDERED[#ORDERED + 1] = { id = id, item = item }
    end
end

table.sort(ORDERED, function(a, b)
    return a.item.timestamp > b.item.timestamp
end)

math.randomseed(os.time())

-- ---------- Helpers ----------

-- returns all items sharing the most recent date
local function getLatestGroup()
    local topDate = ORDERED[1].item.date
    local group = {}

    for _, entry in ipairs(ORDERED) do
        if entry.item.date == topDate then
            group[#group + 1] = entry
        else
            break
        end
    end
    return group
end

-- ---------- Rendering ----------

local function renderDate(dateStr, pageLanguage)
    return pageLanguage:formatDate("j F Y", dateStr)
end

local function renderInfobox(id, item, contents, latest)
    local pageLanguage = common.getPageLanguage()

    local displayName = common.translate("DLC", id .. "-name")
    local englishName = common.translate("DLC", id .. "-name", "en")

    local displayType = common.translate("DLC", "type-" .. item.type)
    local englishType = common.translate("DLC", "type-" .. item.type, "en")

    local out = {}
    out[#out + 1] = "{{DLCInfobox"

    if latest then out[#out + 1] = "|title_header=Latest Downloadable Content" end  -- this is for DLCWidget on Main Page so doesn't need to be localised

    out[#out + 1] = "|title=" .. englishName
    out[#out + 1] = "|display_title=" .. displayName
    out[#out + 1] = "|type=" .. englishType
    out[#out + 1] = "|display_type=" .. displayType
    out[#out + 1] = "|release_date=" .. renderDate(item.date, pageLanguage)

    if item.steam then out[#out + 1] = "|steam=" .. item.steam end
    if item.xbox then out[#out + 1] = "|xbox=" .. item.xbox end
    if item.ps then out[#out + 1] = "|ps=" .. item.ps end

    if contents then out[#out + 1] = "|contents=" .. contents end

    out[#out + 1] = "}}"
    return table.concat(out, "\n")
end

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

function p.get(frame)
    local args = frame.args

    local raw = common.trim(args[1])
    if raw == "" then return common.inputError("No DLC specified") end

    local key = common.slugify(raw)

    local item = FLAT_DATA[key]
    if not item then return common.inputError("Unknown DLC: <code>%s</code>", raw) end

    local contents = args.contents
    return frame:preprocess(renderInfobox(key, item, contents))
end

function p.latest(frame)
    local group = getLatestGroup()
    local pick = group[math.random(#group)]

    return frame:preprocess(renderInfobox(pick.id, pick.item, frame.args.contents, true))
end

return p