Module:Damage: Difference between revisions

From Space Station 14 Wiki
Aliser (talk | contribs)
No edit summary
Aliser (talk | contribs)
No edit summary
Line 42: Line 42:
local display = damage_type_lookup.display
local display = damage_type_lookup.display
local color = damage_type_lookup.color
local color = damage_type_lookup.color
local outline_color = damage_type_lookup["outline color"]


if not display then
if not display then
Line 53: Line 54:
if color then
if color then
el:css("color", color);
el:css("color", color);
end
if outline_color then
el:css("--shadow-col", outline_color);
el:css("text-shadow", "-1px -1px 0 var(--shadow-col), 1px -1px 0 var(--shadow-col), -1px 1px 0 var(--shadow-col), 1px 1px 0 var(--shadow-col)");
end
end



Revision as of 12:11, 26 March 2025

Module documentation
View or edit this documentation (about module documentation)
Uses JSON data

Implements {{Damage}}.


local p = {}
local getArgs = require('Module:Arguments').getArgs

local damage_types_json = mw.loadJsonData("Module:Damage/damage types.json")

local function assert_not_nil(value, error_message)
    if value == nil then
        if error_message == nil then
            error("value is nil")
        else
            error(error_message)
        end
    end
end

-- =========================

-- Lookups damage type from the json. If a damage type is defined, returns the object defining it, or `nil` otherwise.
local function lookup_damage_type(damage_type)
	local result = damage_types_json[string.lower(damage_type)]
	if type(result) == "string" then
		-- alias. lookup actual definition.
		return damage_types_json[result]
	else
		return result
	end
end

-- Generates a term element with a tooltip and/or a link.
function p.main(frame)
	local args = getArgs(frame)
	local damage_type = args[1]
	assert_not_nil(damage_type, "failed to generate damage type: damage type not provided")

	-- ==================

	local damage_type_lookup = lookup_damage_type(damage_type);
	if not damage_type_lookup then
		error("failed to generate a damage type: unknown damage type '" .. damage_type .. "'")
	end

	local display = damage_type_lookup.display
	local color = damage_type_lookup.color
	local outline_color = damage_type_lookup["outline color"]

	if not display then
		error("failed to generate a damage type: damage type is defined, but it does not have a 'display' property")
	end

	local el = mw.html.create('span')
		:addClass("damage-type")
		:wikitext("<b>" .. display .. "</b>")

	if color then
		el:css("color", color);
	end

	if outline_color then
		el:css("--shadow-col", outline_color);
		el:css("text-shadow", "-1px -1px 0 var(--shadow-col), 1px -1px 0 var(--shadow-col), -1px 1px 0 var(--shadow-col), 1px 1px 0 var(--shadow-col)");
	end

	return el:allDone()
end

return p