Module:GeneralPortSectorSummary: Difference between revisions

From MicrasWiki
Jump to navigationJump to search
NewZimiaGov (talk | contribs)
No edit summary
NewZimiaGov (talk | contribs)
No edit summary
 
(4 intermediate revisions by the same user not shown)
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" -- stronger green
return "#d8f0d2", "#1f5f1f"
elseif pct > 0 then
return "#edf7e8", "#2f6f2f" -- light green
elseif pct <= -5 then
return "#f8d7d7", "#8b1e1e" -- stronger red
elseif pct < 0 then
elseif pct < 0 then
return "#fbeaea", "#a33a3a" -- light red
return "#f8d7d7", "#8b1e1e"
else
else
return "#f3f3f3", "#555"
return "#f3f3f3", "#555555"
end
end
end
end


local function trendWord(pct)
local function getSectorData(key)
if pct >= 5 then
if key == "agriculture" or key == "ag" then
return "Rising"
return ag.getAverageData()
elseif pct > 0 then
elseif key == "industry" or key == "manufacturing" or key == "manf" then
return "Slightly Rising"
return manf.getAverageData()
elseif pct <= -5 then
elseif key == "services" or key == "serv" then
return "Falling"
return serv.getAverageData()
elseif pct < 0 then
return "Slightly Falling"
else
return "Stable"
end
end
return nil
end
end


local function parseSummaryFromPage(pageName)
local function renderRow(row)
local title = mw.title.new(pageName)
local bg, fg = colorForPct(row.pctChange)
if not title then
 
return nil
return table.concat({
end
'|-',
'| style="text-align:left;" | ' .. row.label,
string.format(
'| style="background:%s; color:%s; font-weight:bold;" | %s',
bg, fg, fmtPct(row.pctChange)
),
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
)
}, "\n")
end


local text = title:getContent() or ""
function p.main(frame)
if text == "" then
local sectors = {
return nil
ag.getAverageData(),
end
manf.getAverageData(),
serv.getAverageData()
}


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


-- Looks for lines like:
for _, row in ipairs(sectors) do
-- Average adjusted (1.79) vs base (1.75): ↑ Low
table.insert(out, renderRow(row))
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
end


if #matches == 0 then
table.insert(out, '|}')
return nil
return table.concat(out, "\n")
end
end


-- Use the LAST summary line on the page
function p.row(frame)
local m = matches[#matches]
local key = mw.text.trim((frame.args[1] or frame.args.sector or ""):lower())
local row = getSectorData(key)


local pct = 0
if not row then
if m.base and m.base ~= 0 then
return "No valid sector specified."
pct = ((m.adjusted - m.base) / m.base) * 100
end
end


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')
 
table.insert(out, renderRow(row))
for _, sector in ipairs(sectors) do
table.insert(out, '|}')
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")
return table.concat(out, "\n")
end
end


return p
return p

Latest revision as of 17:40, 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

local function getSectorData(key)
	if key == "agriculture" or key == "ag" then
		return ag.getAverageData()
	elseif key == "industry" or key == "manufacturing" or key == "manf" then
		return manf.getAverageData()
	elseif key == "services" or key == "serv" then
		return serv.getAverageData()
	end
	return nil
end

local function renderRow(row)
	local bg, fg = colorForPct(row.pctChange)

	return table.concat({
		'|-',
		'| style="text-align:left;" | ' .. row.label,
		string.format(
			'| style="background:%s; color:%s; font-weight:bold;" | %s',
			bg, fg, fmtPct(row.pctChange)
		),
		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
		)
	}, "\n")
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
		table.insert(out, renderRow(row))
	end

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

function p.row(frame)
	local key = mw.text.trim((frame.args[1] or frame.args.sector or ""):lower())
	local row = getSectorData(key)

	if not row then
		return "No valid sector specified."
	end

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

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

return p