RuneScape Wiki
Register
Advertisement

Documentation for this module may be created at Module:Get store info/doc

-- <pre>
local p = {}

function p.main(frame)
	-- This template is fairly expensive to use, so it should be limited
	mw.incrementExpensiveFunctionCount()

	local args = frame:getParent().args

	local store = string.gsub(args['?store'] or args.store or '','[][]','')
	store = mw.text.decode(store)

	local item = string.lower(args.userparam or args.item or '')
	item = mw.text.decode(item)

	-- escape lua characters
	local _item = item:gsub('([%^%$%(%)%%%.%[%]%*%+%-%?])','%%%1')

	local smwdata = frame:preprocess('{{'..
					'{{{|safesubst:}}}#show:'..
					store..
				[[
					|?Store JSON
					|headers=plain
					|format=list
					}}
				]])

	smwdata = mw.text.decode(tostring(smwdata))

	local _found = false

	local json = smwdata

	json = string.format('[ %s ]',json)

	_found = mw.ustring.find(json:lower(), '%{ "name": "'.._item..'"')

	if not _found then
		return ''
	end

	local mems = frame:preprocess('{{{{{|safesubst:}}}#show:'..store..'|?Is members only}}')
	
	local location = frame:preprocess('{{{{{|safesubst:}}}#show:'..store..'|?Store location}}')

	local stock,price,currency = mw.ustring.match(json:lower(), '%{ "name": "'.._item..'", "stock": "(.-)", "price": "([%d%?]+)", "currency": "([%w ]+)"')

	stock = stock or 0
	price = price or ''
	currency = currency:gsub("^%l", string.upper) or 'coins'

	return frame:preprocess(string.format('{{SellersLine|store=[[%s]]|location=[[%s]]|stock=%s|price=%s|currency=%s|members=%s}}',store,location,stock,price,currency,mems))
end

return p
Advertisement