Module:HeroStats2/Infobox

From Darkest Dungeon Wiki
Jump to navigation Jump to search
Template-info.svg Documentation

This module renders infoboxes for Heroes in Darkest Dungeon II.

There are two API functions, render and query, both with Lua and Wikitext variants. The primary point of access is the _query function; other functions mostly exist for testing purposes.

render and _render

Renders the given hero data into an infobox. In addition to all the hero attributes (see Template:HeroStats2/Add), it also accepts the following parameters:

  • (stat)_cdf: numeric, for each stat. E.g. hp_cdf or upgraded_bleed_cdf. This (optional) value determines the colour of the displayed stat.
  • _pageName: string. This is used to generate an edit link for entries that are loaded from Cargo. (Click on the small "+" button to view it.)

default_name and permanent_conditions are both optional.

Examples

Test Custom Hero
Spearman.png
Default Name
Test Default Name
Stats
BaseUpgraded
HP HP11
Speed Speed34
Resistances
Bleed Bleed5%6%
Blight Blight7%8%
Burn Burn9%9%
Disease Disease17%18%
Stun Stun11%12%
Move Move13%14%
Debuff Debuff15%15%
Deathblow Deathblow19%19%
LowMediumHighUpgraded
Movement
21 forward, 22 backward
Permanent Condition(s)
On Combat Start:
  • StealthBlock+
  • This is a test.
No Optionals
Swordman.png
Stats
HP HP1
Speed Speed3
Resistances
Bleed Bleed5%
Blight Blight7%
Burn Burn9%
Disease Disease17%
Stun Stun11%
Move Move13%
Debuff Debuff15%
Deathblow Deathblow19%
LowMediumHigh

query and _query

As above, but fetches the data from Cargo instead of using arguments.

Examples

Bounty Hunter
Bh 2.png
Stats
HP HP48
Speed Speed3
Resistances
Bleed Bleed30%
Blight Blight30%
Burn Burn30%
Disease Disease40%
Stun Stun40%
Move Move30%
Debuff Debuff40%
Deathblow Deathblow75%
LowMediumHigh
Movement
2 forward, 2 backward
Permanent Condition(s)
When Target Combo:
  • Reapply Combo (50%)
Grave Robber
Gr wiki.png
Default Name
Audrey
Stats
BaseUpgraded
HP HP2929
Speed Speed57
Resistances
Bleed Bleed30%30%
Blight Blight30%40%
Burn Burn30%30%
Disease Disease30%30%
Stun Stun20%20%
Move Move20%20%
Debuff Debuff30%30%
Deathblow Deathblow60%75%
LowMediumHighUpgraded
Movement
2 forward, 2 backward
Leper
Lep wiki2.png
Default Name
Baldwin
Stats
BaseUpgraded
HP HP4452
Speed Speed22
Resistances
Bleed Bleed10%10%
Blight Blight10%10%
Burn Burn20%20%
Disease Disease30%30%
Stun Stun30%30%
Move Move20%30%
Debuff Debuff20%20%
Deathblow Deathblow60%75%
LowMediumHighUpgraded
Movement
1 forward
Permanent Condition(s)
Combat Start:
Add Blind (50%)
Add Blind (25%)

local Cargo = mw.ext.cargo

local Histogram = require("Module:Histogram")
local Utils = require("Module:HeroStats2/Utils")

local UPGRADE_LINK = "[[Altar of Hope/The Living City#%s|Upgraded]]"
local COMPARE_LINK = "[[Heroes (Darkest Dungeon II)#Stats|[Compare Stats]]]"
local EDIT_LINK = "[[%s|[View/Edit Data]]]"

local function _render(hero)
	local has_upgrade = false
	for _, stat in ipairs(Utils.BASE_STATS) do
		if hero[stat.name] ~= hero[stat.upgraded.name] then
			has_upgrade = true
		end
	end

	local colspan = has_upgrade and 3 or 2

	local infobox = mw.html.create("table")
		:addClass("char-box-2")

	infobox:tag("tr"):tag("th"):attr("colspan", colspan)
		:wikitext(hero.name)
	infobox:tag("tr"):tag("td"):attr("colspan", colspan)
		:addClass("ctr")
		:wikitext(("[[File:%s|250px]]"):format(hero.image))

	if hero.default_name then
		infobox:tag("tr"):tag("th"):attr("colspan", colspan)
			:wikitext("Default Name")
		infobox:tag("tr"):tag("td"):attr("colspan", colspan)
			:addClass("ctr")
			:wikitext(("''%s''"):format(hero.default_name))
	end

	local stat_links = {COMPARE_LINK}
	if hero._pageName ~= nil then
		table.insert(stat_links, EDIT_LINK:format(hero._pageName))
	end

	infobox:tag("tr"):tag("th"):attr("colspan", colspan)
		:addClass("mw-collapsible")
		:addClass("mw-collapsed")
		:attr("data-expandtext", "+")
		:attr("data-collapsetext", "-")
		:wikitext("Stats")
		:tag("div")
		:addClass("mw-collapsible-content")
		:tag("small")
		:wikitext(table.concat(stat_links, " "))

	if has_upgrade then
		local stats_header = infobox:tag("tr")
		stats_header:tag("td")
			:addClass("char-stat-label-2")
		stats_header:tag("td")
			:addClass("char-stat-label-2")
			:wikitext("Base")
		stats_header:tag("td")
			:addClass("char-stat-label-2")
			:wikitext(UPGRADE_LINK:format(hero.name))
	end

	for _, stat in ipairs(Utils.BASE_STATS) do
		local row = infobox:tag("tr")
		row:tag("td")
			:addClass("char-stat-label-2")
			:wikitext(stat:long_label())

		row:node(stat:create_cell(hero))
		if has_upgrade then
			local upgraded_cell = stat.upgraded:create_cell(hero)
			if hero[stat.name] ~= hero[stat.upgraded.name] then
				upgraded_cell:addClass("char-stat-upgraded-2")
			end
			row:node(upgraded_cell)
		end

		if stat.name == "speed" then
			infobox:tag("tr"):tag("th"):attr("colspan", colspan)
				:wikitext("Resistances")
		end
	end

	local color_daemon = Utils.STATS[1]
	local key = infobox:tag("tr"):tag("td")
		:attr("colspan", colspan)
		:addClass("char-stat-key-2")
	key:tag("span"):css("color", color_daemon:to_color(0))
		:wikitext("Low")
	key:wikitext(" ▪ ")
	key:tag("span"):css("color", color_daemon:to_color(0.5))
		:wikitext("Medium")
	key:wikitext(" ▪ ")
	key:tag("span"):css("color", color_daemon:to_color(1))
		:wikitext("High")
	if has_upgrade then
		key:wikitext(" ▪ ")
		key:tag("span"):addClass("char-stat-upgraded-2")
			:wikitext("Upgraded")
	end

	local movement = {}
	if hero.forward > 0 then
		table.insert(movement, hero.forward .. " forward")
	end
	if hero.backward > 0 then
		table.insert(movement, hero.backward .. " backward")
	end
	if #movement > 0 then
		infobox:tag("tr"):tag("th")
			:attr("colspan", colspan)
			:wikitext("Movement")
		infobox:tag("tr"):tag("td")
			:attr("colspan", colspan)
			:addClass("ctr")
			:wikitext(table.concat(movement, ", "))
	end

	if hero.permanent_conditions then
		local frame = mw.getCurrentFrame()
		infobox:tag("tr"):tag("th")
			:attr("colspan", colspan)
			:addClass("ctr")
			:wikitext("Permanent Condition(s)")
		infobox:tag("tr"):tag("td")
			:attr("colspan", colspan)
			:tag("div")
			:addClass("char-conditions-2")
			:wikitext(frame:preprocess(hero.permanent_conditions))
	end

	return infobox
end

local function _query(name)
	local fields = {
		"_pageName", -- for edit links
		"name",
		"image",
		"default_name",
		"forward",
		"backward",
		"permanent_conditions"
	}
	for _, stat in ipairs(Utils.STATS) do
		table.insert(fields, stat.name)
	end
	local data = Cargo.query(
		"HeroStats2",
		table.concat(fields, ","),
		{where=("name='%s'"):format(name)}
	)
	if #data == 0 then
		return ('Could not find data for hero "%s".'):format(name)
	end

	local hero = Utils._parse_hero(data[1])
	local hs = Histogram._load_table("HeroStats2Histograms")
	for _, stat in ipairs(Utils.STATS) do
		hero[stat.name .. "_cdf"] = Histogram._interpolate_cdf(
			hs[stat.name], hero[stat.name])
	end
	return _render(hero)
end

local function render(frame)
	return _render(Utils._parse_hero(frame.args))
end

local function query(frame)
	return _query(frame.args[1])
end

return {
	render=render,
	_render=_render,
	query=query,
	_query=_query
}