Module:ItemTables

Revision as of 03:47, 23 September 2024 by Sylphoid (talk | contribs) (test formatting)

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

local p = {};
local data = mw.loadJsonData("Data:ItemData.json")
local dataDataData = mw.loadJsonData("Module:Sandbox/Sylphoid/data")

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


--{{#invoke:Sandbox/Sylphoid|get_prop|ITEM_NAME|PROPERTY}}--
--check Data:ItemData.json for properties
p.get_prop = function(frame)
	local shopTableValues = {}
	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
	if(type(item[property]) == "table") then
		for i,v in pairs(item[property]) do
			local shopTableValues = table.insert(shopTableValues, v)
			--local tableValues = get_json_item(item)
		end
		return table.concat(shopTableValues, ", ")
	end
	return item[property]
	--return tableValues[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


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

--{{#invoke:Sandbox/Sylphoid|get_prop2|ITEM_NAME|PROPERTY}}--
--check Data:ItemData.json for properties
p.get_prop2 = function(frame)
	local shopTableValues = {}
	local item_name = frame.args[1]
	local property = frame.args[2]
	local item = get_json_item2(item_name)
	if(item == nil) then return "Item Not Found" end
	if(type(item[property]) == "table") then
		for i,v in pairs(item[property]) do
			local shopTableValues = table.insert(shopTableValues, v)
			--local tableValues = get_json_item(item)
		end
		return table.concat(shopTableValues, ", ")
	end
	return item[property]
	--return tableValues[property]
end



return p