Module:GeneralPortSectorSummary: Difference between revisions

From MicrasWiki
Jump to navigationJump to search
NewZimiaGov (talk | contribs)
No edit summary
NewZimiaGov (talk | contribs)
No edit summary
Line 1: Line 1:
-- Module:GeneralPortSectorSummary
local p = {}
local p = {}


local sectors = {
local ag = require("Module:AutoUpdateAgPrices")
{
local manf = require("Module:AutoUpdateManfPrices")
label = "Agriculture",
local serv = require("Module:AutoUpdateServPrices")
page = "General Port of Lake Morovia - Agriculture"
},
{
label = "Mining / Manufacturing / Energy",
page = "General Port of Lake Morovia - Mining/Manufacturing/Energy"
},
{
label = "Services",
page = "General Port of Lake Morovia - Services"
}
}


local function round(num, places)
local function round(num, places)
Line 29: Line 17:
return string.format("%.1f%%", num)
return string.format("%.1f%%", num)
else
else
return "0.0%"
return "0.0%%"
end
end
end
end


local function colorForPct(pct)
local function colorForPct(pct)
if pct >= 5 then
if pct > 0 then
return "#d8f0d2", "#1f5f1f"
return "#d8f0d2", "#1f5f1f"
elseif pct > 0 then
elseif pct < 0 then
return "#edf7e8", "#2f6f2f"
elseif pct <= -5 then
return "#f8d7d7", "#8b1e1e"
return "#f8d7d7", "#8b1e1e"
elseif pct < 0 then
return "#fbeaea", "#a33a3a"
else
else
return "#f3f3f3", "#555555"
return "#f3f3f3", "#555555"
Line 47: Line 31:
end
end


local function readingForPct(pct)
function p.main(frame)
if pct >= 5 then
local sectors = {
return "Rising"
ag.getAverageData(),
elseif pct > 0 then
manf.getAverageData(),
return "Slightly Rising"
serv.getAverageData()
elseif pct <= -5 then
return "Falling"
elseif pct < 0 then
return "Slightly Falling"
else
return "Stable"
end
end
 
local function getRenderedPageText(frame, pageName)
-- Pull rendered/transcluded output instead of raw source.
local ok, rendered = pcall(function()
return frame:preprocess("{{:" .. pageName .. "}}")
end)
 
if not ok or not rendered or rendered == "" then
return nil
end
 
-- Normalize whitespace a bit.
rendered = rendered:gsub("&nbsp;", " ")
rendered = rendered:gsub("%s+", " ")
 
return rendered
end
 
local function parseLastSummaryFromRendered(frame, pageName)
local text = getRenderedPageText(frame, pageName)
if not text then
return nil
end
 
local matches = {}
 
-- Capture the rendered summary line directly.
for adjusted, base, arrow, level in text:gmatch("Average adjusted %(([%d%.]+)%) vs base %(([%d%.]+)%)%:%s*([↑↓•])%s*([A-Za-z%s%(%)%-]+)") do
table.insert(matches, {
adjusted = tonumber(adjusted),
base = tonumber(base),
arrow = arrow,
level = mw.text.trim(level or "")
})
end
 
-- Fallback: just get the two numbers if arrow/level formatting varies.
if #matches == 0 then
for adjusted, base in text:gmatch("Average adjusted %(([%d%.]+)%) vs base %(([%d%.]+)%)") do
table.insert(matches, {
adjusted = tonumber(adjusted),
base = tonumber(base),
arrow = "",
level = ""
})
end
end
 
if #matches == 0 then
return nil
end
 
local m = matches[#matches]
if not m.base or m.base == 0 then
return nil
end
 
local pct = ((m.adjusted - m.base) / m.base) * 100
 
return {
adjusted = m.adjusted,
base = m.base,
arrow = m.arrow,
level = m.level,
pct = pct
}
}
end


function p.main(frame)
local out = {}
local out = {}
table.insert(out, '{| class="wikitable" style="width:100%; text-align:center;"')
table.insert(out, '{| class="wikitable" style="width:100%; text-align:center;"')
table.insert(out, '|-')
table.insert(out, '|-')
table.insert(out, '! Sector !! Average Daily Change !! Summary !! Reading')
table.insert(out, '! Sector !! Average Daily Change !! Summary')


for _, sector in ipairs(sectors) do
for _, row in ipairs(sectors) do
local data = parseLastSummaryFromRendered(frame, sector.page)
local bg, fg = colorForPct(row.pctChange)


if data then
table.insert(out, '|-')
local bg, fg = colorForPct(data.pct)
table.insert(out, '| style="text-align:left;" | ' .. row.label)
local summaryText = string.format(
table.insert(out, string.format(
"Average adjusted (%.4f) vs base (%.4f)%s",
'| style="background:%s; color:%s; font-weight:bold;" | %s',
data.adjusted,
bg, fg, fmtPct(row.pctChange)
data.base,
))
(data.arrow ~= "" and (": " .. data.arrow .. " " .. data.level) or "")
table.insert(out, string.format(
)
'| style="background:%s; color:%s;" | Average adjusted (%.4f) vs base (%.4f): %s %s',
 
bg, fg, row.avgNew, row.avgBase, row.avgSym, row.avgLevel
table.insert(out, '|-')
))
table.insert(out, '| style="text-align:left;" | ' .. sector.label)
table.insert(out, string.format(
'| style="background:%s; color:%s; font-weight:bold;" | %s',
bg, fg, fmtPct(data.pct)
))
table.insert(out, string.format(
'| style="background:%s; color:%s;" | %s',
bg, fg, summaryText
))
table.insert(out, string.format(
'| style="background:%s; color:%s;" | %s',
bg, fg, readingForPct(data.pct)
))
else
table.insert(out, '|-')
table.insert(out, '| style="text-align:left;" | ' .. sector.label)
table.insert(out, '| colspan="3" style="color:#888;" | No summary found')
end
end
end


table.insert(out, '|}')
table.insert(out, '|}')
table.insert(out, "''Average Daily Change'' = ((adjusted − base) ÷ base) × 100, using the rendered summary line from each sector page.")
return table.concat(out, "\n")
return table.concat(out, "\n")
end
function p.debug(frame)
local page = frame.args[1] or "General Port of Lake Morovia - Services"
local text = getRenderedPageText(frame, page)
if not text then
return "No rendered text found."
end
local startPos = text:find("Average adjusted", 1, true)
if not startPos then
return "Rendered page found, but no 'Average adjusted' string detected."
end
local snippet = text:sub(math.max(1, startPos - 120), math.min(#text, startPos + 220))
return "<pre>" .. mw.text.nowiki(snippet) .. "</pre>"
end
end


return p
return p

Revision as of 17:34, 20 March 2026

Documentation for this module may be created at Module:GeneralPortSectorSummary/doc

local p = {}

local ag = require("Module:AutoUpdateAgPrices")
local manf = require("Module:AutoUpdateManfPrices")
local serv = require("Module:AutoUpdateServPrices")

local function round(num, places)
	local mult = 10 ^ (places or 0)
	return math.floor(num * mult + 0.5) / mult
end

local function fmtPct(num)
	num = round(num, 1)
	if num > 0 then
		return string.format("+%.1f%%", num)
	elseif num < 0 then
		return string.format("%.1f%%", num)
	else
		return "0.0%%"
	end
end

local function colorForPct(pct)
	if pct > 0 then
		return "#d8f0d2", "#1f5f1f"
	elseif pct < 0 then
		return "#f8d7d7", "#8b1e1e"
	else
		return "#f3f3f3", "#555555"
	end
end

function p.main(frame)
	local sectors = {
		ag.getAverageData(),
		manf.getAverageData(),
		serv.getAverageData()
	}

	local out = {}
	table.insert(out, '{| class="wikitable" style="width:100%; text-align:center;"')
	table.insert(out, '|-')
	table.insert(out, '! Sector !! Average Daily Change !! Summary')

	for _, row in ipairs(sectors) do
		local bg, fg = colorForPct(row.pctChange)

		table.insert(out, '|-')
		table.insert(out, '| style="text-align:left;" | ' .. row.label)
		table.insert(out, string.format(
			'| style="background:%s; color:%s; font-weight:bold;" | %s',
			bg, fg, fmtPct(row.pctChange)
		))
		table.insert(out, string.format(
			'| style="background:%s; color:%s;" | Average adjusted (%.4f) vs base (%.4f): %s %s',
			bg, fg, row.avgNew, row.avgBase, row.avgSym, row.avgLevel
		))
	end

	table.insert(out, '|}')
	return table.concat(out, "\n")
end

return p