Module:GeneralPortOperations

From MicrasWiki
Revision as of 01:56, 24 April 2025 by NewZimiaGov (talk | contribs)
Jump to navigationJump to search

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

-- Module:GeneralPortDailyLog
local p = {}

-- 1) Current PSSC date
function p.getCurrentDate()
    local start = os.time{year=1999,month=8,day=6}
    local now   = os.time()
    local secPerDay, daysPerYear = 86400, 183
    local elapsed = math.floor((now - start) / secPerDay)
    local yearFrac = elapsed / daysPerYear
    local y = math.floor(yearFrac)
    local d = math.floor((yearFrac - y) * daysPerYear) + 1
    return y, d
end

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

-- 3) Home‐port cities & flags
local cities = {
    Vaeringheim     = "[[File:VaeringheimFlag.png|20px]] Vaeringheim",
    Somniumpolis    = "[[File:SomniumpolisFlag.png|20px]] Somniumpolis",
    Delphica        = "[[File:DelphicaFlag.png|20px]] Delphica",
    Ephyra          = "[[File:EphyraFlag.png|20px]] Ephyra",
    Norsolyra       = "[[File:NorsolyraFlag.png|20px]] Norsolyra",
    Skyrophos       = "[[File:SkyrophosFlag.png|20px]] Skyrophos",
    Riddersborg     = "[[File:RiddersborgFlag.png|20px]] Riddersborg",
    Ardclach        = "[[File:ArdclachFlag.png|20px]] Ardclach",
    Jogi            = "[[File:JogiFlag.png|20px]] Jogi",
    Sufriya         = "[[File:SufriyaFlag.png|20px]] Sufriya",
    ["Keybir-Aviv"] = "[[File:KeybirAvivFlag.png|20px]] Keybir-Aviv",
    Aegirheim       = "[[File:AegirheimFlag.png|20px]] Aegirheim",
    Bjornopolis     = "[[File:BjornopolisFlag.png|20px]] Bjornopolis"
}

-- 4) Owner/Operator pools
local ritualCults = {
  "Ordo Solis Invicti","Order of Aurora Mystica","Harmony Sanctum","Ignis Aeternum",
  "Celestial Harmony Sect","Guild of Golden Shadows","Azure Sentinel Sect","Reverie Nebulous",
  "Eon Fellowship","Order of the Umbral Oracle","Mystery of the Verdant Embrace","Conclave Illuminara",
  "Sanctum Vitalis","Temple Alabaster","Court of the Ironclad","Accord Concordia",
  "Mystery of the Stygian Veil","Sylvan Fellowship","Mystery of Red Mirth","Rex Catonis",
  "Sanctum Delphica","Ordo Amicitia","Temple Illuminata"
}
local militaryDivs = {
  "Alpazkigz Division","Odiferia Division","Vaeringheim Division","Haifa Division",
  "Jeseri Division","Hatch Ministry Division","New South Jangsong Division","Haifan Bassaridia Division"
}
local function randCaptain()
  local caps = {
    {f="Talvios",l="Karnis",n="Erebektos"},{f="Mirek",l="Davros",n="Zammarion"},
    {f="Ilion",l="Quoros"},{f="Sevon",l="Halek",n="Thamuthis"},{f="Caldris",l="Onir",n="Votharion"},
    {f="Loras",l="Tarnok"},{f="Vaelen",l="Koros"},{f="Daskos",l="Meyron",n="Orontion"},
    {f="Kyric",l="Fenros",n="Mekharsan"},{f="Omir",l="Thalos"},{f="Baryon",l="Syrek",n="Satharkon"},
    {f="Orien",l="Damar"},{f="Tharkos",l="Nyros",n="Vathmenos"},{f="Halion",l="Trevik"},
    {f="Kelvir",l="Raan",n="Nabarrion"},{f="Lethan",l="Kohr"},{f="Zarik",l="Tharn"},
    {f="Varas",l="Jorik",n="Tharnukhet"},{f="Yasrek",l="Tymon"},{f="Aethon",l="Morvek",n="Vathromen"},
    {f="Crivos",l="Elyen"},{f="Saran",l="Valic"},{f="Drevos",l="Helkan",n="Khamsuron"},
    {f="Nyros",l="Qalor"},{f="Talen",l="Vorric",n="Valkhoret"}
  }
  local c = randomChoice(caps)
  if c.n and math.random()<0.25 then
    return ("Captain %s \"%s\" %s"):format(c.f,c.n,c.l)
  else
    return ("Captain %s %s"):format(c.f,c.l)
  end
end

-- 5) Cargo‐company lists
local agriProducts = {
  "Rice","Edible Algae","Shrimp","Anterran Spicy Onion","Queen's Garlic",
  "Morovian Water Buffalo","Morovian Water Buffalo Beef",
  "Unprocessed Noctic-Rabrev Flower","Processed Noctic Rabrev Flower","Noctic-Rabrev Flower Concentrate",
  "Unprocessed Noctic-Rabrev Leaf","Processed Noctic-Rabrev Leaf",
  "Unprocessed Noctic-Rabrev Leaf Concentrate","Processed Noctic-Rabrev Leaf Concentrate",
  "Manwine","Milkwine","Zoe Elm Resin (Unrefined)","Zoe Elm Resin (Refined)",
  "Sea Elm Flavored Mead","Wheat","Oats","Cereal","Hay",
  "Ale of the Night","Camels","Camel Meat","Camel Milk","Camel Hump Water","Camel Hump Water Tea",
  "Pearls","Abalone","Opium","Hash","Mead","Cinammon",
  "Velvet Worm Meat","Velvet Worm Eggs","Silk","Horehound Mead","Horehound Beer",
  "Sea Cow Beef","Atterian Armored Pufferfish","Morovian Saucer Eyed Giant Eel","Amina Crab",
  "Mugwort Oil","Ground Mugwort","Mugwort Tincture","Blood Infused Red Wine",
  "Wisp Rice","Giid-Lisea-Eda","Giid-Lisea-Eda Beef","Vaeringheim Turkey Meat",
  "Salurian Swamp Quail Eggs","Giant Fanged Penguin Blubber","Giant Fanged Penguin Blubber Oil Extract",
  "Coffee"
}
local mineProducts = {
  "Purple Granite","Iron","Steel","Concrete","Coal","Diamonds","Timber",
  "Mens' Boots","Womens' Boots","Garganram Sign Plains Fertilizer",
  "Gold Necklaces","Gold Rings","Gold Bracelets","Gold Earrings",
  "Robes","Dresses","Tarnkappes","Coats","Perfume","Cologne","Incense","Candles"
}

-- 6) Choose operator and inventory
local function getOperator(vt)
    if vt=="Ritual"    then return randomChoice(ritualCults) end
    if vt=="Military"  then return randomChoice(militaryDivs) end
    if vt=="Privateer" then return randCaptain() end
    return nil
end
local function getOperatorOrCargoCo(vt,ct)
    local op = getOperator(vt)
    if op then return op end
    -- cargo operators were baked into company lists before; here we just show the cargo pool
    return "Independent Consortium"
end

local function getInventoryHtml(ct)
    if ct=="Agricultural" then
        -- pick 3–5 random distinct products
        local pick,used = {},{}
        for _=1,math.random(3,5) do
            local p
            repeat p = randomChoice(agriProducts) until not used[p]
            used[p]=true; pick[#pick+1]=p
        end
        local html = "<ul>"
        for _,i in ipairs(pick) do html = html.."<li>"..i.."</li>" end
        return html.."</ul>"
    elseif ct=="Mining/Manufacturing" then
        local pick,used = {},{}
        for _=1,math.random(3,5) do
            local p
            repeat p = randomChoice(mineProducts) until not used[p]
            used[p]=true; pick[#pick+1]=p
        end
        local html = "<ul>"
        for _,i in ipairs(pick) do html = html.."<li>"..i.."</li>" end
        return html.."</ul>"
    else
        return ""
    end
end

-- 7) Main table generation
function p.generateShipTable()
    local vesselTypes = {'Cargo','Military','Ritual','Privateer'}
    local cargoCats    = {'Agricultural','Mining/Manufacturing','Services'}
    local allCats      = {'Agricultural','Mining/Manufacturing','Services','Unspecified'}
    local errorTypes   = {
      "Cargo Mislabeling","Improper Documentation",
      "Surveillance Anomaly","Inspection Failure","Unregistered Ritual Cargo"
    }

    -- collect city keys
    local cityKeys = {}
    for k in pairs(cities) do cityKeys[#cityKeys+1]=k end

    -- seed & date
    local y,d = p.getCurrentDate()
    local dateStr = ("Day %d, Y.%d"):format(d,y)

    -- build ships
    local ships = {}
    for i=1,math.random(50,80) do
        local h,m = math.random(0,23), math.random(0,59)
        local vt  = randomChoice(vesselTypes)
        local ct  = (vt=="Cargo") and randomChoice(cargoCats) or randomChoice(allCats)
        ships[#ships+1] = {
            date = dateStr,
            time = ("%02d:%02d"):format(h,m),
            sort = h*60 + m,
            id   = "VM-"..math.random(10000,99999),
            home = cities[randomChoice(cityKeys)],
            type = vt,
            op   = getOperatorOrCargoCo(vt,ct),
            berth= math.random(1,124),
            teus = math.random(100,1000),
            tonnage = ("%.2f"):format(math.random()*19000+1000),
            cat  = ct,
            inv  = getInventoryHtml(ct),
            queue     = ("%.2f"):format(math.random()*5.5+0.5),
            turnaround= ("%.2f"):format(math.random()*32+4),
            err = (math.random()<0.2) and randomChoice(errorTypes) or "None"
        }
    end

    table.sort(ships, function(a,b) return a.sort < b.sort end)

    -- assemble wikitext
    local out = "{| class=\"wikitable sortable\"\n" ..
        "! Date !! Time !! Vessel ID !! Home City !! Type !! Owner/Operator !! Berth !! TEUs !! Tonnage !! Cargo Type !! Queue (hrs) !! Turnaround (hrs) !! Error"
    for _,s in ipairs(ships) do
        -- cargo cell with embedded inventory
        local cargoCell = s.cat .. s.inv
        out = out .. ("\n|- \n| %s || %s || %s || %s || %s || %s || %d || %d || %s || %s || %s || %s")
          :format(s.date, s.time, s.id, s.home, s.type, s.op,
                  s.berth, s.teus, s.tonnage, cargoCell, s.queue, s.turnaround)
        if s.err ~= "None" then
            out = out .. (" || style=\"background-color:#fdd;\" | %s"):format(s.err)
        else
            out = out .. " || None"
        end
    end
    out = out .. "\n|}\n\n'''Error Key:'''\n" ..
        "* '''Cargo Mislabeling:''' Mismatch between manifest and actual goods\n" ..
        "* '''Improper Documentation:''' Missing or outdated transit papers\n" ..
        "* '''Surveillance Anomaly:''' Unexpected heat or movement signature\n" ..
        "* '''Inspection Failure:''' Failed customs or hatch review\n" ..
        "* '''Unregistered Ritual Cargo:''' Religious items not pre-cleared by the Temple Bank\n"
    return out
end

function p.dailyReport()
    local y,d = p.getCurrentDate()
    local hdr = ("'''General Port of Lake Morovia: Operational Log – Day %d, Year %d PSSC'''\n"):format(d,y)
    return hdr .. p.generateShipTable()
end

return p