Module:BassaridEmployment: Difference between revisions

From MicrasWiki
Jump to navigationJump to search
NewZimiaGov (talk | contribs)
No edit summary
NewZimiaGov (talk | contribs)
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


local market = require("Module:BassaridMarketActivity")
---------------------------------------------------------
-- Module:BassaridEmployment
--
-- What this module does
--  • Computes a daily "previous vs current" employment estimate per company
--  • Uses deterministic per-company variation so:
--      - results are stable for a given day
--      - results do NOT depend on loop order / table order
--  • Normalizes employment percentages so they behave as weights
--  • Uses a lower default daily swing for more realistic totals
---------------------------------------------------------


local CITY_POP = {
---------------------------------------------------------
["Vaeringheim"] = 20887650.86,
-- 1) getCurrentDate
["Lunalis Sancta"] = 7832869.07,
---------------------------------------------------------
["Luminaria"] = 6382335.99,
local function getCurrentDate()
["Serena"] = 6672442.61,
local startDate = os.time({ year = 1999, month = 8, day = 6 })
["Symphonara"] = 4351592.87,
local secondsInDay = 86400
["Aurelia"] = 3771379.63,
local daysPerYear = 183
["Sylvapolis"] = 3481276.21,
local currentDate = os.time()
["Delphica"] = 2030743.12,
local totalDaysElapsed = math.floor((currentDate - startDate) / secondsInDay)
["Catonis Atrium"] = 1682617.10,
local yearFraction = totalDaysElapsed / daysPerYear
["Ferrum Citadel"] = 928339.26,
local psscYear = math.floor(yearFraction)
["Pyralis"] = 12965917.88,
local dayOfYear = math.floor((yearFraction - psscYear) * daysPerYear) + 1
["Nexa"] = 7979025.10,
local month, dayOfMonth
["Saluria"] = 7729680.74,
 
["Acheron"] = 4238856.91,
if dayOfYear <= 61 then
["Koinonía"] = 5485581.50,
month = 1
["Aureum"] = 4488201.27,
dayOfMonth = dayOfYear
["Aetherium"] = 3740168.19,
elseif dayOfYear <= 122 then
["Erythros"] = 3241479.47,
month = 2
["Somniumpolis"] = 16035032.55,
dayOfMonth = dayOfYear - 61
["Hafaan"] = 3568579.65,
else
["Jeseri"] = 2430778.52,
month = 3
["Skýrophos"] = 5132630.96,
dayOfMonth = dayOfYear - 122
["Bjornopolis"] = 4016840.25,
end
["Aegerheim"] = 3347368.98,
 
["Norsolyra"] = 2901052.70,
return string.format("%d/%d/%d [[PSSC]]", dayOfMonth, month, psscYear), dayOfMonth, month, psscYear, dayOfYear
["Thorsalon"] = 2454736.41,
end
["Myrene"] = 1338945.70,
 
["Ephyra"] = 1115790.71,
---------------------------------------------------------
["Thyrea"] = 892632.57,
-- 2) Company table
["Pelagia"] = 669474.43,
---------------------------------------------------------
["Halicarn"] = 446316.28,
local companies = {
["Keybir-Aviv"] = 4592562.31,
{name = "Lake Morovia Blockade Fund", employmentPercentage = 0.0300},
["Tel-Amin"] = 4155174.90,
{name = "Gladeseed Farmers' Union", employmentPercentage = 0.1205},
["Jogi"] = 3936483.03,
{name = "Atterian Cattle Drivers", employmentPercentage = 0.0502},
["Lewisburg"] = 3280401.91,
{name = "Roving Wind Farm Corporation", employmentPercentage = 0.0210},
["Diamandis"] = 2843014.50,
{name = "Baratar Corporation", employmentPercentage = 0.0401},
["Thermosalem"] = 1312160.40,
{name = "Noctic Fleet", employmentPercentage = 0.0603},
["Akróstadium"] = 1093466.69,
{name = "Keepers of the Bloodbaths of Laprind", employmentPercentage = 0.0105},
["Lykopolis"] = 437387.41,
{name = "Jogiani Merchants of Manwine", employmentPercentage = 0.0208},
["Sufriya"] = 218693.71,
{name = "Keepers of the Grove of Zoe Elm", employmentPercentage = 0.0112},
["Riddersborg"] = 11757190.77,
{name = "Plains of Jogi", employmentPercentage = 0.0807},
["Ardclach"] = 5426395.09,
{name = "Ale of the Night Central Brewery", employmentPercentage = 0.0201},
["Ourid"] = 31826487.08,
{name = "Camel Herders of the Jangsong Province", employmentPercentage = 0.0306},
["Bashkim"] = 23046766.50
{name = "Thalassian Temple Granite Export Cooperative", employmentPercentage = 0.0310},
{name = "East Keltian Iron Company", employmentPercentage = 0.0415},
{name = "Mylecian Coal Ports", employmentPercentage = 0.0317},
{name = "Merchants of the Valley of Diamonds", employmentPercentage = 0.0102},
{name = "Maritime Guild of the Cult of Maskmakers", employmentPercentage = 0.0109},
{name = "Hatch Ministry", employmentPercentage = 0.0203},
{name = "Anterran Imports and Services", employmentPercentage = 0.0113},
{name = "East Keltian Timber Company", employmentPercentage = 0.0312},
{name = "Grand Cave Bee Meadery", employmentPercentage = 0.0215},
{name = "Merchant Guild of the Poppy Goddess", employmentPercentage = 0.0111},
{name = "Cinnamon Plains of Rouge", employmentPercentage = 0.0218},
{name = "Seamstresses of Rouge", employmentPercentage = 0.0114},
{name = "Salurian Temple of Sacred Horehound", employmentPercentage = 0.0212},
{name = "Suncliff Fisheries", employmentPercentage = 0.0309},
{name = "Bird Keepers of Saluria", employmentPercentage = 0.0108},
{name = "Velvet Homestead", employmentPercentage = 0.0110},
{name = "Hunters of the Giant Fanged Penguin", employmentPercentage = 0.0116},
{name = "Professional Pillarion League", employmentPercentage = 0.0052},
{name = "La Sái Ebile", employmentPercentage = 0.0104},
{name = "Temple Bank of the Reformed Stripping Path", employmentPercentage = 0.0118},
{name = "Market Hanavusu", employmentPercentage = 0.0157},
{name = "Couriers of the Lizard Queen", employmentPercentage = 0.0117},
{name = "Temple University of Delphica", employmentPercentage = 0.0209},
{name = "Trans-Morovian Railway Company", employmentPercentage = 0.0155},
}
}


---------------------------------------------------------
-- 3) Normalize the company percentages into weights
---------------------------------------------------------
local totalEmploymentWeight = 0
for _, company in ipairs(companies) do
totalEmploymentWeight = totalEmploymentWeight + company.employmentPercentage
end
---------------------------------------------------------
-- 4) Deterministic RNG helpers
---------------------------------------------------------
local MOD = 2147483647
local MOD = 2147483647
local A   = 1103515245
local A = 1103515245
local C   = 12345
local C = 12345


local function hashString(s)
local function hashString(s)
Line 71: Line 115:
end
end


local function randUnit(nextInt)
local function randIntInclusive(nextInt, lo, hi)
return (nextInt() % 100000) / 100000
if hi < lo then lo, hi = hi, lo end
local span = (hi - lo) + 1
local r = (nextInt() + nextInt()) % MOD
return lo + (r % span)
end
end


local function clamp(x, lo, hi)
---------------------------------------------------------
if x < lo then return lo end
-- 5) Per-company calculation
if x > hi then return hi end
---------------------------------------------------------
return x
local function calculateEmployees(company, employedPopulation, maxChangeFrac, dailySeed)
end
local normalizedShare = company.employmentPercentage / totalEmploymentWeight
local base = math.floor(employedPopulation * normalizedShare)


local function round(num)
local h = hashString(company.name)
return math.floor(num + 0.5)
local nextInt = lcg(dailySeed + h)
end


local function getCurrentDate()
local maxChange = math.floor(base * maxChangeFrac)
local startDate = os.time({ year = 1999, month = 8, day = 6 })
if maxChange < 0 then maxChange = 0 end
local secondsInDay = 86400
local daysPerYear = 183
local currentDate = os.time()
local totalDaysElapsed = math.floor((currentDate - startDate) / secondsInDay)
local yearFraction = totalDaysElapsed / daysPerYear
local psscYear = math.floor(yearFraction)
local dayOfYear = math.floor((yearFraction - psscYear) * daysPerYear) + 1
local month, dayOfMonth
if dayOfYear <= 61 then
month = 1; dayOfMonth = dayOfYear
elseif dayOfYear <= 122 then
month = 2; dayOfMonth = dayOfYear - 61
else
month = 3; dayOfMonth = dayOfYear - 122
end
return string.format("%d/%d/%d [[PSSC]]", dayOfMonth, month, psscYear)
end


local function getSectorModule(sector)
local changeAmount = 0
sector = tostring(sector or "agriculture"):lower()
if maxChange > 0 then
if sector == "agriculture" or sector == "ag" then
changeAmount = randIntInclusive(nextInt, -maxChange, maxChange)
return require("Module:AutoUpdateAgPrices"), "Agriculture"
elseif sector == "industry" or sector == "manufacturing" or sector == "manf" then
return require("Module:AutoUpdateManfPrices"), "Mining / Manufacturing / Energy"
elseif sector == "services" or sector == "serv" then
return require("Module:AutoUpdateServPrices"), "Services"
end
end
return nil, nil
end


local function getEmploymentSignal(city, cache)
local prevEmployees = base
city = city or "Vaeringheim"
local currEmployees = base + changeAmount
if cache[city] then
if currEmployees < 1 then
return cache[city]
currEmployees = 1
end
end


local sig
return prevEmployees, currEmployees, normalizedShare
if market.getEmploymentSignals then
sig = market.getEmploymentSignals(city)
else
sig = { marketScore = 0, volatility = 0 }
end
 
cache[city] = sig
return sig
end
 
local function computeWeights(companies)
local total = 0
for _, co in ipairs(companies) do
local cityPop = CITY_POP[co.city] or 1000000
local cityFactor = math.max(0.60, math.min(1.50, cityPop / 5000000))
local weight = (co.count or 1) * math.max(0.01, co.avgBase or 0.01) * cityFactor
co.weight = weight
total = total + weight
end
 
for _, co in ipairs(companies) do
co.weight = co.weight / total
end
end
end


---------------------------------------------------------
-- 6) generateTable
---------------------------------------------------------
function p.generateTable(frame)
function p.generateTable(frame)
frame = frame or {}
frame = frame or {}
Line 154: Line 157:
local fargs = frame.args or {}
local fargs = frame.args or {}


local sectorArg = fargs.sector or args.sector or "agriculture"
local population = tonumber(fargs.population or args.population) or 55499301
local population = tonumber(fargs.population or args.population) or 293833483.50
local unemploymentRate = tonumber(fargs.unemployment or args.unemployment) or 0.04
local unemploymentRate = tonumber(fargs.unemployment or args.unemployment) or 0.04
local maxChangeFrac = tonumber(fargs.maxChange or args.maxChange) or 0.04


local sectorModule, sectorLabel = getSectorModule(sectorArg)
if unemploymentRate < 0 then unemploymentRate = 0 end
if not sectorModule then
if unemploymentRate > 1 then unemploymentRate = 1 end
return "Invalid sector. Use agriculture, industry, or services."
if maxChangeFrac < 0 then maxChangeFrac = 0 end
end
if maxChangeFrac > 1 then maxChangeFrac = 1 end
if not sectorModule.getCompanyEmploymentInputs then
return "This sector module does not expose getCompanyEmploymentInputs() yet."
end


local employedPopulation = math.floor(population * (1 - unemploymentRate))
local employedPopulation = math.floor(population * (1 - unemploymentRate))
local dateString = getCurrentDate()
local localTime = os.date("*t")
local localTime = os.date("*t")
local dailySeed = localTime.year * 1000 + localTime.yday
local dailySeed = localTime.year * 1000 + localTime.yday
local dateString = getCurrentDate()


local companies = sectorModule.getCompanyEmploymentInputs()
computeWeights(companies)
table.sort(companies, function(a, b) return a.name < b.name end)
local signalCache = {}
local out = {}
local out = {}
out[#out + 1] = '{| class="wikitable sortable"\n'
out[#out + 1] = '{| class="wikitable sortable"\n'
out[#out + 1] = '! Date !! Sector !! Company !! City !! Price Change !! Previous Employees !! Current Employees !! Change\n'
out[#out + 1] = '! Date !! Company !! Employment % !! Previous Employees !! Current Employees !! Change\n'


local totalEmployed = 0
local totalEmployed = 0


for _, co in ipairs(companies) do
for _, company in ipairs(companies) do
local baseEmployees = round(employedPopulation * co.weight)
local prevE, currE, normalizedShare = calculateEmployees(company, employedPopulation, maxChangeFrac, dailySeed)


local sig = getEmploymentSignal(co.city, signalCache)
local changeType
local nextInt = lcg(dailySeed + hashString((co.name or "") .. "|" .. (co.city or "")))
if currE > prevE then
local jitter = (randUnit(nextInt) - 0.5) * 0.04
changeType = "Increased"
local priceEffect = (tonumber(co.pctChange) or 0) * 0.30 / 100
elseif currE < prevE then
local marketEffect = sig.marketScore or 0
changeType = "Decreased"
local volDrag = (sig.volatility or 0) * 0.03
else
changeType = "Same"
end


local netEffect = clamp(priceEffect + marketEffect + jitter - volDrag, -0.18, 0.18)
local rowStyle
local currEmployees = round(baseEmployees * (1 + netEffect))
if changeType == "Increased" then
if currEmployees < 1 then currEmployees = 1 end
rowStyle = ' style="background-color:lightblue;"'
 
elseif changeType == "Same" then
local changeText = "Same"
rowStyle = ' style="background-color:lightgreen;"'
local rowStyle = ' style="background-color:#f3f3f3;"'
else
if currEmployees > baseEmployees then
rowStyle = ' style="background-color:#ffcccb;"'
changeText = "Increased"
rowStyle = ' style="background-color:#d8f0d2;"'
elseif currEmployees < baseEmployees then
changeText = "Decreased"
rowStyle = ' style="background-color:#f8d7d7;"'
end
end


totalEmployed = totalEmployed + currEmployees
totalEmployed = totalEmployed + currE


out[#out + 1] = string.format(
out[#out + 1] = string.format(
'|-%s\n| %s || %s || %s || %s || %+0.2f%% || %d || %d || %s\n',
'|-%s\n| %s || %s || %.2f%% || %d || %d || %s\n',
rowStyle,
rowStyle,
dateString,
dateString,
sectorLabel,
company.name,
co.name,
normalizedShare * 100,
co.city or "",
prevE,
co.pctChange or 0,
currE,
baseEmployees,
changeType
currEmployees,
changeText
)
)
end
end


local totalUnemployed = math.max(0, math.floor(population - totalEmployed))
local totalUnemployed = population - totalEmployed
out[#out + 1] = string.format('|-\n! colspan="6" | Total Employed || %d || \n', totalEmployed)
if totalUnemployed < 0 then totalUnemployed = 0 end
out[#out + 1] = string.format('|-\n! colspan="6" | Total Unemployed || %d || \n', totalUnemployed)
 
out[#out + 1] = string.format('|-\n! colspan="4" | Total Employed || %d || \n', totalEmployed)
out[#out + 1] = string.format('|-\n! colspan="4" | Total Unemployed || %d || \n', totalUnemployed)
out[#out + 1] = '|}'
out[#out + 1] = '|}'



Revision as of 18:20, 20 March 2026

Documentation for this module may be created at Module:BassaridEmployment/doc

local p = {}

---------------------------------------------------------
-- Module:BassaridEmployment
--
-- What this module does
--  • Computes a daily "previous vs current" employment estimate per company
--  • Uses deterministic per-company variation so:
--      - results are stable for a given day
--      - results do NOT depend on loop order / table order
--  • Normalizes employment percentages so they behave as weights
--  • Uses a lower default daily swing for more realistic totals
---------------------------------------------------------

---------------------------------------------------------
-- 1) getCurrentDate
---------------------------------------------------------
local function getCurrentDate()
	local startDate = os.time({ year = 1999, month = 8, day = 6 })
	local secondsInDay = 86400
	local daysPerYear = 183
	local currentDate = os.time()
	local totalDaysElapsed = math.floor((currentDate - startDate) / secondsInDay)
	local yearFraction = totalDaysElapsed / daysPerYear
	local psscYear = math.floor(yearFraction)
	local dayOfYear = math.floor((yearFraction - psscYear) * daysPerYear) + 1
	local month, dayOfMonth

	if dayOfYear <= 61 then
		month = 1
		dayOfMonth = dayOfYear
	elseif dayOfYear <= 122 then
		month = 2
		dayOfMonth = dayOfYear - 61
	else
		month = 3
		dayOfMonth = dayOfYear - 122
	end

	return string.format("%d/%d/%d [[PSSC]]", dayOfMonth, month, psscYear), dayOfMonth, month, psscYear, dayOfYear
end

---------------------------------------------------------
-- 2) Company table
---------------------------------------------------------
local companies = {
	{name = "Lake Morovia Blockade Fund", employmentPercentage = 0.0300},
	{name = "Gladeseed Farmers' Union", employmentPercentage = 0.1205},
	{name = "Atterian Cattle Drivers", employmentPercentage = 0.0502},
	{name = "Roving Wind Farm Corporation", employmentPercentage = 0.0210},
	{name = "Baratar Corporation", employmentPercentage = 0.0401},
	{name = "Noctic Fleet", employmentPercentage = 0.0603},
	{name = "Keepers of the Bloodbaths of Laprind", employmentPercentage = 0.0105},
	{name = "Jogiani Merchants of Manwine", employmentPercentage = 0.0208},
	{name = "Keepers of the Grove of Zoe Elm", employmentPercentage = 0.0112},
	{name = "Plains of Jogi", employmentPercentage = 0.0807},
	{name = "Ale of the Night Central Brewery", employmentPercentage = 0.0201},
	{name = "Camel Herders of the Jangsong Province", employmentPercentage = 0.0306},
	{name = "Thalassian Temple Granite Export Cooperative", employmentPercentage = 0.0310},
	{name = "East Keltian Iron Company", employmentPercentage = 0.0415},
	{name = "Mylecian Coal Ports", employmentPercentage = 0.0317},
	{name = "Merchants of the Valley of Diamonds", employmentPercentage = 0.0102},
	{name = "Maritime Guild of the Cult of Maskmakers", employmentPercentage = 0.0109},
	{name = "Hatch Ministry", employmentPercentage = 0.0203},
	{name = "Anterran Imports and Services", employmentPercentage = 0.0113},
	{name = "East Keltian Timber Company", employmentPercentage = 0.0312},
	{name = "Grand Cave Bee Meadery", employmentPercentage = 0.0215},
	{name = "Merchant Guild of the Poppy Goddess", employmentPercentage = 0.0111},
	{name = "Cinnamon Plains of Rouge", employmentPercentage = 0.0218},
	{name = "Seamstresses of Rouge", employmentPercentage = 0.0114},
	{name = "Salurian Temple of Sacred Horehound", employmentPercentage = 0.0212},
	{name = "Suncliff Fisheries", employmentPercentage = 0.0309},
	{name = "Bird Keepers of Saluria", employmentPercentage = 0.0108},
	{name = "Velvet Homestead", employmentPercentage = 0.0110},
	{name = "Hunters of the Giant Fanged Penguin", employmentPercentage = 0.0116},
	{name = "Professional Pillarion League", employmentPercentage = 0.0052},
	{name = "La Sái Ebile", employmentPercentage = 0.0104},
	{name = "Temple Bank of the Reformed Stripping Path", employmentPercentage = 0.0118},
	{name = "Market Hanavusu", employmentPercentage = 0.0157},
	{name = "Couriers of the Lizard Queen", employmentPercentage = 0.0117},
	{name = "Temple University of Delphica", employmentPercentage = 0.0209},
	{name = "Trans-Morovian Railway Company", employmentPercentage = 0.0155},
}

---------------------------------------------------------
-- 3) Normalize the company percentages into weights
---------------------------------------------------------
local totalEmploymentWeight = 0
for _, company in ipairs(companies) do
	totalEmploymentWeight = totalEmploymentWeight + company.employmentPercentage
end

---------------------------------------------------------
-- 4) Deterministic RNG helpers
---------------------------------------------------------
local MOD = 2147483647
local A = 1103515245
local C = 12345

local function hashString(s)
	s = tostring(s or "")
	local h = 0
	for i = 1, #s do
		h = (h * 31 + s:byte(i)) % MOD
	end
	return h
end

local function lcg(seed)
	seed = seed % MOD
	return function()
		seed = (A * seed + C) % MOD
		return seed
	end
end

local function randIntInclusive(nextInt, lo, hi)
	if hi < lo then lo, hi = hi, lo end
	local span = (hi - lo) + 1
	local r = (nextInt() + nextInt()) % MOD
	return lo + (r % span)
end

---------------------------------------------------------
-- 5) Per-company calculation
---------------------------------------------------------
local function calculateEmployees(company, employedPopulation, maxChangeFrac, dailySeed)
	local normalizedShare = company.employmentPercentage / totalEmploymentWeight
	local base = math.floor(employedPopulation * normalizedShare)

	local h = hashString(company.name)
	local nextInt = lcg(dailySeed + h)

	local maxChange = math.floor(base * maxChangeFrac)
	if maxChange < 0 then maxChange = 0 end

	local changeAmount = 0
	if maxChange > 0 then
		changeAmount = randIntInclusive(nextInt, -maxChange, maxChange)
	end

	local prevEmployees = base
	local currEmployees = base + changeAmount
	if currEmployees < 1 then
		currEmployees = 1
	end

	return prevEmployees, currEmployees, normalizedShare
end

---------------------------------------------------------
-- 6) generateTable
---------------------------------------------------------
function p.generateTable(frame)
	frame = frame or {}
	local args = (frame.getParent and frame:getParent() and frame:getParent().args) or {}
	local fargs = frame.args or {}

	local population = tonumber(fargs.population or args.population) or 55499301
	local unemploymentRate = tonumber(fargs.unemployment or args.unemployment) or 0.04
	local maxChangeFrac = tonumber(fargs.maxChange or args.maxChange) or 0.04

	if unemploymentRate < 0 then unemploymentRate = 0 end
	if unemploymentRate > 1 then unemploymentRate = 1 end
	if maxChangeFrac < 0 then maxChangeFrac = 0 end
	if maxChangeFrac > 1 then maxChangeFrac = 1 end

	local employedPopulation = math.floor(population * (1 - unemploymentRate))
	local localTime = os.date("*t")
	local dailySeed = localTime.year * 1000 + localTime.yday
	local dateString = getCurrentDate()

	local out = {}
	out[#out + 1] = '{| class="wikitable sortable"\n'
	out[#out + 1] = '! Date !! Company !! Employment % !! Previous Employees !! Current Employees !! Change\n'

	local totalEmployed = 0

	for _, company in ipairs(companies) do
		local prevE, currE, normalizedShare = calculateEmployees(company, employedPopulation, maxChangeFrac, dailySeed)

		local changeType
		if currE > prevE then
			changeType = "Increased"
		elseif currE < prevE then
			changeType = "Decreased"
		else
			changeType = "Same"
		end

		local rowStyle
		if changeType == "Increased" then
			rowStyle = ' style="background-color:lightblue;"'
		elseif changeType == "Same" then
			rowStyle = ' style="background-color:lightgreen;"'
		else
			rowStyle = ' style="background-color:#ffcccb;"'
		end

		totalEmployed = totalEmployed + currE

		out[#out + 1] = string.format(
			'|-%s\n| %s || %s || %.2f%% || %d || %d || %s\n',
			rowStyle,
			dateString,
			company.name,
			normalizedShare * 100,
			prevE,
			currE,
			changeType
		)
	end

	local totalUnemployed = population - totalEmployed
	if totalUnemployed < 0 then totalUnemployed = 0 end

	out[#out + 1] = string.format('|-\n! colspan="4" | Total Employed || %d || \n', totalEmployed)
	out[#out + 1] = string.format('|-\n! colspan="4" | Total Unemployed || %d || \n', totalUnemployed)
	out[#out + 1] = '|}'

	return table.concat(out)
end

return p