Module:Ships

local p = {} local ShipData = require( 'Module:Ships/data' ) local CategoryData = mw.loadData ( 'Module:Ships/categoryData' ) local ArmFunctions = require( 'Module:Armaments' ) local SquadFunctions = require( 'Module:Squadrons' ) local sF = require ( 'Module:SharedFunctions' ) function p.getShip(ShipName) local ship = ShipData[ShipName] if ship ~= nil and ship.Name == ShipName then return ship else for key, Ship in pairs(ShipData) do           if(Ship.Name == ShipName or key == ShipName or Ship.WikiPage == ShipName) then return Ship end end end return nil end local function getCategory(CatName) local cat = CategoryData[CatName] if cat ~= nil then return cat else for key, Category in pairs(CategoryData) do           if(Category == CatName or key == CatName) then return Cat end end end return nil end function p.checkShip(ship, ShipName) if ship == nil or type(ship) ~= 'table' then return p.getShip(ShipName) elseif type(ship) == 'table' then return ship end end local function returnCategoryValue(focusInput, asString) if(focusInput) then local category = getCategory(focusInput) if asString then if category then return sF.stringToLink(category, focusInput)..sF.stringToLink(":"..category, focusInput) else return sF.stringToLink(focusInput) end else if category then return category else return focusInput end end else return sF.nilValue(asString) end end local function getValue(Ship, Value, asString) if(asString == nil) then asString = false end if Ship == nil then return nil end ValName = string.upper(Value) if(string.match(ValName, "NAME")) then local focus = Ship.Name return sF.stringValue(focus, asString) elseif(string.match(ValName, "IMAGE")) then local focus = Ship.Image return sF.stringValue(focus, asString) elseif(string.match(ValName, "FRANCHISE")) then local focus = Ship.Franchise return returnCategoryValue(focus, asString) elseif(string.match(ValName, "FACTION")) then local focus = Ship.Faction return returnCategoryValue(focus, asString) elseif(string.match(ValName, "TIER")) then if(Ship.Tier) then if (asString) then return sF.intToRoman(Ship.Tier) else return Ship.Tier end else sF.nilValue(asString) end elseif(string.match(ValName, "CLASSIFICATION")) then local focus = Ship.Classification return sF.stringValue(focus, asString) elseif(string.match(ValName, "SPEED")) then local focus = Ship.Speed return sF.stringValue(focus, asString, " Zkt") elseif(string.match(ValName, "MANEUVERABILITY")) then local focus = Ship.Maneuverability return sF.stringValue(focus, asString) elseif(string.match(ValName, "HEALTH")) then local focus = Ship.Health return sF.intValue(focus, asString) elseif(string.match(ValName, "SHIELDS")) then local focus = Ship.Shields return sF.intValue(focus, asString) elseif(string.match(ValName, "COSTS")) then if(Ship.XPCost or Ship.CurrencyCost) then if (asString) then --return Ship.XPCost.PrereqShip local finalString = "" local CreditCost = Ship.CurrencyCost.Credit local GoldCost = Ship.CurrencyCost.Gold local PrereqShip = Ship.XPCost.PrereqShip local XPCost = Ship.XPCost.Cost local priorData = false if PrereqShip ~= "" then local ActualPrereqShip = p.getShip(PrereqShip) if ActualPrereqShip then if ActualPrereqShip.WikiPage then PrereqShip = sF.stringToLink(ActualPrereqShip.WikiPage, PrereqShip) else PrereqShip = sF.stringToLink(PrereqShip) end end if XPCost > 0 then finalString = string.format("%s\* %s with %s XP", finalString, PrereqShip, sF.intToString(XPCost)) else finalString = string.format("%s\* %s", finalString, PrereqShip) end priorData = true end if CreditCost > 0 then if priorData then finalString = string.format("%s\n", finalString) end finalString = string.format("%s\* %s C", finalString, sF.intToString(CreditCost)) priorData = true end if GoldCost > 0 then if priorData then finalString = string.format("%s\n", finalString) end finalString = string.format("%s\* %s G", finalString, sF.intToString(GoldCost)) end return finalString end else sF.nilValue(asString) end elseif(string.match(ValName, "XPCOST")) then if(Ship.XPCost) then if (asString) then --return Ship.XPCost.PrereqShip local finalString = "" local PrereqShip = Ship.XPCost.PrereqShip local XPCost = Ship.XPCost.Cost if PrereqShip ~= "" then if XPCost > 0 then finalString = string.format("%s\* %s with %s XP", finalString, PrereqShip, sF.intToString(XPCost)) else finalString = string.format("%s\* %s", finalString, PrereqShip) end end return finalString end else sF.nilValue(asString) end elseif(string.match(ValName, "COST")) then if(Ship.CurrencyCost) then if (asString) then --return Ship.XPCost.PrereqShip local finalString = "" local CreditCost = Ship.CurrencyCost.Credit local GoldCost = Ship.CurrencyCost.Gold local priorData = false if CreditCost > 0 then finalString = string.format("%s\* %s C", finalString, sF.intToString(CreditCost)) priorData = true end if GoldCost > 0 then if priorData then finalString = string.format("%s\n", finalString) end finalString = string.format("%s\* %s G", finalString, sF.intToString(GoldCost)) end return finalString end else sF.nilValue(asString) end elseif(string.match(ValName, "LASERS")) then local focus = Ship.Lasers return sF.armsValue(focus, asString) elseif(string.match(ValName, "MISSILES")) then local focus = Ship.Missiles return sF.armsValue(focus, asString) elseif(string.match(ValName, "SQUADRONS")) then local focus = Ship.Squadrons return sF.armsValue(focus, asString) elseif(string.match(ValName, "POINTDEFENSES")) then local focus = Ship.PointDefenses return sF.armsValue(focus, asString) elseif(string.match(ValName, "ABILITIES")) then local focus = Ship.Abilities if (focus) then else sF.nilValue(asString) end elseif(string.match(ValName, "LASERDPS")) then local focus = Ship.Lasers return sF.getAllDPS(focus, asString, ArmFunctions, false) elseif(string.match(ValName, "MISSILEDPS")) then local focus = Ship.Missiles return sF.getAllDPS(focus, asString, ArmFunctions, false) elseif(string.match(ValName, "SQUADRONDPS")) then local focus = Ship.Squadrons return sF.getAllDPS(focus, asString, SquadFunctions, false) elseif(ValName == "DPS") then local focus = { ["Lasers"] = getValue(Ship, "LaserDPS", false), ["Missiles"] = getValue(Ship, "MissileDPS", false), ["Squadrons"] = getValue(Ship, "SquadronDPS", false) } if (focus) then local totalHullMin = 0 local totalHullMax = 0 local totalShieldMin = 0 local totalShieldMax = 0 for key, group in pairs(focus) do totalHullMin = totalHullMin+ group.Hull.Min totalHullMax = totalHullMax + group.Hull.Max totalShieldMin = totalShieldMin + group.Shield.Min totalShieldMax = totalShieldMax + group.Shield.Max end return sF.damageValue({Min = totalHullMin, Max = totalHullMax}, {Min = totalShieldMin, Max = totalShieldMax}, asString) else return sF.nilValue(asString) end elseif(ValName == "AVGDPS") then local focus = getValue(Ship, "DPS", false) if (focus) then local avgHull = (focus.Hull.Min + focus.Hull.Max)/2 local avgShield = (focus.Shield.Min + focus.Shield.Max)/2 if asString then if (avgHull == avgShield) then return string.format("%s", sF.intToString(avgHull)) else local output = string.format("%s Hull", sF.intToString(avgHull)) if avgShield ~= 0 and avgShield then output = string.format("%s\n%s Shield", output, sF.intToString(avgShield)) end return output end else return {Hull = avgHull, Shield = avgShield} end local newTable = {Hull = {Min = totalHullMin, Max = totalHullMax}, Shield = {Min = totalShieldMin, Max = totalShieldMax}} return sf.damageValue(newTable, asString) else return sF.nilValue(asString) end elseif(ValName == "TOTALSQUADHP") then local focus = getValue(Ship, "SQUADRONS", false) if focus then return SquadFunctions.getTotalHealth(focus, asString) else return sF.nilValue(asString) end elseif(ValName == "TOTALINTERCEPTDAMAGE") then local focus = getValue(Ship, "SQUADRONS", false) if focus then return SquadFunctions.getTotalIntercept(focus, asString, false) else return sF.nilValue(asString) end end end function p.getValue(ship) local ShipName = ship.args[1] local ValName = ship.args[2] if(ShipName == nil) then return "" elseif(ValName == nil) then return "ERROR: No value selected" end local actualShip = p.getShip(ShipName) if(actualShip == nil) then return "" end return getValue(actualShip, ValName, true) end return p