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

From Kancolle Wiki
Jump to navigation Jump to search
m
Line 7: Line 7:
 
local NodeInfo = BaseTable({
 
local NodeInfo = BaseTable({
 
     _item_class = EnemyShip,
 
     _item_class = EnemyShip,
     _header_template = nil,
+
     _header_template = [[!Node
 +
    !Formation
 +
    !Fleet
 +
    !AS]],
 
     _column_cell_templates = {
 
     _column_cell_templates = {
 
         node = [[| colspan="${colspan}" rowspan="${rowspan}" style="text-align: center; background-color: ${bg_color};" |${values.node} ${values.time}]],
 
         node = [[| colspan="${colspan}" rowspan="${rowspan}" style="text-align: center; background-color: ${bg_color};" |${values.node} ${values.time}]],
Line 39: Line 42:
  
 
function NodeInfo:fleet(row)
 
function NodeInfo:fleet(row)
     return { values = { formation = row.fleet }, bg_color = self._column_bg_colors.fleet }
+
     return { values = { fleet = row.fleet }, bg_color = self._column_bg_colors.fleet }
 
end
 
end
  
 
function NodeInfo:as(row)
 
function NodeInfo:as(row)
     return { values = { formation = row.fleet }, bg_color = self._column_bg_colors.as }
+
     return { values = { as = row.as }, bg_color = self._column_bg_colors.as }
 
end
 
end
  
Line 57: Line 60:
 
end
 
end
 
return node, time
 
return node, time
 +
end
 +
 +
function NodeInfo:insert_item(node, time, boss, formation, fleet, as, item_key)
 +
    table.insert(self._items, {
 +
        node = node,
 +
        time = time,
 +
boss = boss,
 +
formation = Formatting:format_image{formation, caption = item_key, align = "center", valign = "center"},
 +
fleet = fleet,
 +
as = as,
 +
})
 
end
 
end
  
 
function NodeInfo:create_items()
 
function NodeInfo:create_items()
 
     local mode = 1
 
     local mode = 1
     local fleet = ""
+
     local node, time, boss, formation_image, fleet, as_rating = nil, nil, false, nil, "", 0
 
for index, item_key in ipairs(self._args) do
 
for index, item_key in ipairs(self._args) do
 
if item_key == "-" then
 
if item_key == "-" then
    table.insert(self._items, { fleet = fleet })
+
    self:insert_item(node, time, boss, formation_image, fleet, as_rating, item_key)
 
table.insert(self._items, "break")
 
table.insert(self._items, "break")
fleet = ""
+
 +
node, time, boss, formation_image, fleet, as_rating = nil, nil, false, nil, "", 0
 
mode = 1
 
mode = 1
 
else
 
else
Line 73: Line 88:
 
    --First item should always be the node
 
    --First item should always be the node
 
    --Node format: !node/day|night where the second part is day -or- night; ! indicates a boss node
 
    --Node format: !node/day|night where the second part is day -or- night; ! indicates a boss node
    local boss = false
 
 
    if mw.ustring.sub(item_key, 1, 1) == "!" then
 
    if mw.ustring.sub(item_key, 1, 1) == "!" then
 
        boss = true
 
        boss = true
Line 79: Line 93:
 
    end
 
    end
 
     
 
     
    local node, time = self:process_node(item_key)
+
    node, time = self:process_node(item_key)
   
 
    table.insert(self._items, { node = node, time = time, boss = boss })
 
 
    mode = 2
 
    mode = 2
 
    elseif mode == 2 then
 
    elseif mode == 2 then
        local formation_image = item_key:gsub("%s", "") .. ".jpg"
+
        formation_image = item_key:gsub("%s", "") .. ".jpg"
        table.insert(self._items, { values = { formation = Formatting.format_image{formation_image, caption = item_key} } })
 
 
        mode = 3
 
        mode = 3
 
    else
 
    else
Line 94: Line 105:
 
        local ship = EnemyShip(ship_name, ship_suffix)
 
        local ship = EnemyShip(ship_name, ship_suffix)
 
         
 
         
        fleet = fleet .. Formatting.format_image{ship:battle_card(), ship:name()}
+
        if fleet ~= "" then
 +
            fleet = fleet .. " " -- Actually matters
 +
        end
 +
        fleet = fleet .. Formatting:format_image{ship:battle_card(), ship:name()}
 +
       
 +
        if ship:as_rating() then
 +
            as_rating = as_rating + ship:as_rating()
 +
        end
 
             end
 
             end
 
end
 
end
 +
    end
 +
    self:insert_item(node, time, boss, formation_image, fleet, as_rating, item_key)
 +
end
 +
 +
function NodeInfo:create_data_rows()
 +
    for index, item in ipairs(self._items) do
 +
local row_values
 +
if type(item) == "string" then
 +
row_values = item
 +
else
 +
row_values = {}
 +
for _, column in ipairs(self._columns) do
 +
row_values[column] = self[column](self, item)
 +
end
 +
if index > 1 then
 +
    for _, column in ipairs(self._columns) do
 +
    for i = index - 1, 1, -1 do
 +
        if column == "node" then
 +
    local previous_cell = self._data_rows[i][column]
 +
    if previous_cell then
 +
        if row_values[column].values.node == previous_cell.values.node then
 +
        previous_cell.rowspan = previous_cell.rowspan + 1
 +
        row_values[column] = nil
 +
        else
 +
        row_values[column].rowspan = 1
 +
        row_values[column].colspan = 1
 +
        end
 +
        break
 +
        end
 +
    end
 +
            end
 +
        end
 +
    else
 +
for _, column in ipairs(self._columns) do
 +
    row_values[column].rowspan = 1
 +
    row_values[column].colspan = 1
 +
end
 +
end
 +
end
 +
table.insert(self._data_rows, row_values)
 
end
 
end
 
end
 
end
 
NodeInfo.create_data_rows = NodeInfo.create_data_rows_merge_vertical
 
  
 
return NodeInfo
 
return NodeInfo

Revision as of 16:21, 15 April 2015

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

local BaseTable = require("Module:BaseTable")
local Formatting = require('Module:Formatting')
local EnemyShip = require("Module:EnemyShip")

local format = require('Module:StringInterpolation').format

local NodeInfo = BaseTable({
    _item_class = EnemyShip,
    _header_template = [[!Node
    !Formation
    !Fleet
    !AS]],
    _column_cell_templates = {
        node = [[| colspan="${colspan}" rowspan="${rowspan}" style="text-align: center; background-color: ${bg_color};" |${values.node} ${values.time}]],
        formation = [[| style="background-color: ${bg_color}" |${values.formation}]],
        fleet = [[| style="background-color: ${bg_color}" |${values.fleet}]],
        as = [[| style="text-align: center; background-color: ${bg_color}" |${values.as}]],
    },
    _column_bg_colors = {
        node = "transparent",
        formation = "transparent",
        fleet = "transparent",
        as = "transparent",
    },
    _columns = {
        "node",
        "formation",
        "fleet",
        "as",
    },
    _day_battle_color = "yellow",
    _night_battle_color = "purple",
})

function NodeInfo:node(row)
    return { values = { node = row.node, time = row.time }, bg_color = self._column_bg_colors.node }
end

function NodeInfo:formation(row)
    return { values = { formation = row.formation }, bg_color = self._column_bg_colors.formation }
end

function NodeInfo:fleet(row)
    return { values = { fleet = row.fleet }, bg_color = self._column_bg_colors.fleet }
end

function NodeInfo:as(row)
    return { values = { as = row.as }, bg_color = self._column_bg_colors.as }
end

function NodeInfo:process_node(item)
    local split = mw.ustring.find(item, '/')
	local node, time
	if split then
	    node = mw.ustring.sub(item, 1, split - 1)
	    time = "(" .. mw.ustring.sub(item, split + 1):gsub("^%l", string.upper) .. ")"
	else
	    node = item:upper()
	    time = ""
	end
	return node, time
end

function NodeInfo:insert_item(node, time, boss, formation, fleet, as, item_key)
    table.insert(self._items, {
        node = node,
        time = time,
		boss = boss,
		formation = Formatting:format_image{formation, caption = item_key, align = "center", valign = "center"},
		fleet = fleet,
		as = as,
	})
end

function NodeInfo:create_items()
    local mode = 1
    local node, time, boss, formation_image, fleet, as_rating = nil, nil, false, nil, "", 0
	for index, item_key in ipairs(self._args) do
		if item_key == "-" then
		    self:insert_item(node, time, boss, formation_image, fleet, as_rating, item_key)
			table.insert(self._items, "break")
			
			node, time, boss, formation_image, fleet, as_rating = nil, nil, false, nil, "", 0
			mode = 1
		else
			--Each item will be just a string, so we need to check for one of the _columns, which will be done by mode
			if mode == 1 then
			    --First item should always be the node
			    --Node format: !node/day|night where the second part is day -or- night; ! indicates a boss node
			    if mw.ustring.sub(item_key, 1, 1) == "!" then
			        boss = true
			        item_key = mw.ustring.sub(item_key, 2)
			    end
			    
			    node, time = self:process_node(item_key)
			    mode = 2
		    elseif mode == 2 then
		        formation_image = item_key:gsub("%s", "") .. ".jpg"
		        mode = 3
		    else
		        --Fleets are of variable size, so we append onto a string until we hit the next node declaration
		        local split = mw.ustring.find(item_key, '/')
		        local ship_name = mw.ustring.sub(item_key, 1, split - 1)
		        local ship_suffix = mw.ustring.sub(item_key, split + 1)
		        local ship = EnemyShip(ship_name, ship_suffix)
		        
		        if fleet ~= "" then
		            fleet = fleet .. " " -- Actually matters
		        end
		        fleet = fleet .. Formatting:format_image{ship:battle_card(), ship:name()}
		        
		        if ship:as_rating() then
		            as_rating = as_rating + ship:as_rating()
		        end
            end
		end
    end
    self:insert_item(node, time, boss, formation_image, fleet, as_rating, item_key)
end

function NodeInfo:create_data_rows()
    for index, item in ipairs(self._items) do
		local row_values
		if type(item) == "string" then
			row_values = item
		else
			row_values = {}
			for _, column in ipairs(self._columns) do
				row_values[column] = self[column](self, item)
			end
			if index > 1 then
			    for _, column in ipairs(self._columns) do
			    	for i = index - 1, 1, -1 do
			    	    if column == "node" then
						    local previous_cell = self._data_rows[i][column]
    						if previous_cell then
	    					    if row_values[column].values.node == previous_cell.values.node then
		    				    	previous_cell.rowspan = previous_cell.rowspan + 1
			    			    	row_values[column] = nil
				    		    else
					    	    	row_values[column].rowspan = 1
    					    		row_values[column].colspan = 1
    	    					end
	    	    				break
	    	    			end
		    			end
		            end
		        end
		    else
				for _, column in ipairs(self._columns) do
			    	row_values[column].rowspan = 1
			    	row_values[column].colspan = 1
				end
			end
		end
		table.insert(self._data_rows, row_values)
	end
end

return NodeInfo