Module:GeneralPortSectorSummary: Difference between revisions

From MicrasWiki
Jump to navigationJump to search
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 ""))..."
 
NewZimiaGov (talk | contribs)
No edit summary
Line 4: Line 4:
local sectors = {
local sectors = {
{
{
key = "agriculture",
label = "Agriculture",
label = "Agriculture",
page = "General Port of Lake Morovia - Agriculture"
page = "General Port of Lake Morovia - Agriculture"
},
},
{
{
key = "industry",
label = "Mining / Manufacturing / Energy",
label = "Mining / Manufacturing / Energy",
page = "General Port of Lake Morovia - Mining/Manufacturing/Energy"
page = "General Port of Lake Morovia - Mining/Manufacturing/Energy"
},
},
{
{
key = "services",
label = "Services",
label = "Services",
page = "General Port of Lake Morovia - 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 function round(num, places)
Line 29: Line 22:
end
end


local function fmtSigned(num)
local function fmtPct(num)
num = round(num, 1)
num = round(num, 1)
if num > 0 then
if num > 0 then
Line 40: Line 33:
end
end


local function trendColor(score)
local function colorForPct(pct)
if score > 10 then
if pct >= 5 then
return "#dff3e3", "#1f6b2a"   -- strong green
return "#d8f0d2", "#1f5f1f" -- stronger green
elseif score > 0 then
elseif pct > 0 then
return "#eef8f0", "#2e7d32"   -- light green
return "#edf7e8", "#2f6f2f" -- light green
elseif score < -10 then
elseif pct <= -5 then
return "#fde7e7", "#a12622"   -- strong red
return "#f8d7d7", "#8b1e1e" -- stronger red
elseif score < 0 then
elseif pct < 0 then
return "#fdf1f1", "#b23b3b"   -- light red
return "#fbeaea", "#a33a3a" -- light red
else
else
return "#f3f3f3", "#555555"   -- neutral
return "#f3f3f3", "#555"
end
end
end
end


local function trendLabel(score)
local function trendWord(pct)
if score > 10 then
if pct >= 5 then
return "Rising"
return "Rising"
elseif score > 0 then
elseif pct > 0 then
return "Slightly Rising"
return "Slightly Rising"
elseif score < -10 then
elseif pct <= -5 then
return "Falling"
return "Falling"
elseif score < 0 then
elseif pct < 0 then
return "Slightly Falling"
return "Slightly Falling"
else
else
Line 68: Line 61:
end
end


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


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


local up, down, flat = 0, 0, 0
local matches = {}


-- Counts Δ cell markers in the source table.
-- Looks for lines like:
-- Assumes rows use ↑, ↓, or • as the fluctuation marker.
-- Average adjusted (1.79) vs base (1.75): ↑ Low
for symbol in text:gmatch("|%s*([↑↓•])%s*") do
for adjusted, base, arrow, level in text:gmatch("Average adjusted %(([%d%.]+)%) vs base %(([%d%.]+)%)%:%s*([↑↓•])%s*([%a%s%(%)%-]+)") do
if symbol == "↑" then
table.insert(matches, {
up = up + 1
adjusted = tonumber(adjusted),
elseif symbol == "↓" then
base = tonumber(base),
down = down + 1
arrow = arrow,
elseif symbol == "•" then
level = mw.text.trim(level or "")
flat = flat + 1
})
end
end
 
if #matches == 0 then
return nil
end
end


local total = up + down + flat
-- Use the LAST summary line on the page
local score = 0
local m = matches[#matches]


-- Net directional average:
local pct = 0
-- ↑ = +1, ↓ = -1, • = 0
if m.base and m.base ~= 0 then
-- converted to percent of total rows
pct = ((m.adjusted - m.base) / m.base) * 100
if total > 0 then
score = ((up - down) / total) * 100
end
end


return {
return {
page = pageName,
adjusted = m.adjusted,
up = up,
base = m.base,
down = down,
arrow = m.arrow,
flat = flat,
level = m.level,
total = total,
pct = pct
score = score
}
}
end
end
Line 116: Line 108:
function p.main(frame)
function p.main(frame)
local out = {}
local out = {}
table.insert(out, '{| class="wikitable sortable" 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 Fluctuation !! Up !! Down !! Flat !! Reading')
table.insert(out, '! Sector !! Average Daily Change !! Summary !! Reading')


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


table.insert(out, '|-')
table.insert(out, '|-')
table.insert(out, '| style="text-align:left;" | ' .. sector.label)
table.insert(out, '| style="text-align:left;" | ' .. sector.label)
table.insert(out, string.format(
table.insert(out, string.format(
'| style="background:%s; color:%s; font-weight:bold;" | %s',
'| style="background:%s; color:%s; font-weight:bold;" | %s',
bg, fg, fmtSigned(data.score)
bg, fg, fmtPct(data.pct)
))
))
table.insert(out, '| ' .. data.up)
table.insert(out, string.format(
table.insert(out, '| ' .. data.down)
'| style="background:%s; color:%s;" | Average adjusted (%.4f) vs base (%.4f): %s %s',
table.insert(out, '| ' .. data.flat)
bg, fg, data.adjusted, data.base, data.arrow, data.level
table.insert(out, string.format(
))
'| style="background:%s; color:%s;" | %s',
table.insert(out, string.format(
bg, fg, reading
'| 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
end


table.insert(out, '|}')
table.insert(out, '|}')
table.insert(out, "''Average Daily Fluctuation'' is a net directional index based on each table's Δ markers: ↑ = positive, ↓ = negative, • = unchanged.")
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")
return table.concat(out, "\n")
end
end


return p
return p

Revision as of 14:42, 17 March 2026

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