• 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
Line 23: Line 23:
 
     battle_node_label = [[<div class="kcRoute" style="vertical-align:middle"><div class="kcRouteNode" style="background:${color};">${label}</div></div>]],
 
     battle_node_label = [[<div class="kcRoute" style="vertical-align:middle"><div class="kcRouteNode" style="background:${color};">${label}</div></div>]],
  
 +
}
 +
 +
local node_colors = {
 +
    grey = "grey",
 +
    battle = "#FF5252", -- Red A200
 +
    resource = "#40C4FF", -- Light Green A700
 +
    storm = "#E040FB", -- Purple A200
 +
    empty = "#40C4FF", -- Light Blue A200
 
}
 
}
  
Line 29: Line 37:
 
         return "'''Start'''"
 
         return "'''Start'''"
 
     elseif label:match("%d") then
 
     elseif label:match("%d") then
         return format{templates.battle_node_label, label = label, color = "grey"}
+
         return format{templates.battle_node_label, label = label, color = node_colors.grey}
 
     else
 
     else
         return format{templates.battle_node_label, label = label, color = color}
+
         return format{templates.battle_node_label, label = label, color = node_colors[color] or color or node_colors.battle}
 
     end
 
     end
 
end
 
end
Line 63: Line 71:
 
             templates.table_row_start,
 
             templates.table_row_start,
 
             rowspan = #branching[from].index,
 
             rowspan = #branching[from].index,
             from = formatNodeLabel(from, branching[from].color or "red"),
+
             from = formatNodeLabel(from, branching[from].color),
 
         })
 
         })
 
         local first = true
 
         local first = true
Line 70: Line 78:
 
                 templates.table_row,
 
                 templates.table_row,
 
                 separator = first and "" or "|-\n",
 
                 separator = first and "" or "|-\n",
                 to = formatNodeLabel(to, branching[from][to].color or "red"),
+
                 to = formatNodeLabel(to, branching[from][to].color),
 
                 rules = branching[from][to].rules,
 
                 rules = branching[from][to].rules,
 
             })
 
             })
Line 86: Line 94:
 
end
 
end
  
-- MapBranchingTable.t = MapBranchingTable.format(nil, { ["0 -> 1"] = "Fixed route", ["1 -> B/green"] = "Random", ["1 -> C"] = "Random" })
+
MapBranchingTable.t = MapBranchingTable.format(nil, { ["0 -> 1"] = "Fixed route", ["1 -> B/green"] = "Random", ["1 -> C"] = "Random" })
  
 
return MapBranchingTable
 
return MapBranchingTable

Revision as of 01:40, 7 November 2016

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 = {

    table_header = [[{| class="wikitable mw-collapsible mw-collapsed typography-xl-optout branching-table" style="width:100%;"
|-
!colspan="3"|<span style="float:left;padding-left:10px;">Branching Rules</span>
|-
!colspan="2"|Nodes||Rules
]],

    table_footer = "\n|}",

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

    table_row = [[${separator}|style="text-align:center;width:10%;"|${to}
|
${rules}]],

    battle_node_label = [[<div class="kcRoute" style="vertical-align:middle"><div class="kcRouteNode" style="background:${color};">${label}</div></div>]],

}

local node_colors = {
    grey = "grey",
    battle = "#FF5252", -- Red A200
    resource = "#40C4FF", -- Light Green A700
    storm = "#E040FB", -- Purple A200
    empty = "#40C4FF", -- Light Blue A200
}

function formatNodeLabel(label, color)
    if label == "0" then
        return "'''Start'''"
    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

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