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):

  • get - renders a vehicle infobox using Template:VehicleInfobox.
  • type - validates the provided vehicle type and outputs the corresponding MediaWiki category if valid.

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}}

Safe wiki category output

Vehicle types are validated against VEHICLE_TYPES below. If valid, the corresponding MediaWiki category is returned. Ignored, invalid or unknown types produce no output.

{{#invoke:Vehicles|type|Ground}}

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.

type

1
string
A vehicle type. This is slugified before lookup against VEHICLE_TYPES below.

Notes

get

  • Display strings (name, vehicle type, and category label) 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

-- define valid vehicle types for wiki category emission on Vehicle pages, allowing types like Special to be defined in /data but not emitted
local VEHICLE_TYPES = {
    ground = "Ground Vehicles",
    aerial = "Aerial Vehicles",
}

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

local function renderInfobox(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 out = {}
    out[#out + 1] = "{{VehicleInfobox"

    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] = "|category=" .. englishCategory
    out[#out + 1] = "|display_category=" .. displayCategory
    out[#out + 1] = "|minimum_tier=" .. vehicle.minimum_tier
    out[#out + 1] = "|crew_capacity=" .. vehicle.crew_capacity

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

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

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

    local key = common.slugify(raw)

    local vehicle = FLAT_DATA[key]
    if not vehicle then return common.error("Unknown vehicle: %s", raw) end

    return frame:preprocess(renderInfobox(key, vehicle))
end

function p.type(frame)
    local vehicleType = common.slugify(frame.args[1] or "")
    local cat = VEHICLE_TYPES[vehicleType]

    if cat then
        return "[[Category:" .. cat .. "]]"
    end

    return
end

return p