Module:WeaponsData: Difference between revisions

From MENACE Official Wiki
No edit summary
No edit summary
Line 3: Line 3:


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


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


--endregion


--endregion
--region Public methods


--region Public Methods
---Returns a wiki table row for a weapon by Name field
---@public
---@param weaponName string The Name identifier of the weapon (e.g., "weapon.generic_assault_rifle_tier1_ARC_762")
---gets a table of all weapons
---@return string The formatted wiki table row
---
function WeaponsData.getWeaponRow(weaponName)
---@return table of weapons
function WeaponsData.getAllWeapons()
     loadData()
     loadData()
 
   
     local weaponNames = {}
     -- Find the weapon by Name
     for name, _ in pairs(weaponsData) do
     for _, weapon in ipairs(weaponsData) do
         table.insert(weaponNames, name)
         if weapon.Name == weaponName then
            return formatWeaponRow(weapon)
        end
     end
     end
     return weaponNames
   
     return "Weapon not found: " .. (weaponName or "nil")
end
end


---@public
---Returns a wiki table row for a weapon by Title field
---prints a row of the table given a name
---@param weaponTitle string The Title of the weapon (e.g., "R14A2 ARC")
---
---@return string The formatted wiki table row
---@return string of table row
function WeaponsData.getWeaponRowByTitle(weaponTitle)
function WeaponsData.printRow(weapon)
     loadData()
     local weaponNames = WeaponsData.getAllWeapons()
      
     local tableRow = weaponNames[weapon[SCHEMA.TITLE]] .. "|" .. weaponNames[weapon[SCHEMA.SHORTNAME]] .. "|" .. weaponsNames[weapon[SCHEMA.DAMAGE]] .. "|" .. weaponNames[weapon[SCHEMA.TRADEVALUE]]
    -- Find the weapon by Title
    return tableRow
    for _, weapon in ipairs(weaponsData) do
        if weapon.Title == weaponTitle then
            return formatWeaponRow(weapon)
        end
    end
   
    return "Weapon not found: " .. (weaponTitle or "nil")
end
end


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


     -- Find the weapon by name
---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
     for _, weapon in ipairs(weaponsData) do
         if weapon.Title == weaponName then
         table.insert(tableRows, formatWeaponRow(weapon))
            return formatWeaponRow(weapon)
        end
     end
     end
 
   
     return "Weapon not found: " .. ("fuck")
    -- Close table
    table.insert(tableRows, '|}')
   
     return table.concat(tableRows, "\n")
end
end



Revision as of 06:30, 3 October 2025

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 weaponName string The Name identifier of the weapon (e.g., "weapon.generic_assault_rifle_tier1_ARC_762")
---@return string The formatted wiki table row
function WeaponsData.getWeaponRow(weaponName)
    loadData()
    
    -- 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: " .. (weaponName or "nil")
end

---Returns a wiki table row for a weapon by Title field
---@param weaponTitle string The Title of the weapon (e.g., "R14A2 ARC")
---@return string The formatted wiki table row
function WeaponsData.getWeaponRowByTitle(weaponTitle)
    loadData()
    
    -- 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: " .. (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