- 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:MapBranchingTable"
Jump to navigation
Jump to search
m |
|||
Line 2: | Line 2: | ||
local format = require("Module:StringInterpolation").format | local format = require("Module:StringInterpolation").format | ||
local find = require("Module:Utils").find | local find = require("Module:Utils").find | ||
+ | local BaseData = require("Module:BaseData") | ||
− | local | + | local MapBranchingTable = BaseData{ |
+ | |||
+ | _grammar = { | ||
+ | nodes = "^(%S+)%s*->%s*(%S+)$", | ||
+ | node_and_color = "^(%S+)/(%S+)$", | ||
+ | digit_node = "^%d$", | ||
+ | }, | ||
+ | |||
+ | _id = "mapbranchingtable", -- not supporting unique ids for now | ||
+ | _title = "Branching Rules", | ||
+ | _width = "auto", | ||
+ | _start = "'''Start'''", | ||
-- .branching-table is defined in MediaWiki:Common.css | -- .branching-table is defined in MediaWiki:Common.css | ||
− | + | _template = [[{| class="wikitable typography-xl-optout branching-table" style="width:${width};" | |
|- class="mw-customtoggle-${id}" style="cursor:pointer;" | |- class="mw-customtoggle-${id}" style="cursor:pointer;" | ||
− | !colspan="3"| | + | !colspan="3"|${title} |
|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}" | |- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}" | ||
− | !colspan="2"|Nodes||Rules]], | + | !colspan="2"|Nodes||Rules |
+ | ${rows} | ||
+ | |}]], | ||
− | + | _row_start_template = [[|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}" | |
− | |||
− | |||
|rowspan="${rowspan}" style="text-align:center;vertical-align:middle;width:10%"|${from}]], | |rowspan="${rowspan}" style="text-align:center;vertical-align:middle;width:10%"|${from}]], | ||
− | + | _row_separator_template = [[|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}" | |
]], | ]], | ||
− | + | _row_template = [[${separator}|class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}" style="text-align:center;width:10%;"|${to} | |
| | | | ||
${rules}]], | ${rules}]], | ||
-- .kcRoute is defined in MediaWiki:Common.css | -- .kcRoute is defined in MediaWiki:Common.css | ||
− | + | _node_template = [[<div class="kcRoute" style="vertical-align:middle"><div class="kcRouteNode" style="background:${color};">${label}</div></div>]], | |
− | } | + | _node_colors = { |
+ | grey = "grey", | ||
+ | battle = "#FF1744", -- Red A400 | ||
+ | resource = "#64DD17", -- Light Green A700 | ||
+ | storm = "#EA80FC", -- Purple A100 | ||
+ | empty = "#40C4FF", -- Light Blue A200 | ||
+ | }, | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | function | + | function make_id_from_title(title) |
− | return | + | return title:gsub("%s", ""):lower() |
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | + | function MapBranchingTable:parse(args) | |
− | function | + | self._vars = { |
− | + | id = args.id or args.title and make_id_from_title(args.title) or self._id, | |
− | + | title = args.title or self._title, | |
− | + | width = args.width or self._width, | |
− | + | branching = { index = {} }, | |
− | + | } | |
+ | local branching = self._vars.branching | ||
for route, rules in pairs(args) do | for route, rules in pairs(args) do | ||
− | local from, to = route:match( | + | if type(route) ~= "number" then |
− | + | local from, to = route:match(self._grammar.nodes) | |
− | + | if from and to then | |
− | + | local from_, from_color = from:match(self._grammar.node_and_color) | |
− | + | local to_, to_color = to:match(self._grammar.node_and_color) | |
− | + | from = from_color and from_ or from | |
− | + | to = to_color and to_ or to | |
− | + | if not find(branching.index, from) then | |
− | + | table.insert(branching.index, from) | |
− | + | end | |
− | + | if not branching[from] then | |
− | + | branching[from] = { color = from_color, index = {} } | |
− | + | end | |
− | + | branching[from][to] = { color = to_color, rules = rules } | |
− | + | if not find(branching[from].index, to) then | |
+ | table.insert(branching[from].index, to) | ||
+ | end | ||
end | end | ||
end | end | ||
Line 75: | Line 86: | ||
table.sort(branching[from].index) | table.sort(branching[from].index) | ||
end | end | ||
− | + | end | |
+ | |||
+ | function MapBranchingTable:format_node(label, color) | ||
+ | return label == "0" and self._start or format{ | ||
+ | self._node_template, | ||
+ | label = label, | ||
+ | color = label:match(self._grammar.digit_node) | ||
+ | and self._node_colors.grey | ||
+ | or self._node_colors[color] | ||
+ | or color | ||
+ | or self._node_colors.battle | ||
+ | } | ||
+ | end | ||
+ | |||
+ | function MapBranchingTable:prepare_rows() | ||
+ | local branching = self._vars.branching | ||
local rows = {} | local rows = {} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
for _, from in ipairs(branching.index) do | for _, from in ipairs(branching.index) do | ||
table.insert(rows, format{ | table.insert(rows, format{ | ||
− | + | self._row_start_template, | |
rowspan = #branching[from].index, | rowspan = #branching[from].index, | ||
− | from = | + | from = self:format_node(from, branching[from].color), |
− | id = id, | + | id = self._vars.id, |
}) | }) | ||
local first = true | local first = true | ||
for _, to in ipairs(branching[from].index) do | for _, to in ipairs(branching[from].index) do | ||
table.insert(rows, format{ | table.insert(rows, format{ | ||
− | + | self._row_template, | |
− | separator = first and "" or format{ | + | separator = first and "" or format{self._row_separator_template, id = self._vars.id}, |
− | to = | + | to = self:format_node(to, branching[from][to].color), |
rules = branching[from][to].rules, | rules = branching[from][to].rules, | ||
− | id = id, | + | id = self._vars.id, |
}) | }) | ||
first = false | first = false | ||
end | end | ||
end | end | ||
− | + | self._vars.rows = table.concat(rows, "\n") | |
− | |||
end | end | ||
− | + | function MapBranchingTable:format(args) | |
+ | self:parse(args) | ||
+ | self:prepare_rows() | ||
+ | return format(self._template, self._vars) | ||
+ | end | ||
− | function MapBranchingTable. | + | function MapBranchingTable.Table(frame, args) |
− | + | return MapBranchingTable:format(args or getArgs{frame = frame:getParent()}) | |
− | |||
end | end | ||
− | -- MapBranchingTable.t = MapBranchingTable. | + | -- MapBranchingTable.t = MapBranchingTable.Table(nil, { ["0 -> 1"] = "Fixed route", ["1 -> B/green"] = "Random", ["1 -> C"] = "Random", ["title"] = "A Custom Title", "?" }) |
return MapBranchingTable | return MapBranchingTable |
Revision as of 00:33, 4 February 2017
Documentation for this module may be created at Module:MapBranchingTable/doc
local getArgs = require("Module:GetArgs")
local format = require("Module:StringInterpolation").format
local find = require("Module:Utils").find
local BaseData = require("Module:BaseData")
local MapBranchingTable = BaseData{
_grammar = {
nodes = "^(%S+)%s*->%s*(%S+)$",
node_and_color = "^(%S+)/(%S+)$",
digit_node = "^%d$",
},
_id = "mapbranchingtable", -- not supporting unique ids for now
_title = "Branching Rules",
_width = "auto",
_start = "'''Start'''",
-- .branching-table is defined in MediaWiki:Common.css
_template = [[{| class="wikitable typography-xl-optout branching-table" style="width:${width};"
|- class="mw-customtoggle-${id}" style="cursor:pointer;"
!colspan="3"|${title}
|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}"
!colspan="2"|Nodes||Rules
${rows}
|}]],
_row_start_template = [[|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}"
|rowspan="${rowspan}" style="text-align:center;vertical-align:middle;width:10%"|${from}]],
_row_separator_template = [[|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}"
]],
_row_template = [[${separator}|class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}" style="text-align:center;width:10%;"|${to}
|
${rules}]],
-- .kcRoute is defined in MediaWiki:Common.css
_node_template = [[<div class="kcRoute" style="vertical-align:middle"><div class="kcRouteNode" style="background:${color};">${label}</div></div>]],
_node_colors = {
grey = "grey",
battle = "#FF1744", -- Red A400
resource = "#64DD17", -- Light Green A700
storm = "#EA80FC", -- Purple A100
empty = "#40C4FF", -- Light Blue A200
},
}
function make_id_from_title(title)
return title:gsub("%s", ""):lower()
end
function MapBranchingTable:parse(args)
self._vars = {
id = args.id or args.title and make_id_from_title(args.title) or self._id,
title = args.title or self._title,
width = args.width or self._width,
branching = { index = {} },
}
local branching = self._vars.branching
for route, rules in pairs(args) do
if type(route) ~= "number" then
local from, to = route:match(self._grammar.nodes)
if from and to then
local from_, from_color = from:match(self._grammar.node_and_color)
local to_, to_color = to:match(self._grammar.node_and_color)
from = from_color and from_ or from
to = to_color and to_ or to
if not find(branching.index, from) then
table.insert(branching.index, from)
end
if not branching[from] then
branching[from] = { color = from_color, index = {} }
end
branching[from][to] = { color = to_color, rules = rules }
if not find(branching[from].index, to) then
table.insert(branching[from].index, to)
end
end
end
end
table.sort(branching.index)
for _, from in ipairs(branching.index) do
table.sort(branching[from].index)
end
end
function MapBranchingTable:format_node(label, color)
return label == "0" and self._start or format{
self._node_template,
label = label,
color = label:match(self._grammar.digit_node)
and self._node_colors.grey
or self._node_colors[color]
or color
or self._node_colors.battle
}
end
function MapBranchingTable:prepare_rows()
local branching = self._vars.branching
local rows = {}
for _, from in ipairs(branching.index) do
table.insert(rows, format{
self._row_start_template,
rowspan = #branching[from].index,
from = self:format_node(from, branching[from].color),
id = self._vars.id,
})
local first = true
for _, to in ipairs(branching[from].index) do
table.insert(rows, format{
self._row_template,
separator = first and "" or format{self._row_separator_template, id = self._vars.id},
to = self:format_node(to, branching[from][to].color),
rules = branching[from][to].rules,
id = self._vars.id,
})
first = false
end
end
self._vars.rows = table.concat(rows, "\n")
end
function MapBranchingTable:format(args)
self:parse(args)
self:prepare_rows()
return format(self._template, self._vars)
end
function MapBranchingTable.Table(frame, args)
return MapBranchingTable:format(args or getArgs{frame = frame:getParent()})
end
-- MapBranchingTable.t = MapBranchingTable.Table(nil, { ["0 -> 1"] = "Fixed route", ["1 -> B/green"] = "Random", ["1 -> C"] = "Random", ["title"] = "A Custom Title", "?" })
return MapBranchingTable