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

From Kancolle Wiki
Jump to navigation Jump to search
m
m
Line 5: Line 5:
 
local templates = {
 
local templates = {
  
 +
    -- .branching-table is defined in MediaWiki:Common.css
 
     table_header = [[{| class="wikitable typography-xl-optout branching-table" style="width:${width};"
 
     table_header = [[{| class="wikitable typography-xl-optout branching-table" style="width:${width};"
 
|- class="mw-customtoggle-${id}" style="cursor:pointer;"
 
|- class="mw-customtoggle-${id}" style="cursor:pointer;"
Line 16: Line 17:
 
|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 = [[|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}"
+
     table_row_separator = [[|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}"
 
]],
 
]],
  
Line 23: Line 24:
 
${rules}]],
 
${rules}]],
  
     battle_node_label = [[<div class="kcRoute" style="vertical-align:middle"><div class="kcRouteNode" style="background:${color};">${label}</div></div>]],
+
     -- .kcRoute is defined in MediaWiki:Common.css
 +
    node = [[<div class="kcRoute" style="vertical-align:middle"><div class="kcRouteNode" style="background:${color};">${label}</div></div>]],
  
 
}
 
}
Line 35: Line 37:
 
}
 
}
  
local special_parameters = { "width", "id" }
+
function formatNode(label, color)
 +
    return label == "0" and "'''Start'''" or format{
 +
        templates.node,
 +
        label = label,
 +
        color = label:match("^%d$") and node_colors.grey or node_colors[color] or color or node_colors.battle
 +
    }
 +
end
  
function formatNodeLabel(label, color)
+
-- not supporting unique ids for now
     if label == "0" then
+
function defaultId(args)
        return "'''Start'''"
+
     return "mapbranchingtable"
    elseif label:match("%d") then
 
        return format{templates.battle_node_label, label = label, color = node_colors.grey}
 
    else
 
        return format{templates.battle_node_label, label = label, color = node_colors[color] or color or node_colors.battle}
 
    end
 
 
end
 
end
  
 
function formatTable(args)
 
function formatTable(args)
     local branching = { index = {}, }
+
     local branching = { index = {} }
 
     for route, rules in pairs(args) do
 
     for route, rules in pairs(args) do
         if not find(special_parameters, route) then
+
         local from, to = route:match("^(%S+)%s*->%s*(%S+)$")
            local from, to = route:match("(%S+)%s*->%s*(%S+)")
+
        if from and to then
             local from_color = from:match("%S+/(%S+)")
+
             local from_color = from:match("^%S+/(%S+)$")
             local to_color = to:match("%S+/(%S+)")
+
             local to_color = to:match("^%S+/(%S+)$")
             from = from_color and from:match("(%S+)/") or from
+
             from = from_color and from:match("^(%S+)/") or from
             to = to_color and to:match("(%S+)/") or to
+
             to = to_color and to:match("^(%S+)/") or to
 
             if not find(branching.index, from) then
 
             if not find(branching.index, from) then
 
                 table.insert(branching.index, from)
 
                 table.insert(branching.index, from)
Line 72: Line 75:
 
         table.sort(branching[from].index)
 
         table.sort(branching[from].index)
 
     end
 
     end
     local id = args.id or "mapbranchingtable"
+
     local id = args.id or defaultId(args)
 
     local rows = {}
 
     local rows = {}
 
     table.insert(rows, format{
 
     table.insert(rows, format{
 
         templates.table_header,
 
         templates.table_header,
         width = args.width or "100%",
+
         width = args.width or "auto",
 
         id = id,
 
         id = id,
 
     })
 
     })
Line 83: Line 86:
 
             templates.table_row_start,
 
             templates.table_row_start,
 
             rowspan = #branching[from].index,
 
             rowspan = #branching[from].index,
             from = formatNodeLabel(from, branching[from].color),
+
             from = formatNode(from, branching[from].color),
 
             id = id,
 
             id = id,
 
         })
 
         })
Line 90: Line 93:
 
             table.insert(rows, format{
 
             table.insert(rows, format{
 
                 templates.table_row,
 
                 templates.table_row,
                 separator = first and "" or format{templates.row_separator, id = id},
+
                 separator = first and "" or format{templates.table_row_separator, id = id},
                 to = formatNodeLabel(to, branching[from][to].color),
+
                 to = formatNode(to, branching[from][to].color),
 
                 rules = branching[from][to].rules,
 
                 rules = branching[from][to].rules,
 
                 id = id,
 
                 id = id,

Revision as of 22:59, 3 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 templates = {

    -- .branching-table is defined in MediaWiki:Common.css
    table_header = [[{| class="wikitable typography-xl-optout branching-table" style="width:${width};"
|- class="mw-customtoggle-${id}" style="cursor:pointer;"
!colspan="3"|Branching Rules
|- class="mw-collapsible mw-collapsed" id="mw-customcollapsible-${id}"
!colspan="2"|Nodes||Rules]],

    table_footer = "|}",

    table_row_start = [[|- class="mw-collapsible mw-collapsed"  id="mw-customcollapsible-${id}"
|rowspan="${rowspan}" style="text-align:center;vertical-align:middle;width:10%"|${from}]],

    table_row_separator = [[|- 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}
|
${rules}]],

    -- .kcRoute is defined in MediaWiki:Common.css
    node = [[<div class="kcRoute" style="vertical-align:middle"><div class="kcRouteNode" style="background:${color};">${label}</div></div>]],

}

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)
    return label == "0" and "'''Start'''" or format{
        templates.node,
        label = label,
        color = label:match("^%d$") and node_colors.grey or node_colors[color] or color or node_colors.battle
    }
end

-- not supporting unique ids for now
function defaultId(args)
    return "mapbranchingtable"
end

function formatTable(args)
    local branching = { index = {} }
    for route, rules in pairs(args) do
        local from, to = route:match("^(%S+)%s*->%s*(%S+)$")
        if from and to then
            local from_color = from:match("^%S+/(%S+)$")
            local to_color = to:match("^%S+/(%S+)$")
            from = from_color and from:match("^(%S+)/") or from
            to = to_color and to:match("^(%S+)/") 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
    table.sort(branching.index)
    for _, from in ipairs(branching.index) do
        table.sort(branching[from].index)
    end
    local id = args.id or defaultId(args)
    local rows = {}
    table.insert(rows, format{
        templates.table_header,
        width = args.width or "auto",
        id = id,
    })
    for _, from in ipairs(branching.index) do
        table.insert(rows, format{
            templates.table_row_start,
            rowspan = #branching[from].index,
            from = formatNode(from, branching[from].color),
            id = id,
        })
        local first = true
        for _, to in ipairs(branching[from].index) do
            table.insert(rows, format{
                templates.table_row,
                separator = first and "" or format{templates.table_row_separator, id = id},
                to = formatNode(to, branching[from][to].color),
                rules = branching[from][to].rules,
                id = id,
            })
            first = false
        end
    end
    table.insert(rows, templates.table_footer)
    return table.concat(rows, "\n")
end

local MapBranchingTable = {}

function MapBranchingTable.format(frame, args_)
    local args = args_ or getArgs{frame = frame:getParent()}
    return formatTable(args)
end

-- MapBranchingTable.t = MapBranchingTable.format(nil, { ["0 -> 1"] = "Fixed route", ["1 -> B/green"] = "Random", ["1 -> C"] = "Random" })

return MapBranchingTable