Module:WeaponsData: Difference between revisions

From MENACE Official Wiki
No edit summary
Khitrir (talk | contribs)
Test edit to see if culls many wanted pages
 
(5 intermediate revisions by one other user not shown)
Line 194: Line 194:


---Returns a complete wiki table with all weapons and all available fields as columns
---Returns a complete wiki table with all weapons and all available fields as columns
---Preserves the order of keys as they appear in the JSON
---Column order is hardcoded to match Weapons.json structure
---@return string The complete wiki table with all fields
---@return string The complete wiki table with all fields
function WeaponsData.getAllWeaponsTableComplete()
function WeaponsData.getAllWeaponsTableComplete()
Line 203: Line 203:
     end
     end
      
      
     -- Get the key order from the first weapon
     -- Hardcoded column order matching Weapons.json
    -- This preserves the JSON key order
     local allKeys = {
     local allKeys = {}
        "Icon",
    local keySet = {}
        "Title",
   
        "ShortName",
    -- First, collect keys from the first weapon to establish order
        "Description",
    if weaponsData[1] then
        "Tags",
         for key, _ in pairs(weaponsData[1]) do
        "Rarity",
            table.insert(allKeys, key)
        "MinCampaignProgress",
            keySet[key] = true
        "TradeValue",
         end
        "IconEquipment",
    end
        "IconSkillBar",
   
        "IconSkillBarAlternative",
    -- Then check all other weapons for any additional keys
        "DeployCosts",
    for i = 2, #weaponsData do
        "SkillsGranted",
         local weapon = weaponsData[i]
        "MinRange",
         for key, _ in pairs(weapon) do
        "IdealRange",
            if not keySet[key] then
        "MaxRange",
                table.insert(allKeys, key)
        "AccuracyBonus",
                keySet[key] = true
         "AccuracyDropoff",
            end
        "Damage",
         end
        "DamageDropoff",
     end
         "DamagePctCurrentHitpoints",
        "DamagePctCurrentHitpointsMin",
        "DamagePctMaxHitpoints",
        "DamagePctMaxHitpointsMin",
        "ArmorPenetration",
         "ArmorPenetrationDropoff",
         "DamageToArmorDurability",
        "DamageToArmorDurabilityMult",
        "DamageToArmorDurabilityDropoff",
        "DamageToArmorDurabilityDropoffMult",
        "Suppression",
         "Name"
     }
      
      
     local tableRows = {}
     local tableRows = {}
Line 234: Line 246:
      
      
     -- Add table header with all keys
     -- Add table header with all keys
     local headerCells = {}
    -- First header cell uses !, subsequent ones use !!
     for _, key in ipairs(allKeys) do
     local headerParts = {}
         if _ == 0 then
     for i, key in ipairs(allKeys) do
             table.insert(headerCells, "! " .. key)
         if i == 1 then
             table.insert(headerParts, "! " .. key)
         else
         else
             table.insert(headerCells, "!! " .. key)
             table.insert(headerParts, "!! " .. key)
         end
         end
     end
     end
     table.insert(tableRows, table.concat(headerCells, " "))
     table.insert(tableRows, table.concat(headerParts, " "))
      
      
     -- Add each weapon as a row
     -- Add each weapon as a row
Line 257: Line 269:
             if value == nil then
             if value == nil then
                 cellValue = ""
                 cellValue = ""
            elseif key == "Name" then
                -- Format Name as a MediaWiki link
                cellValue = string.format("%s", tostring(value))
            elseif key == "Title" then
                -- Format Title as a MediaWiki link
                cellValue = string.format("[[%s]]", tostring(value))
             elseif type(value) == "table" then
             elseif type(value) == "table" then
                 -- For arrays, show count or items
                 -- For arrays, show count or items

Latest revision as of 08:02, 17 April 2026

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 ""))
    
    -- Range
    table.insert(row, string.format("|colspan=3| Min Ideal Max \n %d | %d | %d", weapon.MinRange or "", weapon.IdealRange, weapon.MaxRange))
    
    -- DamageToArmorDurability
    table.insert(row, string.format("| %.1f", weapon.DamageToArmorDurability or 0))
    
    -- ArmorPenetration
    table.insert(row, string.format("| %.1f", weapon.ArmorPenetration or 0))
    
    -- Suppression
    table.insert(row, string.format("| %.1f", weapon.Suppression or 0))
    
    -- AccuracyBonus
    table.insert(row, string.format("| %.1f", weapon.AccuracyBonus or 0))
    
    -- DamageDropoff
    table.insert(row, string.format("| %.1f", weapon.DamageDropoff or 0))
    
    -- TradeValue
    table.insert(row, string.format("| %d", weapon.TradeValue or 0))
    
    -- DeployCosts
    table.insert(row, string.format("| %.1f", weapon.DeployCosts 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

---Returns a complete wiki table with all weapons and all available fields as columns
---Column order is hardcoded to match Weapons.json structure
---@return string The complete wiki table with all fields
function WeaponsData.getAllWeaponsTableComplete()
    loadData()
    
    if #weaponsData == 0 then
        return "No weapons data available"
    end
    
    -- Hardcoded column order matching Weapons.json
    local allKeys = {
        "Icon",
        "Title",
        "ShortName",
        "Description",
        "Tags",
        "Rarity",
        "MinCampaignProgress",
        "TradeValue",
        "IconEquipment",
        "IconSkillBar",
        "IconSkillBarAlternative",
        "DeployCosts",
        "SkillsGranted",
        "MinRange",
        "IdealRange",
        "MaxRange",
        "AccuracyBonus",
        "AccuracyDropoff",
        "Damage",
        "DamageDropoff",
        "DamagePctCurrentHitpoints",
        "DamagePctCurrentHitpointsMin",
        "DamagePctMaxHitpoints",
        "DamagePctMaxHitpointsMin",
        "ArmorPenetration",
        "ArmorPenetrationDropoff",
        "DamageToArmorDurability",
        "DamageToArmorDurabilityMult",
        "DamageToArmorDurabilityDropoff",
        "DamageToArmorDurabilityDropoffMult",
        "Suppression",
        "Name"
    }
    
    local tableRows = {}
    
    -- Add table start
    table.insert(tableRows, '{| class="wikitable sortable"')
    table.insert(tableRows, '|-')
    
    -- Add table header with all keys
    -- First header cell uses !, subsequent ones use !!
    local headerParts = {}
    for i, key in ipairs(allKeys) do
        if i == 1 then
            table.insert(headerParts, "! " .. key)
        else
            table.insert(headerParts, "!! " .. key)
        end
    end
    table.insert(tableRows, table.concat(headerParts, " "))
    
    -- Add each weapon as a row
    for _, weapon in ipairs(weaponsData) do
        table.insert(tableRows, "|-")
        
        -- Add each field value
        local rowCells = {}
        for _, key in ipairs(allKeys) do
            local value = weapon[key]
            local cellValue = ""
            
            if value == nil then
                cellValue = ""
            elseif key == "Name" then
                -- Format Name as a MediaWiki link
                cellValue = string.format("%s", tostring(value))
            elseif key == "Title" then
                -- Format Title as a MediaWiki link
                cellValue = string.format("[[%s]]", tostring(value))
            elseif type(value) == "table" then
                -- For arrays, show count or items
                if #value == 0 then
                    cellValue = "[]"
                else
                    cellValue = "[" .. #value .. " items]"
                end
            elseif type(value) == "boolean" then
                cellValue = tostring(value)
            elseif type(value) == "number" then
                -- Format numbers appropriately
                if value % 1 == 0 then
                    cellValue = string.format("%d", value)
                else
                    cellValue = string.format("%.2f", value)
                end
            else
                cellValue = tostring(value)
            end
            
            table.insert(rowCells, "| " .. cellValue)
        end
        
        table.insert(tableRows, table.concat(rowCells, "\n"))
    end
    
    -- Close table
    table.insert(tableRows, '|}')
    
    return table.concat(tableRows, "\n")
end

--endregion

return WeaponsData