Module:EliteShipsKai

Revision as of 06:59, 29 January 2015 by com>Ckwng (Cell value highlighting, color tweaks)

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

local ResourceIcons = require('Module:ResourceIcons')
local StatIcons = require('Module:StatIcons')
local MiscIcons = require('Module:MiscIcons')
local format = require('Module:StringInterpolation').format

local EliteShipsKai = {
	_table_start = [[{| class="wikitable"]],
	_row_starter = "|-",
	_header_template = [[!No.
!Name
!Class
! style="white-space:nowrap;" | ${remodel_icon}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: LightCoral;" | ${firepower_icon}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: LightSkyBlue;" | ${torpedo_icon}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: SandyBrown;" | ${aa_icon}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: #9C8FEE;" | ${asw_icon}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: Aquamarine;" | ${los_icon}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: PaleGreen;" | ${luck_icon}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: Pink;" | ${hp_icon}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: #F2E279;" | ${armor_icon}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: Violet;" | ${evasion_icon}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: #72E6E6;" | ${speed_icon}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: Silver;" | ${aircraft_icon}
! ${fuel_icon}
! ${ammo_icon}
! style="text-align:left;" | Notes]],
	_image_template = "[[File:%s]]",
	_columns = {
		"class",
		"remodel",
		"firepower",
		"torpedo",
		"aa",
		"asw",
		"los",
		"luck",
		"hp",
		"armor",
		"evasion",
		"speed",
		"aircraft",
		"fuel",
		"ammo",
		"notes",
	},
	_cell_color = {
		firepower = "#F0C0C0",
		torpedo = "#C0E4FA",
		aa = "#F4CAA6",
		asw = "#C5BEEE",
		los = "#CCFFEE",
		luck = "#C9FBC9",
		hp = "#FFD9DF",
		armor = "#F2ECC2",
		evasion = "#EEBEEE",
		speed = "#B8E6E6",
		aircraft = "#D9D9D9",
	},
	_good_stat_color = "yellow",
	_outstanding_stat_color = "lime",
	_cell = [[| colspan="1" rowspan="${rowspan}" style="text-align:center; background-color: ${bg_color}" | ${value}]],
	_table_end = [[|}]],
	_rowspan_suffix = "_rowspan",
	_bg_color_suffix = "_bg_color",
	_transparent = "transparent",
	_operators = {
		["<"] = function(x,y) return x < y end,
		[">"] = function(x,y) return x > y end,
	}
}

function EliteShipsKai:name(ship)
	return format{"${link} ${japanese_name}", link = ship:link(), japanese_name = ship:japanese_name()}
end

function EliteShipsKai:id(ship)
	return ship:id()
end

function EliteShipsKai:class(ship)
	return ship:class()
end

function EliteShipsKai:remodel_level(ship)
	return ship:remodel_level()
end

function EliteShipsKai:firepower_max(ship)
	return ship:firepower_max()
end

function EliteShipsKai:torpedo_max(ship)
	return ship:torpedo_max()
end

function EliteShipsKai:aa_max(ship)
	return ship:aa_max()
end

function EliteShipsKai:asw_max(ship)
	return ship:asw_max()
end

function EliteShipsKai:los_max(ship)
	return ship:los_max()
end

function EliteShipsKai:luck_max(ship)
	return ship:luck_max()
end

function EliteShipsKai:hp(ship)
	return ship:hp()
end

function EliteShipsKai:armor_max(ship)
	return ship:armor_max()
end

function EliteShipsKai:evasion_max(ship)
	return ship:evasion_max()
end

function EliteShipsKai:speed(ship)
	return ship:speed()
end

function EliteShipsKai:aircraft(ship)
	return ship:total_space()
end

function EliteShipsKai:fuel(ship)
	return ship:fuel()
end

function EliteShipsKai:ammo(ship)
	return ship:ammo()
end

function EliteShipsKai:notes(ship)
	--stubbed out for now
	return ""
end

function EliteShipsKai:Table(ships, ranges)
	if not ranges then
		ranges = {}
	end
	local header_icons = {
		remodel_icon = MiscIcons.remodel, 
		firepower_icon = StatIcons.firepower, 
		torpedo_icon = StatIcons.torpedo, 
		aa_icon = StatIcons.aa, 
		asw_icon = StatIcons.asw, 
		los_icon = StatIcons.los, 
		luck_icon = StatIcons.luck, 
		hp_icon = StatIcons.hp, 
		armor_icon = StatIcons.armor,
		evasion_icon = StatIcons.evasion,
		speed_icon = StatIcons.speed,
		aircraft_icon = StatIcons.aircraft,
		fuel_icon = ResourceIcons.fuel,
		ammo_icon = ResourceIcons.ammo,
	}
	for key, value in pairs(header_icons) do
		header_icons[key] = mw.ustring.format(self._image_template, value)
	end
	local header = format(self._header_template, header_icons)

	local rows = {self._table_start, header}

	local data_rows = {}
	for index, ship in ipairs(ships) do
		local row_values = {
			id = self:id(ship),
			name = self:name(ship),
			class = self:class(ship),
			remodel = self:remodel_level(ship),
			firepower = self:firepower_max(ship),
			torpedo = self:torpedo_max(ship),
			aa = self:aa_max(ship),
			asw = self:asw_max(ship),
			los = self:los_max(ship),
			luck = self:luck_max(ship),
			hp = self:hp(ship),
			armor = self:armor_max(ship),
			evasion = self:evasion_max(ship),
			speed = self:speed(ship),
			aircraft = self:aircraft(ship),
			fuel = self:fuel(ship),
			ammo = self:ammo(ship),
			notes = self:notes(ship),
		}
		if index > 1 then
			for _, column in ipairs(self._columns) do
				for i=index-1,1,-1 do
					if data_rows[i][column] and row_values[column] == data_rows[i][column] then
						data_rows[i][column .. self._rowspan_suffix] = data_rows[i][column .. self._rowspan_suffix] + 1
						row_values[column] = nil
					end
				end
			end
		end
		for _, column in ipairs(self._columns) do
			if row_values[column] then
				row_values[column .. self._rowspan_suffix] = 1
				row_values[column .. self._bg_color_suffix] = self._cell_color[column] or self._transparent
				if ranges[column] then
					local operator = self._operators[ranges[column].operator]
					if operator(row_values[column], ranges[column].outstanding) then
						row_values[column .. self._bg_color_suffix] = self._outstanding_stat_color
					elseif operator(row_values[column], ranges[column].good) then
						row_values[column .. self._bg_color_suffix] = self._good_stat_color
					end
				end
			end
		end
		table.insert(data_rows, row_values)
	end

	for index, row_values in ipairs(data_rows) do
		table.insert(rows, self._row_starter)
		table.insert(rows, format{self._cell, value = row_values.id, rowspan = 1, bg_color = self._transparent})
		table.insert(rows, format{self._cell, value = row_values.name, rowspan = 1, bg_color = self._transparent})
		for _, column in ipairs(self._columns) do
			if row_values[column] then
				table.insert(rows, format{self._cell, value = row_values[column], rowspan = row_values[column .. self._rowspan_suffix], bg_color = row_values[column .. self._bg_color_suffix]})
			end
		end
	end

	table.insert(rows, self._row_starter)
	table.insert(rows, header)
	table.insert(rows, self._table_end)
	return table.concat(rows, "\n")
end

return EliteShipsKai