Module:UtilityModuleLister

From Terra Invicta Official Wiki

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

---
---Reads the UtilityModule data files and presents them in readable formats, with additional calculated statistics.
---
---@module UtilityModuleLister
local UtilityModuleLister = {}

--region Dependencies
local IconModule = require('Module:Icon')
local TVModule = require('Module:TV')
local UtilityModuleData = mw.loadData('Module:CSVReader/UtilityModule')
local UtilityModuleNames = mw.loadData('Module:ENReader/UtilityModule')
local ProjectNameBuilder = require('Module:ProjectLister/NameBuilder')
--endregion

--region Private constants
local sfind = string.find
local ssub = string.sub
local floor = math.floor
--endregion



--region Private member variables
--none
--endregion

--region Private methods

-- Replaces ONE instance of the stringToReplace with replacementString in the fullString
local sreplace = function(fullString, stringToReplace, replacementString)
	local si, ei = sfind(fullString, stringToReplace, 1, true)
	if not si then
		return fullString
	end
	local prefix = ssub(fullString,1,si - 1)
	local suffix = ssub(fullString,ei+1)
	return prefix .. replacementString .. suffix
end

--endregion

--region Public methods
function UtilityModuleLister.ListDetailedUtilityModules()
	return UtilityModuleLister.ListUtilityModules(true)
end

function UtilityModuleLister.ListBasicUtilityModules()
	return UtilityModuleLister.ListUtilityModules(false)
end

---@param detailed boolean whether to show the detailed or basic version.
---@return wiki table of UtilityModules
function UtilityModuleLister.ListUtilityModules(detailed)
	local returnString = '{| class="wikitable sortable mw-collapsible" style="text-align: center;"'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n! rowspan=2| Utility Module'
	if detailed then
		returnString = returnString .. '\n! rowspan=2| Required Project'
	end
	returnString = returnString .. '\n! rowspan=2| Min<br>Ship<br>Tier'
	if detailed then
		returnString = returnString .. '\n! rowspan=2| Crew'
	end
	returnString = returnString .. '\n! rowspan=2| Mass<br>(tons)'
	if detailed then 
		returnString = returnString .. '\n! colspan=7| Resources'
		returnString = returnString .. '\n! rowspan=2| Required<br>Power (MW)'
		returnString = returnString .. '\n! rowspan=2| Description'
		returnString = returnString .. '\n|-'
		returnString = returnString .. '\n!' .. IconModule.Icon({args={'water'}})
		returnString = returnString .. '\n!' .. IconModule.Icon({args={'volatiles'}})
		returnString = returnString .. '\n!' .. IconModule.Icon({args={'metals'}})
		returnString = returnString .. '\n!' .. IconModule.Icon({args={'nobles'}})
		returnString = returnString .. '\n!' .. IconModule.Icon({args={'fissiles'}})
		returnString = returnString .. '\n!' .. IconModule.Icon({args={'antimatter'}})
		returnString = returnString .. '\n!' .. IconModule.Icon({args={'exotics'}})
	else
		returnString = returnString .. '\n! rowspan=2| Resources'
		returnString = returnString .. '\n! rowspan=2| Description'
	end
	returnString = returnString .. '\n|-'
	
	for rowID, row in pairs(UtilityModuleData) do
		local moduleName = UtilityModuleNames['TIUtilityModuleTemplate.displayName.' .. rowID]
		if not moduleName or moduleName == 'Unknown' then
			moduleName = row['friendlyName']
		end
		if not moduleName or moduleName == 'Unknown' then
			moduleName = rowID
		end
		returnString = returnString .. '\n| ' .. moduleName
		
		if detailed then 
			returnString = returnString .. '\n| ' .. ProjectNameBuilder.buildName({args={row['requiredProjectName']}})
		end
		
		returnString = returnString .. '\n| ' .. row['minConsTier']
		
		if detailed then 
			returnString = returnString .. '\n| ' ..  row['crew']
		end
		
		local massWithCrew = (tonumber(row['mass_tons']) or 0) + (tonumber(row['crew']) or 0) * 4
		returnString = returnString .. '\n| ' .. massWithCrew
				
		local massTons = tonumber(row['mass_tons']) or 0
		local waterCost = massTons * (tonumber(row['weightedBuildMaterials/water']) or 0) / 10 + (tonumber(row['crew']) or 0) / 5
		local volatileCost = massTons * (tonumber(row['weightedBuildMaterials/volatiles']) or 0) / 10 + (tonumber(row['crew']) or 0) / 5
		local metalCost = massTons * (tonumber(row['weightedBuildMaterials/metals']) or 0) / 10
		local nobleCost = massTons * (tonumber(row['weightedBuildMaterials/nobleMetals']) or 0) / 10
		local fissileCost = massTons * (tonumber(row['weightedBuildMaterials/fissiles']) or 0) / 10
		local antimatterCost = massTons * (tonumber(row['weightedBuildMaterials/antimatter']) or 0) / 10
		local exoticCost = massTons * (tonumber(row['weightedBuildMaterials/exotics']) or 0) / 10
		
		if detailed then 
			returnString = returnString .. '\n| ' .. tostring(waterCost)
			returnString = returnString .. '\n| ' .. tostring(volatileCost)
			returnString = returnString .. '\n| ' .. tostring(metalCost)
			returnString = returnString .. '\n| ' .. tostring(nobleCost)
			returnString = returnString .. '\n| ' .. tostring(fissileCost)
			returnString = returnString .. '\n| ' .. tostring(antimatterCost)
			returnString = returnString .. '\n| ' .. tostring(exoticCost)
			returnString = returnString .. '\n| ' .. (row['powerRequirement_MW'] or 0)
		else
			returnString = returnString .. '\n| '
			if waterCost ~= 0 then
				returnString = returnString .. TVModule.TV({args={'water', waterCost, 'true'}}) .. ' '
			end
			if volatileCost ~= 0 then
				returnString = returnString .. TVModule.TV({args={'volatiles', volatileCost, 'true'}}) .. ' '
			end
			if metalCost ~= 0 then
				returnString = returnString .. TVModule.TV({args={'metals', metalCost, 'true'}}) .. ' '
			end
			if nobleCost ~= 0 then
				returnString = returnString .. TVModule.TV({args={'nobles', nobleCost, 'true'}}) .. ' '
			end
			if fissileCost ~= 0 then
				returnString = returnString .. TVModule.TV({args={'fissiles', fissileCost, 'true'}}) .. ' '
			end
			if antimatterCost ~= 0 then
				returnString = returnString .. TVModule.TV({args={'antimatter', antimatterCost, 'true'}}) .. ' '
			end
			if exoticCost ~= 0 then
				returnString = returnString .. TVModule.TV({args={'exotics', exoticCost, 'true'}}) .. ' '
			end
		end
		
		local srules = {}
		for i = 0, 3 do
			if row['specialModuleRules/' .. i] and row['specialModuleRules/' .. i] ~= '' then
				srules[row['specialModuleRules/' .. i]] = true
			end
		end
		
		local dvalue = {}
		dvalue[0] = row['specialModuleValue']
		dvalue[1] = (((tonumber(row['specialModuleValue'] or 0) or 0) - 1) * 100) .. '%'
		dvalue[2] = (((tonumber(row['specialModuleValue'] or 0) or 0) - 1) * 100) .. '%'
		dvalue[3] = row['specialModuleValue']
		dvalue[4] = row['powerRequirement_MW']
		dvalue[5] = ((tonumber(row['specialModuleValue'] or 0) or 0) * 100) .. '%'
		dvalue[6] = ((tonumber(row['specialModuleValue'] or 0) or 0) * 100) .. '%'
		dvalue[7] = ((1 - (tonumber(row['specialModuleValue'] or 0) or 0)) * 100) .. '%'
		dvalue[8] = ((tonumber(row['specialModuleValue'] or 0) or 0) * 100) .. '%'
		dvalue[9] = ((tonumber(row['specialModuleValue'] or 0) or 0) * 100) .. '%'
		dvalue[10] = ((1 - (tonumber(row['specialModuleValue'] or 0) or 0)) * 100) .. '%'
		dvalue[11] = ((tonumber(row['specialModuleValue'] or 0) or 0) * 100) .. '%'
		dvalue[12] = row['specialModuleValue']
		dvalue[13] = row['specialModuleValue']
		dvalue[14] = ((tonumber(row['specialModuleValue'] or 0) or 0) * 100) .. '%'
		
		local desc = UtilityModuleNames['TIUtilityModuleTemplate.description.' .. rowID] or ''
		if srules['RequiresHydrogenPropellant'] then
			desc = desc .. '<br>' .. UtilityModuleNames['TIUtilityModuleTemplate.description.RequiresHydrogenPropellant']	
		end
		if srules['RequiresFissionDrive'] then
			desc = desc .. '<br>' .. UtilityModuleNames['TIUtilityModuleTemplate.description.RequiresFissionDrive']	
		end
		if srules['RequiresNuclearDrive'] then
			desc = desc .. '<br>' .. UtilityModuleNames['TIUtilityModuleTemplate.description.RequiresNuclearDrive']	
		end
		if srules['RequiresFusionDrive'] then
			desc = desc .. '<br>' .. UtilityModuleNames['TIUtilityModuleTemplate.description.RequiresFusionDrive']	
		end
		if srules['RequiresNonISRUDrive'] then
			desc = desc .. '<br>' .. UtilityModuleNames['TIUtilityModuleTemplate.description.RequiresNonISRUDrive']	
		end
		if srules['ImmuneToDamage'] then
			desc = desc .. '<br>' .. UtilityModuleNames['TIUtilityModuleTemplate.description.ImmuneToDamage']	
		end
		if srules['FullRepairCost'] then
			desc = desc .. '<br>' .. sreplace(UtilityModuleNames['TIUtilityModuleTemplate.description.FullRepairCost'], '{0}', '25%')
		end
		
		for i = 0, 14 do
			desc = sreplace(desc, '{' .. i .. '}', dvalue[i])
			desc = sreplace(desc, '{' .. i .. '}', dvalue[i])
		end
		desc = sreplace(desc, '<color=#FFFFFFFF><sprite name="army_level"></color>', IconModule.Icon({args={'marine'}}))
		desc = sreplace(desc, '<color=#FFFFFFFF><sprite name="water"></color>', IconModule.Icon({args={'water'}}))
		desc = sreplace(desc, '<h>', "''")
		desc = sreplace(desc, '</h>', "''")
		desc = sreplace(desc, '<h>', "''")
		desc = sreplace(desc, '</h>', "''")
		desc = sreplace(desc, '<h>', "''")
		desc = sreplace(desc, '</h>', "''")
		
		
		returnString = returnString .. '\n| '.. desc
		
    	returnString = returnString .. '\n|-'
	end
	returnString = returnString .. '\n|}'
	
	return returnString
end

--endregion

return UtilityModuleLister