Module:EliteShipsKai

Revision as of 23:18, 6 March 2015 by com>Ckwng

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 BaseData = require('Module:BaseData')
local Ship = require('Module:Ship')
local Formatting = require('Module:Formatting')
local format = require('Module:StringInterpolation').format

local EliteShipsKai = BaseData({
	_table_start = [[{| class="wikitable"]],
	_row_starter = "|-",
	_header_template = [[!No.
!Name
!Class
! style="white-space:nowrap;" | ${remodel}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: lightcoral;" | ${firepower}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: lightskyblue;" | ${torpedo}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: sandybrown;" | ${aa}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: #9C8FEE;" | ${asw}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: aquamarine;" | ${los}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: palegreen;" | ${luck}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: pink;" | ${hp}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: #F2E279;" | ${armor}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: violet;" | ${evasion}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: #72E6E6;" | ${speed}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: silver;" | ${aircraft}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: forestgreen;" | ${fuel}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: darkgoldenrod;" | ${ammo}
! style="text-align:left;" | Notes]],
	_column_cell_templates = {
		name = [[| colspan="1" rowspan="${rowspan}" style="text-align:center; background-color: ${bg_color};" | ${values.link} ${values.japanese_name}]],
		luck = [[| colspan="1" rowspan="${rowspan}" style="text-align:center; background-color: ${bg_color};" | ${values.luck}(${values.luck_max})]],
		id = [[| colspan="1" rowspan="${rowspan}" style="text-align:center; background-color: ${bg_color};${extra_style}" |${values.value}]]
	},
	_columns = {
		"id",
		"name",
		"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",
		fuel = "#94E094",
		ammo = "palegoldenrod"
	},
	_good_stat_color = "#ffff40",
	_outstanding_stat_color = "#73ff4d",
	_cell = [[| colspan="1" rowspan="${rowspan}" style="text-align:center; background-color: ${bg_color};" |${values.value}]],
	_table_end = [[|}]],
	_rowspan_suffix = "_rowspan",
	_bg_color_suffix = "_bg_color",
	_outstanding_suffix = "_outstanding",
	_good_suffix = "_good",
	_operator_suffix = "_operator",
	_notes_suffix = "_notes",
	_highlight_suffix = "_highlight",
	_transparent = "transparent",
	_no_remodel_level = "-",
	_emphasis_template = "'''${value}'''",
	_operators = {
		["<"] = function(x,y) return x < y end,
		[">"] = function(x,y) return x > y end,
	}
})

function EliteShipsKai:get_bg_color(default, value, operator, outstanding, good)
	if not value then
		return default or self._transparent
	end
	local comparison = self._operators[operator or ">"]
	if outstanding and comparison(value, tonumber(outstanding)) then
		return self._outstanding_stat_color
	elseif good and comparison(value, tonumber(good)) then
		return self._good_stat_color
	else
		return default or self._transparent
	end
end

function EliteShipsKai:get_emphasis(value, formatted, operator, good)
	local comparison = self._operators[operator or ">"]
	if value and good and comparison(value, tonumber(good)) then
		return format{self._emphasis_template, value = formatted}
	end
	return formatted
end

function EliteShipsKai:name(ship)
	return {values = {link = Formatting:format_link(ship:link()), japanese_name = ship:japanese_name()}, bg_color = self._transparent}
end

function EliteShipsKai:id(ship)
	local back = ship:back()
	return {values = {value = ship:id()}, bg_color = Formatting:format_ship_back(back), extra_style = Formatting:extra_style_background_image(back and back > 7, back and back > 6) or ""}
end

function EliteShipsKai:class(ship, args)
	local class = ship:class()
	local ship_type = ship:type()
	local value = class:base_name()
	local reference_type = args.reference_type
	local show_type
	if reference_type then
		show_type = ship_type ~= tonumber(reference_type)
	else
		show_type = ship_type ~= class:base_type()
	end
	if show_type then
		value = Formatting:format_stat_with_detail(value, Formatting:format_ship_code(ship_type))
	end
	return {values = {value = value}, bg_color = self._transparent}
end

function EliteShipsKai:remodel(ship, args)
	local remodel_level = ship:remodel_level()
	return {values = {value = Formatting:format_stat((not remodel_level and (ship:buildable() or ship:buildable_lsc())) and self._no_remodel_level or remodel_level)}, bg_color = self:get_bg_color(self._cell_color.remodel, remodel_level, args.remodel_level_operator, args.remodel_level_outstanding, args.remodel_level_good)}
end

function EliteShipsKai:firepower(ship, args)
	local firepower_max = ship:firepower_max()
	if firepower_max == false then
		firepower_max = ship:firepower()
	end
	return {values = {value = Formatting:format_stat(firepower_max)}, bg_color = self:get_bg_color(self._cell_color.firepower, firepower_max, args.firepower_operator, args.firepower_outstanding, args.firepower_good)}
end

function EliteShipsKai:torpedo(ship, args)
	local torpedo_max = ship:torpedo_max()
	if torpedo_max == false then
		torpedo_max = ship:torpedo()
	end
	return {values = {value = Formatting:format_stat(torpedo_max)}, bg_color = self:get_bg_color(self._cell_color.torpedo, torpedo_max, args.torpedo_operator, args.torpedo_outstanding, args.torpedo_good)}
end

function EliteShipsKai:aa(ship, args)
	local aa_max = ship:aa_max()
	if aa_max == false then
		aa_max = ship:aa()
	end
	return {values = {value = Formatting:format_stat(aa_max)}, bg_color = self:get_bg_color(self._cell_color.aa, aa_max, args.aa_operator, args.aa_outstanding, args.aa_good)}
end

function EliteShipsKai:asw(ship, args)
	local asw_max = ship:asw_max()
	if asw_max == false then
		asw_max = ship:asw()
	end
	return {values = {value = Formatting:format_stat(asw_max)}, bg_color = self:get_bg_color(self._cell_color.asw, asw_max, args.asw_operator, args.asw_outstanding, args.asw_good)}
end

function EliteShipsKai:los(ship, args)
	local los_max = ship:los_max()
	if los_max == false then
		los_max = ship:los()
	end
	return {values = {value = Formatting:format_stat(los_max)}, bg_color = self:get_bg_color(self._cell_color.los, los_max, args.los_operator, args.los_outstanding, args.los_good)}
end

function EliteShipsKai:luck(ship, args)
	local luck = ship:luck()
	local luck_max = ship:luck_max()
	return {values = {luck = Formatting:format_stat(luck), luck_max = self:get_emphasis(luck_max, Formatting:format_stat(luck_max), args.luck_max_operator, args.luck_max_good)}, bg_color = self:get_bg_color(self._cell_color.luck, luck, args.luck_operator, args.luck_outstanding, args.luck_good)}
end

function EliteShipsKai:hp(ship, args)
	local hp = ship:hp()
	return {values = {value = Formatting:format_stat(hp)}, bg_color = self:get_bg_color(self._cell_color.hp, hp, args.hp_operator, args.hp_outstanding, args.hp_good)}
end

function EliteShipsKai:armor(ship, args)
	local armor_max = ship:armor_max()
	if armor_max == false then
		armor_max = ship:armor()
	end
	return {values = {value = Formatting:format_stat(armor_max)}, bg_color = self:get_bg_color(self._cell_color.armor, armor_max, args.armor_operator, args.armor_outstanding, args.armor_good)}
end

function EliteShipsKai:evasion(ship, args)
	local evasion_max = ship:evasion_max()
	if evasion_max == false then
		evasion_max = ship:evasion()
	end
	return {values = {value = Formatting:format_stat(evasion_max)}, bg_color = self:get_bg_color(self._cell_color.evasion, evasion_max, args.evasion_operator, args.evasion_outstanding, args.evasion_good)}
end

function EliteShipsKai:speed(ship, args)
	local speed = ship:speed()
	return {values = {value = Formatting:format_speed(speed)}, bg_color = self:get_bg_color(self._cell_color.speed, speed, args.speed_operator, args.speed_outstanding, args.speed_good)}
end

function EliteShipsKai:aircraft(ship, args)
	local aircraft = ship:total_space()
	local slots = {}
	if aircraft > 0 then
		for i = 1, ship:slots() do
			_, size = ship:slot(i)
			table.insert(slots, self:get_emphasis(size, Formatting:format_stat(size), args.aircraft_slot_operator, args.aircraft_slot_good))
		end
	end
	return {values = {value = Formatting:format_detailed_aircraft(aircraft, slots)}, bg_color = self:get_bg_color(self._cell_color.aircraft, aircraft, args.aircraft_operator, args.aircraft_outstanding, args.aircraft_good)}
end

function EliteShipsKai:fuel(ship, args)
	local fuel = ship:fuel()
	return {values = {value = Formatting:format_stat(fuel)}, bg_color = self:get_bg_color(self._cell_color.fuel, fuel, args.fuel_operator, args.fuel_outstanding, args.fuel_good)}
end

function EliteShipsKai:ammo(ship, args)
	local ammo = ship:ammo()
	return {values = {value = Formatting:format_stat(ammo)}, bg_color = self:get_bg_color(self._cell_color.ammo, ammo, args.ammo_operator, args.ammo_outstanding, args.ammo_good)}
end

function EliteShipsKai:notes(ship, args)
	local note = args.notes[ship]
	if note then
		local bg_color
		if note.highlight == "outstanding" then
			bg_color = self._outstanding_stat_color
		elseif note.highlight == "good" then
			bg_color = self._good_stat_color
		else
			bg_color = self._transparent
		end
		return {values = {value = note.note or ""}, bg_color = bg_color}
	end
	return {values = {value = ""}, bg_color = self._transparent}
end

function EliteShipsKai:compare_values(val1, val2)
	if #val1 == #val2 then
		for value_type, value in pairs(val1) do
			if value ~= val2[value_type] then
				return false
			end
		end
	else
		return false
	end
	return true
end

function EliteShipsKai:Table(args)
	--Process requested ships
	local ships = {}
	args.notes = {}
	for index, ship_name in ipairs(args) do
		if ship_name == "-" then
			ships[index] = "break"
		elseif ship_name == "#" then
			ships[index] = "header"
		else
			local split = mw.ustring.find(ship_name, '/')
			local ship_base_name, ship_suffix
			if split == nil then
				ship_base_name = ship_name
			else
				ship_base_name = mw.ustring.sub(ship_name, 1, split - 1)
				ship_suffix = mw.ustring.sub(ship_name, split + 1, -1)
			end
			local ship = Ship:create(ship_base_name, ship_suffix)
			ships[index] = ship
			local note = args[ship_name .. self._notes_suffix]
			if note then
				note = {note = note}
				local note_highlight = args[ship_name .. self._notes_suffix .. self._highlight_suffix]
				if note_highlight then
					note.highlight = note_highlight
				end
				args.notes[ship] = note
			end
		end
	end

	local header_icons = {
		remodel = MiscIcons.remodel, 
		firepower = StatIcons.firepower, 
		torpedo = StatIcons.torpedo, 
		aa = StatIcons.aa, 
		asw = StatIcons.asw, 
		los = StatIcons.los, 
		luck = StatIcons.luck, 
		hp = StatIcons.hp, 
		armor = StatIcons.armor,
		evasion = StatIcons.evasion,
		speed = StatIcons.speed,
		aircraft = StatIcons.aircraft,
		fuel = ResourceIcons.fuel,
		ammo = ResourceIcons.ammo,
	}
	for key, value in pairs(header_icons) do
		header_icons[key] = Formatting:format_image{value, caption = Formatting:format_stat_name(key)}
	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
		if type(ship) == "string" then
			row_values = ship
		else
			row_values = {}
			for _, column in ipairs(self._columns) do
				row_values[column] = self[column](self, ship, args)
			end
			if index > 1 then
				for _, column in ipairs(self._columns) do
					for i=index-1,1,-1 do
						local previous_row = data_rows[i]
						local previous_row_type = type(previous_row) ~= "string"
						local previous_cell = previous_row_type and previous_row[column] or false
						if previous_cell then
							if self:compare_values(row_values[column].values, previous_cell.values) then
								previous_cell.rowspan = previous_cell.rowspan + 1
								row_values[column] = nil
							else
								row_values[column].rowspan = 1
							end
							break
						elseif not previous_row_type then
							row_values[column].rowspan = 1
							break
						end
					end
				end
			else
				for _, column in ipairs(self._columns) do
					row_values[column].rowspan = 1
				end
			end
		end

		table.insert(data_rows, row_values)
	end

	for index, row_values in ipairs(data_rows) do
		if row_values ~= "break" then
			table.insert(rows, self._row_starter)
			if row_values == "header" then
				table.insert(rows, header)
			else
				for _, column in ipairs(self._columns) do
					if row_values[column] then
						table.insert(rows, format(self._column_cell_templates[column] or self._cell, row_values[column]))
					end
				end
			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