Module:Vehicles

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

Module:Vehicles

Provides structured access to vehicle data defined in Module:Vehicles/data.

This module provides the following function(s):

Usage

Render infobox for current vehicle page

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

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

Render infobox for a specific vehicle

A specific vehicle key can also be provided manually:

{{#invoke:Vehicles|get|Sandbike}}

Parameters

get

1
string
The vehicle name. This is slugified before lookup, so it does not need to exactly match the key casing/formatting used in Module:Vehicles/data.
Returns an error message if no vehicle is specified, or if the slugified name has no match in the dataset.

Notes

get

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

local p = {}

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

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

local FLAT_DATA = {}
for vehicleType, items in pairs(data) do
    for id, item in pairs(items) do
        item.type = vehicleType
        FLAT_DATA[id] = item
    end
end

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

local function renderInfobox(frame, id, vehicle)
    local displayName = common.translate("Vehicles", id .. "-name")
    local englishName = common.translate("Vehicles", id .. "-name", "en")

    local displayType = common.translate("Vehicles", "type-" .. vehicle.type)
    local englishType = common.translate("Vehicles", "type-" .. vehicle.type, "en")

    local displayCategory = common.translate("Vehicles", "category-" .. vehicle.category)
    local englishCategory = common.translate("Vehicles", "category-" .. vehicle.category, "en")

    local args = {
        title            = englishName,
        display_title    = displayName,
        type             = englishType,
        display_type     = displayType,
        category         = englishCategory,
        display_category = displayCategory,
        minimum_tier     = vehicle.minimum_tier,
    }

    if vehicle.crew_capacity then args.crew_capacity = vehicle.crew_capacity end

    return frame:expandTemplate{
        title = "VehicleInfobox",
        args = args
    }
end

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

function p.get(frame)
    local raw = common.trim(frame.args[1])
    if raw == "" then return common.renderError("No vehicle specified") end

    local key = common.slugify(raw)

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

    return renderInfobox(frame, key, vehicle)
end

return p