Module:ItemTables: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Sylphoid (talk | contribs)
setup initial search crawl
Sylphoid (talk | contribs)
fix null names
Line 84: Line 84:
for internalName, statName in pairs(data) do --statName is the key. go through whole table. in this case itemName can be upgrade_improved_stamina
for internalName, statName in pairs(data) do --statName is the key. go through whole table. in this case itemName can be upgrade_improved_stamina
if(statName[property] ~= nil) then
if(statName[property] ~= nil and statName["Name"] ~= nil) then
listofItems = listofItems .. statName["Name"] .. "<br/>"
listofItems = listofItems .. statName["Name"] .. "<br/>"
-- TableValues = table.insert(TableValues, statName["Name"]) -- i may want to make a table first, so it includes weapon and cost to make it sortable.
-- TableValues = table.insert(TableValues, statName["Name"]) -- i may want to make a table first, so it includes weapon and cost to make it sortable.

Revision as of 23:22, 25 September 2024

Documentation for this module may be created at Module:ItemTables/doc

local p = {};
local data = mw.loadJsonData("Data:ItemData.json")

-- 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



--{{#invoke:Sandbox/Sylphoid|get_prop|ITEM_NAME|PROPERTY}}--
--check Data:ItemData.json for properties
p.get_prop = function(frame)
	local item_name = frame.args[1]
	local property = frame.args[2]
	local item = get_json_item(item_name)
	if(item == nil) then return "Item Not Found" end
	
	return Deepcopy(item[property])
end


--{{#invoke:ItemData|get_cost|ITEM_NAME}}--
p.get_cost = function(frame)
	local item_name = frame.args[1]
	local item = get_json_item(item_name)
	if(item == nil) then return "<span style=\"color:red;\">Item Not Found.</span>" end
	
	local cost = 0
	local cur_item = item
	repeat
		cost = cost + cur_item["Cost"]
		if(cur_item["Components"] == nil) then
			break
		end
		cur_item = data[cur_item["Components"][1]]
	until cur_item == nil

	return Format(cost)
end

p.itemPropTable = function(frame)
	local TableValues = {}
	local listofItems = ""
	local property = frame.args[1]
	-- local item = get_json_item(item_name)
	-- if(item == nil) then return "Item Not Found what" end
	for internalName, statName in pairs(data) do --statName is the key. go through whole table. in this case itemName can be upgrade_improved_stamina
			
			if(statName[property] ~= nil and statName["Name"] ~= nil) then
				listofItems = listofItems .. statName["Name"] .. "<br/>"
				--	TableValues = table.insert(TableValues, statName["Name"]) -- i may want to make a table first, so it includes weapon and cost to make it sortable.
			end
		
	end	
	return listofItems
	--return table.concat(TableValues, ", ")
	--for i,v in pairs(data) do
	--	if(type(item[property]) == "table") then
	--		for i,v in pairs(item[property]) do
	--			if(v == property) then
	--				TableValues = table.insert(TableValues, item["Name"])
	--			end
	--		end
	--		return table.concat(TableValues, ", ")
	--	end
	--end
	--return item[property]
	--return tableValues[property]
end

return p