Module:GETotal

This is the current revision of this page, as edited by Alex (talk | contribs) at 00:12, 17 October 2024 (Created page with "--<nowiki> --[[ Implements helper templates for {{GECoinsTotal}} and {{GECoinsTotalqty}} Adds all prices together and creates an easy to parse number --]] local p = {} local geprice = require('Module:Exchange')._price --[[ For simple sets, 1 item at a time {{GETotal|a|b|c}} will add the prices of a, b, and c Does not allow quantities of items Technically unlimited --]] function p.simple(frame) local args = frame:getParent().args local prices = {} for _, v in i..."). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:GETotal/doc. [edit]
Module:GETotal requires Module:Exchange.

--<nowiki>
 
--[[
Implements helper templates for {{GECoinsTotal}} and {{GECoinsTotalqty}}
Adds all prices together and creates an easy to parse number
--]]
 
local p = {}
 
local geprice = require('Module:Exchange')._price
 
--[[
For simple sets, 1 item at a time
{{GETotal|a|b|c}} will add the prices of a, b, and c
Does not allow quantities of items
Technically unlimited
--]]
function p.simple(frame)
	local args = frame:getParent().args
	local prices = {}
	for _, v in ipairs(args) do
		table.insert(prices,geprice(v))
	end
	return p._main(prices)
end
 
--[[
For stuff that's a but more complex
{{GETotalqty|name1=a|qty1=4|name2=b|qty2=1/4}}
Will add together the price of a * 4 and b / 4
Can recognize and parse vulgar fractions
All fractions and decimals will be truncated off the final price
Technically limited to 20, can be expanded as needed
--]]
function p.quantities(frame)
	local args = frame:getParent().args
	local prices = {}
	for i=1,20,1 do
		local itemx = args['name'..i]
		if itemx then
			local priceret = geprice(itemx)
			local qtyx = args['qty'..i] or 1
			local qtyret = tonumber(qtyx) or frac(qtyx) or 1
			table.insert(prices,math.floor(priceret * qtyret))
		end
	end
	return p._main(prices)
end
 
--[[
Helper function to parse vulgar fractions
If not readable as a vulgar fraction (i.e. no '/' character)
Returns nil, even if it can be read as a number
--]]
function frac(num)
	if not num then
		return nil
	elseif not num:find('/') then
		return nil
	end
	local parts = mw.text.split(num,'/')
	local numer,denom = tonumber(parts[1]),tonumber(parts[2])
	if numer and denom then
		return numer / denom
	else
		return nil
	end
end
 
--[[
Function to add prices together
]]--
function p._main(list)
	local price = 0
	for _, v in ipairs(list) do
		price = price + v
	end
	return price
end
 
--[[
---- Module Access Point ----
Modified version of quantity.
Will add together the price of a * 4 and b / 4
Can recognize and parse vulgar fractions
All fractions and decimals will be truncated off the final price
Technically unlimited
 
To use:
variable 'a' = array of {quantity,"item name", etc...}
variable 'b' = number of unique items to be included
--]]
function p._quantity(a,b)
	local values = a or {}
	local count  = b or 0
	local prices = {}
 
	for i=1 , (count*2) , 2 do
		local itemx = a[i+1]
		if itemx then
			local priceret = geprice(itemx)
			local qtyx = a[i] or 1
			local qtyret = tonumber(qtyx) or frac(qtyx) or 1
			table.insert(prices,math.floor(priceret * qtyret))
		end
    end
 
	return p._main(prices)
end
 
return p