From Dark and Darker Wiki
(Created page with "local p = {} local MD = mw.loadJsonData("Data:Misc.json")--Global var holding tables from Data:Misc.json local insert = table.insert local concat = table.concat ---Get the size of the item in pixels for the iconbox --- @param item table --- @return string local function size(item) return tostring(item.invwidth*45).."x"..tostring(item.invheight*45).."px" end ---Counts number of keys. Tables from Misc.json don't work with next() or the # operator --- @param t table --...") |
(Reenabled dynamic item names.) |
||
| Line 41: | Line 41: | ||
local function iconbox(item) | local function iconbox(item) | ||
local color = 2 | local color = 2 | ||
local bold_link = "|<b>".. | local bold_link = "|<b>"..item.name | ||
if count(item.rarities) == 1 then | if count(item.rarities) == 1 then | ||
color = item.rarities[1] | color = item.rarities[1] | ||
bold_link = "|<b class=cr"..color..">".. | bold_link = "|<b class=cr"..color..">"..item.name | ||
end | 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'>" | 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'>" | .."<div class='rarity"..color.." rounded relative'>" | ||
.."[[File:".. | .."[[File:"..item.name..".png|"..size(item).."|link="..item.name.."]]" | ||
.."<span class='iconamount' style='pointer-events:none;color:#EEEA;font-size:16px'>"..item.maxcount.."</span></div>" | .."<span class='iconamount' style='pointer-events:none;color:#EEEA;font-size:16px'>"..item.maxcount.."</span></div>" | ||
.."[[".. | .."[["..item.name..bold_link.."</b>]]</div>" | ||
end | end | ||
Revision as of 23:27, 7 March 2026
Overview
Functions for making Misc table. Data comes from Data:Misc.json.
Functions
draw_table
Creates a table of misc items
Parameters
- 1 - <Type>
draw_table examples
Gems
{{#invoke:Misc|draw_table|Gem}}
| Name | Single Item | Maximum Item Stack | ||
|---|---|---|---|---|
| Sell Price | Item Achieve | Sell Price / Slot Size | Item Achieve / Slot Size | |
| 3 5 12 25 50 100 175 | 1 1 2 4 6 10 20 | 9 15 36 75 150 300 525 | 3 3 6 12 18 30 60 | |
| 3 5 12 25 50 100 175 | 1 1 2 4 6 10 20 | 9 15 36 75 150 300 525 | 3 3 6 12 18 30 60 | |
| 3 5 12 25 50 100 175 | 1 1 2 4 6 10 20 | 9 15 36 75 150 300 525 | 3 3 6 12 18 30 60 | |
| 3 5 12 25 50 100 175 | 1 1 2 4 6 10 20 | 9 15 36 75 150 300 525 | 3 3 6 12 18 30 60 | |
Container
{{#invoke:Misc|draw_table|Container}}
| Name | Sell Price | Capacity |
|---|---|---|
| 3 | 2500 | |
| 25000 | ||
| 1 | 500 | |
| 1 | 100 | |
| 5 | 100 | |
| 5 | 5000 |
local p = {}
local MD = mw.loadJsonData("Data:Misc.json")--Global var holding tables from Data:Misc.json
local insert = table.insert
local concat = table.concat
---Get the size of the item in pixels for the iconbox
--- @param item table
--- @return string
local function size(item)
return tostring(item.invwidth*45).."x"..tostring(item.invheight*45).."px"
end
---Counts number of keys. Tables from Misc.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
---Get the type of the armor as a string
--- @param item table
--- @return string
local function get_type(item)
--Assumes that the item only has one type
for key,_ in pairs(item.types) do
--Scribunto uses meta tables so we can't use next() here
return key
end
return ""
end
---Create css/html wikitext for iconbox
--- @param item table
--- @return string
local function iconbox(item)
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
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|"..size(item).."|link="..item.name.."]]"
.."<span class='iconamount' style='pointer-events:none;color:#EEEA;font-size:16px'>"..item.maxcount.."</span></div>"
.."[["..item.name..bold_link.."</b>]]</div>"
end
---Marks up a list of values with color based on rarity
--- @param values string|table
--- @param misc table
--- @return string
local function color_values(values,misc)
if values == nil then return "" end
if type(values)=="string" or type(values)=="number" then
if count(misc.rarities) == 1 then return "<span class='cr"..misc.rarities[1].."'>"..values.."</span>" end
return tostring(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, key in ipairs(misc.rarities) do
wikitext = wikitext..newline.."<span class='cr"..key.."'>"..tostring(values[tonumber(key)] or "").."</span>"
if i == 1 then newline = "<br>" end
end
return wikitext
end
---Return an array whose values have been divided by the provided integer
--- @param values table
--- @param multiplier number
local function mult_array(values, multiplier)
local result = {}
for i, value in pairs(values) do
if i ~= "order" then
result[i] = tonumber(string.format("%.3f", value * multiplier))
end
end
return result
end
---Return a table row of data cells containing misc information
--- @param misc table
--- @return string
local function row(misc)
local wikitext = {}
insert(wikitext,"<tr><td>"..iconbox(misc))
insert(wikitext,"</td><td>"..color_values(misc.sellprices,misc))
if not misc.types["Container"] then
local max_stack_per_slotsize = misc.maxcount / (misc.invwidth * misc.invheight)
insert(wikitext,"</td><td>"..color_values(misc.ap,misc))
insert(wikitext,"</td><td>"..color_values(mult_array(misc.sellprices,max_stack_per_slotsize),misc))
insert(wikitext,"</td><td>"..color_values(mult_array(misc.ap,max_stack_per_slotsize),misc))
else
insert(wikitext,"</td><td>"..color_values(misc.maxcontentscount,misc))
end
insert(wikitext,"</td></tr>")
return concat(wikitext)
end
---Return a list of misc keys to be used in the table
--- @param frame table
--- @return table - strings
local function get_list(frame)
return MD[frame.args[1]] or {}
end
--- Return a table row of appropriate headers cells
--- @param frame table
--- @return string
local function table_header(frame)
if (frame.args[1]):lower() == "container" then
return [=[<table class="wikitable sortable jquery-tablesorter stripedtable" cellspacing="0" style="color:#eee;background:transparent;text-align:center;vertical-align:middle">
<tr><th>Name</th><th>Sell Price</th><th>Capacity</th>]=]
end
-- Default misc table header
return [=[<table class="wikitable sortable jquery-tablesorter stripedtable" cellspacing="0" style="color:#eee;background:transparent;text-align:center;vertical-align:middle">
<tr><th rowspan="2">Name</th><th colspan="2">Single Item</th><th colspan="2">Maximum Item Stack</th></tr>
<tr><th>Sell Price</th><th>Item Achieve</th><th>Sell Price / Slot Size</th><th>Item Achieve / Slot Size</th></tr>]=]
end
---Create Misc table wikitext
---- Test on wiki with: mw.log(p.draw_table({args={"Container"}}))
---- mw.log(p.draw_table({args={"Gem"}}))
--- @param frame table
--- @return string
function p.draw_table(frame)
if not MD then return "<span style='color:red;'>Error: Could not load Misc data in [[Module:Misc]]</span>" end
local wikitext = {}
insert(wikitext, table_header(frame))
for _, key in ipairs(get_list(frame)) do
insert(wikitext, row(MD.Misc[key]))
end
insert(wikitext, "</table>")
return concat(wikitext)
end
return p