Module:Currency

-- local p = {} local yesno = require('Module:Yesno') local string = require('Module:String') local getArgs = require('Module:Arguments').getArgs local formatNum = string._formatNum local shorten = string._formatShortNum local toNumber = string._toNumber --local getBazaarPrice = require('Module:Bazaar')._getProductData -- -- access point -- function p.coins(frame) local args = getArgs(frame) if args[1] and args[1]:match(' ') then return args[1] end if args[1] then s = args[1]:lower:gsub('[%[%]]+', ):gsub('coins?', ) end local m = args[2] local useShort = yesno(args["short"] or args["s"], false) local useApproximate = yesno(args["approximate"] or args["a"], false) local useImage = yesno(args["image"] or args["i"] or args["si"] or args["show_image"], false) local noLink = yesno(args["nolink"] or args["nl"], false) local imageInFront = yesno(args["image_in_front"] or args["iif"], false) local alt = args['alt'] return p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, 'coins') end function p._coins(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt) return p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, 'coins') end -- -- access point -- function p.bits(frame) local args = getArgs(frame) if args[1] and args[1]:match(' ') then return args[1] end if args[1] then s = args[1]:lower:gsub('[%[%]]+', ):gsub('(skyblock)?bits?', ) end local m = args[2] local useShort = yesno(args["short"] or args["s"], false) local useApproximate = yesno(args["approximate"] or args['approx'] or args["a"], false) local useImage = yesno(args["image"] or args["i"] or args["si"] or args["show_image"], false) local noLink = yesno(args["nolink"] or args["nl"], false) local imageInFront = yesno(args["image_in_front"] or args["iif"], false) local alt = args['alt'] return p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, 'bits') end function p._bits(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt) return p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, 'bits') end -- -- access point -- function p.gems(frame) local args = getArgs(frame) if args[1] and args[1]:match(' ') then return args[1] end if args[1] then s = args[1]:lower:gsub('[%[%]]+', ):gsub('%s*(skyblock)?gems?', ) end local m = args[2] local useShort = yesno(args["short"] or args["s"], false) local useApproximate = yesno(args["approximate"] or args['approx'] or args["a"], false) local useImage = yesno(args["image"] or args["i"] or args["si"] or args["show_image"], false) local noLink = yesno(args["nolink"] or args["nl"], false) local imageInFront = yesno(args["image_in_front"] or args["iif"], false) local alt = args['alt'] return p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, 'gems') end function p._gems(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt) return p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, 'gems') end -- -- US$ access point -- function p.currency(frame) local args = getArgs(frame) local s = args[1] or nil if args[1] and args[1]:match(' ') then return args[1] end if s then s = args[1]:gsub('%s*([Ss]ky[Bb]lock)?', '') end local m = args[2] local useShort = yesno(args["short"] or args["s"], false) local useApproximate = yesno(args["approximate"] or args['approx'] or args["a"], false) local useImage = yesno(args["image"] or args["i"] or args["si"] or args["show_image"], false) local noLink = yesno(args["nolink"] or args["nl"], false) local imageInFront = yesno(args["image_in_front"] or args["iif"], false) local alt = args['alt'] return p._currencyTemplate(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt) end function p._currencyTemplate(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt) if s:find('[Gg]ems?') then s = s:gsub('[Gg]ems?', '') return p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, 'gems') elseif s:find('[Bb]its?') then s = s:gsub('[Bb]its?', '') return p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, 'bits') elseif s:find('[Pp]elts?') then s = s:gsub('[Pp]elts?', '') return p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, 'pelts') else if s:find('[Cc]oins?') then s = s:gsub('[Cc]oins?', '') end return p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, 'coins') end end

local function getImg(val, t)	if type(t) ~= 'table' or type(t[1]) ~= 'table' then return t end for i,v in ipairs(t) do		if not v.lim or v.lim == 'inf' -- no upper limit, or upper limit is inf or i == #t -- reached last element in table or val < v.lim then return v		end end end -- -- Module access point -- function p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, currency) if s then temp = string.split(tostring(s),"-") if (#temp == 2) then s = temp[1] m = temp[2] end end if not currency then currency = 'coins' end local lang = mw.language.getContentLanguage -- Default values useShort = useShort or false useApproximate = useApproximate or false local currencyData = mw.loadData('Module:Currency/Data') currency = currencyData[currency] -- If first value is scientific notation if s and string.find(tostring(s), "10%^%d*") then --if not useShort, return scientific notation. if useShort, convert scientific notation to number if not useShort then s = s:gsub("%s+?%*%s+?", " × ") s = s:gsub("10%^(%d*)", "10%1") return mw.html.create('span') :css("color", currency.color) :wikitext(table.concat({ "", s, "",					currency.name,					" " }))		else if s:find("%*") then local number = s:match("(%d*)%s?*%s+?10%^%d*") local exponent = s:match("%d*%s?*%s+?10%^(%d*)") s = tonumber(number) * math.pow(10, tonumber(exponent)) else local toShort = s:match("10%^(%d*)") s = math.pow(10, tonumber(toShort)) end end -- If first value isn't a valid number, just return the invalid number as a fallthrough elseif s and toNumber(s) == nil then return s	-- If first value is null, just return colored currency text elseif s == nil then return mw.html.create('span') :css("color", currency.color) :wikitext(table.concat({ "",					alt and alt or currency.name..'s',					" " }))	end -- Convert params to numbers - nil if not a number s = toNumber(s) if m ~= nil then m = toNumber(m) end local sAsNumber = s	image = '' if (useImage or imageInFront) and currency.images then local hash = 0 local img_ = getImg(s, currency.images) image = table.concat{ ''		}		image = image..'&thinsp;' end local tooltip = nil if useShort then if m then tooltip = formatNum(s).."–"..formatNum(m) elseif s then tooltip = formatNum(s) end s = shorten(s) if m then m = shorten(m) end else s = formatNum(s) if m then m = formatNum(m) end end g = mw.html.create('span') :attr("title", tooltip) :css("font-variant-numeric", "tabular-nums") if useApproximate then g:wikitext("&#8776;") end if m then t = s.."–"..m.." " g:wikitext(t) elseif s then t = s.." " g:wikitext(t) end return mw.html.create('span') :css("color", currency.color) :wikitext(imageInFront and image or '') :node(g) :wikitext(			(imageInFront and '' or image),			"",			alt and alt or lang:plural(sAsNumber, currency.name, currency.name..'s'),			" "		) end

-- Template:Coins to Dollars -- -- Used to convert value in coins to US Dollars, based on Booster Cookie prices

function p.coins_to_dollars(frame) local args = getArgs(frame) local coins = args[1] local isFormatted = yesno(args['format'] or args['f'], true) return p._coins_to_dollars(coins, type) end function p._coins_to_dollars(coins, isFormatted) if isFormatted == nil then isFormatted = true end local cookiePriceCoins = getBazaarPrice('BOOSTER_COOKIE', 'buy', true, false) --id, type, isLong, isFormatted local cookiePriceGems = 325 local cookiePriceDollars = 4.99 / 675 * cookiePriceGems local coinPriceDollars = cookiePriceCoins / cookiePriceDollars if not isFormatted then return coins / coinPriceDollars end local dollars = coins / coinPriceDollars dollars = dollars * 100 dollars = math.floor(dollars) dollars = dollars / 100 return '$'..formatNum(dollars) end

-- Template:Bits to Coins -- -- Used to convert value in cbits to coins, based on prices across items with fairly stable prices and demand

function p.bits_to_coins(frame) local args = getArgs(frame) local bits = args[1] return p._bits_to_coins(bits) end function p._bits_to_coins(bits) local coins = bits * 300 -- based on enrichments which are worth 1.5m and cost 5000 bits return p._coins(coins, nil, true, true) end

return p