Module:ItemTables: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
add new column for quantity of stat increase
remove get type function, cleanup replace, apply friendly name
Line 42: Line 42:
     end
     end
     return copy
     return copy
end
local function get_type(item_name)
local item = get_json_item(item_name)
if(item == nil) then return "<span style=\"color:red;\">Item Not Found.</span>" end
if (item["Slot"] == "Weapon") then
return "Weapon"
elseif (item["Slot"] == "Armor") then
return "Vitality"
elseif (item["Slot"] == "Tech") then
return "Spirit"
else
return nil
end
end
p.get_type = function(frame)
local item_name = frame.args[1]
return get_type(item_name)
end
end


Line 75: Line 55:
for link, patterns in pairs(link_patterns) do
for link, patterns in pairs(link_patterns) do
for i, v in ipairs(patterns) do
for i, v in ipairs(patterns) do
     local a, b = desc:find(v)
     local a = desc:find(v)
if a then
if a then desc = link break
        desc = link
        break
     end
     end
     end
     end
Line 90: Line 68:
local listofItems = ""
local listofItems = ""
local property = frame.args[1] or mw.title.getCurrentTitle().text
local property = frame.args[1] or mw.title.getCurrentTitle().text
local friendlyPropertyName = property
property = FriendlyNames(property)
property = FriendlyNames(property)
-- local item = get_json_item(item_name)
-- local item = get_json_item(item_name)
Line 125: Line 104:
:tag('td'):wikitext(get_cost(itemName["Name"])):done()
:tag('td'):wikitext(get_cost(itemName["Name"])):done()
:tag('td'):wikitext(get_type(itemName["Name"])):done()
:tag('td'):wikitext(get_type(itemName["Name"])):done()
:tag('td'):wikitext(itemName[property].. " " .. property):done()
:tag('td'):wikitext("+" .. itemName[property].. " " .. friendlyPropertyName):done()
table.insert(requirements, tableData)
table.insert(requirements, tableData)

Revision as of 04:36, 29 September 2024

Module used to create item tables with Template:Item stat table.

To add a new stat, add the name of the stat as "friendly to internal", followed by the property as listed on Data:ItemData.json, as well as adding the property to the "local unitSuffix" list corresponding to its suffix.


local p = {};
local data = mw.loadJsonData("Data:ItemData.json")
local get_cost = require("Module:ItemData")._get_cost
local get_type = require("Module:ItemData")._get_type

-- returns the table of a specific item
function get_json_item(name)
	for i,v in pairs(data) do
		if (v["Name"] == name) then
			return v
		end
	end
	return nil
end

-- Adds commas delimiter to the thousands place
function Format(amount)
    local formatted = amount
    while true do
        formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
        if (k == 0) then
            break
        end
    end
    return formatted
end

--- Copies original table with its children as deep as possible. Does not handle metatables. (Copy is needed because Lua passes by reference, not value)
-- @function   Deepcopy
-- @param      {n-D table}
-- @return     {n-D table}
function Deepcopy(orig)
    local orig_type = type(orig)
    local copy
    if orig_type == 'table' then
        copy = {}
        for orig_key, orig_value in next, orig, nil do
            copy[Deepcopy(orig_key)] = Deepcopy(orig_value)
        end
    else
        copy = orig
    end
    return copy
end


local link_patterns = {}
link_patterns["BonusClipSizePercent"] = { "[Aa]mmo" }
link_patterns["BulletLifestealPercent"] = { "[Bb]ullet [Ll]ifesteal" }
link_patterns["BonusHealth"] = { "[Hh]ealth" }
link_patterns["BonusHealthRegen"] = { "[Hh]ealth [Rr]egen" }
link_patterns["BonusFireRate"] = { "[Ff]ire [Rr]ate" }

function FriendlyNames(desc)
	for link, patterns in pairs(link_patterns) do
		for i, v in ipairs(patterns) do
    		local a = desc:find(v)
				if a then desc = link break
    		end
    	end
	end
	return desc
end

p.itemPropTable = function(frame)
	local TableValues = {}
	local requirements = {}
	local listofItems = ""
	local property = frame.args[1] or mw.title.getCurrentTitle().text
	local friendlyPropertyName = property
	property = FriendlyNames(property)
	-- local item = get_json_item(item_name)
	-- if(item == nil) then return "Item Not Found what" end
	
	local createTable = mw.html.create('table')
			:addClass('wikitable sortable')
			:tag('caption'):wikitext("List of Items"):done()
			
	local createTableRow = mw.html.create('tr')
	createTableRow
		:tag('th'):wikitext('Name'):done()
		:tag('th'):wikitext('Cost'):done()
		:tag('th'):wikitext('Category'):done()
		:tag('th'):wikitext('Stat change'):done()
		
	createTable:node(createTableRow)

	-- query all items
	for internalName, itemName in pairs(data) do --itemName is the key. go through whole table. in this case itemName can be upgrade_improved_stamina
		
		local display = frame:expandTemplate{
		title = 'ItemIcon',
		args = {
			itemName["Name"]
			}
		}
		
			if(itemName[property] ~= nil and itemName["Name"] ~= nil) then
				listofItems = listofItems .. itemName["Name"] .. "<br/>"
					
				local tableData = mw.html.create('tr')
				tableData
					:tag('td'):wikitext(display):done()
					:tag('td'):wikitext(get_cost(itemName["Name"])):done()
					:tag('td'):wikitext(get_type(itemName["Name"])):done()
					:tag('td'):wikitext("+" .. itemName[property].. " " .. friendlyPropertyName):done()
					
				table.insert(requirements, tableData)
				--	TableValues = table.insert(TableValues, itemName["Name"]) -- i may want to make a table first, so it includes weapon and cost to make it sortable.
			end
		
	end	
	
	for _, row in ipairs(requirements) do
		createTableRow:node(row)
	end
	--createTable:node(createTableRow)
	return tostring(createTable)
	--return listofItems -- this is just a string list, same thing as createTable. delete this when line when finished
	--return table.concat(TableValues, ", ")

end

return p