• 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
(Created page with "local getArgs = require("Module:GetArgs") local format = require("Module:StringInterpolation").format local templates = { table_header = [[{| class="wikitable mw-collaps...")
 
Line 1: Line 1:
 
local getArgs = require("Module:GetArgs")
 
local getArgs = require("Module:GetArgs")
 
local format = require("Module:StringInterpolation").format
 
local format = require("Module:StringInterpolation").format
 +
local find = require("Module:Utils").find
 +
 +
function tablelength(T)
 +
    local count = 0
 +
    for _ in pairs(T) do count = count + 1 end
 +
    return count
 +
end
  
 
local templates = {
 
local templates = {
  
     table_header = [[{| class="wikitable mw-collapsible mw-collapsed typography-xl-optout" style="width:100%;"
+
     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="3"|<span style="float:left;padding-left:10px;">Branching Rules</span>
 
|-
 
|-
 
!colspan="2"|Nodes||Rules
 
!colspan="2"|Nodes||Rules
|-
 
 
]],
 
]],
  
 
     table_footer = "\n|}",
 
     table_footer = "\n|}",
  
     table_row_start = [[|rowspan="${rowspan}" style="text-align:center;vertical-align:middle;width:10%"|${from}]],
+
     table_row_start = [[|-
 +
|rowspan="${rowspan}" style="text-align:center;vertical-align:middle;width:10%"|${from}]],
  
 
     table_row = [[${separator}|style="text-align:center;"|${to}||${rules}]],
 
     table_row = [[${separator}|style="text-align:center;"|${to}||${rules}]],
 +
 +
    battle_node_label = [[<div class="kcRoute" style="vertical-align:middle"><div class="kcRouteNode" style="background:#FF0000;">${label}</div></div>]],
  
 
}
 
}
 +
 +
function formatNodeLabel(label)
 +
    if label == "Start" then
 +
        return "'''Start'''"
 +
    else
 +
        return format{templates.battle_node_label, label = label}
 +
    end
 +
end
  
 
function formatTable(args)
 
function formatTable(args)
     local branching = {}
+
     local branching = {
 +
        index = {},
 +
    }
 
     for route, rules in pairs(args) do
 
     for route, rules in pairs(args) do
 
         local from, to = route:match("(%S+)%s*->%s*(%S+)")
 
         local from, to = route:match("(%S+)%s*->%s*(%S+)")
         if branching[from] then
+
         if not find(branching.index, from) then
             branching[from][to] = rules
+
            table.insert(branching.index, from)
         else
+
        end
            branching[from] = {}
+
        if not branching[from] then
             branching[from][to] = rules
+
             branching[from] = { index = {} }
 +
        end
 +
        branching[from][to] = rules
 +
         if not find(branching[from].index, to) then
 +
             table.insert(branching[from].index, to)
 
         end
 
         end
 
     end
 
     end
 
     local rows = {}
 
     local rows = {}
     for from, tos in pairs(branching) do
+
     for _, from in ipairs(branching.index) do
 
         table.insert(rows, format{
 
         table.insert(rows, format{
 
             templates.table_row_start,
 
             templates.table_row_start,
             rowspan = 3,
+
             rowspan = #branching[from].index,
             from = from,
+
             from = formatNodeLabel(from),
 
         })
 
         })
 
         local first = true
 
         local first = true
         for to, rules in pairs(tos) do
+
         for _, to in ipairs(branching[from].index) do
 
             table.insert(rows, format{
 
             table.insert(rows, format{
 
                 templates.table_row,
 
                 templates.table_row,
                 separator = first and "|-\n" or "",
+
                 separator = first and "" or "|-\n",
                 to = to,
+
                 to = formatNodeLabel(to),
                 rules = rules,
+
                 rules = branching[from][to],
 
             })
 
             })
 
             first = false
 
             first = false
Line 59: Line 82:
 
end
 
end
  
-- MapBranchingTable.t = MapBranchingTable.format(nil, { ["1 -> 2"] = "..." })
+
-- MapBranchingTable.t = MapBranchingTable.format(nil, { ["Start -> A"] = "Fixed route", ["A -> B"] = "Random", ["A -> C"] = "Random" })
  
 
return MapBranchingTable
 
return MapBranchingTable

Revision as of 00:01, 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

function tablelength(T)
    local count = 0
    for _ in pairs(T) do count = count + 1 end
    return count
end

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;"|${to}||${rules}]],

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

}

function formatNodeLabel(label)
    if label == "Start" then
        return "'''Start'''"
    else
        return format{templates.battle_node_label, label = label}
    end
end

function formatTable(args)
    local branching = {
        index = {},
    }
    for route, rules in pairs(args) do
        local from, to = route:match("(%S+)%s*->%s*(%S+)")
        if not find(branching.index, from) then
            table.insert(branching.index, from)
        end
        if not branching[from] then
            branching[from] = { index = {} }
        end
        branching[from][to] = rules
        if not find(branching[from].index, to) then
            table.insert(branching[from].index, to)
        end
    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),
        })
        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),
                rules = branching[from][to],
            })
            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, { ["Start -> A"] = "Fixed route", ["A -> B"] = "Random", ["A -> C"] = "Random" })

return MapBranchingTable