Difference between revisions of "Module:Market"

From Idlescape Wiki
Jump to navigation Jump to search
m (Separated functions for templates and modules)
m (Default to 0 if data not loaded)
 
(5 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
local loaded_data_modules = {}
 
local loaded_data_modules = {}
  
function p.loadPrice (item, data_type)  
+
function p.loadPrice (item, data_type, league)  
 
local module_name = data_module_names[data_type]
 
local module_name = data_module_names[data_type]
 
if loaded_data_modules[module_name] == nil then
 
if loaded_data_modules[module_name] == nil then
 
loaded_data_modules[module_name] = mw.loadData(module_name)
 
loaded_data_modules[module_name] = mw.loadData(module_name)
 
end
 
end
+
if not loaded_data_modules[module_name][league] then
return loaded_data_modules[module_name][item]
+
return 0
 +
end
 +
return loaded_data_modules[module_name][league][item]
 
end
 
end
  
Line 21: Line 23:
 
function p.price(frame)
 
function p.price(frame)
 
local args = frame:getParent().args
 
local args = frame:getParent().args
p._price(args)
+
return p._price(args)
 
end
 
end
  
Line 28: Line 30:
 
local multi = args[2]
 
local multi = args[2]
 
local separator = args[3]
 
local separator = args[3]
 +
local league = args[4]
 
local price = 0
 
local price = 0
 +
 +
multi = tonumber(multi)
 
if multi == nil then
 
if multi == nil then
 
multi = 1
 
multi = 1
 
end
 
end
 +
if league == nil then
 +
league = "1"
 +
end
 +
 
item = string.lower(item)
 
item = string.lower(item)
price = p.loadPrice (item, 'price') * multi
 
 
 
if separator == "1" then
+
price = p.loadPrice (item, 'price', league)
return p.addSeparator(price)
+
price = tonumber(price)
else
+
return price
+
if price then
 +
price = price * multi
 +
if separator == "1" then
 +
price = p.addSeparator(price)
 +
end
 
end
 
end
 +
 +
return price
 
end
 
end
  
 
return p
 
return p

Latest revision as of 15:03, 28 May 2024

Gets the market price when passing the item name.

1: itemName(required) - Name of the item. Letter case does not matter.

2: multiplier(optional) - Price for multiple items. Default is 1.

3: separator(optional) - Adds ',' as thousands separators. Use 1 to enable.

4: league(optional) - 1 for main, 5 for pre-season, 6 for season 1. Default is 1.


local p = {}

local data_module_names = {
	price = 'Module:MarketPrices/data'
}
local loaded_data_modules = {}

function p.loadPrice (item, data_type, league) 
	local module_name = data_module_names[data_type]
	if loaded_data_modules[module_name] == nil then
		loaded_data_modules[module_name] = mw.loadData(module_name)
	end
	if not loaded_data_modules[module_name][league] then
		return 0
	end
	return loaded_data_modules[module_name][league][item]
end

function p.addSeparator(num)
	return tostring(num):reverse():gsub("(%d%d%d)","%1,"):gsub(",(%-?)$","%1"):reverse()
end

function p.price(frame)
	local args = frame:getParent().args
	return p._price(args)
end

function p._price(args)
	local item = args[1]
	local multi = args[2]
	local separator = args[3]
	local league = args[4]
	local price = 0
	
	multi = tonumber(multi)
	if multi == nil then
		multi = 1
	end
	if league == nil then
		league = "1"
	end
	
	item = string.lower(item)
	
	price = p.loadPrice (item, 'price', league)
	price = tonumber(price)
	
	if price then
		price = price * multi
		if separator == "1" then
			price = p.addSeparator(price)
		end
	end
	
	return price
end

return p