Module:Prize pool currency

local p = {} local Template = require('Module:Template') local getArgs = require('Module:Arguments').getArgs local lang = mw.language.new('en')

local function message(msg, category) -- Return formatted message text for an error. local a = ' Error: ' local b = ' ' if mw.title.getCurrentTitle:inNamespaces(0) then b = b .. category end return a .. mw.text.nowiki(msg) .. b end

function p.get(frame) local args = getArgs(frame) return p._get(args) end

function p._get(args) local output local currency = args.currency or 'USD' local date = args.date or os.date("!%F") local text = args.text or 'Currency exchange rate taken from exchangerate.host' local prizepool, prizepoolUSD prizepool = tonumber( string.gsub(args.prizepool or , '[^%d.?]', ) or '' ) prizepoolUSD = tonumber( string.gsub(args.prizepoolusd or , '[^%d.?]', ) or '' ) if prizepool == nil and prizepoolUSD == nil then if mw.title.getCurrentTitle:inNamespaces(0) then return (args.prizepool or args.prizepoolusd or '') .. ''	 	else return (args.prizepool or args.prizepoolusd or '') end end date = string.gsub(date, '[^%d.-]', '') if date == '' or string.len(date) ~= 10 then date = os.date("!%F") mw.log('Infobox league: Invalid currency exchange date -> default date (' .. date .. ')') 	elseif date > os.date("!%F") then date = os.date("!%F") end local currencyRate currency = string.upper(currency) if currency ~= 'USD' then --extension currencyRate = mw.ext.CurrencyExchange.currencyexchange( 1, currency, 'USD', date ) if currencyRate == false then --else manual input currencyRate = (args.rate or '') text = args.text or 'Currency exchange rate taken from XE.com' if (args.date or ) ==  then return message('Need valid exchange date', '') end end if currencyRate ~=  and currencyRate ~= math.huge and (prizepool or ) ~= '' then prizepoolUSD = lang:formatNum( math.floor( prizepool * tonumber(currencyRate) ) ) prizepool = lang:formatNum( math.floor( prizepool ) ) elseif currencyRate ~=  and (prizepool or ) ~= '' then prizepoolUSD = '' prizepool = lang:formatNum( math.floor( prizepool ) ) elseif (prizepoolUSD or ) ~=  and (prizepool or ) ~=  then prizepoolUSD = lang:formatNum( math.floor( prizepoolUSD ) ) prizepool = lang:formatNum( math.floor( prizepool ) ) elseif (prizepoolUSD or ) ~=  then prizepoolUSD = lang:formatNum( math.floor( prizepoolUSD ) ) else return message('Need valid currency, exchange rate', '') end elseif (prizepoolUSD or ) ~=  then prizepoolUSD = lang:formatNum( math.floor( prizepoolUSD ) ) else return message('Need valid currency', '') end mw.ext.VariablesLua.vardefine('tournament_currency', args.currency and currency) mw.ext.VariablesLua.vardefine('tournament_currency_date', date) mw.ext.VariablesLua.vardefine('tournament_currency_rate', currencyRate) mw.ext.VariablesLua.vardefine('tournament_currency_text', text) mw.ext.VariablesLua.vardefine('tournament_prizepool_local', prizepool) mw.ext.VariablesLua.vardefine('tournament_prizepool_usd', prizepoolUSD) -- legacy compatibility mw.ext.VariablesLua.vardefine('currency', args.currency and currency) mw.ext.VariablesLua.vardefine('currency date', date) mw.ext.VariablesLua.vardefine('currency rate', currencyRate) mw.ext.VariablesLua.vardefine('prizepool', prizepool) mw.ext.VariablesLua.vardefine('prizepool usd', prizepoolUSD) mw.ext.VariablesLua.vardefine('tournament_prizepool', prizepoolUSD) if prizepool ~= nil then output = Template.safeExpand(mw.getCurrentFrame,'Local currency',{currency, prizepool = prizepool}) output = output .. ' (≃ $' .. prizepoolUSD .. ' ' .. Template.safeExpand(mw.getCurrentFrame,'Abbr/USD') .. ')' else output = '$' .. prizepoolUSD .. ' ' .. Template.safeExpand(mw.getCurrentFrame,'Abbr/USD') end return output end return p