Module:ProjectLister/Context

From Terra Invicta Official Wiki

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

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

--region Dependencies
local IconModule = require('Module:Icon')
local TVModule = require('Module:TV')
local ProjectData = mw.loadData('Module:CSVReader/Project')

local ProjectNameBuilder = require('Module:ProjectLister/NameBuilder')


local ProjectCosts = mw.loadData('Module:ProjectLister/Costs')

--endregion

--region Private constants
local insert = table.insert
local concat = table.concat
local slower = string.lower
local CouncilIcons = {
	DestroyCouncil='destroy',
	ResistCouncil='resist',
	EscapeCouncil='escape',
	ExploitCouncil='exploit',
	CooperateCouncil='cooperate',
	AppeaseCouncil='appease',
	SubmitCouncil='submit',
	AlienCouncil='alien'
}
local ScienceCategories = {
	Energy = 'energy',
	InformationScience='information',
	LifeScience='life',
	Materials='material',
	MilitaryScience='military',
	SocialScience='social',
	SpaceScience='space',
	Xenology='xenology'
}
local contextToTV = {
	boost = 'boost priority',
	mc = 'mission control priority',
	military = 'military priority',
}
local contextToFile = {
	
}
--endregion



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

--region Private methods
--endregion

--region Public methods

---@param context The context to look at.
---@return wiki table of Projects that give context
function ContextProjectLister.ListProjects(frame)
	local context = frame.args[1]
	local contextTV = contextToTV[mw.text.trim(slower(context))] or context
	local contextFile = contextToFile[mw.text.trim(slower(context))] or context
	local ContextProjectData = mw.loadData('Module:EffectLister/ContextProjects/' .. contextFile)
	
	
	local bonusName = frame.args['bonusName']
	if bonusName == nil then
		bonusName = TVModule.TV({args={contextTV}})
	end
	local showAsPercent = frame.args['showAsPercent']
	if showAsPercent == 'false' then
		showAsPercent = false
	else
		showAsPercent = true
	end
	local returnString = '{| class="wikitable sortable mw-collapsible" style="text-align: center;"'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n! Project'
	returnString = returnString .. '\n! Type'
	returnString = returnString .. '\n! Research<br>Cost'
	returnString = returnString .. '\n! Full<br>Cost'
	returnString = returnString .. '\n! ' .. bonusName .. ' Bonus'
	returnString = returnString .. '\n! rowspan=2|Available To:'
	returnString = returnString .. '\n|-'
	
	local factionTotals = {
		destroy = 0,
		resist = 0,
		escape = 0,
		exploit = 0,
		cooperate = 0,
		appease = 0,
		submit = 0,
	}
	local factionMultipliers = {
		destroy = 1,
		resist = 1,
		escape = 1,
		exploit = 1,
		cooperate = 1,
		appease = 1,
		submit = 1,
	}
	for projectID, ContextBonus in pairs(ContextProjectData) do
		returnString = returnString .. '\n| ' .. ProjectNameBuilder.buildName({args={projectID}})
		returnString = returnString .. '\n| ' .. IconModule.Icon({args={ScienceCategories[ProjectData[projectID]['techCategory']]}})
		returnString = returnString .. '\n| ' .. ProjectData[projectID]['researchCost']
		returnString = returnString .. '\n| ' .. tostring(ProjectCosts[projectID]['total'])
		if ContextBonus[1] == 'Additive' then
			if showAsPercent then
				returnString = returnString .. '\n| +' .. (ContextBonus[2] * 100) .. '%'
			else
				returnString = returnString .. '\n| +' .. ContextBonus[2]
			end
		elseif ContextBonus[1] == 'Multiplicative' then
			returnString = returnString .. '\n| ×' .. ContextBonus[2]
		else
			returnString = returnString .. '\n| Error: Unknown effect operation: ' .. ContextBonus[1]
		end
		
		if not ProjectData[projectID]['factionPrereq/0'] or ProjectData[projectID]['factionPrereq/0'] == '' then
			returnString = returnString .. '\n| All'
			if ContextBonus[1] == 'Additive' then
				factionTotals['destroy'] = factionTotals['destroy'] + ContextBonus[2]
				factionTotals['resist'] = factionTotals['resist'] + ContextBonus[2]
				factionTotals['escape'] = factionTotals['escape'] + ContextBonus[2]
				factionTotals['exploit'] = factionTotals['exploit'] + ContextBonus[2]
				factionTotals['cooperate'] = factionTotals['cooperate'] + ContextBonus[2]
				factionTotals['appease'] = factionTotals['appease'] + ContextBonus[2]
				factionTotals['submit'] = factionTotals['submit'] + ContextBonus[2]
			elseif ContextBonus[1] == 'Multiplicative' then
				factionMultipliers['destroy'] = factionMultipliers['destroy'] * ContextBonus[2]
				factionMultipliers['resist'] = factionMultipliers['resist'] * ContextBonus[2]
				factionMultipliers['escape'] = factionMultipliers['escape'] * ContextBonus[2]
				factionMultipliers['exploit'] = factionMultipliers['exploit'] * ContextBonus[2]
				factionMultipliers['cooperate'] = factionMultipliers['cooperate'] * ContextBonus[2]
				factionMultipliers['appease'] = factionMultipliers['appease'] * ContextBonus[2]
				factionMultipliers['submit'] = factionMultipliers['submit'] * ContextBonus[2]
			end
		else
			local councils = {}
			for i = 0, 5 do
				local freq = ProjectData[projectID]['factionPrereq/' .. i]
				if freq and freq ~= '' and freq ~= 0 then
					insert(councils, IconModule.Icon({args={CouncilIcons[freq]}}))
					if ContextBonus[1] == 'Additive' then
						factionTotals[CouncilIcons[freq]] = factionTotals[CouncilIcons[freq]] + ContextBonus[2]
					elseif ContextBonus[1] == 'Multiplicative' then
						factionMultipliers[CouncilIcons[freq]] = factionMultipliers[CouncilIcons[freq]] * ContextBonus[2]
					end
				end
			end
			returnString = returnString .. '\n| ' .. concat(councils, '')
		end
		returnString = returnString .. '\n|-'
	end
	returnString = returnString .. '\n|- class="sortbottom"'
	returnString = returnString .. "\n! Total " .. bonusName .. ' Bonus:' 
	returnString = returnString .. "\n! colspan=5 | "
	returnString = returnString .. '\n{| class="wikitable" style="text-align: center;"'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n! ' .. IconModule.Icon({args={'destroy'}})
	returnString = returnString .. '\n! ' .. IconModule.Icon({args={'resist'}})
	returnString = returnString .. '\n! ' .. IconModule.Icon({args={'escape'}})
	returnString = returnString .. '\n! ' .. IconModule.Icon({args={'exploit'}})
	returnString = returnString .. '\n! ' .. IconModule.Icon({args={'cooperate'}})
	returnString = returnString .. '\n! ' .. IconModule.Icon({args={'appease'}})
	returnString = returnString .. '\n! ' .. IconModule.Icon({args={'submit'}})
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n| '
	if factionTotals['destroy'] ~= 0 then
		returnString = returnString .. '+'
		if showAsPercent then
			returnString = returnString .. (factionTotals['destroy'] * 100) .. '% '
		else 
			returnString = returnString .. factionTotals['destroy'] .. ' '
		end
	end
	if factionMultipliers['destroy'] ~= 1 then
		returnString = returnString .. '×' .. factionMultipliers['destroy']
	end
	returnString = returnString .. '\n| '
	if factionTotals['resist'] ~= 0 then
		returnString = returnString .. '+'
		if showAsPercent then
			returnString = returnString .. (factionTotals['resist'] * 100) .. '% '
		else 
			returnString = returnString .. factionTotals['resist'] .. ' '
		end
	end
	if factionMultipliers['resist'] ~= 1 then
		returnString = returnString .. '×' .. factionMultipliers['resist']
	end
	returnString = returnString .. '\n| '
	if factionTotals['escape'] ~= 0 then
		returnString = returnString .. '+'
		if showAsPercent then
			returnString = returnString .. (factionTotals['escape'] * 100) .. '% '
		else 
			returnString = returnString .. factionTotals['escape'] .. ' '
		end
	end
	if factionMultipliers['escape'] ~= 1 then
		returnString = returnString .. '×' .. factionMultipliers['escape']
	end
	returnString = returnString .. '\n| '
	if factionTotals['exploit'] ~= 0 then
		returnString = returnString .. '+'
		if showAsPercent then
			returnString = returnString .. (factionTotals['exploit'] * 100) .. '% '
		else 
			returnString = returnString .. factionTotals['exploit'] .. ' '
		end
	end
	if factionMultipliers['exploit'] ~= 1 then
		returnString = returnString .. '×' .. factionMultipliers['exploit']
	end
	returnString = returnString .. '\n| '
	if factionTotals['cooperate'] ~= 0 then
		returnString = returnString .. '+'
		if showAsPercent then
			returnString = returnString .. (factionTotals['cooperate'] * 100) .. '% '
		else 
			returnString = returnString .. factionTotals['cooperate'] .. ' '
		end
	end
	if factionMultipliers['cooperate'] ~= 1 then
		returnString = returnString .. '×' .. factionMultipliers['cooperate']
	end
	returnString = returnString .. '\n| '
	if factionTotals['appease'] ~= 0 then
		returnString = returnString .. '+'
		if showAsPercent then
			returnString = returnString .. (factionTotals['appease'] * 100) .. '% '
		else 
			returnString = returnString .. factionTotals['appease'] .. ' '
		end
	end
	if factionMultipliers['appease'] ~= 1 then
		returnString = returnString .. '×' .. factionMultipliers['appease']
	end
	returnString = returnString .. '\n| '
	if factionTotals['submit'] ~= 0 then
		returnString = returnString .. '+'
		if showAsPercent then
			returnString = returnString .. (factionTotals['submit'] * 100) .. '% '
		else 
			returnString = returnString .. factionTotals['submit'] .. ' '
		end
	end
	if factionMultipliers['submit'] ~= 1 then
		returnString = returnString .. '×' .. factionMultipliers['submit']
	end
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n|}'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n|}'
	
	return returnString
end

--endregion

return ContextProjectLister