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 templates = {
+
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
−
table_header = [[{| class="wikitable typography-xl-optout branching-table" style="width:${width};"
+
_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"|Branching Rules
+
!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}
+
|}]],
−
table_footer = "|}",
+
_row_start_template = [[|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}"
−
−
table_row_start = [[|- 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}]],
−
table_row_separator = [[|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}"
+
_row_separator_template = [[|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}"
]],
]],
−
table_row = [[${separator}|class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}" style="text-align:center;width:10%;"|${to}
+
_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 = [[<div class="kcRoute" style="vertical-align:middle"><div class="kcRouteNode" style="background:${color};">${label}</div></div>]],
+
_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
+
},
−
local node_colors = {
−
grey = "grey",
−
battle = "#FF1744", -- Red A400
−
resource = "#64DD17", -- Light Green A700
−
storm = "#EA80FC", -- Purple A100
−
empty = "#40C4FF", -- Light Blue A200
}
}
−
function formatNode(label, color)
+
function make_id_from_title(title)
−
return label == "0" and "'''Start'''" or format{
+
return title:gsub("%s", ""):lower()
−
templates.node,
−
label = label,
−
color = label:match("^%d$") and node_colors.grey or node_colors[color] or color or node_colors.battle
−
}
end
end
−
-- not supporting unique ids for now
+
function MapBranchingTable:parse(args)
−
function defaultId(args)
+
self._vars = {
−
return "mapbranchingtable"
+
id = args.id or args.title and make_id_from_title(args.title) or self._id,
−
end
+
title = args.title or self._title,
−
+
width = args.width or self._width,
−
function formatTable(args)
+
branching = { index = {} },
−
local 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("^(%S+)%s*->%s*(%S+)$")
+
if type(route) ~= "number" then
−
if from and to then
+
local from, to = route:match(self._grammar.nodes)
−
local from_color = from:match("^%S+/(%S+)$")
+
if from and to then
−
local to_color = to:match("^%S+/(%S+)$")
+
local from_, from_color = from:match(self._grammar.node_and_color)
−
from = from_color and from:match("^(%S+)/") or from
+
local to_, to_color = to:match(self._grammar.node_and_color)
−
to = to_color and to:match("^(%S+)/") or to
+
from = from_color and from_ or from
−
if not find(branching.index, from) then
+
to = to_color and to_ or to
−
table.insert(branching.index, from)
+
if not find(branching.index, from) then
−
end
+
table.insert(branching.index, from)
−
if not branching[from] then
+
end
−
branching[from] = { color = from_color, index = {} }
+
if not branching[from] then
−
end
+
branching[from] = { color = from_color, index = {} }
−
branching[from][to] = { color = to_color, rules = rules }
+
end
−
if not find(branching[from].index, to) then
+
branching[from][to] = { color = to_color, rules = rules }
−
table.insert(branching[from].index, to)
+
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
−
local id = args.id or defaultId(args)
+
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 = {}
−
table.insert(rows, format{
−
templates.table_header,
−
width = args.width or "auto",
−
id = id,
−
})
for _, from in ipairs(branching.index) do
for _, from in ipairs(branching.index) do
table.insert(rows, format{
table.insert(rows, format{
−
templates.table_row_start,
+
self._row_start_template,
rowspan = #branching[from].index,
rowspan = #branching[from].index,
−
from = formatNode(from, branching[from].color),
+
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{
−
templates.table_row,
+
self._row_template,
−
separator = first and "" or format{templates.table_row_separator, id = id},
+
separator = first and "" or format{self._row_separator_template, id = self._vars.id},
−
to = formatNode(to, branching[from][to].color),
+
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
−
table.insert(rows, templates.table_footer)
+
self._vars.rows = table.concat(rows, "\n")
−
return table.concat(rows, "\n")
end
end
−
local MapBranchingTable = {}
+
function MapBranchingTable:format(args)
+
self:parse(args)
+
self:prepare_rows()
+
return format(self._template, self._vars)
+
end
−
function MapBranchingTable.format(frame, args_)
+
function MapBranchingTable.Table(frame, args)
−
local args = args_ or getArgs{frame = frame:getParent()}
+
return MapBranchingTable:format(args or getArgs{frame = frame:getParent()})
−
return formatTable(args)
end
end
−
-- MapBranchingTable.t = MapBranchingTable.format(nil, { ["0 -> 1"] = "Fixed route", ["1 -> B/green"] = "Random", ["1 -> C"] = "Random" })
+
-- MapBranchingTable.t = MapBranchingTable.Table(nil, { ["0 -> 1"] = "Fixed route", ["1 -> B/green"] = "Random", ["1 -> C"] = "Random", ["title"] = "A Custom Title", "?" })
return MapBranchingTable
return MapBranchingTable