• Welcome to the Kancolle Wiki!
  • If you have any questions regarding site content, account registration, etc., please visit the KanColle Wiki Discord

Difference between revisions of "Module:ListOfSortable"

From Kancolle Wiki
Jump to navigation Jump to search
m
 
(17 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
-- TODO: Get stats from Module:Data/Ship instead of relying on Module:ELiteShipsKai to retrieve from Module:Data/Ships/<ShipName>
 
-- 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: Automate coloring without Module:EliteShipsKai
 +
-- TODO: Handle equipment
  
 +
local ListOfSortable = {}
 
local U = require('Module:Core')
 
local U = require('Module:Core')
local EliteShips = require('Module:EliteShips')
 
  
 
local template = {
 
local template = {
 
footer = "}}",
 
footer = "}}",
header = [[{{EliteShipsKaiSortable]],
+
-- header = [[{{EliteShipsKaiSortable]],
item = [=[
+
header = [[]],
|%s]=],
+
item = [=[%s]=],
 
}
 
}
  
local function generateTable(shipClass, remodelSuffix, notes, coloring, ...)
+
local function GenerateTable(equipmentOrShip, itemType, remodelSuffix, notes, coloring, ...)
local shipClass = shipClass or frame.args[1] or "??"
+
local success,data
local success, data = U.loadData("Data/Ship")
+
local equipment = false
if not success then return "No data for ships: " .. shipClass end
+
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 result = { string.format(template.header) }
Line 29: Line 38:
 
end
 
end
 
 
for k, v in pairs(data) do
+
local first = true
if v.type == shipClass and v.remodellv ~= '' then
+
if not equipment then
local name = v.name[3]
+
for k, v in pairs(data) do
local success = 0
+
if v.type == itemType and v.remodellv ~= '' then
for k, v in pairs(remodelSuffix) do
+
local name = v.name[3]
if success == 0 then
+
local success = 0
name,success = name:gsub("." .. v, "/" .. v)
+
for k, v in pairs(remodelSuffix) do
 +
if success == 0 and v ~= nil and v ~= "" then
 +
name,success = name:gsub("." .. v, "/" .. v)
 +
end
 
end
 
end
 +
if first then table.insert(result, name) first = false
 +
else   table.insert(result, "|" .. name) end
 
end
 
end
if success == 0 then
+
end
name,success = name:gsub(".Kai", "/Kai")
+
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
table.insert(result, string.format(template.item, name))
 
 
end
 
end
 
end
 
end
 
 
 
for k, v in pairs(notes) do
 
for k, v in pairs(notes) do
table.insert(result, string.format(template.item, v))
+
table.insert(result, "|" .. v)
 
end
 
end
 
 
 
for k, v in pairs(coloring) do
 
for k, v in pairs(coloring) do
table.insert(result, string.format(template.item, v))
+
table.insert(result, "|" .. v)
 
end
 
end
  
table.insert(result, template.footer)
+
-- table.insert(result, template.footer)
 
return table.concat(result, "\n")
 
return table.concat(result, "\n")
 
end
 
end
  
local function generateShipList(shipClass, remodelSuffix)
+
function ListOfSortable.ParseAndGenerate(frame)
local shipClass = shipClass or frame.args[1] or "??"
+
local equipmentOrShip = {}
local success, data = U.loadData("Data/Ship")
+
if frame.args['equipmentOrShip'] ~= nil then equipmentOrShip = frame.args['equipmentOrShip'] end
if not success then return "No data for ships: " .. shipClass end
+
local itemType = {}
+
if frame.args['itemType']     ~= nil then itemType        = frame.args['itemType'] end
local result = {}
+
local remodelSuffix = {}
+
if frame.args['remodelSuffix']   ~= nil then remodelSuffix  = U.split(frame.args['remodelSuffix'], ',') end
local suffix = {}
+
local notes = {}
local index = 1
+
if frame.args['notes']     ~= nil then notes     = U.split(frame.args['notes'], ',\n') end
for k, v in pairs(remodelSuffix) do
+
local coloring = {}
suffix[index] = v
+
if frame.args['coloring']     ~= nil then coloring     = U.split(frame.args['coloring'], ',\n') end
index = index + 1
 
end
 
 
for k, v in pairs(data) do
 
if v.type == shipClass and v.remodellv ~= '' then
 
local name = v.name[3]
 
local success = 0
 
for k, v in pairs(remodelSuffix) do
 
if success == 0 then
 
name,success = name:gsub("." .. v, "/" .. v)
 
end
 
end
 
if success == 0 then
 
name,success = name:gsub(".Kai", "/Kai")
 
end
 
table.insert(result, string.format(template.item, name))
 
end
 
end
 
 
return result
 
end
 
 
 
local function eliteShipsCall(frame)
 
local result = generateShipList(frame.args['shipClass'], frame.args['remodelSuffix'])
 
local args = U.split(result, '|')
 
return EliteShips.SortableTable(args)
 
end
 
 
 
local function render(frame)
 
local shipClass = frame.args['shipClass']
 
local remodelSuffix = U.split(frame.args['remodelSuffix'], ',')
 
local notes = U.split(frame.args['notes'], ',')
 
local coloring = U.split(frame.args['coloring'], ',')
 
 
 
return generateTable(shipClass, remodelSuffix, notes, coloring)
+
return GenerateTable(equipmentOrShip, itemType, remodelSuffix, notes, coloring)
 
end
 
end
  
Line 147: Line 132:
 
 
 
local frame = {}
 
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 = {}
 
local frame = {}
 
frame.args = {}
 
frame.args = {}
frame.args['shipClass'] = "Destroyer"
+
mw.log(ListOfSortable.ParseAndGenerate(frame))
frame.args['remodelSuffix'] = "B Kai, D Kai, Mk.II, Zwei"
+
 +
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['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"
 
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(render(frame))
+
mw.log(ListOfSortable.ParseAndGenerate(frame))
 
end
 
end
  
return { render = render, test = test }
+
--return { test = test } --swap comments to test
 +
return ListOfSortable
  
 
-- TEST: p.test()
 
-- TEST: p.test()

Latest revision as of 05:19, 7 September 2022

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()