Module:GeneralPortSectorSummary

From MicrasWiki
Revision as of 14:42, 17 March 2026 by NewZimiaGov (talk | contribs)
Jump to navigationJump to search

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

-- Module:GeneralPortSectorSummary
local p = {}

local sectors = {
	{
		label = "Agriculture",
		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 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 >= 5 then
		return "#d8f0d2", "#1f5f1f" -- stronger green
	elseif pct > 0 then
		return "#edf7e8", "#2f6f2f" -- light green
	elseif pct <= -5 then
		return "#f8d7d7", "#8b1e1e" -- stronger red
	elseif pct < 0 then
		return "#fbeaea", "#a33a3a" -- light red
	else
		return "#f3f3f3", "#555"
	end
end

local function trendWord(pct)
	if pct >= 5 then
		return "Rising"
	elseif pct > 0 then
		return "Slightly Rising"
	elseif pct <= -5 then
		return "Falling"
	elseif pct < 0 then
		return "Slightly Falling"
	else
		return "Stable"
	end
end

local function parseSummaryFromPage(pageName)
	local title = mw.title.new(pageName)
	if not title then
		return nil
	end

	local text = title:getContent() or ""
	if text == "" then
		return nil
	end

	local matches = {}

	-- Looks for lines like:
	-- Average adjusted (1.79) vs base (1.75): ↑ Low
	for adjusted, base, arrow, level in text:gmatch("Average adjusted %(([%d%.]+)%) vs base %(([%d%.]+)%)%:%s*([↑↓•])%s*([%a%s%(%)%-]+)") do
		table.insert(matches, {
			adjusted = tonumber(adjusted),
			base = tonumber(base),
			arrow = arrow,
			level = mw.text.trim(level or "")
		})
	end

	if #matches == 0 then
		return nil
	end

	-- Use the LAST summary line on the page
	local m = matches[#matches]

	local pct = 0
	if m.base and m.base ~= 0 then
		pct = ((m.adjusted - m.base) / m.base) * 100
	end

	return {
		adjusted = m.adjusted,
		base = m.base,
		arrow = m.arrow,
		level = m.level,
		pct = pct
	}
end

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

	for _, sector in ipairs(sectors) do
		local data = parseSummaryFromPage(sector.page)

		if data then
			local bg, fg = colorForPct(data.pct)

			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;" | Average adjusted (%.4f) vs base (%.4f): %s %s',
				bg, fg, data.adjusted, data.base, data.arrow, data.level
			))
			table.insert(out, string.format(
				'| style="background:%s; color:%s;" | %s',
				bg, fg, trendWord(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

	table.insert(out, '|}')
	table.insert(out, "''Average Daily Change'' = ((adjusted − base) ÷ base) × 100, based on the last summary row on each sector page.")
	return table.concat(out, "\n")
end

return p