Module:Buildingbox2
From Against the Storm Official Wiki
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" local PARAM_SPECIALIZATION = "specialization" --endregion --region Private methods ---renderSpecializations takes the ID of a building and looks up its specializations, calls external view templates to convert those specializations into usable content strings for display, and returns those strings as a list to be processed and displayed by the calling scope. ---@param id string the unique ID of the building ---@return table an array of content strings, each ready for display 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 concatenatedStrings = "" 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"] = "small", }, } 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"] = "small", [1] = "notext", }, } -- Separate entries with a comma for readability if #concatenatedStrings > 1 then concatenatedStrings = concatenatedStrings .. "<br />" end concatenatedStrings = concatenatedStrings .. specializationString .. " " .. speciesString end if #concatenatedStrings < 1 then return "None" end return concatenatedStrings 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