From Dark and Darker Wiki

Overview

Functions for making Utility tables. Data comes from Data:Utility.json.

Functions

draw_table

Creates a table of utilities

Parameters

  • 1 - <Type>


draw_table examples

Drink


{{#invoke:Utility|draw_table|Drink}}


NameMovement Speed
-10
-10
-10
-10
-10
-10
-10
-10
-10
-10
-10
-10

Code


local p = {}
local UD = mw.loadJsonData("Data:Utility.json")
local concat = table.concat

---Get the item's size in pixels
local function size(item)
	return tostring(item.invwidth*45).."x"..tostring(item.invheight*45).."px"
end

local function count(t)
	local c = 0
	for _ in pairs(t) do c = c + 1 end
	return c
end

---Marks up a list of values with color based on rarity
local function color_values(utility, values)
	if values == nil then return "" end
	if type(values)=="string" or type(values)=="number" then
		if count(utility.rarities) == 1 then return "<span class='cr"..utility.rarities[1].."'>"..values.."</span>" end
		return tostring(values)
	end

	local wt = ""
	local newline = ""
	for i, key in ipairs(utility.rarities) do
		wt = wt..newline.."<span class='cr"..key.."'>"..tostring(values[tonumber(key)] or "").."</span>"
		if i == 1 then newline = "<br>" end
	end
	return wt
end

---Returns a predetermined utility list
local function get_list(frame)
	if not (frame.args and frame.args[1]) then return {} end
	if not (UD[frame.args[1]] and UD[frame.args[1]]) then return {} end

	return UD[frame.args[1]]
end

---Formats a table cell for the iconbox
local function name(item, is_header)
	if is_header then return [[<th style="width:5%">Name</th>]] end

	local color = "2"
	local bold_link = "|<b>"..item.name
	if count(item.rarities) == 1 then
		color = item.rarities[1]
		bold_link = "|<b class=cr"..color..">"..item.name
	end

	local icon_amount = ""
	if item.maxammocount > 0 then
		icon_amount = "<span class='iconamount' style='pointer-events:none;color:#EEEA;font-size:16px'>"..item.maxammocount.."</span>"
	end

	return "<td><div class='iconbox' style='width:max-content;align-items:center'><div class='rarity"..color.." rounded relative'>[[File:"..item.name..".png|"..size(item).."|link="..item.name.."]]"
				..icon_amount
			.."</div><br>[["..item.name..bold_link.."</b>]]</div></td>"
end

---Formats a table cell of move speeds
local function move_speed(utility,is_header)
	if is_header then return [[<th style="width:5%">Movement Speed</th>]] end

	return "<td>"..color_values(utility,utility.stats["move speed"]).."</td>"
end

local function determine_table_type(utility)
	if utility.args then
		return utility.args[1]
	elseif utility.utilitytype then
		return utility.utilitytype
	end
end

---Determines which table data goes into the table row
local row_type = {
	["Drink"] = {name,move_speed}, --,effect,action_time,action_move_speed_penalty
	["Default"] = {name,move_speed,effect,action_time,action_move_speed_penalty}
}

---Return a table row of data cells containing utility information,
local function row(utility, is_header)
	is_header = is_header or false
	local wt = {}
	wt[#wt+1] = "<tr>"
	for _,content in ipairs(row_type[determine_table_type(utility)]) do
		wt[#wt+1] = content(utility,is_header)
	end
	wt[#wt+1] = "</tr>"

	return concat(wt)
end

function p.draw_table(frame)
	local wt = {}
	local item_list = get_list(frame)
	if count(item_list) == 0 then return "<span style='font-size:24pt'>There are currently no items of this type.</span>" end

	wt[#wt+1] = "<table cellspacing='0' class='wikitable sortable jquery-tablesorter' style='text-align:center; vertical-align:middle;'>"
	wt[#wt+1] = row(frame, true)
	for _,item_name in ipairs(item_list) do
		wt[#wt+1] = row(UD.Utility[item_name])
	end
	wt[#wt+1] = "</table>"

	return concat(wt)
end

return p


-- Tests
-- mw.log(p.draw_table({args={"Drink"}}))