Module:Mmgtable/json
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Mmgtable/json/doc
-- <nowiki>
local timefunc = require('Module:Time')
local exg = require('Module:Exchange')._price
local p = {}
local lang = mw.getContentLanguage()
function gep(x)
return exg(x, 1, nil, nil, 0)
end
local MEMBERS_ICON = {
[false] = "[[File:Free-to-play icon.png|center|link=Free-to-play]]",
[true] = "[[File:Member icon.png|center|link=Members]]"
}
function round1k(x, f)
if not tonumber(x) then
return x
end
local _x = math.abs(x)
_x = 1000 * math.floor(_x / 1000 + 0.5)
if x < 0 then
_x = _x * -1
end
if f then
return lang:formatNum(_x)
end
return _x
end
function round1dp(x, f)
if not tonumber(x) then
return x
end
local _x = math.abs(x)
_x = math.floor(_x * 10 + 0.5) / 10
if x < 0 then
_x = _x * -1
end
if f then
return lang:formatNum(_x)
end
return _x
end
function calc_value(args, kph)
local total = 0
for i,v in ipairs(args) do
local val
if v.pricetype == 'value' then
val = v.qty * v.value
elseif v.pricetype == 'gemw' then
val = gep(v.name) * v.qty
end
if kph>0 and not v.isph then
val = val * kph
end
total = total + val
end
return total
end
function make_json_row(fullpagename, raw_data, ismulti)
local data = mw.text.jsonDecode(mw.text.decode(raw_data))
local row = {}
-- Extract the method name from the full page name
local pagename = string.match(fullpagename, 'Money making guide/(.*)')
if ismulti and data.version then
pagename = pagename .. ' (' .. data.version .. ')'
end
row["Method"] = pagename
row["Category"] = data.category or ""
row["Intensity"] = data.intensity or ""
row["Members"] = data.members
local inputval = calc_value(data.inputs, data.prices.default_kph or 0)
local outputval = calc_value(data.outputs, data.prices.default_kph or 0)
local val = outputval - inputval
row["Hourly profit"] = round1k(val, true)
return row
end
function parse_profit(profit_str)
-- Remove commas and convert to number for sorting
return tonumber((profit_str:gsub(",", "")))
end
function p.main(frame)
local args = frame:getParent().args
local query = {'[[Category:Worthwhile money making guides]]', '[[MMG JSON::+]]', '?MMG JSON', '?=#', limit=10000}
if args[1] then
table.insert(query, 2, '[[Category:'..args[1]..']]')
end
local data = mw.smw.ask(query)
local methods = {}
for _, v in ipairs(data) do
if string.find(v[1], 'Money making guide/') then
if type(v['MMG JSON']) == 'table' then
for _, u in ipairs(v['MMG JSON']) do
table.insert(methods, make_json_row(v[1], u, true))
end
else
table.insert(methods, make_json_row(v[1], v['MMG JSON'], false))
end
end
end
table.sort(methods, function(a, b)
return parse_profit(a["Hourly profit"]) > parse_profit(b["Hourly profit"])
end)
return mw.text.jsonEncode(methods)
end
function p.rec(frame)
local data = mw.smw.ask({'[[MMG recurring JSON::+]]', '[[MMG value::+]]', '?MMG recurring JSON', '?=#', limit=10000})
local methods = {}
for _, v in ipairs(data) do
if type(v['MMG recurring JSON']) == 'table' then
for _, u in ipairs(v['MMG recurring JSON']) do
table.insert(methods, make_json_row(v[1], u, true))
end
else
table.insert(methods, make_json_row(v[1], v['MMG recurring JSON'], false))
end
end
table.sort(methods, function(a, b)
return parse_profit(a["Hourly profit"]) > parse_profit(b["Hourly profit"])
end)
return mw.text.jsonEncode(methods)
end
return p
-- </nowiki>