From Dark and Darker Wiki
Overview
Functions for making Armor table. Data comes from Data:Armor.json.
Functions
draw_table
Creates a table of armors
Parameters
- 1 - <Slot Type>
- 2 - Craftable or Uncraftable
draw_table examples
Back
{{#invoke:Armor|draw_table|Back|Craftable}}
Head
{{#invoke:Armor|draw_table|Head|Uncraftable}}
local p = {}
local AD = mw.loadJsonData("Data:Armor.json")
local is_special = {["Move Speed"]=true, ["Armor Rating"]=true, ["Magical Resistance"]=true}
local is_primitive = {["Strength"]=true, ["Vigor"]=true, ["Agility"]=true, ["Dexterity"]=true, ["Will"]=true, ["Knowledge"]=true, ["Resourcefulness"]=true}
-- local color_rarity = {"cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7", "cr8"}
local function get_type(item)
--Assumes that the item only has one type
local key,_ = next(item.types)
return key or ""
end
local function size(item)
return tostring(item.invwidth*45).."x"..tostring(item.invheight*45).."px"
end
---Create css/html wikitext for iconbox
---@param item table
---@return string
local function iconbox(item)
local wikitext = "<div class='iconbox' style='display:inline-flex;width:max-content;max-width:initial;flex-direction:column;align-items:center;flex-wrap:wrap;white-space:pre-wrap'>"
local image = "[[File:"..item.name..".png|"..size(item).."|link="..item.name.."]]"
local amount = "<span class='iconamount' style='pointer-events:none;color:#EEEA;font-size:16px'>"..get_type(item).."</span>"
local caption = "[["..item.name
local color = "2"
if #item.rarities == 1 then
color = item.rarities[1]
caption = caption.."|<b class=cr"..color..">"..item.name.."</b>"
end
image = "<div class='rarity-"..color.." rounded relative'>"..image..amount.."</div>"
return wikitext..image..caption.."]]</div>"
end
local function color_values(values)
if type(values)=="string" then return values end
--TODO this assumes i is the same as the color rating, this may result in a bug
local wikitext = ""
local newline = ""
for i, value in ipairs(values) do
wikitext = wikitext..newline.."<span class='cr"..i.."'>"..value.."</span>"
if i == 1 then newline = "<br>" end
end
return wikitext
end
local function inline_block(stat, values)
if values == nil then return "" end
return "<div style='display:inline-block; margin:0 15px 0 15px'><b style='color:#eee8'>"..stat..":</b><br>"..color_values(values).."</div>"
end
---Return a table row of data cells containing armor information
--- @param armor table
--- @return string
local function row(armor)
local wikitext = "<tr><td>"
..iconbox(armor)
.."</td><td>"
if armor.slottype ~= "Back" then
wikitext = wikitext
.."[["
..table.concat(armor.classes,"]]<br>[[")
.."]]</td><td>"
..color_values(armor.stats["move speed"])
.."</td><td>"
end
wikitext = wikitext
..inline_block("Armor Rating", armor.stats["armor rating"])
..inline_block("Magical Resistance", armor.stats["magical resistance"])
.."</td><td>"
-- create the table data cell for attributes
for _,stat in ipairs(armor.stats.order) do
if is_primitive[stat] then
wikitext = wikitext..inline_block(stat, armor.stats[(stat):lower()])
end
end
wikitext = wikitext.."</td><td>"
-- create the table data cell for the rest of the stats
for _, stat in ipairs(armor.stats.order) do
if not is_primitive[stat] and not is_special[stat] then
wikitext = wikitext..inline_block(stat, armor.stats[(stat):lower()])
end
end
return wikitext.."</td></tr>"
end
--- Return a list of armor keys to be used in the table
--- @param frame table
--- @return table - strings
local function get_armor_list(frame)
--TODO add minor string validation and correction, e.g. "DeMoN" -> "demon" ("DeMoN"):lower()
return AD[frame.args[1] or ""] or {}
end
--- Return a table row of appropriate headers cells
--- @param frame table
--- @return string
local function table_header(frame)
local wikitext = [=[<tr><th style="width:5%">Name</th>]=]
if (frame.args[1] or ""):lower() ~= "back" then
wikitext = wikitext..[=[<th style="width:5%">Class Requirements</th><th style="width:5%">Movement Speed</th>]=]
end
return wikitext..[=[<th style="width:10%">Armor/magical Rating</th><th style="width:10%">Attributes</th><th style="width:25%">Other</th></tr>]=]
end
---Create monster table wikitext
---- Test on wiki with: mw.log(p.draw_table({args={"bosses"}})) or mw.log(p.draw_table({args={"Demon","true"}}))
--- @param frame table
--- @return string
function p.draw_table(frame)
if not AD then return "<span style='color:red;'>Error: Could not load Armor data in [[Module:Armor]]</span>" end
local wikitext = table_header(frame) or "<table class='wikitable sortable'>"
for _, key in ipairs(get_armor_list(frame)) do
wikitext = wikitext..row(AD.armor[key])
end
return wikitext.."</table>"
end
return p