Module:Township: Difference between revisions

Refactoring
(Resource icons and stats are now accessed via Icons.Icon. Added GetBuildingTable)
(Refactoring)
Line 5: Line 5:




local p = {}


local Township = GameData.getSkillData('melvorD:Township')
local Township = GameData.getSkillData('melvorD:Township')
 
p.Township = Township
local p = {}


-- Returns the namespace name (eventually we should use an icon)
-- Returns the namespace name (eventually we should use an icon)
Line 87: Line 87:
-- Returns a list of all the Township resources along with the Trader's trade ratios
-- Returns a list of all the Township resources along with the Trader's trade ratios
function p._TraderData()
function p._TraderData()
-- Get the list of resources
-- Get the list of resources. We get a copy instead of directly using p.resources because we are going to modify the table
local resources = p._ResourcesData()
local resources = p._ResourcesData()
Line 184: Line 184:
return resources
return resources
end
end
p.resources = p._TraderData()


-- Builds the table of trader items
-- Builds the table of trader items
function p.GetTraderTable(frame)
function p.GetTraderTable(frame)
-- Get the resources data with associated trader data
-- Get the resources data with associated trader data
local resources = p._TraderData()
-- Build the text
-- Build the text
local ret = {}
local ret = {}
for _, resource in ipairs(resources) do
for _, resource in ipairs(p.resources) do
if #resource.itemConversions ~= 0 then -- Skips GP
if #resource.itemConversions ~= 0 then -- Skips GP
local ret_resource = {}
local ret_resource = {}
Line 316: Line 316:
return Township.populationForTier[tier]
return Township.populationForTier[tier]
end
end
-- Returns a string containing the Township level and population requirements for a tier
function p._GetTierText(tierlevel)
local tier = p._GetTierRequirements(tierlevel)
return '<b>Tier '..tierlevel..'</b><ul><li>'..Icons._SkillReq('Township', tier.level, false)..'</li><li>'..Icons.Icon({'Population', type='township', notext=true})..'&nbsp;'..tier.population..'</li></ul>'
end


-- Gets a building and prepares all the relevant stats for the building
-- Gets a building and prepares all the relevant stats for the building
Line 321: Line 328:
local name = frame.args ~= nil and frame.args[1] or frame
local name = frame.args ~= nil and frame.args[1] or frame
local building = Shared.clone(p._GetBuildingByName(name))
local building = Shared.clone(p._GetBuildingByName(name))
local resources = p._ResourcesData()
local ret = {}
local ret = {}


Line 335: Line 341:
table.insert(ret, '\r\n|-\r\n| <b>Type:</b> '..building.type)
table.insert(ret, '\r\n|-\r\n| <b>Type:</b> '..building.type)
-- Tier
-- Tier
local tier = p._GetTierRequirements(building.tier)
local tier = p._GetTierText(building.tier)
table.insert(ret, '\r\n|-\r\n| <b>Tier '..building.tier..'</b><ul><li>'..Icons._SkillReq('Township', tier.level, false)..'</li><li>'..Icons.Icon({'Population', type='township', notext=true})..'&nbsp;'..tier.population..'</li></ul>')
table.insert(ret, '\r\n|-\r\n| '..tier)
 
-- Upgrades From
-- Upgrades From
table.insert(ret, '\r\n|-\r\n| <b>Base Cost:</b>')
table.insert(ret, '\r\n|-\r\n| <b>Base Cost:</b>')
if building.upgradesFrom ~= nil then
local upgradesFrom = p._GetBuildingDowngrade(building)
local upgradesFromBuilding = p._GetBuildingByID(building.upgradesFrom)
if upgradesFrom ~= nil then
table.insert(ret, '<br>'..Icons.Icon({upgradesFromBuilding.name, type='building'})..'<br>')
table.insert(ret, '<br>'..Icons.Icon({upgradesFrom.name, type='building'}))
end
end
-- Cost
-- Cost
local cost = {}
local cost = p._GetBuildingBaseCost(building)
for _, resource in ipairs(building.cost) do
table.insert(ret, '<br>'..cost)
local resource_data = GameData.getEntityByID(resources, resource.id)
table.insert(cost, Icons.Icon({resource_data.name, type='resource', notext=true})..'&nbsp;'..resource.quantity)
end
table.insert(ret, table.concat(cost, ', '))
-- Upgrades To
-- Upgrades To
local function checkFunc(entity)
local upgradesTo = p._GetBuildingIDUpgrade(building.id)
return entity.upgradesFrom ~= nil and entity.upgradesFrom == building.id
if upgradesTo ~= nil then
end
table.insert(ret, '\r\n|-\r\n| <b>Upgrades To:</b>')
local upgradesTo = GameData.getEntities(Township.buildings, checkFunc)
table.insert(ret, '<br>'..Icons.Icon({upgradesTo.name, type='building'}))
if #upgradesTo > 0 then
local upgrade_cost = p._GetBuildingBaseCost(upgradesTo)
table.insert(ret, '\r\n|-\r\n| <b>Upgrades To:</b><br>'..Icons.Icon({upgradesTo[1].name, type='building'}))
table.insert(ret, '<br>'..upgrade_cost)
-- Cost
local upgrade_cost = {}
for _, resource in ipairs(upgradesTo[1].cost) do
local resource_data = GameData.getEntityByID(resources, resource.id)
table.insert(upgrade_cost, Icons.Icon({resource_data.name, type='resource', notext=true})..'&nbsp;'..resource.quantity)
end
table.insert(ret, '<br>'..table.concat(upgrade_cost, ', '))
end
end


-- Fixed benefits
-- Fixed benefits
local benefits = {}
local benefits = p._GetBuildingBenefits(building)
local stats = {
if benefits ~= nil then
population = 'Population',
table.insert(ret, '\r\n|-\r\n| <b>Provides:</b> '..benefits)
happiness = 'Happiness',
education = 'Education',
storage = 'Storage',
deadStorage = 'Dead Storage'
}
for key, stat in pairs(stats) do
if building.provides[key] ~= nil and building.provides[key] ~= 0 then
table.insert(benefits, Icons.Icon({stat, type='township', notext=true})..'&nbsp;'..building.provides[key])
end
end
if #benefits > 0 then
table.insert(ret, '\r\n|-\r\n| <b>Provides:</b> '..table.concat(benefits, ', '))
end
end


Line 428: Line 411:
end
end


-- Gets a string displaying the base production of a building
-- Gets a string displaying the base production of a building, or nil if no production
function p._GetBuildingBaseProduction(building)
function p._GetBuildingBaseProduction(building)
local production = Shared.clone(building.provides.resources)
local production = Shared.clone(building.provides.resources)
local resources = p._ResourcesData()
if #production == 0 then
if #production == 0 then
Line 444: Line 426:
-- Sourced from township.js -> Township.computeTownResourceGain()
-- Sourced from township.js -> Township.computeTownResourceGain()
local production = resource.quantity*100*(Township.tickLength/10)
local production = resource.quantity*100*(Township.tickLength/10)
local resource_data = GameData.getEntityByID(resources, resource.id)
local resource_data = p._GetResourceByID(resource.id)
table.insert(retProduction, '<span style="color:green">'..Icons.Icon({resource_data.name, type='resource', notext=true})..'&nbsp;+'..production..'</span>')
table.insert(retProduction, '<span style="color:green">'..Icons.Icon({resource_data.name, type='resource', notext=true})..'&nbsp;+'..production..'</span>')
if resource_data.requires ~= nil and #resource_data.requires > 0 then
if resource_data.requires ~= nil and #resource_data.requires > 0 then
for _, required_resource in ipairs(resource_data.requires) do
for _, required_resource in ipairs(resource_data.requires) do
local demand = production*required_resource.quantity*100
local demand = production*required_resource.quantity*100
local required_resource_data = GameData.getEntityByID(resources, resource.id)
local required_resource_data = p._GetResourceByID(required_resource.id)
table.insert(retProduction, '<span style="color:red">'..Icons.Icon({required_resource_data.name, type='resource', notext=true})..'&nbsp;-'..demand..'</span>')
table.insert(retProduction, '<span style="color:red">'..Icons.Icon({required_resource_data.name, type='resource', notext=true})..'&nbsp;-'..demand..'</span>')
end
end
Line 459: Line 441:
end
end


-- Testing
-- Gets a string displaying the building's benefits, or nil if no benefits
p.GameData = GameData
function p._GetBuildingBenefits(building)
p.Icons = Icons
local benefits = {}
p.Township = Township
local stats = {
population = 'Population',
happiness = 'Happiness',
education = 'Education',
storage = 'Storage',
deadStorage = 'Dead Storage'
}
for key, stat in pairs(stats) do
if building.provides[key] ~= nil and building.provides[key] ~= 0 then
table.insert(benefits, Icons.Icon({stat, type='township', notext=true})..'&nbsp;'..building.provides[key])
end
end
if #benefits > 0 then
return table.concat(benefits, ', ')
end
return nil
end
 
-- Given a building id, find the next building upgrade
function p._GetBuildingIDUpgrade(buildingid)
local function checkFunc(entity)
return entity.upgradesFrom ~= nil and entity.upgradesFrom == buildingid
end
local upgradesTo = GameData.getEntities(Township.buildings, checkFunc)
if #upgradesTo > 0 then
return upgradesTo[1]
end
return nil
end
 
-- Given a building, find the building's downgrade
function p._GetBuildingDowngrade(building)
if building.upgradesFrom ~= nil then
return p._GetBuildingByID(building.upgradesFrom)
end
return nil
end
 
-- Given a building, find the base resource cost
function p._GetBuildingBaseCost(building, _join)
local join = _join ~= nil and _join or ', '
local cost = {}
for _, resource in ipairs(building.cost) do
local resource_data = p._GetResourceByID(resource.id)
table.insert(cost, Icons.Icon({resource_data.name, type='resource', notext=true})..'&nbsp;'..resource.quantity)
end
return table.concat(cost, join)
end


-- Gets a resource from id
function p._GetResourceByID(id)
return GameData.getEntityByID(p.resources, id)
end


return p
return p
572

edits