Module:Damage: Difference between revisions

From Space Station 14 Wiki
Aliser (talk | contribs)
No edit summary
Aliser (talk | contribs)
No edit summary
Line 32: Line 32:
local damage_type = args[1]
local damage_type = args[1]
assert_not_nil(damage_type, "failed to generate damage type: damage type not provided")
assert_not_nil(damage_type, "failed to generate damage type: damage type not provided")
local amount = args[2]


-- ==================
-- ==================
Line 48: Line 50:
end
end


local el = mw.html.create('span')
local label_wrapper_el = mw.html.create('span')
:addClass("damage-type")
:addClass("damage-type")
local label_el = mw.html.create('span')
:addClass("label")
:wikitext("<b>" .. display .. "</b>")
:wikitext("<b>" .. display .. "</b>")
local amount_el = mw.html.create('span')
:addClass("amount")
:wikitext(amount and amount .. " " or "")
label_wrapper_el:node(amount_el)
label_wrapper_el:node(label_el)


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


if outline_color then
if outline_color then
el:css("--shadow-col", outline_color);
label_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)");
label_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


return el:allDone()
return label_wrapper_el:allDone()
end
end


return p
return p

Revision as of 12:25, 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 amount = args[2]

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

	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 label_wrapper_el = mw.html.create('span')
		:addClass("damage-type")

	local label_el = mw.html.create('span')
		:addClass("label")
		:wikitext("<b>" .. display .. "</b>")

	local amount_el = mw.html.create('span')
		:addClass("amount")
		:wikitext(amount and amount .. " " or "")

	label_wrapper_el:node(amount_el)
	label_wrapper_el:node(label_el)

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

	if outline_color then
		label_el:css("--shadow-col", outline_color);
		label_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 label_wrapper_el:allDone()
end

return p