Module:Buildingbox2

From Against the Storm Official Wiki
Revision as of 02:06, 8 October 2024 by Aeredor (talk | contribs) (fixed rendering specializations private method)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Buildingbox2/doc

---
--- Retrieves data for the specified building, resource, perk, etc. and sends the data over to the view, another template.
---
--- @module Buildingbox2
local Buildingbox2 = {}



--region Dependencies

--Some dependencies in this module are loaded lazily.

local RecipeController = require("Module:RecipeController")

local ViewTemplate = "Template:Infobox"

--endregion



--region Private constants

local ARG_ID = "id"

local PARAM_TITLE = "title"
local PARAM_SUBTITLE = "subtitle"
local PARAM_DESCRIPTION = "description"

--endregion



--region Private methods

---renderSpecializations takes the ID of a building and looks up its specialization
---@param id table
function Buildingbox2.renderSpecializations(id)

    local SpecializationsData = mw.loadData("Module:SpecializationsData")
    local SpeciesData = mw.loadData("Module:SpeciesData")
    local frame = mw.getCurrentFrame()

    local listOfSpecializationsAtBuilding = SpecializationsData.buildingSpecializations[id]

    if not listOfSpecializationsAtBuilding then
        return nil
    end

    local listOfStringsToReturn = {}
    for i, specIndex in ipairs(listOfSpecializationsAtBuilding) do

        local specData = SpecializationsData.specializations[specIndex]

        local specializationString = frame:expandTemplate{
            title = "Specialization",
            args = {
                ["iconfilename"] = specData[SpecializationsData.ICON_FILENAME],
                ["name"] = specData[SpecializationsData.NAME],
                ["type"] = specData[SpecializationsData.IS_COMFORTABLE] and "Comfort" or "Proficiency",
                ["size"] = "medium",
            }, }

        local species = specData[SpecializationsData.SPECIES]

        local speciesString = frame:expandTemplate{
            title = "Species",
            args = {
                ["iconfilename"] = SpeciesData.species[species][SpeciesData.ICON_FILENAME] .. ".png",
                ["name"] = SpeciesData.species[species][SpeciesData.NAME],
                ["size"] = "medium",
                [1] = "notext",
            }, }

        table.insert(listOfStringsToReturn, specializationString .. " " .. speciesString)
    end

    if #listOfStringsToReturn < 1 then
        return "None"
    end

    return listOfStringsToReturn
end



function Buildingbox2.tryWorkshopsData(id)

    local WorkshopsData = require("Module:WorkshopsData")

    local name = WorkshopsData.getWorkshopNameByID(id)
    if not name then
        return false
    end

    local viewParameters = {}
    viewParameters[PARAM_TITLE] = name
    viewParameters[PARAM_SUBTITLE] = "Production Building (" .. WorkshopsData.getWorkshopCategory(name) .. ")"
    viewParameters[PARAM_DESCRIPTION] = WorkshopsData.getWorkshopDescription(name)
    viewParameters[PARAM_SPECIALIZATION] = renderSpecializations(id)


    viewParameters = {
        ["title"] = "Furnace",
        ["subtitle"] = "Production Building",
        ["description"] = "Can produce: Pie (**), Skewers (**), Copper Bars (**). Can use: Drizzle Water.",
        ["specialization"] = "Warmth",
        ["iconfilename"] = "Furnace_icon.png",
        ["workplaces"] = 3,
        ["recipes"] = "Recipe content goes here",
        ["construction"] = "Construction content goes here",
        ["cityscore"] = 10,
        ["constructiontime"] = "00:40",
        ["constructionsize"] = "3 × 2",
        ["id"] = "Furnace",
    }

    return viewParameters
end

--endregion



--region Public methods

--- For calling from another template, like Infobox. Handles data only, does not interface with the Buildingbox view.
---@param id string the id of the building about which to retrieve data
---@return table the essential data about the building extracted
function Buildingbox2.compileBoxContents(id)

    -- Lazy-load of external data, since it's expensive. Start with the most likely so that most pages load as fast as possible.
    local viewParameters = tryWorkshopsData(id)
    if not viewParameters then
        viewParameters = tryInstitutionsData(id)
        if not viewParameters then
            viewParameters = tryFarmsData(id)
            if not viewParameters then
                viewParameters = tryCollectorsData(id)
                if not viewParameters then
                    viewParameters = tryCampsData(id)
                    if not viewParameters then
                        return("No building found with id: " .. id .. ".")
                    end
                end
            end
        end
    end

    -- Make sure something exists in the data before returning it; the ID should always exist.
    if not viewParameters["id"] then
        error("Specified ID resulted in no data.")
    end

    return viewParameters
end



--- For calling from Template:Buildingbox. After handling the frame, forwards control to the primary method, compileBoxData, then calls the view template with the compiled box data. Does not interface with any data modules.
---
---@param frame table the calling template's context
---@return string wiki markup, constructed with the template view
function Buildingbox2.main(frame)

    local id = frame.args[ARG_ID]
    if not id then
        error("You must specify the id of the building. Please see the template documentation for how to use the parameters.")
    end

    local viewParameters = Buildingbox2.compileBoxContents(id)

    return frame:expandTemplate{ title = ViewTemplate, args = viewParameters }
end

--endregion

return Buildingbox2