Module:ObjectiveLister

From Terra Invicta Official Wiki

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

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

--region Dependencies
local IconModule = require('Module:Icon')
local ObjectiveData = mw.loadData('Module:CSVReader/Objective')
local ObjectiveNames = mw.loadData('Module:ENReader/Objective')
local OrgNames = mw.loadData('Module:ENReader/Org')
local MissionNames = mw.loadData('Module:ENReader/Mission')
local ProjectNames = mw.loadData('Module:ENReader/Project')
local ProjectData = mw.loadData('Module:CSVReader/Project')

local ObjectivePrereqs = mw.loadData('Module:ObjectiveLister/Prereqs')
--endregion

--region Private constants
local hiddenMilestones = {
	AlienDiplomacy = 'Alien Diplomacy',
	DetectXenoforming = 'Detect Xenoforming',
	AlienArmyDestroyed = 'Alien Army Destroyed',
	TargetedByTerrorMission = 'Targeted By Terror Mission',
	AccessAlienLanguage = 'Access Alien Language',
}
local councilToIdeo = {
	DestroyCouncil='destroy',
	ResistCouncil='resist',
	EscapeCouncil='escape',
	ExploitCouncil='exploit',
	CooperateCouncil='cooperate',
	AppeaseCouncil='appease',
	SubmitCouncil='submit'
}
local sfind = string.find
local ssub = string.sub
--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 ObjectiveLister.ListResistObjectives()
	return ObjectiveLister.ListObjectives('Resist')
end

function ObjectiveLister.ListDestroyObjectives()
	return ObjectiveLister.ListObjectives('Destroy')
end

function ObjectiveLister.ListEscapeObjectives()
	return ObjectiveLister.ListObjectives('Escape')
end

function ObjectiveLister.ListExploitObjectives()
	return ObjectiveLister.ListObjectives('Exploit')
end

function ObjectiveLister.ListCooperateObjectives()
	return ObjectiveLister.ListObjectives('Cooperate')
end

function ObjectiveLister.ListAppeaseObjectives()
	return ObjectiveLister.ListObjectives('Appease')
end

function ObjectiveLister.ListSubmitObjectives()
	return ObjectiveLister.ListObjectives('Submit')
end

function ObjectiveLister.ListAllObjectives()
	return ObjectiveLister.ListObjectives()
end

---@param forFaction the faction to show objectives for. nil means show all objectives.
---@return wiki table of Objectives. Only Campaign and Victory objectives
function ObjectiveLister.ListObjectives(forFaction)
	local returnString = '{| class="wikitable sortable mw-collapsible" style="text-align: center;"'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n! Objective'
	returnString = returnString .. '\n! (Rough)<br>Order'
	returnString = returnString .. '\n! Solution'
	returnString = returnString .. '\n! Prereq<br>Objective(s)'
	returnString = returnString .. '\n|-'
	
	local rowStrings = {}
	local maxOrder = 1
	for rowID, row in pairs(ObjectiveData) do
		if row['objectiveType'] == 'Campaign' or row['objectiveType'] == 'Victory' then
			local chosenFaction = nil
			if not forFaction or forFaction == '' then
				chosenFaction = row['factionDataNames/0']
			else
				for i = 0, 6 do
					local fdn = row['factionDataNames/' .. i]
					if fdn and fdn == forFaction .. 'Council' then
						chosenFaction = fdn
					end
				end
			end
			if chosenFaction and chosenFaction ~= '' then
				local roughOrder = 1
				for _, _ in pairs(ObjectivePrereqs[rowID]) do
					roughOrder = roughOrder + 1
				end
				if roughOrder > maxOrder then
					maxOrder = roughOrder
				end
				if not rowStrings[roughOrder] then
					rowStrings[roughOrder] = ''	
				end
				
				rowStrings[roughOrder]  = rowStrings[roughOrder]  .. '\n| [[' .. rowID .. '|' .. ObjectiveNames['TIObjectiveTemplate.displayName.' .. rowID .. '.' .. chosenFaction] .. ']]<br>'
				for i = 0, 6 do
					local fdn = row['factionDataNames/' .. i]
					if fdn and fdn ~= '' then
						rowStrings[roughOrder]  = rowStrings[roughOrder]  .. IconModule.Icon({args={councilToIdeo[fdn]}})
					end
				end
				
				rowStrings[roughOrder]  = rowStrings[roughOrder]  .. '\n| ' .. roughOrder
				
				local osolution = ObjectiveNames['TIObjectiveTemplate.solution.' .. rowID .. '.' .. chosenFaction]
				osolution = sreplace(osolution, '{winnerOrg}', OrgNames['TIOrgTemplate.displayName.' .. sreplace(chosenFaction,'Council','') .. 'Winner'])
				osolution = sreplace(osolution, '{winnerOrgMission}', IconModule.MissionIcon({args={sreplace(chosenFaction,'Council','') .. 'Win'}}) .. '[[' .. MissionNames['TIMissionTemplate.displayName.' .. sreplace(chosenFaction,'Council','') .. 'Win'] .. ']]')
				rowStrings[roughOrder]  = rowStrings[roughOrder]  .. '\n| ' .. osolution .. '\n| '
				
				
				local uoc = row['unlockingObjectivesConjunction']
				for i = 0, 5 do
					local uon = row['unlockingObjectiveNames/' .. i]
					if uon and uon ~= '' then
						if i > 0 then
							rowStrings[roughOrder]  = rowStrings[roughOrder]  .. ' ' .. uoc .. ' '
						end
						rowStrings[roughOrder]  = rowStrings[roughOrder]  .. '[[' .. uon .. '|' .. (ObjectiveNames['TIObjectiveTemplate.displayName.' .. uon .. '.' .. chosenFaction] or ObjectiveNames['TIObjectiveTemplate.displayName.' .. uon .. '.' .. ObjectiveData[uon]['factionDataNames/0']]) .. ']]'
					end
				end
				
		    	rowStrings[roughOrder]  = rowStrings[roughOrder]  .. '\n|-'
		    end
	    end
	end
	for i = 1, maxOrder do
		if rowStrings[i] then
			returnString = returnString .. rowStrings[i]
		end
	end
	returnString = returnString .. '\n|}'
	
	return returnString
end


---@param frame a set of arguments that includes:
----frame.args[1] the dataName of the objective
----frame.args['faction'] the faction to show the objective for, e.g., ResistCouncil
----frame.args['notes'] any additional notes the output table should contain
---@return wiki table of details for that objective
function ObjectiveLister.SingleObjectiveTable(frame)
	local ObjectiveID = frame.args[1]
    local ObjectiveFaction = frame.args['faction'] or ''
    local ObjectiveNotes = frame.args['notes'] or ''
    
    local validFaction = false
    for i = 0, 6 do
		local fdn = ObjectiveData[ObjectiveID]['factionDataNames/' .. i]
		if fdn and fdn == ObjectiveFaction then
			validFaction = true
		end
    end

	if not validFaction then
		return 'Not Available to this Faction.'
	end
    
	local returnString = '{| class="wikitable" style="width: 100%;text-align:center;"'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n! colspan=4 | <big>' .. (ObjectiveNames['TIObjectiveTemplate.displayName.' .. ObjectiveID .. '.' .. ObjectiveFaction] or ObjectiveID) .. '</big>'
	returnString = returnString .. '\n|-'
	
	local odescription = ObjectiveNames['TIObjectiveTemplate.description.' .. ObjectiveID .. '.' .. ObjectiveFaction]
	local osolution = ObjectiveNames['TIObjectiveTemplate.solution.' .. ObjectiveID .. '.' .. ObjectiveFaction]
	osolution = sreplace(osolution, '{winnerOrg}', OrgNames['TIOrgTemplate.displayName.' .. sreplace(ObjectiveFaction,'Council','') .. 'Winner'])
	osolution = sreplace(osolution, '{winnerOrgMission}', IconModule.MissionIcon({args={sreplace(ObjectiveFaction,'Council','') .. 'Win'}}) .. '[[' .. MissionNames['TIMissionTemplate.displayName.' .. sreplace(ObjectiveFaction,'Council','') .. 'Win'] .. ']]')
	
	local oresolution = ObjectiveNames['TIObjectiveTemplate.resolution.' .. ObjectiveID .. '.' .. ObjectiveFaction]
	local oquote = ObjectiveNames['TIObjectiveTemplate.quote.' .. ObjectiveID .. '.' .. ObjectiveFaction]
	
	if osolution then
		returnString = returnString .. '\n! colspan=4 | Solution'
		returnString = returnString .. '\n|-'
		returnString = returnString .. '\n| colspan=4 | ' .. osolution
		returnString = returnString .. '\n|-'
	end
	
	if odescription and oresolution then
		returnString = returnString .. '\n! colspan=2 style="width: 50%" | Description'
		returnString = returnString .. '\n! colspan=2 style="width: 50%" | Resolution'
		returnString = returnString .. '\n|-'
		returnString = returnString .. '\n| colspan=2 | ' .. odescription
		returnString = returnString .. '\n| colspan=2 | ' .. oresolution
		returnString = returnString .. '\n|-'
	else
		if odescription then
			returnString = returnString .. '\n! colspan=4 | Description'
			returnString = returnString .. '\n|-'
			returnString = returnString .. '\n| colspan=4 | ' .. odescription
			returnString = returnString .. '\n|-'
		end
		if oresolution then
			returnString = returnString .. '\n! colspan=4 | Resolution'
			returnString = returnString .. '\n|-'
			returnString = returnString .. '\n| colspan=4 | ' .. oresolution
			returnString = returnString .. '\n|-'
		end
	end
	
	if oquote then
		returnString = returnString .. '\n! colspan=4 | Quote'
		returnString = returnString .. '\n|-'
		returnString = returnString .. '\n| colspan=4 | ' .. oquote
		returnString = returnString .. '\n|-'
	end
	
	returnString = returnString .. '\n! colspan=4 | Prerequisite Objective(s)'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n| colspan=4 | ' 
	local uoc = ObjectiveData[ObjectiveID]['unlockingObjectivesConjunction']
	local firstitem = true
	for i = 0, 5 do
		local uon = ObjectiveData[ObjectiveID]['unlockingObjectiveNames/' .. i]
		if uon and uon ~= '' and ObjectiveNames['TIObjectiveTemplate.displayName.' .. uon .. '.' .. ObjectiveFaction] then
			if not firstitem then
				returnString = returnString .. ' ' .. uoc .. ' '
			else
				firstitem = false
			end
			returnString = returnString .. '[[' .. uon .. '|' .. ObjectiveNames['TIObjectiveTemplate.displayName.' .. uon .. '.' .. ObjectiveFaction] .. ']]'
		end
	end
	returnString = returnString .. '\n|-'
	
	if ObjectiveData[ObjectiveID]['resourcesGranted/0/resource'] and ObjectiveData[ObjectiveID]['resourcesGranted/0/resource'] ~= '' then
		returnString = returnString .. '\n! colspan=2 style="width: 50%" | Resources Granted:'
		returnString = returnString .. '\n| colspan=2 |'
		
		for i = 0, 1 do
			if ObjectiveData[ObjectiveID]['resourcesGranted/' .. i .. '/resource'] then
				returnString = returnString .. ObjectiveData[ObjectiveID]['resourcesGranted/' .. i .. '/value'] .. IconModule.Icon({args={ObjectiveData[ObjectiveID]['resourcesGranted/' .. i .. '/resource']}})
			end
		end
		returnString = returnString .. '\n|-'
	end
	
	local omilestone = ObjectiveData[ObjectiveID]['targetMilestone']
	if omilestone and omilestone ~= '' then
		returnString = returnString .. '\n! colspan=2 style="width: 50%" | Can be completed by Milestone:'
		returnString = returnString .. '\n| colspan=2 | [[' .. omilestone .. '|' .. ObjectiveNames['TIObjectiveTemplate.MilestoneFulfilled.' .. omilestone] .. ']]'
		returnString = returnString .. '\n|-'
	end
	
	local oproject = ObjectiveData[ObjectiveID]['targetProjectTemplateName']
	if oproject and oproject ~= '' then
		returnString = returnString .. '\n! colspan=2 style="width: 50%" | Can be completed by [[Technology|Project]]:'
		returnString = returnString .. '\n| colspan=2 | [[' .. oproject .. '|' .. ProjectNames['TIProjectTemplate.displayName.' .. oproject] .. ']]'
		returnString = returnString .. '\n|-'
	end
	
	local omission = ObjectiveData[ObjectiveID]['targetMissionTemplateName']
	if omission and omission ~= '' then
		returnString = returnString .. '\n! colspan=2 style="width: 50%" | Can be completed by [[Missions|Mission]]:'
		returnString = returnString .. '\n| colspan=2 | ' .. IconModule.MissionIcon({args={omission}}) .. '[[' .. MissionNames['TIMissionTemplate.displayName.' .. omission] .. ']]'
		returnString = returnString .. '\n|-'
	end
	
	local oprojects = ''
	for rowID, row in pairs(ProjectData) do
		if row['requiredObjectiveName'] and (row['requiredObjectiveName'] == ObjectiveID or row['altRequiredObjectiveName'] == ObjectiveID) then
			if oprojects ~= '' then
				oprojects = oprojects .. ' • '	
			end
			oprojects = oprojects .. '[[' .. rowID .. '|' .. ProjectNames['TIProjectTemplate.displayName.' .. rowID] .. ']]'
		end
	end
	if oprojects ~= '' then
		returnString = returnString .. '\n! colspan=2 style="width: 50%" | Unlocks [[Technology|Project]](s):'
		returnString = returnString .. '\n| colspan=2 | ' .. oprojects
		returnString = returnString .. '\n|-'
	end
	
	returnString = returnString .. '\n! colspan=4 | Notes'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n| colspan=4 | ' .. ObjectiveNotes
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n! colspan=4 | Data Sources'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n| colspan=4 | [[TIMissionTemplate_EN]] • [[TIObjectiveTemplate_CSV]] • [[TIObjectiveTemplate_EN]] • [[TIOrgTemplate_EN]] • [[TIProjectTemplate_CSV]] • [[TIProjectTemplate_EN]]'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n! colspan=4 | Related Pages'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n| colspan=4 | [[Objective List]] • [[Factions]]'
	returnString = returnString .. '\n|-'
	
	returnString = returnString .. '\n|}'

	return returnString
end


---@param frame a set of arguments that includes:
----frame.args[1] the dataName of the milestone
----frame.args['notes'] any additional notes the output table should contain
---@return wiki table of details for that milestone
function ObjectiveLister.SingleMilestoneTable(frame)
	local MilestoneID = frame.args[1]
    local MilestoneNotes = frame.args['notes'] or ''
    
    
	local returnString = '{| class="wikitable" style="width: 100%;text-align:center;"'
	returnString = returnString .. '\n|-'
	if hiddenMilestones[MilestoneID] then
		returnString = returnString .. '\n! colspan=4 | <big>' .. hiddenMilestones[MilestoneID] .. '</big>'
		returnString = returnString .. '\n|-'
		
		returnString = returnString .. '\n! colspan=4 | Hidden Milestone'
		returnString = returnString .. '\n|-'
	else
		returnString = returnString .. '\n! colspan=4 | <big>' .. (ObjectiveNames['TIObjectiveTemplate.MilestoneFulfilled.' .. MilestoneID] or MilestoneID) .. '</big>'
		returnString = returnString .. '\n|-'
		
		returnString = returnString .. '\n! colspan=4 | Summary'
		returnString = returnString .. '\n|-'
		returnString = returnString .. '\n| colspan=4 | ' .. (ObjectiveNames['TIObjectiveTemplate.MilestoneFulfilled.Summary.' .. MilestoneID] or 'ERROR: No summary found.')
		returnString = returnString .. '\n|-'
		
		returnString = returnString .. '\n! colspan=4 | Detail'
		returnString = returnString .. '\n|-'
		returnString = returnString .. '\n| colspan=4 | ' .. (ObjectiveNames['TIObjectiveTemplate.MilestoneFulfilled.Detail.' .. MilestoneID] or 'ERROR: No detail found.')
		returnString = returnString .. '\n|-'
	end
	
	local mobjectives = ''
	for rowID, row in pairs(ObjectiveData) do
		if row['targetMilestone'] and row['targetMilestone'] == MilestoneID then
			if mobjectives ~= '' then
				mobjectives = mobjectives .. ' • '	
			end
			mobjectives = mobjectives .. '[[' .. rowID .. '|' .. ObjectiveNames['TIObjectiveTemplate.displayName.' .. rowID .. '.' .. row['factionDataNames/0']] .. ']]'
		end
	end
	if mobjectives ~= '' then
		returnString = returnString .. '\n! colspan=2 style="width: 50%" | Completes Objective(s):'
		returnString = returnString .. '\n| colspan=2 | ' .. mobjectives
		returnString = returnString .. '\n|-'
	end
	
	local mprojects = ''
	for rowID, row in pairs(ProjectData) do
		if row['requiredMilestone'] and row['requiredMilestone'] == MilestoneID then
			if mprojects ~= '' then
				mprojects = mprojects .. ' • '	
			end
			mprojects = mprojects .. '[[' .. rowID .. '|' .. ProjectNames['TIProjectTemplate.displayName.' .. rowID] .. ']]'
		end
	end
	if mprojects ~= '' then
		returnString = returnString .. '\n! colspan=2 style="width: 50%" | Unlocks [[Technology|Project]](s):'
		returnString = returnString .. '\n| colspan=2 | ' .. mprojects
		returnString = returnString .. '\n|-'
	end
	
	returnString = returnString .. '\n! colspan=4 | Notes'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n| colspan=4 | ' .. MilestoneNotes
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n! colspan=4 | Data Sources'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n| colspan=4 | [[TIObjectiveTemplate_CSV]] • [[TIObjectiveTemplate_EN]] • [[TIProjectTemplate_CSV]] • [[TIProjectTemplate_EN]]'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n! colspan=4 | Related Pages'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n| colspan=4 | [[Objective List]] • [[Factions]]'
	returnString = returnString .. '\n|-'
	
	returnString = returnString .. '\n|}'

	return returnString
end

--endregion

return ObjectiveLister