From Dark and Darker Wiki

Revision as of 04:45, 29 January 2026 by Sur (talk | contribs) (data load moved to global scope)

Overview

Functions for making Monsters table. Data comes from Data:Monster.json.

Functions

draw_table

Create's a table of monsters

Parameters

  • 1 - list key of monsters to use
  • 2 - is the list key a race?

draw_table Examples

{{#invoke:Monster|draw_table|bosses}}

Lua error at line 321: attempt to index field 'monsters' (a nil value).



{{#invoke:Monster|draw_table|Demon|true}}

Lua error at line 321: attempt to index field 'monsters' (a nil value).


local p = {}
local monster_data = mw.loadJsonData("Data:Monster.json")
local suppressed_color = "#1A91C4"
local neutral_color = "#EEE8"

---Check existence and equality of elite and nightmare variants to common
---@param common any
---@param elite any
---@param nightmare any
---@return boolean
local function is_suppressed(common, elite, nightmare)
	if common and common == elite then
		if nightmare then
			if elite == nightmare then
				return true
			end
		else
			return true
		end
	end
	return false
end


--- Create css/html wikitext for iconbox
--- @param name string
--- @return string
local function draw_iconbox(name)
	return "<div class='iconbox'><div class='rarity-1 rounded relative'>[[File:"..name..".png|x80px|link="..name.."]]</div><br>[["..name.."|<b class=rarity-1>"..name.."</b>]]</div>"
end


--- Create color coded string for common/elite/nightmare variants
--- @param common any
--- @param elite any
--- @param nightmare any
--- @param unit any
--- @return string
local function variant_color_coding(common, elite, nightmare, unit)
	if common == nil  or common == "" then return "" end

	unit = unit or ""
	local output_str = ""
	if is_suppressed(common, elite, nightmare) then
		output_str = "<span style='color:"..suppressed_color.."'>"..tostring(common)..unit.."</span>"	
	else
		if common then
			output_str = "<span>"..tostring(common)..unit.."</span>"
		end
		if elite then
			if output_str ~= "" then output_str = output_str.."/" end
			output_str = output_str.."<span class='colorrarityElite'>"..tostring(elite)..unit.."</span>"
		end
		if nightmare then
			if output_str ~= "" then output_str = output_str.."/" end
			output_str = output_str.."<span class='colorrarityNightmare'>"..tostring(nightmare)..unit.."</span>"
		end
	end

	return output_str
end


--- Create tooltip wikitext
--- @param color string
--- @param display_text string
--- @param tooltip_text string
--- @return string
local function tooltip(color, display_text, tooltip_text)
	if color == "" or color == nil then color = " style='color:"..color.."'" end
	return "<span class='tooltip'"..color.."><u>["..tostring(display_text).."]</u><span class='tooltiptext-right'>"..tooltip_text.."</span></span>"
end


---Create color coded string of damage reductions
---@param common table
---@param elite table
---@param nightmare table
---@return string
local function damage_reductions(common, elite, nightmare)
	local output_str = ""
	for k, v in pairs(common.damageReductions) do
		if output_str ~= "" then output_str = output_str.."<br>" end

		output_str = output_str.."<span style='color:"..neutral_color.."'>"..k:gsub(" Damage Reduction",""):gsub(" Magical",""):gsub(" Physical","")..": </span>"

		if tostring(v):find("*") then 
			local color = ""
			if is_suppressed(v, elite.damageReductions and elite.damageReductions[k], nightmare.damageReductions and nightmare.damageReductions[k]) then
				color = " style='color:"..suppressed_color.."'"
			end
			output_str = output_str..tooltip(
				color,
				variant_color_coding(
					tostring(v):match("^(.*)%*"),
					tostring(elite.damageReductions and elite.damageReductions[k]):match("^(.*)%*"),
					tostring(nightmare.damageReductions and nightmare.damageReductions[k]):match("^(.*)%*"),"%"
				),
				tostring(v):match("%*(.*)$") -- assume policy text is the same across variants
			)
		else
			output_str = output_str
				..variant_color_coding(
					v,
					elite.damageReductions and elite.damageReductions[k],
					nightmare.damageReductions and nightmare.damageReductions[k],"%")
		end
	end
	return output_str
end


local function string_to_list(string)
	local output_list = {}
	for token in string:gmatch("([^,]+)") do
		output_list[#output_list+1] = token:gsub("^%s*(.-)%s*$", "%1")
	end
	return output_list
end


local function policy_tooltips(MD, string)
	if string == "" or string == nil then return "" end

	local output_str = ""
	for _,policy in pairs(string_to_list(string)) do
		output_str = output_str..tooltip(
			"",
			MD.policies[policy],
			policy
		)
	end
	return output_str
end


--TODO
local function statuses(MD, common, elite, nightmare)
	local output_str = ""

	local highest_variant = common
	if next(nightmare) ~= nil then highest_variant = nightmare
	elseif next(elite) ~= nil then highest_variant = elite end

	for k,v in pairs(highest_variant.statuses) do
		if output_str ~= "" then output_str = output_str.."<br>" end
		local status_effect = v:match("^(.*)%*") or v
		local policy_text = policy_tooltips(MD, v:match("%*(.*)$"))
		output_str = output_str.."<span style='color:"..neutral_color.."'>"..k..":&nbsp;</span>"..status_effect.." "..policy_text
	end
	return output_str
end


---Check for consistency across variants and format string of an ability's effects
---@param common table
---@param elite table
---@param nightmare table
---@return string
local function process_effect(common, elite, nightmare)
	-- check for consistency across variants
	if next(elite) and (common.impact ~= elite.impact) or (next(nightmare) and (common.impact ~= nightmare.impact)) then
		return "<span color='red'>Error: Impact power differs across variants.</span>"
	end

	local impact_string = ""
	if is_suppressed(common.impact, elite.impact, nightmare.impact) then
		impact_string = "<span style='color:"..suppressed_color.."'> ("..tostring(common.impact)..") </span> "
	elseif common.impact then
		impact_string =  " ("..tostring(common.impact)..") "
	end

	-- get values for each variant
	return variant_color_coding(common.value, elite.value, nightmare.value)
		..impact_string
		..variant_color_coding(common.text, elite.text, nightmare.text)
end


-- Assumptions:
---- If the variants exists, lower variants have a subset of higher variant ability set.
---- An ability, if present, has identical names across variants. Strings associated with an Ability can vary. Order of abilities matters.
---- Important ability values that vary by variant type are followed by the "#" token.
---- Statuses applied by an ability are preceded by the "$" token.
---- Impact Power of an ability is invariant.
--- @param name string
--- @param common table
--- @param elite table
--- @param nightmare table
--- @param highest_variant table
--- @return string
local function process_ability(name, common, elite, nightmare, highest_variant)
	local output_str = ""

	if highest_variant.abilities[name].effects then
		for k,_ in pairs(highest_variant.abilities[name].effects) do
			if output_str ~= "" then output_str = output_str..",&nbsp;" end
			output_str = output_str..process_effect(
				common.abilities[name].effects[k],
				elite.abilities and elite.abilities[name] and elite.abilities[name].effects[k] or {},
				nightmare.abilities and nightmare.abilities[name] and nightmare.abilities[name].effects[k] or {})
		end
	end

	if not highest_variant.abilities[name].status_effects then
		return output_str
	end

	if output_str ~= "" then output_str = output_str.."&nbsp;+&nbsp;" end
	if highest_variant.abilities[name].status_effects then
		for k,v in pairs(highest_variant.abilities[name].status_effects) do
			if output_str ~= "" and k ~= 1 then output_str = output_str..",&nbsp;" end
			output_str = output_str..variant_color_coding(
				common.abilities[name].status_effects[k].name,
				elite.abilities and elite.abilities[name] and elite.abilities[name].status_effects[k].name,
				nightmare.abilities and nightmare.abilities[name] and nightmare.abilities[name].status_effects[k].name)
		end
	end

	return output_str
end

---Create color coded string of abilities
---@param common table
---@param elite table
---@param nightmare table
---@return string
local function abilities(common, elite, nightmare)
	local highest_variant = common
	if next(nightmare) ~= nil then highest_variant = nightmare
	elseif next(elite) ~= nil then highest_variant = elite end

	local output_str = ""

	for k, v in pairs(highest_variant.abilities) do
		if output_str ~= "" then output_str = output_str.."<br>" end
		output_str = output_str.."<span style='color:"..neutral_color.."'>"..k..":&nbsp;</span>"
			..process_ability(k, common, elite, nightmare, highest_variant)
	end

	return output_str
end


---Hyphen seperated list of hyperlinked races.  Assumes all variants same races.
---@param monster table
---@return string
local function races(monster)
	local output_str = ""
	for k,v in pairs(monster.races) do
		if output_str ~= "" then output_str = output_str.."-" end
		output_str = output_str.."[["..k.."]]"
	end

	return output_str
end


---@return table
---@return table
local function get_monster_variants(MD, monster)
	local elite_string = MD.monsters.monster_localized_names[monster.name].Elite
	local nightmare_string = MD.monsters.monster_localized_names[monster.name].Nightmare
	return MD.monsters.monsters_ids[elite_string] or {}, MD.monsters.monsters_ids[nightmare_string] or {}
end


local function draw_row(MD, monster)
	local monster_elite, monster_nightmare = get_monster_variants(MD, monster)

	return "<tr><td>"
		..draw_iconbox(monster.name).."</td><td>"
		..variant_color_coding(monster.hp, monster_elite.hp, monster_nightmare.hp).."</td><td>"
		..variant_color_coding(monster.move, monster_elite.move, monster_nightmare.move).."</td><td>"
			.."<div style='margin:auto; height:50%;'>"..variant_color_coding(monster.xp, monster_elite.xp, monster_nightmare.xp)
			.."</div><div style='margin:auto; height:50%;'>"..variant_color_coding(monster.ap, monster_elite.ap, monster_nightmare.ap).."</td><td>"
		..damage_reductions(monster, monster_elite, monster_nightmare).."</td><td>"
		..abilities(monster, monster_elite, monster_nightmare).."</td><td>"
		..statuses(MD, monster, monster_elite, monster_nightmare).."</td><td>"
		..races(monster).."</td><td>"
		..variant_color_coding(monster.actionspeed, monster_elite.actionspeed, monster_nightmare.actionspeed,"%").."</td></tr>"
end


--- @param frame table: frame args
---- keys.args[1] string: {"bosses", "minibosses", "mimics", "normal"} or a race list like {"Demon", "Undead", ...
---- keys.args[2] bool or string: determines if the first key is a race list
--- @return table: ordered list of monster ids
local function get_monster_list(MD, frame)
	local key = frame.args[1] ~= nil and frame.args[1] or "all"
	local is_race_list = frame.args[2] ~= nil and frame.args[2]

	if is_race_list then
		return MD.races[key]
	else
		return MD[key]
	end
end

--Create monster table wikitext
----Test on wiki with: mw.log(p.draw_table({args={"bosses"}}))
---@param frame table
---@return string
function p.draw_table(frame)
	local MD = monster_data
	if not MD then return "<span style='color:red;'>Error: Could not load Monster data in [[Module:Monster Table]]</span>" end

	local output_str = [===[<table cellspacing="0" class="wikitable sortable jquery-tablesorter" style="width:100%;color:#eee; background:transparent; text-align:center; vertical-align:middle;"><tr>
<th style="width:3%">Name</th>
<th style="width:3%">Health</th>
<th style="width:3%">Move Speed</th>
<th style="width:3%">Exp<br>AP</th>
<th style="width:3%">Damage Reductions</th>
<th style="width:17%">Abilities' Damage([[Impact Power]]) + Statuses</th>
<th style="width:22%">[[Statuses]]' Effects</th>
<th style="width:3%">Race</th>	
<th style="width:3%">Action&nbsp;Speed</th></tr>]===]

	local monster_list = get_monster_list(MD, frame)
	for _, monster_key in pairs(monster_list) do
		local monster = MD.monsters.monsters_ids[monster_key]
		output_str = output_str..draw_row(MD, monster)
	end

	return output_str.."</table>"
end


return p