Module:ShipModernizationListKai

Revision as of 22:26, 27 April 2015 by com>Ckwng (orz)

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

local BaseTable = require('Module:BaseTable')
local Formatting = require('Module:Formatting')
local StatIcons = require('Module:StatIcons')
local Ship = require('Module:Ship')

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

local ShipModernizationListKai = BaseTable{
	_header_template = [[! style="width: 75px;" | Class
! style="width: 100px;" | Name
! 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: #F2E279;" | ${armor}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: palegreen;" | ${luck}
! style="width: 100px;" | Name
! 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: #F2E279;" | ${armor}
! style="width: 30px; text-align: center; vertical-align: middle; background-color: palegreen;" | ${luck}]],
	_columns = {
		"class",
		"name1",
		"firepower_mod1",
		"torpedo_mod1",
		"aa_mod1",
		"armor_mod1",
		"luck_mod1",
		"name2",
		"firepower_mod2",
		"torpedo_mod2",
		"aa_mod2",
		"armor_mod2",
		"luck_mod2",
	},
	_empty_cell = [[| style="text-align: center; vertical-align: middle;" | -]],
	_column_empty_cells = {
		arrow = [[| style="text-align: center; vertical-align: middle;" | ⇒]],
	},
	_item_class = Ship,
	_dash = "-",
	_question_marks = "??",
	_column_bg_colors = {
		firepower = "lightcoral",
		torpedo = "lightskyblue",
		aa = "sandybrown",
		armor = "#F2E279",
		luck = "palegreen",
	},
}

function ShipModernizationListKai:class(pair)
	self._left = pair[1]
	self._right = pair[2]
	self._left_modernization = self._left and self._left:modernization() or false
	self._right_modernization = self._right and self._right:modernization() or false
	local class = (pair[1] or pair[2]):class()
	return {values = {value = class and class:base_name() or self._question_marks}, bg_color = self._transparent, text_align = self._center_align}
end

function ShipModernizationListKai:name1()
	if self._left then
		return self:name(self._left)
	else
		return {values = {value = self._dash}, bg_color = self._transparent, text_align = self._start_align}
	end
end

function ShipModernizationListKai:name2()
	if self._right then
		return self:name(self._right)
	else
		return {values = {value = self._dash}, bg_color = self._transparent, text_align = self._start_align}
	end
end

function ShipModernizationListKai:firepower_mod1()
	if self._left then
		return self:firepower_mod(self._left_modernization)
	else
		return {values = {value = self._dash}, bg_color = self._column_bg_colors.firepower, text_align = self._start_align}
	end
end

function ShipModernizationListKai:firepower_mod2()
	if self._right then
		return self:firepower_mod(self._right_modernization)
	else
		return {values = {value = self._dash}, bg_color = self._column_bg_colors.firepower, text_align = self._start_align}
	end
end

function ShipModernizationListKai:torpedo_mod1()
	if self._left then
		return self:torpedo_mod(self._left_modernization)
	else
		return {values = {value = self._dash}, bg_color = self._column_bg_colors.torpedo, text_align = self._start_align}
	end
end

function ShipModernizationListKai:torpedo_mod2()
	if self._right then
		return self:torpedo_mod(self._right_modernization)
	else
		return {values = {value = self._dash}, bg_color = self._column_bg_colors.torpedo, text_align = self._start_align}
	end
end

function ShipModernizationListKai:aa_mod1()
	if self._left then
		return self:aa_mod(self._left_modernization)
	else
		return {values = {value = self._dash}, bg_color = self._column_bg_colors.aa, text_align = self._start_align}
	end
end

function ShipModernizationListKai:aa_mod2()
	if self._right then
		return self:aa_mod(self._right_modernization)
	else
		return {values = {value = self._dash}, bg_color = self._column_bg_colors.aa, text_align = self._start_align}
	end
end

function ShipModernizationListKai:armor_mod1()
	if self._left then
		return self:armor_mod(self._left_modernization)
	else
		return {values = {value = self._dash}, bg_color = self._column_bg_colors.armor, text_align = self._start_align}
	end
end

function ShipModernizationListKai:armor_mod2()
	if self._right then
		return self:armor_mod(self._right_modernization)
	else
		return {values = {value = self._dash}, bg_color = self._column_bg_colors.armor, text_align = self._start_align}
	end
end

function ShipModernizationListKai:luck_mod1()
	if self._left then
		return self:luck_mod(self._left_modernization)
	else
		return {values = {value = self._dash}, bg_color = self._column_bg_colors.luck, text_align = self._start_align}
	end
end

function ShipModernizationListKai:luck_mod2()
	if self._right then
		return self:luck_mod(self._right_modernization)
	else
		return {values = {value = self._dash}, bg_color = self._column_bg_colors.luck, text_align = self._start_align}
	end
end

function ShipModernizationListKai:name(ship)
	local link, text, section = ship:link()
	local value = {Formatting:format_link(link, text, section)}
	local japanese_name = ship:japanese_name()
	if japanese_name and (text or link) ~= japanese_name then
		table.insert(value, Formatting:format_stat(ship:japanese_name()))
	end
	return {values = {value = table.concat(value, " ")}, bg_color = self._transparent, text_align = self._start_align}
end

function ShipModernizationListKai:firepower_mod(modernization)
	return {values = {value = Formatting:format_bonus(modernization.firepower, nil, self._dash)}, bg_color = self._column_bg_colors.firepower, text_align = self._center_align}
end

function ShipModernizationListKai:torpedo_mod(modernization)
	return {values = {value = Formatting:format_bonus(modernization.torpedo, nil, self._dash)}, bg_color = self._column_bg_colors.torpedo, text_align = self._center_align}
end

function ShipModernizationListKai:aa_mod(modernization)
	return {values = {value = Formatting:format_bonus(modernization.aa, nil, self._dash)}, bg_color = self._column_bg_colors.aa, text_align = self._center_align}
end

function ShipModernizationListKai:armor_mod(modernization)
	return {values = {value = Formatting:format_bonus(modernization.armor, nil, self._dash)}, bg_color = self._column_bg_colors.armor, text_align = self._center_align}
end

function ShipModernizationListKai:luck_mod(modernization)
	return {values = {value = Formatting:format_bonus(modernization.luck, nil, self._dash, true)}, bg_color = self._column_bg_colors.luck, text_align = self._center_align}
end

ShipModernizationListKai.create_data_rows = ShipModernizationListKai.create_data_rows_merge_vertical

function ShipModernizationListKai:create_items_post()
	local items = {}
	local left = false
	local right = false
	local even = false
	for _, item in ipairs(self._items) do
		if type(item) == "string" then
			if item == "empty" then
				if even then
					right = false
					table.insert(items, {left, right})
					left = false
					right = false
					even = false
				else
					left = false
					even = true
				end
			else
				if even then
					table.insert(items, {left, right})
					left = false
					even = false
				end
				table.insert(items, item)
			end
		elseif even then
			right = item
			table.insert(items, {left, right})
			left = false
			right = false
			even = false
		else
			left = item
			even = true
		end
	end
	if even then
		table.insert(items, {left, right})
	end
	self._items = items
end

function ShipModernizationListKai:create_header()
	local header_icons = {
		firepower = StatIcons.firepower,
		torpedo = StatIcons.torpedo,
		aa = StatIcons.aa,
		armor = StatIcons.armor,
		luck = StatIcons.luck,
	}
	for key, value in pairs(header_icons) do
		header_icons[key] = Formatting:format_image{value, caption = Formatting:format_stat_name(key)}
	end
	self._header = format(self._header_template, header_icons)
end

return ShipModernizationListKai