Module:WeaponsData: Difference between revisions
From MENACE Official Wiki
joljaycups (talk | contribs) No edit summary |
joljaycups (talk | contribs) No edit summary |
||
| Line 185: | Line 185: | ||
for _, weapon in ipairs(weaponsData) do | for _, weapon in ipairs(weaponsData) do | ||
table.insert(tableRows, formatWeaponRow(weapon)) | 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 | |||
---@return string The complete wiki table with all fields | |||
function WeaponsData.getAllWeaponsTableComplete() | |||
loadData() | |||
if #weaponsData == 0 then | |||
return "No weapons data available" | |||
end | |||
-- Collect all unique keys from all weapons | |||
local allKeys = {} | |||
local keySet = {} | |||
for _, weapon in ipairs(weaponsData) do | |||
for key, _ in pairs(weapon) do | |||
if not keySet[key] then | |||
keySet[key] = true | |||
table.insert(allKeys, key) | |||
end | |||
end | |||
end | |||
-- Sort keys alphabetically for consistent column order | |||
table.sort(allKeys) | |||
local tableRows = {} | |||
-- Add table start | |||
table.insert(tableRows, '{| class="wikitable sortable"') | |||
table.insert(tableRows, '|-') | |||
-- Add table header with all keys | |||
local headerCells = {} | |||
for _, key in ipairs(allKeys) do | |||
table.insert(headerCells, "! " .. key) | |||
end | |||
table.insert(tableRows, table.concat(headerCells, " ")) | |||
-- 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 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 | end | ||
Revision as of 18:02, 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 ""))
-- 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
---@return string The complete wiki table with all fields
function WeaponsData.getAllWeaponsTableComplete()
loadData()
if #weaponsData == 0 then
return "No weapons data available"
end
-- Collect all unique keys from all weapons
local allKeys = {}
local keySet = {}
for _, weapon in ipairs(weaponsData) do
for key, _ in pairs(weapon) do
if not keySet[key] then
keySet[key] = true
table.insert(allKeys, key)
end
end
end
-- Sort keys alphabetically for consistent column order
table.sort(allKeys)
local tableRows = {}
-- Add table start
table.insert(tableRows, '{| class="wikitable sortable"')
table.insert(tableRows, '|-')
-- Add table header with all keys
local headerCells = {}
for _, key in ipairs(allKeys) do
table.insert(headerCells, "! " .. key)
end
table.insert(tableRows, table.concat(headerCells, " "))
-- 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 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


