• 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
Line 57: Line 57:
 
-- TODO: check node existence?
 
-- TODO: check node existence?
 
if node then
 
if node then
print(node, diff)
 
 
row.nodes[node] = { color = diff and colors[diff] or colors['?'], diff = diff and diffs[diff] or '?' }
 
row.nodes[node] = { color = diff and colors[diff] or colors['?'], diff = diff and diffs[diff] or '?' }
 
end
 
end

Revision as of 04:11, 17 October 2015

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

local DropList = {}

local args_grammar = {
	to_trim        = '^%s*(.-)%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 colors = {
	['Easy']   = '5a5',
	['Medium'] = 'da6',
	['Hard']   = 'd33',
	['?']      = '0ff'
}

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

function parseArgs(args)

	local table = { nodes = {}, rows = {} }

	-- header args
	for node in string.gmatch(args.nodes, args_grammar.comma_list) do
		local node = node:match(args_grammar.to_trim)
		table.nodes[#table.nodes + 1] = { name = node }
		if node == args.boss then
			table.nodes[#table.nodes].boss = true
		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)
			-- TODO: check ship existence?
			table.rows[ship] = {}
			local row = table.rows[ship]
			row.ship = ship
			row.nodes = {}
			for _, node in pairs(table.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
				-- TODO: check node existence?
				if node then
					row.nodes[node] = {	color = diff and colors[diff] or colors['?'], diff = diff and diffs[diff] or '?' }
				end
			end
		end
	end

	return table

end

local table_format = {
	header           = '{| class="wikitable sortable" align="center" width="90%" style="text-align:center"\n!Ship\n',
	header_node      = '!width="10%%"|%s\n',
	header_boss_node = '!width="10%%" style="background-color:pink;color:red;"|\'\'\'%s\'\'\'\n',
	ship_cell        = '|-\n|[[%s]]\n',
	node_cell        = '|style="background-color:#%s;"|%s\n',
	empty_cell       = '|\n',
	footer           = '|}\n'
}

function showTable(table)

	local res = table_format.header

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

	-- rows
	for ship, row in pairs(table.rows) do
		res = res .. string.format(table_format.ship_cell, ship)
		for _, node in pairs(table.nodes) do
			local node = row.nodes[node.name]
			res = res .. (node and string.format(table_format.node_cell, node.color, node.diff) or table_format.empty_cell)
		end
	end

	res = res .. table_format.footer

	return res

end

function DropList.show(frame)
	return showTable(parseArgs(frame.args))
end

return DropList