Module:ListOfSortable

Revision as of 05:19, 7 September 2022 by Memetails (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

---
-- Automates generation of sortable table by designated class by looking at all ships within Module:Data/Ship
--
-- TODO: Get stats from Module:Data/Ship instead of relying on Module:ELiteShipsKai to retrieve from Module:Data/Ships/<ShipName>
-- TODO: Automate coloring without Module:EliteShipsKai
-- TODO: Handle equipment

local ListOfSortable = {}
local U = require('Module:Core')

local template = {
	footer = "}}",
--	header = [[{{EliteShipsKaiSortable]],
header = [[]],
	item = [=[%s]=],
}

local function GenerateTable(equipmentOrShip, itemType, remodelSuffix, notes, coloring, ...)
	local success,data
	local equipment = false
	if equipmentOrShip == 'Equipment' then 
		success, data = U.loadData("Data/Equipment")
		equipment = true
	elseif equipmentOrShip == "Ship" then
		success, data = U.loadData("Data/Ship")
	else
		return "Invalid equipmentOrShip value"
	end
	if not success then return "No data for ships: " .. itemType end
	
	local result = { string.format(template.header) }
	
	local suffix = {}
	local index = 1
	for k, v in pairs(remodelSuffix) do
		suffix[index] = v
		index = index + 1
	end
	
	local first = true
	if not equipment then
		for k, v in pairs(data) do
			if v.type == itemType and v.remodellv ~= '' then
				local name = v.name[3]
				local success = 0
				for k, v in pairs(remodelSuffix) do
					if success == 0 and v ~= nil and v ~= "" then
						name,success = name:gsub("." .. v, "/" .. v)
					end
				end
				if first then table.insert(result, name) first = false
				else		  table.insert(result, "|" .. name) end
			end
		end
	else
		for k, v in pairs(data) do
			if v.type == itemType then
				local name = v.name[2]
				if first then table.insert(result, name) first = false
				else		  table.insert(result, "|" .. name) end
			end
		end
	end
	
	for k, v in pairs(notes) do
		table.insert(result, "|" ..  v)
	end
	
	for k, v in pairs(coloring) do
		table.insert(result, "|" .. v)
	end

--	table.insert(result, template.footer)
	return table.concat(result, "\n")
end

function ListOfSortable.ParseAndGenerate(frame)
	local equipmentOrShip = {}
	if frame.args['equipmentOrShip'] ~= nil then equipmentOrShip = frame.args['equipmentOrShip'] end
	local itemType = {}
	if frame.args['itemType']	     ~= nil then itemType        = frame.args['itemType'] end
	local remodelSuffix = {}
	if frame.args['remodelSuffix']   ~= nil then remodelSuffix   = U.split(frame.args['remodelSuffix'], ',') end
	local notes = {}
	if frame.args['notes']		     ~= nil then notes		     = U.split(frame.args['notes'], ',\n') end
	local coloring = {}
	if frame.args['coloring']	     ~= nil then coloring 	     = U.split(frame.args['coloring'], ',\n') end
	
	return GenerateTable(equipmentOrShip, itemType, remodelSuffix, notes, coloring)
end

local function test()
--	mw.log(generateTable("Destroyer", {"B Kai", "D Kai", "Mk.II", "Zwei"},
--		{"Mikazuki/Kai_notes = Can equip [[Daihatsu Landing Craft|Daihatsu]]",
--			"Mutsuki/Kai Ni_notes = 2nd remodel. Can equip [[Daihatsu Landing Craft|Daihatsu]]",
--			"Kisaragi/Kai Ni_notes = 2nd remodel. Can equip [[Daihatsu Landing Craft|Daihatsu]]",
--			"Satsuki/Kai Ni_notes = 2nd remodel. Can equip [[Daihatsu Landing Craft|Daihatsu]] & [[Special Type 2 Amphibious Landing Craft|Type 2 Landing Craft]]",
--			"Fumizuki/Kai Ni_notes = 2nd remodel. Can equip [[Daihatsu Landing Craft|Daihatsu]] & [[Special Type 2 Amphibious Landing Craft|Type 2 Landing Craft]]"},
--		{"remodel_level_good = 20",
--			"remodel_level_outstanding = 15",
--			"remodel_level_operator = <",
--			"firepower_good = 56",
--			"firepower_outstanding = 61",
--			"torpedo_good = 83",
--			"torpedo_outstanding = 89",
--			"night_battle = true",
--			"night_battle_good = 140",
--			"night_battle_outstanding = 150",
--			"aa_good = 64",
--			"aa_outstanding = 74",
--			"asw_good = 67",
--			"asw_outstanding = 73",
--			"los_good = 42",
--			"los_outstanding = 49",
--			"hp_good = 34",
--			"hp_outstanding = 36",
--			"armor_good = 50",
--			"armor_outstanding = 58",
--			"evasion_good = 92",
--			"evasion_outstanding = 98",
--			"aircraft_good = 1",
--			"aircraft_outstanding = 1",
--			"aircraft_slot_good = 1",
--			"luck_good = 19",
--			"luck_outstanding = 30",
--			"luck_max_good = 80",
--			"fuel_operator=<",
--			"ammo_good = 20",
--			"ammo_outstanding = 20",
--			"ammo_operator = <",
--			"reference_type = 2"}))
	
	local frame = {}
	frame.args = {}
	frame.args['equipmentOrShip'] = ""
	frame.args['itemType'] = ""
	frame.args['remodelSuffix'] = ""
	frame.args['notes'] = ""
	frame.args['coloring'] = ""
	mw.log(ListOfSortable.ParseAndGenerate(frame))
	
	local frame = {}
	frame.args = {}
	mw.log(ListOfSortable.ParseAndGenerate(frame))
	
	frame = {}
	frame.args = {}
	frame.args['equipmentOrShip'] = "Equipment"
	frame.args['itemType'] = "Small Caliber Main Gun"
	frame.args['remodelSuffix'] = ""
	frame.args['notes'] = ""
	frame.args['coloring'] = ""
	mw.log(ListOfSortable.ParseAndGenerate(frame))
	
	frame = {}
	frame.args = {}
	frame.args['equipmentOrShip'] = "Ship"
	frame.args['itemType'] = "Destroyer"
	frame.args['remodelSuffix'] = "B Kai,D Kai,Mk.II,Zwei,Kai"
	frame.args['notes'] = "Mikazuki/Kai_notes = Can equip [[Daihatsu Landing Craft|Daihatsu]],\nMutsuki/Kai Ni_notes = 2nd remodel. Can equip [[Daihatsu Landing Craft|Daihatsu]],\nKisaragi/Kai Ni_notes = 2nd remodel. Can equip [[Daihatsu Landing Craft|Daihatsu]],\nSatsuki/Kai Ni_notes = 2nd remodel. Can equip [[Daihatsu Landing Craft|Daihatsu]] & [[Special Type 2 Amphibious Landing Craft|Type 2 Landing Craft]],\nFumizuki/Kai Ni_notes = 2nd remodel. Can equip [[Daihatsu Landing Craft|Daihatsu]] & [[Special Type 2 Amphibious Landing Craft|Type 2 Landing Craft]]"
	frame.args['coloring'] = "remodel_level_good = 20,\nremodel_level_outstanding = 15,\nremodel_level_operator = <,\nfirepower_good = 56,\nfirepower_outstanding = 61,\ntorpedo_good = 83,\ntorpedo_outstanding = 89,\nnight_battle = true,\nnight_battle_good = 140,\nnight_battle_outstanding = 150,\nreference_type = 2"
	mw.log(ListOfSortable.ParseAndGenerate(frame))
end

--return { test = test } --swap comments to test
return ListOfSortable

-- TEST: p.test()