Anonymous

Module:Sandbox/GauTest/Township: Difference between revisions

From Melvor Idle
Pull out functions from GetBuildingTable into helper functions
No edit summary
(Pull out functions from GetBuildingTable into helper functions)
Line 84: Line 84:
return resources
return resources
end
end
p.resources = p._ResourcesData()


-- 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 316: Line 317:
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 329:
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 342:
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, 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'})..'<br>')
end
end
-- Cost
-- Cost
local cost = {}
local cost = p._GetBuildingBaseCost(building)
for _, resource in ipairs(building.cost) do
table.insert(ret, 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


Line 466: Line 462:
end
end
return nil
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(upgradesTo.cost) do
local resource_data = p._GetResourceByID(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, join))
end
-- Gets a resource from id
function p._GetResourceByID(id)
return GameData.getEntityByID(p.resources, id)
end
end


return p
return p
572

edits