Module:Collections value
Jump to navigation
Jump to search
Module documentation
This documentation is transcluded from Module:Collections value/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Collections value/doc. [edit]
Module:Collections value's function show_collection_value is invoked by Template:Collections value.
Module:Collections value requires Module:Exchange.
Module:Collections value requires Module:Paramtest.
Module:Collections value requires Module:Yesno.
| Function list |
|---|
| L 8 — starts_with L 12 — query_collection L 26 — convert_artefact L 41 — query_artefacts L 69 — query_artefacts_in_collection L 87 — calculate_artefacts_value L 121 — p._show_collection_value L 139 — p.show_collection_value |
-- <nowiki>
local p = {}
local exchange = require('Module:Exchange')
local yesno = require('Module:Yesno')
local hc = require("Module:Paramtest").has_content;
local function starts_with(str, start)
return string.sub(str, 1, string.len(start)) == start
end
local function query_collection(name)
local query = bucket("infobox_collection")
.select('first', 'recurring', 'chronotes')
.where('name', name)
local bucketdata = query.run()
if #bucketdata == 0 then
error('No collection found. Make sure spelling and capitalisation are correct.')
return {}
end
return bucketdata[1]
end
local function convert_artefact(artefact_name, production, chronotes)
local artefact = {}
artefact.chronotes = chronotes
local materials = {}
for _, material in ipairs(production.materials) do
if not starts_with(material.name, artefact_name) then
table.insert(materials, material)
end
end
artefact.materials = materials
return artefact
end
local function query_artefacts(names)
local artefactsQuery = {}
for _, name in ipairs(names) do
table.insert(artefactsQuery, { 'production_main_output', name })
end
local query = bucket("recipe")
.select('page_name', 'production_json', 'infobox_artefact.chronotes')
.join("infobox_artefact", "infobox_artefact.page_name", "recipe.page_name")
.where(bucket.Or(artefactsQuery))
local bucketdata = query.run()
if #bucketdata == 0 then
return {}
end
local artefacts = {}
for _, v in ipairs(bucketdata) do
local production_json = mw.text.jsonDecode(v['production_json'] or '{}')
if production_json then
table.insert(artefacts, convert_artefact(v['page_name'], production_json, v['infobox_artefact.chronotes']))
end
end
return artefacts
end
local function query_artefacts_in_collection(collection_name)
local query = bucket("infobox_collection_artefacts")
.select('artefact', 'index')
.where('collection', collection_name)
local bucketdata = query.run()
if #bucketdata == 0 then
return {}
end
local names = {}
for _, v in ipairs(bucketdata) do
table.insert(names, v.artefact)
end
return query_artefacts(names)
end
local function calculate_artefacts_value(artefacts, completion_chronotes)
local total_mats = {}
local cumulative_chronotes = 0
for _, artefact in ipairs(artefacts) do
local chronotes = artefact.chronotes or '0'
if tonumber(chronotes, 10) then
cumulative_chronotes = cumulative_chronotes + tonumber(chronotes, 10)
end
for _, mat_info in ipairs(artefact.materials) do
local qty = tonumber(mat_info.quantity, 10) or 1
local mat_name = mat_info.name
if total_mats[mat_name] then
total_mats[mat_name] = total_mats[mat_name] + qty
else
total_mats[mat_name] = qty
end
end
end
local total_cost = 0
for mat_name, qty in pairs(total_mats) do
local mat_price = exchange._price(mat_name, qty, nil, nil, 0)
total_cost = total_cost + mat_price
end
completion_chronotes = completion_chronotes or 0
local total_price = exchange._price('Chronotes', cumulative_chronotes + completion_chronotes, nil, nil, 0)
return total_price - total_cost
end
function p._show_collection_value(args)
local collection_name = args[1]
if not hc(collection_name) then
error('No collection found. Make sure spelling and capitalisation are correct.')
end
local cost = yesno(args.cost, false)
local modifier = 1
if cost then
modifier = -1
end
local collection = query_collection(collection_name)
local artefacts = query_artefacts_in_collection(collection_name)
return calculate_artefacts_value(artefacts, collection.chronotes) * modifier
end
function p.show_collection_value(frame)
local args = frame:getParent().args
return p._show_collection_value(args)
end
return p
-- </nowiki>