Module:GeneralPortSectorSummary

From MicrasWiki
Revision as of 14:38, 17 March 2026 by NewZimiaGov (talk | contribs) (Created page with "-- Module:GeneralPortSectorSummary local p = {} local sectors = { { key = "agriculture", label = "Agriculture", page = "General Port of Lake Morovia - Agriculture" }, { key = "industry", label = "Mining / Manufacturing / Energy", page = "General Port of Lake Morovia - Mining/Manufacturing/Energy" }, { key = "services", label = "Services", page = "General Port of Lake Morovia - Services" } } local function trim(s) return (mw.text.trim(s or ""))...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

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

-- Module:GeneralPortSectorSummary
local p = {}

local sectors = {
	{
		key = "agriculture",
		label = "Agriculture",
		page = "General Port of Lake Morovia - Agriculture"
	},
	{
		key = "industry",
		label = "Mining / Manufacturing / Energy",
		page = "General Port of Lake Morovia - Mining/Manufacturing/Energy"
	},
	{
		key = "services",
		label = "Services",
		page = "General Port of Lake Morovia - Services"
	}
}

local function trim(s)
	return (mw.text.trim(s or ""))
end

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

local function fmtSigned(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 trendColor(score)
	if score > 10 then
		return "#dff3e3", "#1f6b2a"   -- strong green
	elseif score > 0 then
		return "#eef8f0", "#2e7d32"   -- light green
	elseif score < -10 then
		return "#fde7e7", "#a12622"   -- strong red
	elseif score < 0 then
		return "#fdf1f1", "#b23b3b"   -- light red
	else
		return "#f3f3f3", "#555555"   -- neutral
	end
end

local function trendLabel(score)
	if score > 10 then
		return "Rising"
	elseif score > 0 then
		return "Slightly Rising"
	elseif score < -10 then
		return "Falling"
	elseif score < 0 then
		return "Slightly Falling"
	else
		return "Stable"
	end
end

local function parseSector(pageName)
	local title = mw.title.new(pageName)
	if not title then
		return {
			page = pageName,
			up = 0, down = 0, flat = 0, total = 0,
			score = 0
		}
	end

	local text = title:getContent() or ""

	local up, down, flat = 0, 0, 0

	-- Counts Δ cell markers in the source table.
	-- Assumes rows use ↑, ↓, or • as the fluctuation marker.
	for symbol in text:gmatch("|%s*([↑↓•])%s*") do
		if symbol == "↑" then
			up = up + 1
		elseif symbol == "↓" then
			down = down + 1
		elseif symbol == "•" then
			flat = flat + 1
		end
	end

	local total = up + down + flat
	local score = 0

	-- Net directional average:
	-- ↑ = +1, ↓ = -1, • = 0
	-- converted to percent of total rows
	if total > 0 then
		score = ((up - down) / total) * 100
	end

	return {
		page = pageName,
		up = up,
		down = down,
		flat = flat,
		total = total,
		score = score
	}
end

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

	for _, sector in ipairs(sectors) do
		local data = parseSector(sector.page)
		local bg, fg = trendColor(data.score)
		local reading = trendLabel(data.score)

		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, fmtSigned(data.score)
		))
		table.insert(out, '| ' .. data.up)
		table.insert(out, '| ' .. data.down)
		table.insert(out, '| ' .. data.flat)
		table.insert(out, string.format(
			'| style="background:%s; color:%s;" | %s',
			bg, fg, reading
		))
	end

	table.insert(out, '|}')
	table.insert(out, "''Average Daily Fluctuation'' is a net directional index based on each table's Δ markers: ↑ = positive, ↓ = negative, • = unchanged.")

	return table.concat(out, "\n")
end

return p