Difference between revisions of "Module:OInfobox-Building"
Jump to navigation
Jump to search
m |
m |
||
| (81 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- adapted from Levrin's iteration to Sakaratte's original -- | -- adapted by Olessan from Levrin's iteration to Sakaratte's original -- | ||
-- Layout changed and all labels, etc, updated to pull from Module:BuildingINdex -- | |||
-- I may expand later to have functions for other infobox types (but this game doesn't really need any other?? 22/04/2025 -- | |||
local com = require('Module:Common') | local com = require('Module:Common') | ||
local a = require('Module:Arguments') | local a = require('Module:Arguments') | ||
| Line 15: | Line 17: | ||
end | end | ||
local function addImage(box, image | local function addImage(box, image, defaultImage) | ||
if not image and not defaultImage then | if not image and not defaultImage then | ||
box:wikitext('[[Category:Image Needed]]') | box:wikitext('[[Category:Image Needed]]') | ||
return box | return box | ||
end | end | ||
local imageDiv = mw.html.create('div') | local imageDiv = mw.html.create('div') | ||
imageDiv:addClass('infobox-image') | imageDiv:addClass('infobox-image') | ||
local fileName = image or defaultImage | local fileName = image or defaultImage | ||
local fileWiki = '[[File:' .. fileName .. '|260px|frameless]]' | local fileWiki = '[[File:' .. fileName .. '|260px|frameless]]' | ||
imageDiv:wikitext(fileWiki) | imageDiv:wikitext(fileWiki) | ||
box:node(imageDiv) | box:node(imageDiv) | ||
return box | return box | ||
| Line 43: | Line 39: | ||
return box | return box | ||
end | end | ||
if header then | if header then | ||
box:tag('div') | box:tag('div') | ||
| Line 50: | Line 46: | ||
:done() | :done() | ||
end | end | ||
local function createDataRow(label, value, defaultValue) | |||
local actualValue = value or defaultValue | |||
return | |||
if not actualValue or actualValue == '' then | |||
return nil | |||
end | |||
local dataRow = mw.html.create('div') | |||
dataRow:addClass('infobox-data-section') | |||
if label then | |||
dataRow:tag('span') | |||
:addClass('infobox-label') | |||
:wikitext(label) | |||
:done() | |||
:tag('span') | |||
:addClass('infobox-data') | |||
:wikitext(actualValue) | |||
else | |||
dataRow:tag('span') | |||
:addClass('infobox-data') | |||
:wikitext(actualValue) | |||
end | |||
return dataRow | |||
end | end | ||
local rowClass = 'infobox-row' | local rowClass = 'infobox-row' | ||
if columnCount then | if columnCount then | ||
rowClass = rowClass .. ' data-rows-' .. columnCount | rowClass = rowClass .. ' data-rows-' .. columnCount | ||
end | end | ||
local rowContainer = mw.html.create('div') | local rowContainer = mw.html.create('div') | ||
rowContainer:addClass(rowClass) | rowContainer:addClass(rowClass) | ||
for _, item in ipairs(data) do | for _, item in ipairs(data) do | ||
local dataRow = createDataRow(item.label, item.value, item.defaultValue) | local dataRow = createDataRow(item.label, item.value, item.defaultValue) | ||
| Line 90: | Line 88: | ||
end | end | ||
end | end | ||
box:node(rowContainer) | box:node(rowContainer) | ||
return box | return box | ||
end | end | ||
--[[ | --[[Levrin: | ||
I left next section, that I commented "Start" and "End," in here in case you all needed or wanted it (blame Pelendur if it's bad.....just kidding (maybe) :p) | I left next section, that I commented "Start" and "End," in here in case you all needed or wanted it (blame Pelendur if it's bad.....just kidding (maybe) :p) | ||
It seemed to be in other templates, and I didn't want to remove it if it was needed or useful to you all. ]]-- | It seemed to be in other templates, and I didn't want to remove it if it was needed or useful to you all. ]]-- | ||
-- Start | -- Start | ||
local function createIconRow(iconName, value) | local function createIconRow(iconName, value) | ||
if not value then | if not value then | ||
return nil | return nil | ||
end | end | ||
local label = i.icon(iconName, nil, nil, com.firstUpper({iconName}), 'en') | local label = i.icon(iconName, nil, nil, com.firstUpper({iconName}), 'en') | ||
return createDataRow(label, value) | return createDataRow(label, value) | ||
| Line 112: | Line 110: | ||
function p.building(frame) | function p.building(frame) | ||
local args = a.getArgs(frame) | local args = a.getArgs(frame) | ||
local titleArg = args['title'] | local titleArg = args['title'] | ||
local currentTitle = mw.title.getCurrentTitle().text | local currentTitle = mw.title.getCurrentTitle().text | ||
local building = com.modulePagename(titleArg or currentTitle) | local building = com.modulePagename(titleArg or currentTitle) | ||
local buildingData = require('Module:BuildingIndex')[string.lower(building)] | local buildingData = require('Module:BuildingIndex')[string.lower(building)] | ||
local infobox = createInfobox(building) | local infobox = createInfobox(building) | ||
addImage(infobox, args['image | addImage(infobox, args['image'], buildingData and buildingData.defaultImage, buildingData and buildingData.defaultCaption) | ||
if buildingData then | if buildingData then | ||
--SECTION-PRODUCTION | |||
local productionData = {} | local productionData = {} | ||
if buildingData.employees then | if buildingData.employees then | ||
| Line 148: | Line 146: | ||
end | end | ||
end | end | ||
if com.exists(input) then | if com.exists(input) then | ||
table.insert(productionData, {label = 'Consumes', value = input}) | table.insert(productionData, {label = 'Consumes', value = input}) | ||
end | end | ||
end | end | ||
if buildingData.output then | if buildingData.output then | ||
local output = '' | local output = '' | ||
| Line 162: | Line 160: | ||
end | end | ||
end | end | ||
if com.exists(output) then | if com.exists(output) then | ||
table.insert(productionData, {label = 'Produces', value = output}) | table.insert(productionData, {label = 'Produces', value = output}) | ||
end | end | ||
end | end | ||
if #productionData > 0 then | if #productionData > 0 then | ||
addSection(infobox, productionData, 'Production', 2) -- The 2 means 2 columns, it can add more or less | addSection(infobox, productionData, 'Production', 2) -- The 2 means 2 columns, it can add more or less | ||
end | end | ||
--/ | --/ | ||
--SECTION-PREQUISITES | |||
local prereqData = {} | local prereqData = {} | ||
if buildingData.building then | if buildingData.building then | ||
table.insert(prereqData, {label = 'Building', value = buildingData.building}) | table.insert(prereqData, {label = 'Building', value = buildingData.building}) | ||
end | end | ||
if buildingData.population then | if buildingData.population then | ||
table.insert(prereqData, {label = 'Population', value = buildingData.population}) | table.insert(prereqData, {label = 'Population', value = buildingData.population}) | ||
end | end | ||
if buildingData.influence then | if buildingData.influence then | ||
for type, value in pairs(buildingData.influence) do | for type, value in pairs(buildingData.influence) do | ||
| Line 188: | Line 186: | ||
end | end | ||
end | end | ||
if buildingData.splendor then | if buildingData.splendor then | ||
for type, value in pairs(buildingData.splendor) do | for type, value in pairs(buildingData.splendor) do | ||
| Line 194: | Line 192: | ||
end | end | ||
end | end | ||
--/ | |||
--SECTION-UNLOCKING | |||
if | if buildingData.influence then | ||
local unlockData = {} | |||
local first = true | |||
for resource, amount in pairs(buildingData.influence) do | |||
if resource ~= 'tier' and resource ~= 'bsplendour' then -- Exclude 'tier' and 'bsplendour' | |||
local displayAmount = amount | |||
if first and buildingData.influence.tier then | |||
displayAmount = displayAmount .. ' ' .. buildingData.influence.tier | |||
first = false | |||
end | |||
local resourceIcon = i.icon(resource, nil, nil, com.firstUpper({resource}), 'en') | |||
local tierPosition = string.find(displayAmount, "tier", nil, true) | |||
if tierPosition then | |||
local beforeTier = string.sub(displayAmount, 1, tierPosition - 1) | |||
local theTier = string.sub(displayAmount, tierPosition) | |||
table.insert(unlockData, {label = resourceIcon, value = beforeTier .. "(" .. theTier .. ")"}) | |||
else | |||
table.insert(unlockData, {label = resourceIcon, value = displayAmount}) | |||
end | |||
end | end | ||
end | |||
if #unlockData > 0 then | |||
addSection(infobox, unlockData, 'Unlocking Conditions', 2) | |||
end | |||
end | |||
--/ | --/ | ||
---SECTION-CONSTRUCTION | ---SECTION-CONSTRUCTION | ||
if buildingData.construction then | if buildingData.construction then | ||
local | local constructionData = {} | ||
for resource, amount in pairs(buildingData.construction) do | for resource, amount in pairs(buildingData.construction) do | ||
local resourceIcon = i.icon(resource, | local resourceIcon = i.icon(resource, nil, nil, com.firstUpper({resource}), 'en') | ||
table.insert(constructionData, {label = resourceIcon, value = amount}) | |||
end | end | ||
if | if #constructionData > 0 then | ||
addSection(infobox, constructionData, 'Construction Resources', 2) | |||
end | end | ||
end | end | ||
--/ | --/ | ||
--SECTION-RUNNING-COSTS | --SECTION-RUNNING-COSTS | ||
local costData = {} | local costData = {} | ||
local goldCoinsFound = false | |||
if buildingData.costs or buildingData.influence['bsplendour'] then | |||
for resource, amount in pairs(buildingData.costs) do | |||
if resource == 'gold coins' then | |||
local resourceIcon = i.icon('gold coins', nil, nil, 'gold coins', 'en') | |||
table.insert(costData, {label = '[[File:Gold Coins.png|frameless|20x20px]] ' .. '[[Gold Coins|Build Cost]]', value = amount}) | |||
goldCoinsFound = true | |||
elseif resource ~= 'maintenance' then | |||
local resourceIcon = i.icon(resource, nil, nil, com.firstUpper({resource}), 'en') | |||
table.insert(costData, {label = resourceIcon, value = amount}) | |||
end | |||
if resource ~= 'maintenance' then | |||
local displayAmount = amount | |||
if second and buildingData.costs.maintenance then | |||
displayAmount = displayAmount .. ' ' .. buildingData.costs.maintenance | |||
second = false | |||
end | |||
end | |||
end | |||
if buildingData.maintenance then | if buildingData.costs and buildingData.costs['maintenance'] then | ||
local | local resourceIcon = i.icon('maintenance', nil, nil, 'maintenance', 'en') | ||
table.insert(costData, {label = resourceIcon, value = '[[File:Gold Coins.png|frameless|20x20px]] ' .. buildingData.costs['maintenance'] .. ' monthly'}) | |||
end | end | ||
if buildingData. | if buildingData.influence['bsplendour'] then | ||
local | local resourceIcon = i.icon('bsplendour', nil, nil, 'splendor', 'en') | ||
table.insert(costData, {label = '[[File:Splendor.png|frameless|20x20px]] ' .. '[[Splendour|Splendour]]', value = '+ ' .. buildingData.influence['bsplendour'] .. ' ea'}) | |||
end | end | ||
if #costData > 0 then | if #costData > 0 then | ||
addSection(infobox, costData, ' | addSection(infobox, costData, 'Economic Impact', 2) | ||
end | |||
end | end | ||
end | end | ||
--/ | --/ | ||
Latest revision as of 03:31, 22 April 2025
Documentation for this module may be created at Module:OInfobox-Building/doc
-- adapted by Olessan from Levrin's iteration to Sakaratte's original --
-- Layout changed and all labels, etc, updated to pull from Module:BuildingINdex --
-- I may expand later to have functions for other infobox types (but this game doesn't really need any other?? 22/04/2025 --
local com = require('Module:Common')
local a = require('Module:Arguments')
local i = require('Module:Icon')
local p = {}
local function createInfobox(title)
local box = mw.html.create('div')
box:addClass('infobox')
:tag('div')
:addClass('infobox-title')
:wikitext(com.modulePagename(title))
:done()
return box
end
local function addImage(box, image, defaultImage)
if not image and not defaultImage then
box:wikitext('[[Category:Image Needed]]')
return box
end
local imageDiv = mw.html.create('div')
imageDiv:addClass('infobox-image')
local fileName = image or defaultImage
local fileWiki = '[[File:' .. fileName .. '|260px|frameless]]'
imageDiv:wikitext(fileWiki)
box:node(imageDiv)
return box
end
local function addSection(box, data, header, columnCount)
if not data or #data == 0 then
return box
end
if header then
box:tag('div')
:addClass('infobox-header')
:wikitext(header)
:done()
end
local function createDataRow(label, value, defaultValue)
local actualValue = value or defaultValue
if not actualValue or actualValue == '' then
return nil
end
local dataRow = mw.html.create('div')
dataRow:addClass('infobox-data-section')
if label then
dataRow:tag('span')
:addClass('infobox-label')
:wikitext(label)
:done()
:tag('span')
:addClass('infobox-data')
:wikitext(actualValue)
else
dataRow:tag('span')
:addClass('infobox-data')
:wikitext(actualValue)
end
return dataRow
end
local rowClass = 'infobox-row'
if columnCount then
rowClass = rowClass .. ' data-rows-' .. columnCount
end
local rowContainer = mw.html.create('div')
rowContainer:addClass(rowClass)
for _, item in ipairs(data) do
local dataRow = createDataRow(item.label, item.value, item.defaultValue)
if dataRow then
rowContainer:node(dataRow)
end
end
box:node(rowContainer)
return box
end
--[[Levrin:
I left next section, that I commented "Start" and "End," in here in case you all needed or wanted it (blame Pelendur if it's bad.....just kidding (maybe) :p)
It seemed to be in other templates, and I didn't want to remove it if it was needed or useful to you all. ]]--
-- Start
local function createIconRow(iconName, value)
if not value then
return nil
end
local label = i.icon(iconName, nil, nil, com.firstUpper({iconName}), 'en')
return createDataRow(label, value)
end
-- End
--INFOBOX-STRUCTURE
function p.building(frame)
local args = a.getArgs(frame)
local titleArg = args['title']
local currentTitle = mw.title.getCurrentTitle().text
local building = com.modulePagename(titleArg or currentTitle)
local buildingData = require('Module:BuildingIndex')[string.lower(building)]
local infobox = createInfobox(building)
addImage(infobox, args['image'], buildingData and buildingData.defaultImage, buildingData and buildingData.defaultCaption)
if buildingData then
--SECTION-PRODUCTION
local productionData = {}
if buildingData.employees then
for type, value in pairs(buildingData.employees) do
table.insert(productionData, {label = com.firstUpper({type}), value = value})
end
end
if buildingData.workers then
local workers = buildingData.workers
if buildingData.rank then
workers = workers .. ' ' .. buildingData.rank
end
table.insert(productionData, {label = 'Workers', value = workers.firstUpper})
end
--
if buildingData.input then
local input = ''
for k, output in ipairs(buildingData.input) do
input = input .. i.icon(output, nil, nil, com.firstUpper({output}), 'en')
if k < #buildingData.input then
input = input .. '<br />'
end
end
if com.exists(input) then
table.insert(productionData, {label = 'Consumes', value = input})
end
end
if buildingData.output then
local output = ''
for k, product in ipairs(buildingData.output) do
output = output .. i.icon(product, nil, nil, com.firstUpper({product}), 'en')
if k < #buildingData.output then
output = output .. '<br />'
end
end
if com.exists(output) then
table.insert(productionData, {label = 'Produces', value = output})
end
end
if #productionData > 0 then
addSection(infobox, productionData, 'Production', 2) -- The 2 means 2 columns, it can add more or less
end
--/
--SECTION-PREQUISITES
local prereqData = {}
if buildingData.building then
table.insert(prereqData, {label = 'Building', value = buildingData.building})
end
if buildingData.population then
table.insert(prereqData, {label = 'Population', value = buildingData.population})
end
if buildingData.influence then
for type, value in pairs(buildingData.influence) do
table.insert(prereqData, {label = type .. ' Influence', value = value})
end
end
if buildingData.splendor then
for type, value in pairs(buildingData.splendor) do
table.insert(prereqData, {label = type .. ' Splendor', value = value})
end
end
--/
--SECTION-UNLOCKING
if buildingData.influence then
local unlockData = {}
local first = true
for resource, amount in pairs(buildingData.influence) do
if resource ~= 'tier' and resource ~= 'bsplendour' then -- Exclude 'tier' and 'bsplendour'
local displayAmount = amount
if first and buildingData.influence.tier then
displayAmount = displayAmount .. ' ' .. buildingData.influence.tier
first = false
end
local resourceIcon = i.icon(resource, nil, nil, com.firstUpper({resource}), 'en')
local tierPosition = string.find(displayAmount, "tier", nil, true)
if tierPosition then
local beforeTier = string.sub(displayAmount, 1, tierPosition - 1)
local theTier = string.sub(displayAmount, tierPosition)
table.insert(unlockData, {label = resourceIcon, value = beforeTier .. "(" .. theTier .. ")"})
else
table.insert(unlockData, {label = resourceIcon, value = displayAmount})
end
end
end
if #unlockData > 0 then
addSection(infobox, unlockData, 'Unlocking Conditions', 2)
end
end
--/
---SECTION-CONSTRUCTION
if buildingData.construction then
local constructionData = {}
for resource, amount in pairs(buildingData.construction) do
local resourceIcon = i.icon(resource, nil, nil, com.firstUpper({resource}), 'en')
table.insert(constructionData, {label = resourceIcon, value = amount})
end
if #constructionData > 0 then
addSection(infobox, constructionData, 'Construction Resources', 2)
end
end
--/
--SECTION-RUNNING-COSTS
local costData = {}
local goldCoinsFound = false
if buildingData.costs or buildingData.influence['bsplendour'] then
for resource, amount in pairs(buildingData.costs) do
if resource == 'gold coins' then
local resourceIcon = i.icon('gold coins', nil, nil, 'gold coins', 'en')
table.insert(costData, {label = '[[File:Gold Coins.png|frameless|20x20px]] ' .. '[[Gold Coins|Build Cost]]', value = amount})
goldCoinsFound = true
elseif resource ~= 'maintenance' then
local resourceIcon = i.icon(resource, nil, nil, com.firstUpper({resource}), 'en')
table.insert(costData, {label = resourceIcon, value = amount})
end
if resource ~= 'maintenance' then
local displayAmount = amount
if second and buildingData.costs.maintenance then
displayAmount = displayAmount .. ' ' .. buildingData.costs.maintenance
second = false
end
end
end
if buildingData.costs and buildingData.costs['maintenance'] then
local resourceIcon = i.icon('maintenance', nil, nil, 'maintenance', 'en')
table.insert(costData, {label = resourceIcon, value = '[[File:Gold Coins.png|frameless|20x20px]] ' .. buildingData.costs['maintenance'] .. ' monthly'})
end
if buildingData.influence['bsplendour'] then
local resourceIcon = i.icon('bsplendour', nil, nil, 'splendor', 'en')
table.insert(costData, {label = '[[File:Splendor.png|frameless|20x20px]] ' .. '[[Splendour|Splendour]]', value = '+ ' .. buildingData.influence['bsplendour'] .. ' ea'})
end
if #costData > 0 then
addSection(infobox, costData, 'Economic Impact', 2)
end
end
end
--/
return infobox
end
return p