Module:Currency

-- local p = {} local yesno = require('Module:Yesno') local mString = require('Module:String') local getArgs = require('Module:Arguments').getArgs local formatNum = mString._formatNum local shorten = mString._formatShortNum local toNumber = mString._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"], 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"], 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"], 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"], 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') 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 -- -- Module access point -- function p._currency(s, m, useShort, useApproximate, useImage, noLink, imageInFront, alt, currency) if not currency then currency = 'coins' end local lang = mw.language.getContentLanguage -- Default values useShort = useShort or false useApproximate = useApproximate or false local currencyData = { coins = { name = 'coin', destPage = 'Coins', color = '#FFAA00', images = { 'File:Coins.png|24px', 'File:Coins (Golden).png|24px', 'File:Coin_drop_(diamond).png|18px' }       },        bits = { name = 'Bit', destPage = 'Bits', color = '#55FFFF', images = nil },       gems = { name = 'SkyBlock Gem', destPage = 'SkyBlock Gems', color = '#55FF55', images = nil }   }    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 and currency.images then if s < 5 then image = table.concat({               'link=', (noLink and  or currency.destPage),             }) elseif s < 50 then image = table.concat({               'link=', (noLink and  or currency.destPage),             }) elseif s >= 50 then image = table.concat({               'link=', (noLink and  or currency.destPage),             }) end 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

return p