Module:GeneralPortSectorSummary: Difference between revisions

From MicrasWiki
Jump to navigationJump to search
NewZimiaGov (talk | contribs)
No edit summary
NewZimiaGov (talk | contribs)
No edit summary
 
(3 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 34: Line 22:


local function colorForPct(pct)
local function colorForPct(pct)
if pct >= 5 then
if pct > 0 then
return "#d8f0d2", "#1f5f1f"
return "#d8f0d2", "#1f5f1f"
elseif pct > 0 then
elseif pct < 0 then
return "#edf7e8", "#2f6f2f"
elseif pct <= -5 then
return "#f8d7d7", "#8b1e1e"
return "#f8d7d7", "#8b1e1e"
elseif pct < 0 then
return "#fbeaea", "#a33a3a"
else
else
return "#f3f3f3", "#555555"
return "#f3f3f3", "#555555"
Line 47: Line 31:
end
end


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


local text = title:getContent() or ""
return table.concat({
if text == "" then return nil 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


-- normalize some common source noise
function p.main(frame)
text = text:gsub("&nbsp;", " ")
local sectors = {
text = text:gsub("<.->", " ")
ag.getAverageData(),
text = text:gsub("%[%[", "")
manf.getAverageData(),
text = text:gsub("%]%]", "")
serv.getAverageData()
text = text:gsub("'''", "")
}
text = text:gsub("''", "")


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')


-- broad match: only capture the two numbers
for _, row in ipairs(sectors) do
for adjusted, base in text:gmatch("Average%s*adjusted%s*%(([%d%.]+)%)%s*vs%s*base%s*%(([%d%.]+)%)") do
table.insert(out, renderRow(row))
table.insert(matches, {
adjusted = tonumber(adjusted),
base = tonumber(base)
})
end
end


if #matches == 0 then
table.insert(out, '|}')
return nil
return table.concat(out, "\n")
end
end
 
function p.row(frame)
local key = mw.text.trim((frame.args[1] or frame.args.sector or ""):lower())
local row = getSectorData(key)


local m = matches[#matches]
if not row then
if not m.base or m.base == 0 then
return "No valid sector specified."
return nil
end
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 = {}
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)',
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")
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