• 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:DropList"

From Kancolle Wiki
Jump to navigation Jump to search
m (Undo revision 484211 by がか (talk))
(debugger test)
Line 45: Line 45:
 
['?']      = '?'
 
['?']      = '?'
 
}
 
}
 +
 +
function find(tbl, v_, k_)
 +
for k, v in pairs(tbl) do
 +
if k_ and k == k_ and v == v_ or v == v_ then
 +
return true
 +
end
 +
end
 +
return false
 +
end
  
 
function parseArgs(args)
 
function parseArgs(args)
  
local tbl = { nodes = {}, rows = {} }
+
local tbl = { nodes = {}, rows = {}, debug = '' }
  
 
-- header args
 
-- header args
Line 56: Line 65:
 
if node then
 
if node then
 
local node = string.upper(node)
 
local node = string.upper(node)
table.insert(tbl.nodes, { name = node, boss = node == boss })
+
if find(tbl.nodes, node, "name") then
 +
tbl.debug = tbl.debug .. string.format("node duplicate: %s\n", node)
 +
else
 +
table.insert(tbl.nodes, { name = node, boss = node == boss })
 +
end
 
end
 
end
 
end
 
end
Line 66: Line 79:
 
local ship_table = Ship:get_table(ship, "")
 
local ship_table = Ship:get_table(ship, "")
 
if ship_table and ship_table._type then
 
if ship_table and ship_table._type then
table.insert(tbl.rows, {})
+
if find(tbl.rows, ship, "ship") then
local row = tbl.rows[#tbl.rows]
+
tbl.debug = tbl.debug .. string.format("ship duplicate: %s\n", ship)
row.ship = ship
+
else
row.rare = false
+
table.insert(tbl.rows, {})
for _, rare_ship in pairs(rare_ships) do
+
local row = tbl.rows[#tbl.rows]
if ship == rare_ship then
+
row.ship = ship
row.rare = true
+
row.rare = false
break
+
row.rare = find(rare_ships, ship)
 +
row.type = Formatting:format_ship_code(ship_table._type) or '?'
 +
row.nodes = {}
 +
for _, node in pairs(tbl.nodes) do
 +
row.nodes[node.name] = nil
 
end
 
end
end
+
for node_and_diff in string.gmatch(nodes, args_grammar.comma_list) do
row.type = Formatting:format_ship_code(ship_table._type) or '?'
+
local node, diff = node_and_diff:match(args_grammar.node_and_diff)
row.nodes = {}
+
if not node or not diff then
for _, node in pairs(tbl.nodes) do
+
node = node_and_diff:match(args_grammar.just_node)
row.nodes[node.name] = nil
+
diff = nil
end
+
end
for node_and_diff in string.gmatch(nodes, args_grammar.comma_list) do
+
if node then
local node, diff = node_and_diff:match(args_grammar.node_and_diff)
+
local node = string.upper(node)
if not node or not diff then
+
if row.nodes[node] then
node = node_and_diff:match(args_grammar.just_node)
+
tbl.debug = tbl.debug .. string.format("ship node duplicate: %s\n", node)
diff = nil
+
else
end
+
row.nodes[node] = { color = diff and diff_colors[diff] or diff_colors['?'], diff = diff and diff_names[diff] or '?' }
if node then
+
end
    local node = string.upper(node)
+
end
row.nodes[node] = { color = diff and diff_colors[diff] or diff_colors['?'], diff = diff and diff_names[diff] or '?' }
 
 
end
 
end
 
end
 
end
Line 110: Line 126:
 
node_cell        = '|style="background-color:#${color};"|${diff}\n',
 
node_cell        = '|style="background-color:#${color};"|${diff}\n',
 
empty_cell      = '|\n',
 
empty_cell      = '|\n',
footer          = '|}\n'
+
footer          = '|}\n',
 +
debugger        = [[{| style="width:100%;" align="center" cellspacing="0" class="mw-collapsible mw-collapsed"
 +
|-
 +
|<pre>${debug}</pre>
 +
|}]]
 
}
 
}
  
Line 140: Line 160:
  
 
res = res .. table_format.footer
 
res = res .. table_format.footer
 +
 +
if tbl.debug ~= "" then
 +
res = res .. format{table_format.debugger, debug = tbl.debug}
 +
end
  
 
return res
 
return res

Revision as of 10:55, 17 October 2015

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

local format = require("Module:StringInterpolation").format
local getArgs = require('Module:GetArgs')

local Ship = require('Module:Ship')
local Formatting = require('Module:Formatting')

local DropList = {}

-- http://kancolle.wikia.com/wiki/Thread:295964
local rare_ships = {
	
	"Akitsushima", "Akizuki", "Amagi", "Isokaze", "Katsuragi", "Littorio", "Prinz Eugen", "Roma", "Tokitsukaze",
	
	"Akitsu Maru", "Bismarck", "Musashi", "Noshiro", "Yamato", "Taihou",
	
	"Agano", "Akashi", "Amatsukaze", "Asagumo", "Asashimo", "Harusame", "Hatsukaze", "Hayashimo", "I-401", "Katori", "Kiyoshimo", "Maruyu",
	"Mikuma", "Nowaki", "Ooyodo", "Sakawa", "Taigei", "Takanami", "U-511", "Unryuu", "Uzuki", "Yahagi",

	"Z1", "Z3", "Tanikaze", "Maikaze",

	"Libeccio", "Mizuho", "Kazagumo", "Umikaze", "Kawakaze", "Hayasui", "Teruzuki",
	
}

local args_grammar = {
	node           = '^%s*(%a)%s*$',
	comma_list     = '[^,]+',
	ship_and_nodes = '^%s*(.-)%s*:%s*(.-)%s*$',
	just_node      = '^%s*(%a)%s*$',
	node_and_diff  = '^%s*(%a)%s*/%s*(%S-)%s*$'
}

local diff_colors = {
	['Easy']   = '5a5',
	['Medium'] = 'da6',
	['Hard']   = 'd33',
	['?']      = '0ff'
}

local diff_names = {
	['Easy']   = 'Easy+',
	['Medium'] = 'Medium+',
	['Hard']   = 'Hard+',
	['?']      = '?'
}

function find(tbl, v_, k_)
	for k, v in pairs(tbl) do
		if k_ and k == k_ and v == v_ or v == v_ then
			return true
		end
	end
	return false
end

function parseArgs(args)

	local tbl = { nodes = {}, rows = {}, debug = '' }

	-- header args
	local boss = args.boss and string.upper(args.boss) or nil
	for node in string.gmatch(args.nodes, args_grammar.comma_list) do
		local node = node:match(args_grammar.node)
		if node then
			local node = string.upper(node)
			if find(tbl.nodes, node, "name") then
				tbl.debug = tbl.debug .. string.format("node duplicate: %s\n", node)
			else
				table.insert(tbl.nodes, { name = node, boss = node == boss })
			end
		end
	end

	-- ship args
	for arg_name, arg in pairs(args) do
		if tonumber(arg_name) then
			local ship, nodes = arg:match(args_grammar.ship_and_nodes)
			local ship_table = Ship:get_table(ship, "")
			if ship_table and ship_table._type then
				if find(tbl.rows, ship, "ship") then
					tbl.debug = tbl.debug .. string.format("ship duplicate: %s\n", ship)
				else
					table.insert(tbl.rows, {})
					local row = tbl.rows[#tbl.rows]
					row.ship = ship
					row.rare = false
					row.rare = find(rare_ships, ship)
					row.type = Formatting:format_ship_code(ship_table._type) or '?'
					row.nodes = {}
					for _, node in pairs(tbl.nodes) do
						row.nodes[node.name] = nil
					end
					for node_and_diff in string.gmatch(nodes, args_grammar.comma_list) do
						local node, diff = node_and_diff:match(args_grammar.node_and_diff)
						if not node or not diff then
							node = node_and_diff:match(args_grammar.just_node)
							diff = nil
						end
						if node then
							local node = string.upper(node)
							if row.nodes[node] then
								tbl.debug = tbl.debug .. string.format("ship node duplicate: %s\n", node)
							else
								row.nodes[node] = { color = diff and diff_colors[diff] or diff_colors['?'], diff = diff and diff_names[diff] or '?' }
							end
						end
					end
				end
			end
		end
	end

	return tbl

end

local table_format = {
	header           = '{| class="article-table sortable" align="center" width="100%" style="text-align:center"\n!Type\n!Ship\n',
	header_node      = '!width="10%%"|${node}\n',
	header_boss_node = '!width="10%%" style="background-color:pink;color:red;"|\'\'\'${node}\'\'\'\n',
	row              = '|-\n',
	type_cell        = '|${type}\n',
	ship_cell        = '|[[${ship}]]\n',
	rare_ship_cell   = '|[[${ship}|<span style="color:red;">${ship}</span>]]\n',
	node_cell        = '|style="background-color:#${color};"|${diff}\n',
	empty_cell       = '|\n',
	footer           = '|}\n',
	debugger         = [[{| style="width:100%;" align="center" cellspacing="0" class="mw-collapsible mw-collapsed"
|-
|<pre>${debug}</pre>
|}]]
}

function showTable(tbl)

	local res = table_format.header

	-- header
	for _, node in pairs(tbl.nodes) do
		res = res .. format{
			node.boss and table_format.header_boss_node or table_format.header_node,
			node = node.name
		}
	end

	-- rows
	for key, row in pairs(tbl.rows) do
		res = res .. table_format.row
		res = res .. format{table_format.type_cell, type = row.type}
		res = res .. format{
			row.rare and table_format.rare_ship_cell or table_format.ship_cell,
			ship = row.ship
		}
		for _, node in pairs(tbl.nodes) do
			local node = row.nodes[node.name]
			res = res .. (node and format{table_format.node_cell, color = node.color, diff = node.diff} or table_format.empty_cell)
		end
	end

	res = res .. table_format.footer

	if tbl.debug ~= "" then
		res = res .. format{table_format.debugger, debug = tbl.debug}
	end

	return res

end

function DropList.show(frame)
	local args = getArgs{frame = frame:getParent()}
	return showTable(parseArgs(args))
end

return DropList