Owl.png Wiki reading hard on the eyes? Try out our Dark Theme! Visit the preferences page while logged in and select Hydra Dark. Owl.png

Module:Coin expr

From Terraria Wiki
Jump to: navigation, search
Lua logo.svg Documentation The documentation below is transcluded from Module:Coin expr/doc. (edit | history)

Provides the functionality for {{sell expr}} and {{buy expr}}. Only parameters from the template transclusion are considered (instead of from the #invoke call), with the exception of sell, which, if set, causes the module to divide all coin values gathered via {{iteminfo}} by 5.


local trim = mw.text.trim
local iteminfo = require('Module:Iteminfo').getItemStat

local currentFrame
local args_table
local lang

local getArg = function(key)
	local value = args_table[key]
	if not value then
		return nil
	end
	value = trim(value)
	return value
end

-----------------------------------------------------------------
-- main return object
return {
	
go = function(frame, args)
	currentFrame = frame
	args_table = frame:getParent().args
	lang = getArg('lang') or frame:expandTemplate{ title='lang' }
	
	local sell = frame.args['sell']
	
	local str = ''
	local errorStr = ''
	
	local i = 0
	while true do
		i = i+1
		local arg = getArg(i)
		if arg == nil then
			break
		end
		if i % 2 == 0 then
			str = str .. arg
		else
			if arg ~= '' then
				local itemid
				if type(tonumber(arg)) == 'number' and string.byte(arg) ~= 43 and string.byte(arg) ~= 45 then --tonumber accepts '+' and '-' at the start of the string, so we need to manually exclude that
					itemid = tonumber(arg)
				else 
					local _id
					_id = tonumber(frame:expandTemplate{ title='itemIdFromName', args={arg, lang='en'} }) -- try English lookup
					if _id == '' or not _id then
						_id = tonumber(frame:expandTemplate{ title='itemIdFromName', args={arg, lang=lang} }) -- try local language lookup
					end
					if _id and _id ~= '' then
						itemid = _id
					end
				end
				
				if itemid and itemid ~= '' then
					local coinvalue = iteminfo(itemid, 'value')
					if sell then
						coinvalue = math.floor(coinvalue/5)
					end
					str = str .. coinvalue
				else
					errorStr = errorStr .. 'Parameter ' .. i .. ' is not a valid item name or ID (Value: <code>' .. arg .. '</code>)! '
				end
			end
		end
	end
	
	if errorStr ~= '' then
		local t = (sell and 'sell' or 'buy') .. ' expr'
		local tl = frame:expandTemplate{ title='tl', args={t} }
		errorStr = 'Error in ' .. tl .. ': ' ..  errorStr
		return frame:expandTemplate{ title='error', args={errorStr} }
	else
		str = currentFrame:callParserFunction( '#expr', str )
		if getArg('raw') then
			return str
		else
			local round = getArg('round') or 999
			return frame:expandTemplate{ title='coin', args={str, round=round} }
		end
	end
end

}