Module:ProjectLister

From Terra Invicta Official Wiki

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

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

--region Dependencies
local IconModule = require('Module:Icon')
local ProjectData = mw.loadData('Module:CSVReader/Project')
local TechData = mw.loadData('Module:CSVReader/Tech')
local BilateralData = mw.loadData('Module:CSVReader/Bilateral')

local ProjectNameBuilder = require('Module:ProjectLister/NameBuilder')
local TechNames = mw.loadData('Module:ENReader/Tech')

local ProjectPrereqs = mw.loadData('Module:ProjectLister/Prereqs')
local ProjectCosts = mw.loadData('Module:ProjectLister/Costs')
local ProjectProjects = mw.loadData('Module:ProjectLister/Projects')

--endregion

--region Private constants
local insert = table.insert
local concat = table.concat
local floor = math.floor
local sfind = string.find
local ssub = string.sub
local ScienceCategories = {
	Energy = 'energy',
	InformationScience='information',
	LifeScience='life',
	Materials='material',
	MilitaryScience='military',
	SocialScience='social',
	SpaceScience='space',
	Xenology='xenology'
}
local CouncilIcons = {
	DestroyCouncil='destroy',
	ResistCouncil='resist',
	EscapeCouncil='escape',
	ExploitCouncil='exploit',
	CooperateCouncil='cooperate',
	AppeaseCouncil='appease',
	SubmitCouncil='submit',
	AlienCouncil='alien'
}
--endregion



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

--region Private methods
--endregion

--region Public methods
-- Might remove this detailed view. It's too cluttered.
function ProjectLister.ListDetailedProjects()
	return ProjectLister.ListProjects(true)
end

function ProjectLister.ListBasicProjects()
	return ProjectLister.ListProjects(false)
end

---@param detailed boolean whether to show the detailed or basic version.
---@return wiki table of Projects
function ProjectLister.ListProjects(detailed)
	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'
	if detailed then
		returnString = returnString .. '\n! Unlockable<br>Projects'
		returnString = returnString .. '\n! Full<br>Prereqs'
	else
		returnString = returnString .. '\n! Direct<br>Prereqs'
	end
	returnString = returnString .. '\n! Alt<br>Prereq'
	returnString = returnString .. '\n|-'
	
	for rowID, row in pairs(ProjectData) do
		if (not row['disable'] or row['disable'] ~= 'true') and row['factionPrereq/0'] ~= 'AlienCouncil' then
			returnString = returnString .. '\n| ' .. ProjectNameBuilder.buildName({args={rowID}})
			returnString = returnString .. '\n| ' .. IconModule.Icon({args={ScienceCategories[row['techCategory']]}})
			returnString = returnString .. '\n| ' .. row['researchCost']
			returnString = returnString .. '\n| ' .. tostring(ProjectCosts[rowID]['total'])
			
			if detailed then
				if ProjectProjects[rowID] then
					local projects = {}
					for projectID, _ in pairs(ProjectProjects[rowID]) do
						local projectChance = ProjectData[projectID]['factionAvailableChance']
						insert(projects, '(' .. projectChance .. '%)' .. ProjectNameBuilder.buildName({args={projectID}}))
					end
					returnString = returnString .. '\n| ' .. concat(projects, '<br>')
				else
					returnString = returnString .. '\n| '
				end
			end
			
			local prereqs = {}
			if detailed then
				for prereqID, _ in pairs(ProjectPrereqs[rowID]) do
					if string.sub(prereqID,1,7) == 'Project' then
						insert(prereqs, ProjectNameBuilder.buildName({args={prereqID}}))
					else
						insert(prereqs, '[[' .. prereqID .. '|' .. TechNames['TITechTemplate.displayName.' .. prereqID] .. ']]')
					end
				end
			else
				for i = 0, 4 do
					local prereq = row['prereqs/' .. i]
					if prereq and prereq ~= '' and prereq ~= 0 then
						if string.sub(prereq,1,7) == 'Project' then
							insert(prereqs, ProjectNameBuilder.buildName({args={prereq}}))
						else
							insert(prereqs, '[[' .. prereq .. '|' .. TechNames['TITechTemplate.displayName.' .. prereq] .. ']]')
						end
					end
				end
			end
			returnString = returnString .. '\n| ' .. concat(prereqs, '<br>')
			
			local prereq0 = row['prereqs/0']
			local altprereq = row['altPrereq0']
			if not altprereq or altprereq == '' then
				returnString = returnString .. '\n| '
			else
				if string.sub(prereq0,1,7) == 'Project' then
					prereq0 = ProjectNameBuilder.buildName({args={prereq0}})
				else
					prereq0 = '[[' .. prereq0 .. '|' .. TechNames['TITechTemplate.displayName.' .. prereq0] .. ']]'
				end
				if string.sub(altprereq,1,7) == 'Project' then
					altprereq = ProjectNameBuilder.buildName({args={altprereq}})
				else
					altprereq = '[[' .. altprereq .. '|' .. TechNames['TITechTemplate.displayName.' .. altprereq] .. ']]'
				end
				returnString = returnString .. '\n| Replace ' .. prereq0 .. '<br>With ' .. altprereq
			end
			
			returnString = returnString .. '\n|-'
		end
	end
	returnString = returnString .. '\n|}'
	
	return returnString
end

function ProjectLister.ListProjectChances()
	local returnString = '{| class="wikitable sortable mw-collapsible" style="text-align: center;"'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n! rowspan=2|Project'
	returnString = returnString .. '\n! rowspan=2|Base Unlock Chance'
	returnString = returnString .. '\n! colspan=3|Available Chance'
	returnString = returnString .. '\n! rowspan=2|Available To:'
	returnString = returnString .. '\n! rowspan=2|Guaranteed Unlock for:'
	returnString = returnString .. '\n|-'
	returnString = returnString .. '\n! Initial'
	returnString = returnString .. '\n! Monthly Increase'
	returnString = returnString .. '\n! Maximum'
	returnString = returnString .. '\n|-'
	
	for rowID, row in pairs(ProjectData) do
		if (not row['disable'] or row['disable'] ~= 'true') and row['factionPrereq/0'] ~= 'AlienCouncil' then
			returnString = returnString .. '\n| ' .. ProjectNameBuilder.buildName({args={rowID}})
			
			returnString = returnString .. '\n| ' .. row['factionAvailableChance'] .. '%'
			returnString = returnString .. '\n| ' .. row['initialUnlockChance'] .. '%'
			returnString = returnString .. '\n| ' .. row['deltaUnlockChance'] .. '%'
			returnString = returnString .. '\n| ' .. row['maxUnlockChance'] .. '%'
			
			if not row['factionPrereq/0'] or row['factionPrereq/0'] == '' then
				returnString = returnString .. '\n| All'
			else
				local councils = {}
				for i = 0, 5 do
					local freq = row['factionPrereq/' .. i]
					if freq and freq ~= '' and freq ~= 0 then
						insert(councils, IconModule.Icon({args={CouncilIcons[freq]}}))
					end
				end
				returnString = returnString .. '\n| ' .. concat(councils, '')
			end
			
			local falways = row['factionAlways']
			if not falways or falways == '' then
				returnString = returnString .. '\n| '
			elseif falways == 'Random' then
				returnString = returnString .. '\n| Random' 
			else
				returnString = returnString .. '\n| ' .. IconModule.Icon({args={CouncilIcons[falways]}})
			end
			
			returnString = returnString .. '\n|-'
		end
	end
	returnString = returnString .. '\n|}'
	
	return returnString
end

--endregion

return ProjectLister