• 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
(added order parameter)
Line 1: Line 1:
 
local getArgs = require("Module:GetArgs")
 
local getArgs = require("Module:GetArgs")
 
local format = require("Module:StringOperations").format
 
local format = require("Module:StringOperations").format
local find = require("Module:Utils").find
+
local Utils = require("Module:Utils")
 
local BaseData = require("Module:BaseData")
 
local BaseData = require("Module:BaseData")
  
Line 82: Line 82:
 
                     to_node = to_node_color and to_node_ or to_node:match(self._grammar.node)
 
                     to_node = to_node_color and to_node_ or to_node:match(self._grammar.node)
 
                     if from_node and to_node then
 
                     if from_node and to_node then
                         if not find(branching.index, from_node) then
+
                         if not Utils.find(branching.index, from_node) then
 
                             table.insert(branching.index, from_node)
 
                             table.insert(branching.index, from_node)
 
                         end
 
                         end
Line 95: Line 95:
 
                         branching[from_node][to_node] = { color = to_node_color }
 
                         branching[from_node][to_node] = { color = to_node_color }
 
                         end
 
                         end
                         if not find(branching[from_node].index, to_node) then
+
                         if not Utils.find(branching[from_node].index, to_node) then
 
                             table.insert(branching[from_node].index, to_node)
 
                             table.insert(branching[from_node].index, to_node)
 
                         end
 
                         end
Line 103: Line 103:
 
         end
 
         end
 
     end
 
     end
     table.sort(branching.index)
+
    local sorting
 +
    if args.order then
 +
        local order_nodes = mw.text.split(args.order, "%s*,%s*")
 +
        sorting = function(a, b)
 +
            local ai = Utils.ifind(order_nodes, a)
 +
            local bi = Utils.ifind(order_nodes, b)
 +
            if ai and bi then
 +
                return ai < bi
 +
            else
 +
                return a < b
 +
            end
 +
        end
 +
    else
 +
        sorting = function(a, b)
 +
            return a < b
 +
        end
 +
    end
 +
     table.sort(branching.index, sorting)
 
     for _, from in ipairs(branching.index) do
 
     for _, from in ipairs(branching.index) do
         table.sort(branching[from].index)
+
         table.sort(branching[from].index, sorting)
 
     end
 
     end
 
end
 
end
Line 164: Line 181:
 
     ["0 -> 1"] = "Fixed route",
 
     ["0 -> 1"] = "Fixed route",
 
     ["1 -> A, B/battle, C/empty"] = "Random",
 
     ["1 -> A, B/battle, C/empty"] = "Random",
 +
    ["C -> F, G"] = "...",
 +
    ["B -> D, E"] = "...",
 
     ["title"] = "A Custom Title",
 
     ["title"] = "A Custom Title",
 +
    ["order"] = "C, B",
 
     "?",
 
     "?",
 
})
 
})

Revision as of 00:21, 14 February 2017

Documentation for this module may be created at Module:MapBranchingTable/doc

local getArgs = require("Module:GetArgs")
local format = require("Module:StringOperations").format
local Utils = require("Module:Utils")
local BaseData = require("Module:BaseData")

local MapBranchingTable = BaseData{

    _grammar = {
        split = "^%s*(.+)%s*->%s*(.+)%s*$",
        comma_list = "[^,]+",
        node_and_color = "^%s*(%a)%s*/%s*(%S+)%s*$",
        node = "^%s*([%a%d])%s*$",
        digit_node = "^%s*%d%s*$",
    },

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

    _rules = "\n|rowspan=\"${span}\" style=\"padding:10px;\"|\n${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:log(str)
    if not self._vars.log then
        self._vars.log = ""
    end
    self._vars.log = self._vars.log .. str .. "\n"
end

function MapBranchingTable:parse(args)
    self._vars = {
        id = args.id or args.title and make_id_from_title(args.title) or self._id,
        -- id = args.id or args.title and make_id_from_title(args.title) or remiLib.timeHash(args) 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_node, to_nodes = route:match(self._grammar.split)
            if from_node and to_nodes then
                local from_node_, from_node_color = from_node:match(self._grammar.node_and_color)
                from_node = from_node_color and from_node_ or from_node:match(self._grammar.node)
                local first_node = nil
                for to_node in string.gmatch(to_nodes, self._grammar.comma_list) do
                    local to_node_, to_node_color = to_node:match(self._grammar.node_and_color)
                    to_node = to_node_color and to_node_ or to_node:match(self._grammar.node)
                    if from_node and to_node then
                        if not Utils.find(branching.index, from_node) then
                            table.insert(branching.index, from_node)
                        end
                        if not branching[from_node] then
                            branching[from_node] = { color = from_node_color, index = {} }
                        end
                        if not first_node then
                        	first_node = to_node
                        	branching[from_node][to_node] = { color = to_node_color, rules = rules, span = 1 }
                        else
                        	branching[from_node][first_node].span = branching[from_node][first_node].span + 1
                        	branching[from_node][to_node] = { color = to_node_color }
                        end
                        if not Utils.find(branching[from_node].index, to_node) then
                            table.insert(branching[from_node].index, to_node)
                        end
                    end
                end
            end
        end
    end
    local sorting
    if args.order then
        local order_nodes = mw.text.split(args.order, "%s*,%s*")
        sorting = function(a, b)
            local ai = Utils.ifind(order_nodes, a)
            local bi = Utils.ifind(order_nodes, b)
            if ai and bi then
                return ai < bi
            else
                return a < b
            end
        end
    else
        sorting = function(a, b)
            return a < b
        end
    end
    table.sort(branching.index, sorting)
    for _, from in ipairs(branching.index) do
        table.sort(branching[from].index, sorting)
    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:format_rows()
    local branching = self._vars.branching
    local rows = {}
    for _, from_node in ipairs(branching.index) do
        table.insert(rows, format{
            self._row_start_template,
            rowspan = #branching[from_node].index,
            from = self:format_node(from_node, branching[from_node].color),
            id = self._vars.id,
        })
        local first = true
        for _, to_node in ipairs(branching[from_node].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_node, branching[from_node][to_node].color),
                rules = branching[from_node][to_node].rules and format{
                    self._rules,
                    rules = branching[from_node][to_node].rules,
                    span = branching[from_node][to_node].span,
                } or "",
                id = self._vars.id,
            })
            first = false
        end
    end
    self._vars.rows = table.concat(rows, "\n")
end

function MapBranchingTable:format(args)
    self:parse(args)
    self:format_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 -> A, B/battle, C/empty"] = "Random",
    ["C -> F, G"] = "...",
    ["B -> D, E"] = "...",
    ["title"] = "A Custom Title",
    ["order"] = "C, B",
    "?",
})
--print(p.t)
--print(p._vars.log)
]]--

return MapBranchingTable