Module:Cargo orders

From Space Station 14 Wiki
Revision as of 23:18, 18 May 2025 by Aliser (talk | contribs) (typo)
Module documentation
View or edit this documentation (about module documentation)

Implements {{Cargo orders}}.


local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs
local yesNo = require('Module:Yesno')

local orders_table =  mw.loadJsonData("Module:Cargo orders/data/auto/orders.json")

-- =======================


local function numeric_table_length(t)
    local count = 0
    for _ in ipairs(t) do count = count + 1 end
    return count
end

local function table_length(t)
    local count = 0
    for _ in pairs(t) do count = count + 1 end
    return count
end

local function table_has_value(tab, val)
    for _, value in ipairs(tab) do
        if value == val then
            return true
        end
    end

    return false
end

local function assert_value_not_nil(value, error_message)
    if value == nil then
        if error_message == nil then
            error("value is nil")
        else
            error(error_message)
        end
    end
end

-- Makes the first letter uppercase.
-- Source: https://stackoverflow.com/a/2421746
local function capitalize(str)
    return (str:gsub("^%l", string.upper))
end

-- Makes the first letter in every word uppercase.
local function capitalize_all(str)
    local res = { }
    for _, word in ipairs(mw.text.split(str, ' ', true)) do
        table.insert(res, capitalize(word))
    end
    return table.concat(res, " ")
end

local function passthrough_assert_true(value, valueToReturnIfTrue, errorMessageOnFalse)
    if value then
        return valueToReturnIfTrue
    else
        error(errorMessageOnFalse)
    end
end


-- Lookup a field in a json table by key.
-- Json tables are assumed to be tables with lowercase keys. The `key` is automatically lowercased for the lookup.
-- If a value is a string, it's treated as an alias (another key), triggering a new lookup.
-- 
-- Returns the value by the specified key, or by aliased key if the value was an alias.
-- If value doesn't exist, returns `nil`.
local function lookup_json_table(t, key)
	local result = t[string.lower(key)]
	if type(result) == "string" then
		-- alias. lookup actual definition.
		return t[result]
	else
		return result
	end
end

-- ===========================

function p.main(frame)
    local args = getArgs(frame)

    -- =======================

    local container = mw.html.create('div')

    local orders_section = mw.html.create('h2')
        :wikitext("Orders")
    container:node(orders_section)

    local category_entries_by_category = {}
    for _, entry in ipairs(orders_table) do
        local category = entry.category;
        if category ~= nil then
            local category_entry = category_entries_by_category[category]
            if category_entry == nil then
                local category_section = mw.html.create('h3')
                    :wikitext(category)
                container:node(category_section)

                local category_table = mw.html.create('table')
                    :addClass("wikitable")
                container:node(category_table)

                local category_table_h = category_table:tag('tr')
                category_table_h:tag('th'):wikitext("Product")
                category_table_h:tag('th'):wikitext("Unit cost")
                category_table_h:tag('th'):wikitext("Description")

                category_entry = {
                    category_section = category_section,
                    category_table = category_table
                }

                category_entries_by_category[category] = category_entry
            end

            local row = category_entry.category_table:tag('tr')
            row:tag('td'):wikitext(frame:expandTemplate{ title = "item", args = { entry.product }})
            row:tag('td'):wikitext(frame:expandTemplate{ title = "spesos", args = { entry.cost }})
            row:tag('td'):wikitext(entry.description)
        end

    end
    
    return container
end

return p