Module:NanaminFleetKai

Revision as of 10:12, 20 April 2015 by Nanamin (talk | contribs)

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

local getArgs = require('Module:GetArgs')

local Ship = require('Module:Ship')
local ShipCardKai = require('Module:ShipCardKai')
local Equipment = require('Module:Equipment')

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

local Functions = require('Module:NanaminFunctions')

local NanaminFleetKai = {
    _rows = {},
    _items = {},
    
    _table_start = [[{|]],
    _table_end = [[|}]],
    _row_starter = "|-",
    _added_cell_template = [[| colspan="6" style="background: ${bg_color}; text-align: center; font-weight: bold;" |${content}]],
    _ship_template = [[| rowspan="4" style="background: ${bg_color};" |${ship_card}]],
    _equip_template = [[| style="background-color: ${icon_bg_color}; width: 15px;" |${equip_icon}
    | style="background-color: ${equip_bg_color}; width: 175px;text-align: center;" |${equip_link}]],
    
    _ship_card_bg = "transparent",
    _equip_icon_bg = "#8DCDEE",
    _equip_bg = "#E4F3FB",
    _equip_locked_bg = "#CDDBE2",
    _extra_cells_bg = "#98D2F0",
    
    _blank = "Empty_ship_slot.png",
    _size = "120x165px",
    
    _elos = 0,
    _fighter_power = 0,
    _hq_lvl = 0,
}

function NanaminFleetKai:customize_colors()
    self._ship_card_bg = self._args["ship_card_bg"] or self._ship_card_bg
    self._equip_icon_bg = self._args["equip_icon_bg"] or self._equip_icon_bg
    self._equip_bg = self._args["equip_bg"] or self._equip_bg
    self._equip_locked_bg = self._args["equip_locked_bg"] or self._equip_locked_bg
    self._extra_cells_bg = self._args["extra_cells_bg"] or self._extra_cels_bg
end

function NanaminFleetKai:process_hq_level()
    self._hq_lvl = tonumber(self._args["hq"] or 0)
    
    --Get bracket of 5
    self._hq_lvl = self._hq_lvl - (self._hq_lvl % 5) + 5
    if tonumber(self._args["hq"]) % 5 == 0 then
        self._hq_lvl = self._hq_lvl + 5
    end
end

function NanaminFleetKai:process_los(entry)
    local ship = Ship(entry[1], entry[2])
    local los = ship:los() or 0
    local los_max = ship:los_max() or los
    if self._args["use_los"] then
        los = tonumber(entry[3])
    else
        los = math.floor(los + ((los_max - los) * tonumber(entry[3]) / 99))
    end
    self._elos = self._elos + math.sqrt(los) * 1.69
end

function NanaminFleetKai:process_args(args)
    local entry = {}
    for index, item in ipairs(args) do
        if item == "-" and #entry > 0 then
            self:process_los(entry)
            table.insert(self._items, entry)
            entry = {}
        else
            local processed = Functions.split(item, '/')
            for _, item in ipairs(processed) do
                table.insert(entry, item)
            end
        end
    end
    if #entry > 0 then
        self:process_los(entry)
        table.insert(self._items, entry)
    end
end

function NanaminFleetKai:process_equipment(equip, plane_count)
    local multiplier = { [7] = 1.04, [8] = 1.04, [9] = 1.66, [10] = 2.00, [11] = 1.78, [12] = 1.00, [13] = 0.99, [29] = 0.91 }
    if multiplier[equip:type()] ~= nil and equip:los() then
        self._elos = self._elos + (equip:los() * multiplier[equip:type()])
    end
    local can_fight = { [6] = true, [7] = true, [8] = true, [11] = true }
    if can_fight[equip:type()] then
        if plane_count and equip:aa() then
            self._fighter_power = self._fighter_power + math.floor(math.sqrt(plane_count) * equip:aa())
        end
    end
end

function NanaminFleetKai:insert_ship_row(ship)
    local ship_card
    if ship and ship:name() then
        ship_card = ShipCardKai:card{ship = ship, size = self._size, link = ship:link(), caption = ship:name()}
    else
        ship_card = Formatting:format_image{self._blank, size = self._size}
    end
    table.insert(self._rows, format{self._ship_template,
        bg_color = self._ship_card_bg,
        ship_card = ship_card,
    })
end

function NanaminFleetKai:insert_equip_row(ship, entry, slot)
    local slots = ship and ship:slots() or 0
    local equip_icon, equip_bg_color, equip_link
    if slot > slots then
        equip_icon = ""
        equip_bg_color = self._equip_locked_bg
        equip_link = "- Locked -"
    elseif entry[slot + 3] == nil then
        equip_icon = ""
        equip_bg_color = self._equip_locked_bg
        equip_link = "- Unequipped -"
    else
        local equip = Equipment(entry[slot + 3])
        self:process_equipment(equip, select(2, ship:slot(slot)))
        equip_icon = Formatting:format_image{ Formatting:format_equipment_icon(equip:icon()), size = "22x22px" }
        equip_bg_color = self._equip_bg
        equip_link = Formatting:format_link(equip:link())
    end
    table.insert(self._rows, format{self._equip_template,
        icon_bg_color = self._equip_icon_bg,
        equip_icon = equip_icon,
        equip_bg_color = equip_bg_color,
        equip_link = equip_link,
    })
end

function NanaminFleetKai:build_table()
    for i = 1, #self._items, 2 do
        local ship1 = Ship(self._items[i][1], self._items[i][2])
        self:insert_ship_row(ship1)
        self:insert_equip_row(ship1, self._items[i], 1)
        
        local ship2
        if self._items[i + 1] then
            ship2 = Ship(self._items[i + 1][1], self._items[i + 1][2])
            self:insert_ship_row(ship2)
            self:insert_equip_row(ship2, self._items[i + 1], 1)
        else
            self:insert_ship_row(nil)
            self:insert_equip_row(nil, nil, 1)
        end
        
        for j = 2, 4 do
            table.insert(self._rows, self._row_starter)
            self:insert_equip_row(ship1, self._items[i], j)
            if self._items[i + 1] then
                self:insert_equip_row(ship2, self._items[i + 1], j)
            else
                self:insert_equip_row(nil, nil, 1)
            end
        end
        table.insert(self._rows, self._row_starter)
    end
end

function NanaminFleetKai:insert_extra(content)
    table.insert(self._rows, self._row_starter)
    table.insert(self._rows, format{self._added_cell_template, content = content, bg_color = self._extra_cells_bg })
end

function NanaminFleetKai:add_extras()
    local elos = string.format("%.2f", self._elos - (self._hq_lvl * 0.61))
    local content = "Effective Line of Sight: " .. elos .. " // Fighter Power: " .. tostring(self._fighter_power)
    self:insert_extra(content)
    
    content = "Route Taken: " .. (self._args["route"] or "")
    self:insert_extra(content)
    
    if self._args["difficulty"] then
        self:insert_extra("Difficulty Chosen: " .. self._args["difficulty"])
    end
    
    if self._args["drops"] then
        local drops = Functions.split(self._args["drops"], '/')
        content = "Notable Drops<br />"
        for i = 1, #drops do
            local ship = Ship(drops[i], "")
            local card = ShipCardKai:battle_card{ship = ship, link = ship:link(), caption = ship:name()}
            content = content .. card .. " "
            if i % 4 == 0 then
                content = mw.ustring.sub(content, 1, -2) .. "<br />"
            end
        end
        self:insert_extra(content)
    end
end

function NanaminFleetKai:Main(args)
    self._args = args
    
    self:customize_colors()
    self:process_hq_level()
    self:process_args(args)
    table.insert(self._rows, self._table_start)
    self:build_table()
    self:add_extras()
    table.insert(self._rows, self._table_end)
    return table.concat(self._rows, "\n")
end

function NanaminFleetKai.Begin(frame)
	local args = getArgs{frame = frame:getParent()}
	return NanaminFleetKai:Main(args)
end

return NanaminFleetKai