Difference between revisions of "Module:Market"

From Idlescape Wiki
Jump to navigation Jump to search
m
(Added different league market)
 
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
Line 12: Line 12:
 
end
 
end
 
 
return loaded_data_modules[module_name][item]
+
return loaded_data_modules[module_name][league][item]
 
end
 
end
  
Line 28: Line 28:
 
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
 
 
Line 33: Line 34:
 
if multi == nil then
 
if multi == nil then
 
multi = 1
 
multi = 1
 +
end
 +
if league == nil then
 +
league = "1"
 
end
 
end
 
 
 
item = string.lower(item)
 
item = string.lower(item)
 
 
price = p.loadPrice (item, 'price')
+
price = p.loadPrice (item, 'price', league)
 
price = tonumber(price)
 
price = tonumber(price)
 
 

Latest revision as of 21:03, 29 October 2023

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

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