From Dark and Darker Wiki
(Created page with "local p = {} local AD = mw.loadJsonData("Data:Accessory.json") local insert = table.insert local concat = table.concat ---Counts number of keys. Tables from Accessory.json don't work with next() or the # operator --- @param t table --- @return number local function count(t) local c = 0 for _ in pairs(t) do c = c + 1 end return c end ---Create css/html wikitext for iconbox ---@param item table ---@return string local function iconbox(item) local color = "3" local...") |
(Changed to only use table.concat where useful. I.e. where there are large enough strings.) |
||
| Line 19: | Line 19: | ||
---@return string | ---@return string | ||
local function iconbox(item) | local function iconbox(item) | ||
local color = "3" | |||
local caption = "[["..item.name | |||
if count(item.rarities) == 1 then | |||
color = item.rarities[1] | |||
caption = caption.."|<b class=cr"..color..">"..item.name.."</b>" | |||
end | |||
return "<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'>".. | |||
"<div class='rarity"..color.." rounded relative'>".. | |||
"[[File:"..item.name..".png|45x45px|link="..item.name.."]]".. | |||
caption.."]]</div>" | |||
end | end | ||
| Line 46: | Line 44: | ||
--TODO this assumes i is the same as the color rating, this may result in a bug | --TODO this assumes i is the same as the color rating, this may result in a bug | ||
local wikitext = | local wikitext = "" | ||
local newline = "" | local newline = "" | ||
for i, value in ipairs(values) do | for i, value in ipairs(values) do | ||
wikitext = wikitext..newline.."<span class='cr"..i.."'>"..value.."</span>" | |||
if i == 1 then newline = "<br>" end | if i == 1 then newline = "<br>" end | ||
end | end | ||
return | return wikitext | ||
end | end | ||
| Line 70: | Line 68: | ||
--- @return string | --- @return string | ||
local function row(accessory) | local function row(accessory) | ||
local wikitext = "<tr><td>"..iconbox(accessory).."</td><td>" | |||
-- create the table data cell for all of the stats | |||
for _, stat in ipairs(accessory.stats.order) do | |||
wikitext = wikitext..inline_block(stat,accessory) | |||
end | |||
return wikitext.."</td></tr>" | |||
end | end | ||
Revision as of 23:30, 2 March 2026
Overview
Functions for making Accessory table. Data comes from Data:Accessory.json.
Functions
draw_table
Creates a table of accessories
Parameters
- 1 - <Slot Type>
draw_table examples
Back
{{#invoke:Accessory|draw_table|Necklace}}
| Name | Stats |
|---|---|
Will 1 2 3 4 5 | |
Vigor 1 2 3 4 5 | |
Luck 100~120 | |
Headshot Damage Modifier 12~15% | |
Agility 1 2 3 4 5 | |
Physical Power 1 2 3 4 5 Armor Penetration 1% 1.5% 2% 2.5% 3% | |
Dexterity 1 2 3 4 5 | |
Max Health Add 1~2 3~4 5~6 7~8 9~10 | |
Knowledge 1 2 3 4 5 | |
Strength 1 2 3 4 5 | |
Magical Power 1 2 3 4 5 Magical Penetration 1% 1.5% 2% 2.5% 3% | |
Resourcefulness 1 2 3 4 5 | |
Outgoing Magical Healing Add 3 | |
Magical Resistance 25~28 29~31 32~35 36~38 39~41 | |
Armor Rating Add 10~12 13~16 17~20 21~24 25~27 |
Head
{{#invoke:Accessory|draw_table|Ring}}
| Name | Stats |
|---|---|
Vigor 2 Will 1 | |
Strength 2 Vigor 1 Luck 6~12 | |
Strength 1 2 3 4 5 | |
Dexterity 1 2 3 4 5 | |
Agility 1 2 3 4 5 | |
Will 1 2 3 4 5 | |
Resourcefulness 1 2 3 4 5 | |
Vigor 1 2 3 4 5 | |
Knowledge 1 2 3 4 5 |
local p = {}
local AD = mw.loadJsonData("Data:Accessory.json")
local insert = table.insert
local concat = table.concat
---Counts number of keys. Tables from Accessory.json don't work with next() or the # operator
--- @param t table
--- @return number
local function count(t)
local c = 0
for _ in pairs(t) do c = c + 1 end
return c
end
---Create css/html wikitext for iconbox
---@param item table
---@return string
local function iconbox(item)
local color = "3"
local caption = "[["..item.name
if count(item.rarities) == 1 then
color = item.rarities[1]
caption = caption.."|<b class=cr"..color..">"..item.name.."</b>"
end
return "<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'>"..
"<div class='rarity"..color.." rounded relative'>"..
"[[File:"..item.name..".png|45x45px|link="..item.name.."]]"..
caption.."]]</div>"
end
---Marks up a list of values with color based on rarity
---@param values string|table
---@param accessory table
---@return string
local function color_values(values,accessory)
if values == nil then return "" end
if type(values)=="string" then
if count(accessory.rarities) == 1 then return "<span class='cr"..accessory.rarities[1].."'>"..values.."</span>" end
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
---Marks up a stat block with the stat name and colored values and appropriate margins and alignment
--- @param stat string
--- @param accessory table
--- @return string
local function inline_block(stat, accessory)
if accessory.stats[(stat):lower()] == nil then return "" end
return "<div style='display:inline-block;vertical-align:top;margin:0 15px 0 15px'><b style='color:#eee8'>"..stat.."</b><br>"..color_values(accessory.stats[(stat):lower()],accessory).."</div>"
end
---Return a table row of data cells containing accessory information
--- @param accessory table
--- @return string
local function row(accessory)
local wikitext = "<tr><td>"..iconbox(accessory).."</td><td>"
-- create the table data cell for all of the stats
for _, stat in ipairs(accessory.stats.order) do
wikitext = wikitext..inline_block(stat,accessory)
end
return wikitext.."</td></tr>"
end
--- Return a list of accessory keys to be used in the table
--- @param frame table
--- @return table - strings
local function get_list(frame)
return AD[frame.args[1]] or {}
end
--- Return a table row of appropriate headers cells
--- @return string
local function table_header()
return [=[<table cellspacing="0" class="wikitable sortable jquery-tablesorter" style="width:20%;color:#eee;background:transparent;text-align:center;vertical-align:middle;"><tr><th style="width:50%">Name</th><th style="width:50%">Stats</th></tr>]=]
end
---Create Accessory table wikitext
---- Test on wiki with: mw.log(p.draw_table({args={"Neck"}}))
---- mw.log(p.draw_table({args={"Ring"}}))
--- @param frame table
--- @return string
function p.draw_table(frame)
if not AD then return "<span style='color:red;'>Error: Could not load Accessory data in [[Module:Accessory]]</span>" end
local wikitext = {}
insert(wikitext, table_header())
for _, key in ipairs(get_list(frame)) do
insert(wikitext, row(AD.Accessory[key]))
end
insert(wikitext, "</table>")
return concat(wikitext)
end
return p