Module:WeaponsData

From MENACE Official Wiki
Revision as of 06:32, 3 October 2025 by joljaycups (talk | contribs)

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

---@class WeaponsData
local WeaponsData = {}

--region Dependencies
local JsonUtils = require("Module:JsonUtils")
--endregion

--region Private constants

local DATA_FILE = "Module:Data/Weapons.json"

--[[local SCHEMA = {
    NAME = "Name",
    SHORTNAME = "ShortName",
    DESCRIPTION = "Description",
    ICON = "Icon",
    TAGS = "Tags",
    RARITY = "Rarity",
    TRADEVALUE = "TradeValue",
    APEN = "IconEquipment",
    SUPPRESS = "IconSkillBar",
    NUM_HITS = "DeployCosts",
    ROF = "SkillsGranted",
    COST = "MinRange",
    IDEALRANGE = "IdealRange",
    IMAGE = "MaxRange",
    ACCURACYBONUS = "AccuracyBonus",
    ACCURACYDROPOFF = "AccuracyDropoff",
    DAMAGE = "Damage",
    DAMAGEDROPOFF = "DamageDropoff",
    DAMAGEPCTCURRENTHITPOINTS = "DamagePctCurrentHitpoints",
    DAMAGEPCTCURRENTHITPOINTSMIN = "DamagePctCurrentHitpointsMin",
    DAMAGEPCTMAXHITPOINTS = "DamagePctMaxHitpoints",
    DAMAGEPCTMAXHITPOINTSMIN = "DamagePctMaxHitpointsMin",
    ARMORPENETRATION = "ArmorPenetration",
    ARMORPENETRATIONDROPOFF = "ArmorPenetrationDropoff",
    DAMAGETOARMORDURABILITY = "DamageToArmorDurability",
    DAMAGETOARMORDURABILITYMULT = "DamageToArmorDurabilityMult",
    DAMAGETOARMORDURABILITY_DROPOFF = "DamageToArmorDurabilityDropoff",
    DAMAGETOARMORDURABILITYDROPOFFMULT = "DamageToArmorDurabilityDropoffMult",
    SUPPRESSION = "Suppression"
}]]

--endregion

--region Private member variables

local weaponsData

--endregion

--region Private methods

local function loadData()
    if not weaponsData then
        weaponsData = JsonUtils.convertJSONToLuaTable(DATA_FILE)
    end
end

--[[local function getTitle(NAME)
    loadDate()
    return SCHEMA.TITLE
end]]

---Formats a single weapon as a wiki table row
---@param weapon table The weapon data
---@return string The formatted table row
local function formatWeaponRow(weapon)
    local row = {}
    
    -- Add table row start
    table.insert(row, "|-")
    
    -- Name/Title with link
    table.insert(row, string.format("| [[%s]]", weapon.Title or ""))
    
    -- ShortName
    table.insert(row, string.format("| %s", weapon.ShortName or ""))
    
    -- Range (using MaxRange)
    table.insert(row, string.format("| %d", weapon.MaxRange or 0))
    
    -- Damage
    table.insert(row, string.format("| %.1f", weapon.Damage or 0))
    
    -- Armor Damage (DamageToArmorDurability)
    table.insert(row, string.format("| %.1f", weapon.DamageToArmorDurability or 0))
    
    -- Armor Penetration
    table.insert(row, string.format("| %.1f", weapon.ArmorPenetration or 0))
    
    -- Suppression
    table.insert(row, string.format("| %.1f", weapon.Suppression or 0))
    
    -- Trade Value
    table.insert(row, string.format("| %d", weapon.TradeValue or 0))
    
    -- Accuracy Modifier (AccuracyBonus)
    table.insert(row, string.format("| %.1f", weapon.AccuracyBonus or 0))
    
    return table.concat(row, "\n")
end

--endregion

--region Public methods

---Returns a wiki table row for a weapon by Name field
---@param frame table|string The frame object from MediaWiki or direct weapon name
---@return string The formatted wiki table row
function WeaponsData.getWeaponRow(frame)
    loadData()
    
    -- Handle both direct calls and MediaWiki frame calls
    local weaponName
    if type(frame) == "table" and frame.args then
        -- Called from wikitext with {{#invoke:}}
        weaponName = frame.args[1] or frame.args.name
    else
        -- Direct call
        weaponName = frame
    end
    
    -- Find the weapon by Name
    for _, weapon in ipairs(weaponsData) do
        if weapon.Name == weaponName then
            return formatWeaponRow(weapon)
        end
    end
    
    return "Weapon not found: " .. tostring(weaponName or "nil")
end

---Returns a wiki table row for a weapon by Title field
---@param frame table|string The frame object from MediaWiki or direct weapon title
---@return string The formatted wiki table row
function WeaponsData.getWeaponRowByTitle(frame)
    loadData()
    
    -- Handle both direct calls and MediaWiki frame calls
    local weaponTitle
    if type(frame) == "table" and frame.args then
        -- Called from wikitext with {{#invoke:}}
        weaponTitle = frame.args[1] or frame.args.title
    else
        -- Direct call
        weaponTitle = frame
    end
    
    -- Find the weapon by Title
    for _, weapon in ipairs(weaponsData) do
        if weapon.Title == weaponTitle then
            return formatWeaponRow(weapon)
        end
    end
    
    return "Weapon not found: " .. tostring(weaponTitle or "nil")
end

---Returns a wiki table row for the first weapon (for testing)
---@return string The formatted wiki table row
function WeaponsData.getFirstWeaponRow()
    loadData()
    
    if #weaponsData > 0 then
        return formatWeaponRow(weaponsData[1])
    end
    
    return "No weapons found in data"
end

---Returns a complete wiki table with all weapons
---@return string The complete wiki table
function WeaponsData.getAllWeaponsTable()
    loadData()
    
    local tableRows = {}
    
    -- Add table header
    table.insert(tableRows, '{| class="wikitable sortable"')
    table.insert(tableRows, '|-')
    table.insert(tableRows, '! Name !! Type !! Range !! Damage !! Armor Dmg !! Armor Pen !! Suppression !! Trade Value !! Accuracy')
    
    -- Add each weapon as a row
    for _, weapon in ipairs(weaponsData) do
        table.insert(tableRows, formatWeaponRow(weapon))
    end
    
    -- Close table
    table.insert(tableRows, '|}')
    
    return table.concat(tableRows, "\n")
end

--endregion

return WeaponsData