Module:GeneralPortDailyLog

From MicrasWiki
Jump to navigationJump to search

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

local p = {}

-- Date generation logic
function p.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

    return psscYear, dayOfYear
end

-- Random selection utility
local function randomChoice(t)
    return t[math.random(#t)]
end

-- Generate daily energy system log
function p.generateEnergyLog()
    local year, day = p.getCurrentDate()
    local dateStr = string.format("Day %d, Y.%d", day, year)

    local weather = randomChoice({"Clear", "Breezy", "Windy", "Stormy", "Calm"})
    local windEfficiency = {
        Clear = 0.6,
        Breezy = 0.75,
        Windy = 1.0,
        Stormy = 1.2,
        Calm = 0.3
    }

    local windBase = 200
    local windOutput = windBase * windEfficiency[weather] * 4
    local fusionOutput = 600 * 3
    local geothermalBase = 150
    local totalOutput = windOutput + fusionOutput + geothermalBase

    local demand = math.random(1400, 2000)
    local rerouting = demand > totalOutput and "Activated" or "Not Required"
    local backupProtocol = demand > totalOutput + 100 and "Backup Generator Online" or "Normal Status"

    local log = "{| class=\"wikitable\" style=\"width:100%\"\n"
    log = log .. string.format("! colspan=2 | General Port of Lake Morovia – Energy Systems Log (%s)\n", dateStr)
    log = log .. "|-\n"
    log = log .. string.format("| '''Weather Conditions''' || %s\n", weather)
    log = log .. "|-\n"
    log = log .. string.format("| '''Wind Farm Output''' || %.2f MW\n", windOutput)
    log = log .. "|-\n"
    log = log .. string.format("| '''Fusion Substation Output''' || %.2f MW\n", fusionOutput)
    log = log .. "|-\n"
    log = log .. string.format("| '''Geothermal Plant Output''' || %.2f MW\n", geothermalBase)
    log = log .. "|-\n"
    log = log .. string.format("| '''Total Power Generated''' || %.2f MW\n", totalOutput)
    log = log .. "|-\n"
    log = log .. string.format("| '''Port Power Demand''' || %d MW\n", demand)
    log = log .. "|-\n"
    log = log .. string.format("| '''Energy Rerouting''' || %s\n", rerouting)
    log = log .. "|-\n"
    log = log .. string.format("| '''Backup Protocol Status''' || %s\n", backupProtocol)
    log = log .. "|}"

    return log
end

-- Main function to output summary
function p.dailyEnergySummary()
    local year, day = p.getCurrentDate()
    local header = string.format("'''General Port of Lake Morovia: Energy Log – Day %d, Year %d PSSC'''\n", day, year)
    return header .. p.generateEnergyLog()
end

return p