Module:Sandbox/User:The Gaffer/Modules/Infobox Recipe: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
Line 6: Line 6:
local mw = require('mw')
local mw = require('mw')


-- Define raw material costs based on Semantic MediaWiki queries
-- Define raw material costs based on Semantic MediaWiki queries
local rawmat1cost = args.rawmat1 and mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat1 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1') or "0"
local function getCost(queryResult)
local rawmat2cost = args.rawmat2 and mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat2 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1') or "0"
if type(queryResult) == "table" and queryResult[1] then
local rawmat3cost = args.rawmat3 and mw.smw.ask('[[:+]][[Sold item::' .. args.rawmat3 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1') or "0"
return tonumber(queryResult[1]) or 0
end
return 0
end

local rawmat1query = args.rawmat1 and '[[:+]][[Sold item::' .. args.rawmat1 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1'
local rawmat2query = args.rawmat2 and '[[:+]][[Sold item::' .. args.rawmat2 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1'
local rawmat3query = args.rawmat3 and '[[:+]][[Sold item::' .. args.rawmat3 .. ']]|?Shop buy price|sort=Shop buy price|order=asc|limit=1'

local rawmat1cost = args.rawmat1 and getCost(mw.smw.ask(rawmat1query)) or 0
local rawmat2cost = args.rawmat2 and getCost(mw.smw.ask(rawmat2query)) or 0
local rawmat3cost = args.rawmat3 and getCost(mw.smw.ask(rawmat3query)) or 0


-- Calculate total cost and output cost
local totalCost = (tonumber(rawmat1cost) or 0) * (tonumber(args.rawmat1qty) or 1) +
local totalCost = (tonumber(rawmat1cost) or 0) * (tonumber(args.rawmat1qty) or 1) +
(tonumber(rawmat2cost) or 0) * (tonumber(args.rawmat2qty) or 1) +
(tonumber(rawmat2cost) or 0) * (tonumber(args.rawmat2qty) or 1) +
(tonumber(rawmat3cost) or 0) * (tonumber(args.rawmat3qty) or 1)
(tonumber(rawmat3cost) or 0) * (tonumber(args.rawmat3qty) or 1)
local output1query = args.output1 and '[[:+]][[' .. args.output1 .. ']]|?Value|limit=1'
local output1cost = args.output1 and mw.smw.ask('[[:+]][[' .. args.output1 .. ']]|?Value|limit=1') or "0"
local output1cost = args.output1 and getCost(mw.smw.ask(output1query)) or 0


-- Calculate output cost total and profit
local output1costTotal = (tonumber(output1cost) or 0) * (tonumber(args.output1qty) or 1)
local output1costTotal = (tonumber(output1cost) or 0) * (tonumber(args.output1qty) or 1)
local profit = output1costTotal - totalCost
local profit = output1costTotal - totalCost