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 29: Line 29:
return string.format("%.1f%%", num)
return string.format("%.1f%%", num)
else
else
return "0.0%"
return "0.0%%"
end
end
end
end
Line 35: Line 35:
local function colorForPct(pct)
local function colorForPct(pct)
if pct >= 5 then
if pct >= 5 then
return "#d8f0d2", "#1f5f1f" -- stronger green
return "#d8f0d2", "#1f5f1f"
elseif pct > 0 then
elseif pct > 0 then
return "#edf7e8", "#2f6f2f" -- light green
return "#edf7e8", "#2f6f2f"
elseif pct <= -5 then
elseif pct <= -5 then
return "#f8d7d7", "#8b1e1e" -- stronger red
return "#f8d7d7", "#8b1e1e"
elseif pct < 0 then
elseif pct < 0 then
return "#fbeaea", "#a33a3a" -- light red
return "#fbeaea", "#a33a3a"
else
else
return "#f3f3f3", "#555"
return "#f3f3f3", "#555555"
end
end
end
end


local function trendWord(pct)
local function readingForPct(pct)
if pct >= 5 then
if pct >= 5 then
return "Rising"
return "Rising"
Line 63: Line 63:
local function parseSummaryFromPage(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 nil end
return nil
end


local text = title:getContent() or ""
local text = title:getContent() or ""
if text == "" then
if text == "" then return nil end
return nil
 
end
-- normalize some common source noise
text = text:gsub("&nbsp;", " ")
text = text:gsub("<.->", " ")
text = text:gsub("%[%[", "")
text = text:gsub("%]%]", "")
text = text:gsub("'''", "")
text = text:gsub("''", "")


local matches = {}
local matches = {}


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


-- Use the LAST summary line on the page
local m = matches[#matches]
local m = matches[#matches]
if not m.base or m.base == 0 then
return nil
end


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


return {
return {
adjusted = m.adjusted,
adjusted = m.adjusted,
base = m.base,
base = m.base,
arrow = m.arrow,
level = m.level,
pct = pct
pct = pct
}
}
Line 125: Line 123:
))
))
table.insert(out, string.format(
table.insert(out, string.format(
'| style="background:%s; color:%s;" | Average adjusted (%.4f) vs base (%.4f): %s %s',
'| style="background:%s; color:%s;" | Average adjusted (%.4f) vs base (%.4f)',
bg, fg, data.adjusted, data.base, data.arrow, data.level
bg, fg, data.adjusted, data.base
))
))
table.insert(out, string.format(
table.insert(out, string.format(
'| style="background:%s; color:%s;" | %s',
'| style="background:%s; color:%s;" | %s',
bg, fg, trendWord(data.pct)
bg, fg, readingForPct(data.pct)
))
))
else
else
Line 140: Line 138:


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


return p
return p

Revision as of 14:45, 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"
	elseif pct > 0 then
		return "#edf7e8", "#2f6f2f"
	elseif pct <= -5 then
		return "#f8d7d7", "#8b1e1e"
	elseif pct < 0 then
		return "#fbeaea", "#a33a3a"
	else
		return "#f3f3f3", "#555555"
	end
end

local function readingForPct(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

	-- normalize some common source noise
	text = text:gsub("&nbsp;", " ")
	text = text:gsub("<.->", " ")
	text = text:gsub("%[%[", "")
	text = text:gsub("%]%]", "")
	text = text:gsub("'''", "")
	text = text:gsub("''", "")

	local matches = {}

	-- broad match: only capture the two numbers
	for adjusted, base in text:gmatch("Average%s*adjusted%s*%(([%d%.]+)%)%s*vs%s*base%s*%(([%d%.]+)%)") do
		table.insert(matches, {
			adjusted = tonumber(adjusted),
			base = tonumber(base)
		})
	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,
		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)',
				bg, fg, data.adjusted, data.base
			))
			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

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

return p