Module:Calcvalue/sandbox
Jump to navigation
Jump to search
Module documentation
This documentation is transcluded from Module:Calcvalue/sandbox/doc. [edit] [history] [purge]
Module:Calcvalue/sandbox requires Module:Arguments.
Module:Calcvalue/sandbox requires Module:CVTotal.
Module:Calcvalue/sandbox requires Module:Currency.
Module:Calcvalue/sandbox requires Module:TemplateUtil.
| Function list |
|---|
| L 7 — p.main L 28 — parseMultiplier L 46 — p.price L 54 — p._get L 103 — p._set |
Module:Calcvalue testing location. Please test changes here rather than in the base module.
local getArgs = require("Module:Arguments").getArgs;
local coins = require('Module:Currency')._amount
local parseTemplateParams = require("Module:TemplateUtil").parseTemplateParams;
local p = {}
function p.main(frame)
local args = getArgs(frame);
local item = args['item'] or args[1]
local img = false
if args['image'] or args[2] then
img = true
end
if args['set'] then
local val = p._set( args['set'] )
if args['output'] then
return val
else
return
end
end
return p._get(item, img)
end
local expr = mw.ext.ParserFunctions.expr;
local function parseMultiplier(multi)
do
local result = tonumber(multi);
if (result ~= nil) then
return result;
end
end
-- indicated someone is trying to pass an equation as a mulitplier
-- known use cases are fractions, but pass it to #expr to make sure
-- it's handled correctly
if (multi ~= nil and mw.ustring.find(multi, "[/*+-]")) then
return tonumber(expr(multi));
end
return nil;
end
function p.price(frame)
local args = getArgs(frame);
local item = args[1];
local multi = parseMultiplier(args[2]) or 1;
return p._get(item, false, multi);
end
function p._get(item, img, multi)
local calcval
local res = bucket('infobox_item')
.select('calculated_value')
.where(bucket.Or({'page_name', item}, {'item_name', item})) -- Sometimes the input is the page name, sometimes its the item name.
.where(bucket.Not({'calculated_value', bucket.Null()}))
.limit(1)
.run()
if res and res[1] then
calcval = res[1]['calculated_value']
end
if not calcval then
error('No calculated value could be found for: '..item)
end
local price
if tonumber(calcval) then
price = tonumber(calcval)
else
local x = calcval
:gsub('{{[Cc]alcvalue%|([^}]+)}}', p._get)
:gsub("{{[Cc]VP%|([^}]+)}}", function (template)
return p.price(parseTemplateParams(template));
end)
:gsub("{{[Cc]VTotal%|([^}]+)}}", function (template)
return require("Module:CVTotal").simple(parseTemplateParams(template));
end)
price = mw.getCurrentFrame():preprocess("{{#expr: " ..x.." }}")
if tonumber(price) then
price = math.floor(price * 100 + 0.5) / 100
else
mw.logObject(
{ calcval = calcval, x = x, price = price },
"[[Module:Calcvalue]]._get:"
);
error('Error preprocessing calculated value: '..calcval)
end
end
if type(multi) == "number" then
price = multi * price;
end
if img then
return coins(price, 'coins')
end
return price
end
function p._set(val)
local ret
local calculated
if tonumber(val) then
ret = val
mw.log('Fixed value: '..ret)
calculated = val
else
local str = string.gsub( string.gsub(val, '{', '{{'), '}', '}}')
ret = string.gsub(str, '¦', '|')
mw.log( 'Wikitext: '..ret )
calculated = mw.getCurrentFrame():preprocess(ret)
mw.log( 'Resulting value: '.. calculated )
end
bucket('infobox_item').put({
['item_name'] = mw.title.getCurrentTitle().fullText,
['calculated_value'] = ret
})
return calculated
end
return p