Module:ImprovementTable

local Equipment = require("Module:Equipment") local format = require("Module:StringInterpolation").format local first = require("Module:Utils").first

-- * Mocking Module:Ship, only name and suffix functions based on ship_key string.

local Ship = {}

function Ship:create(ship_key) self._name, self._suffix = ship_key:match("(.-)/(.*)") if self._suffix == "" then self._suffix = false end if not self._name then self._name = ship_key end return self end

function Ship:base_name return self._name end

function Ship:suffix return self._suffix end

function Ship:name if self:suffix then return self:base_name .. " " .. self:suffix else return self:base_name end end

-- * No Module:BaseTable due to all the spans and what not.

local ImprovementTable = {

_table_start = {| class="wikitable typography-xl-optout" style="text-align:center;width:100%;", _table_end = |},

_heading = [[|-
 * colspan="16" | ${title} ]],

_header = ,

_row = [=[|- ${days}
 * style="text-align:left;" rowspan="4" id="${link}" | ${link}
 * 0
 * nowrap | ${devmats0}/${devmats0_x}
 * nowrap | ${impmats0}/${impmats0_x}
 * nowrap | ${equips0}
 * style="background-color:rgb(169, 197, 168);" rowspan="2" | ${fuel}
 * style="background-color:rgb(191, 191, 191);" rowspan="2" | ${steel}
 * rowspan="4" | ${ships}
 * rowspan="4" | ${upgrades}
 * rowspan="2" |6
 * nowrap rowspan="2" | ${devmats6}/${devmats6_x}
 * nowrap rowspan="2" | ${impmats6}/${impmats6_x}
 * nowrap rowspan="2" | ${equips6}
 * style="background-color:rgb(194, 184, 168);" rowspan="2" | ${ammo}
 * style="background-color:rgb(237, 206, 184);" rowspan="2" | ${bauxite}
 * 10
 * nowrap | ${devmats10}/${devmats10_x}
 * nowrap | ${impmats10}/${impmats10_x}
 * nowrap | ${equips10}]=],
 * nowrap | ${devmats10}/${devmats10_x}
 * nowrap | ${impmats10}/${impmats10_x}
 * nowrap | ${equips10}]=],

_day_cell = ${ships}, _unavailable_color = rgb(255, 153, 153), _available_color = rgb(170, 235, 170),

}

function format_ships_and_days(ship_keys, equipment, product) local result_ships = {} local result_days = {} local days = { Sunday = {}, Monday = {}, Tuesday = {}, Wednesday = {}, Thursday = {}, Friday = {}, Saturday = {} }   if ship_keys and equipment then local base_forms = {} for _, ship_key in ipairs(ship_keys) do           local availability = equipment:improvement_availability(product, ship_key) if ship_key == true then table.insert(result_ships, "Any") days = availability break else local ship = Ship:create(ship_key) if not base_forms[ship:base_name] then base_forms[ship:base_name] = true table.insert(result_ships, "" .. ship:name .. "") for day, day_availability in pairs(availability) do                       table.insert(days[day], day_availability and string.sub(ship_key, 1, 1)) end end end end for day, ships in pairs(days) do           if ships == true then table.insert(result_days, format{                   ImprovementTable._day_cell,                    background = "rgb(170, 235, 170)",                    ships = "✓",                }) elseif ships == false then table.insert(result_days, format{                   ImprovementTable._day_cell,                    background = ImprovementTable._unavailable_color,                    ships = "✗",                }) else local n_ships = 0 for i, ship in ipairs(days[day]) do                   if ship ~= false then n_ships = n_ships + 1 else days[day][i] = " " end end table.insert(result_days, format{                   ImprovementTable._day_cell,                    background = n_ships > 0 and ImprovementTable._available_color or ImprovementTable._unavailable_color,                    ships = n_ships > 0 and table.concat(days[day], " ") or "✗",                }) end end end return table.concat(result_ships, " "), table.concat(result_days, "\n") end

function format_equipment(resources) if resources then if resources.equipment then equipment, amount = first(resources.equipment) if equipment == true then return tostring(amount) elseif equipment then -- TODO: equipment icons return tostring(amount) end elseif resources.equipment == false then return "-" else return "?" end else return "-" end end

function format_devmat(resources) return resources and (resources.devmat or "?") or "-" end

function format_screw(resources) return resources and (resources.screw or "?") or "-" end

function ImprovementTable:add_row(arg) local equipment = Equipment(arg) if equipment then local products = equipment:improvement_products local resources = equipment:improvement_resources local resources0, resources0_x, resources6, resources6_x, resources10, resources10_x local ships, days local product_names = {} local i = 0 for _, product in ipairs(products) do           if i == 0 then resources0 = equipment:improvement_resources(false, product, 0) resources0_x = equipment:improvement_resources(true, product, 0) resources6 = equipment:improvement_resources(false, product, 6) resources6_x = equipment:improvement_resources(true, product, 6) resources10 = equipment:improvement_resources(false, product, 10) resources10_x = equipment:improvement_resources(true, product, 10) ships, days = format_ships_and_days(equipment:improvement_ships(product), equipment, product) end if product then table.insert(product_names, product) end i = i + 1 end table.insert(self.rows, format{           self._row,            link = equipment:link,            fuel = resources.fuel or 0,            ammo = resources.ammo or 0,            steel = resources.steel or 0,            bauxite = resources.bauxite or 0,            devmats0 = format_devmat(resources0),            devmats0_x = format_devmat(resources0_x),            devmats6 = format_devmat(resources6_x),            devmats6_x = format_devmat(resources6_x),            devmats10 = format_devmat(resources10),            devmats10_x = format_devmat(resources10_x),            impmats0 = format_screw(resources0),            impmats0_x = format_screw(resources0_x),            impmats6 = format_screw(resources6),            impmats6_x = format_screw(resources6_x),            impmats10 = format_screw(resources10),            impmats10_x = format_screw(resources10_x),            equips0 = format_equipment(resources0), equips6 = format_equipment(resources6), equips10 = format_equipment(resources10), upgrades = #product_names == 1 and "" .. product_names[1] .. "" or #product_names > 1 and "Unimplemented" or "None", ships = ships, days = days, })   end end

function ImprovementTable:add_rows(args) for _, arg in ipairs(args) do       if string.sub(arg, 1, 1) == '!' then table.insert(self.rows, format{self._heading, title = string.sub(arg, 2)}) elseif arg == "#" then table.insert(self.rows, self._header) else self:add_row(arg) end end end

function ImprovementTable:Table(args) self.rows = {} if not args.only_rows then table.insert(self.rows, self._table_start) end self:add_rows(args) if not args.only_rows then table.insert(self.rows, self._table_end) end return table.concat(self.rows, "\n") end

return ImprovementTable