Module:TalentTable

From TwogPedia
Revision as of 14:50, 14 February 2024 by Couchor (talk | contribs)

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

local cargo = mw.ext.cargo

local getArgs = require('Module:Arguments').getArgs
local makeFlag = require('Module:Person/Flag').makeFlag
local Links = require('Module:Links')

local p = {}

function p.main(frame)
	local args = getArgs(frame)

	local currentTitle = mw.title.getCurrentTitle().text
	local gameCategory = mw.text.split(currentTitle, '/')[1]
	
	local fullString = ''
	-- Cycle through different tabs
	for key, value in ipairs (args) do
		local tab = mw.text.jsonDecode(value)
		if key == 1 then
			fullString = '<tabber>|-|' .. tab.lang .. '=' 
		else
			fullString = fullString .. '|-|' .. tab.lang .. '='
		end
	
		local tabCell = mw.html.create('div')
		
		local container = mw.html.create('div')
		
		if tab.topNote then container:wikitext(tab.topNote) end
		
		container:node(tabCell)
		
		local lists = {}
		local listCol = 1
		local section = 1
		local firstSection = true
		local sections = {}
		sections[section] = {val = '', lists = {} }
		sections[section].lists[listCol] = ''
		
		for tabKey, tabValue in ipairs (tab) do
			local role = mw.text.jsonDecode(tabValue)
			
			if role.section then
				if firstSection then firstSection = false else 
					section = section + 1 
					sections[section] = {val = '', lists = {}}
					listCol = 1
					sections[section].lists[listCol] = ''
				end
				sections[section].title = mw.html.create('div'):wikitext(role.section)
			end
				if role.div then 
					listCol = listCol + 1 
					sections[section].lists[listCol] = ''
				end
				
				local roleTitle = mw.html.create('div'):addClass('tc'):wikitext(role[1])
				local roleList = mw.html.create('ul')
				local roleCell = mw.html.create('div'):addClass('talent__role'):node(roleTitle):node(roleList)
				
				local i = 1
				
				local people = ''
				-- Loop through all the talent
				if role['t1'] then 
					while role['t' .. i] do
						
						local link = role['t' .. i .. 'link'] or 'People/' .. role['t' .. i]
                        people = people .. link .. ','
						local flag = makeFlag(role['t'.. i .. 'flag'], role['t'.. i .. 'link'] or 'People/' .. role['t'.. i], gameCategory)
						local talentCell = mw.html.create('li'):wikitext(flag):wikitext('[[' .. link ..'|' .. role['t' .. i] .. ']]')
						roleList:node(talentCell)
						i = i + 1
					end
				else 
					roleList:node(mw.html.create('li'):wikitext('TBD'))	
				end
				
				
				
				-- Save talent to cargo
				frame:callParserFunction{name = '#cargo_store:', args = {_table = 'Talent', people = people, role = role[1], lang = tab.lang}}
			
				sections[section].lists[listCol] = tostring(sections[section].lists[listCol]) .. tostring(roleCell)
			
		end

		for i = 1, #sections do
			local wrapper = mw.html.create('div')
			local title = mw.html.create('div'):addClass('talent__stage'):node(sections[i].title)
			local cell = mw.html.create('div'):addClass('talent__container')
			
			for j, subtable in ipairs(sections[i].lists) do
			     cell:node(mw.html.create('div'):node(subtable))
			end
			
			container:node(wrapper:node(title):node(cell))
		end
		
		if tab.botNote then container:wikitext(tab.botNote) end
		
		fullString = fullString .. tostring(container)
	end
	fullString = fullString .. '</tabber>'
	return mw.html.create():wikitext(frame:preprocess(fullString))
end

return p