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
 
(5 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")
key = "agriculture",
local serv = require("Module:AutoUpdateServPrices")
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 function round(num, places)
Line 29: Line 10:
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 36: 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 trendColor(score)
local function colorForPct(pct)
if score > 10 then
if pct > 0 then
return "#dff3e3", "#1f6b2a"  -- strong green
return "#d8f0d2", "#1f5f1f"
elseif score > 0 then
elseif pct < 0 then
return "#eef8f0", "#2e7d32"  -- light green
return "#f8d7d7", "#8b1e1e"
elseif score < -10 then
return "#fde7e7", "#a12622"   -- strong red
elseif score < 0 then
return "#fdf1f1", "#b23b3b"   -- light red
else
else
return "#f3f3f3", "#555555"   -- neutral
return "#f3f3f3", "#555555"
end
end
end
end


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


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


local text = title:getContent() or ""
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


local up, down, flat = 0, 0, 0
function p.main(frame)
local sectors = {
ag.getAverageData(),
manf.getAverageData(),
serv.getAverageData()
}


-- Counts Δ cell markers in the source table.
local out = {}
-- Assumes rows use ↑, ↓, or • as the fluctuation marker.
table.insert(out, '{| class="wikitable" style="width:100%; text-align:center;"')
for symbol in text:gmatch("|%s*([↑↓•])%s*") do
table.insert(out, '|-')
if symbol == "↑" then
table.insert(out, '! Sector !! Average Daily Change !! Summary')
up = up + 1
elseif symbol == "↓" then
down = down + 1
elseif symbol == "•" then
flat = flat + 1
end
end


local total = up + down + flat
for _, row in ipairs(sectors) do
local score = 0
table.insert(out, renderRow(row))
 
-- Net directional average:
-- ↑ = +1, ↓ = -1, • = 0
-- converted to percent of total rows
if total > 0 then
score = ((up - down) / total) * 100
end
end


return {
table.insert(out, '|}')
page = pageName,
return table.concat(out, "\n")
up = up,
down = down,
flat = flat,
total = total,
score = score
}
end
end


function p.main(frame)
function p.row(frame)
local out = {}
local key = mw.text.trim((frame.args[1] or frame.args.sector or ""):lower())
table.insert(out, '{| class="wikitable sortable" style="width:100%; text-align:center;"')
local row = getSectorData(key)
table.insert(out, '|-')
table.insert(out, '! Sector !! Average Daily Fluctuation !! Up !! Down !! Flat !! Reading')


for _, sector in ipairs(sectors) do
if not row then
local data = parseSector(sector.page)
return "No valid sector specified."
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
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, '|}')
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")
return table.concat(out, "\n")

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