Module:WeaponsData: Difference between revisions
From MENACE Official Wiki
joljaycups (talk | contribs) No edit summary |
Test edit to see if culls many wanted pages |
||
| (25 intermediate revisions by 2 users not shown) | |||
| Line 3: | Line 3: | ||
--region Dependencies | --region Dependencies | ||
local JsonUtils = require("Module:JsonUtils") | local JsonUtils = require("Module:JsonUtils") | ||
--endregion | --endregion | ||
| Line 12: | Line 10: | ||
local DATA_FILE = "Module:Data/Weapons.json" | local DATA_FILE = "Module:Data/Weapons.json" | ||
local SCHEMA = { | --[[local SCHEMA = { | ||
NAME = " | NAME = "Name", | ||
SHORTNAME = "ShortName", | |||
DESCRIPTION = "Description", | |||
ICON = "Icon", | |||
TAGS = "Tags", | |||
RARITY = "Rarity", | |||
TRADEVALUE = "TradeValue", | |||
APEN = " | APEN = "IconEquipment", | ||
SUPPRESS = " | SUPPRESS = "IconSkillBar", | ||
NUM_HITS = " | NUM_HITS = "DeployCosts", | ||
ROF = " | ROF = "SkillsGranted", | ||
COST = " | COST = "MinRange", | ||
IDEALRANGE = "IdealRange", | |||
IMAGE = " | 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 | --endregion | ||
| Line 41: | Line 54: | ||
local function loadData() | local function loadData() | ||
if not weaponsData then | 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 | |||
for _, weapon in ipairs( | ---@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 | ||
end | end | ||
return "Weapon not found: " .. tostring(weaponName or "nil") | |||
end | 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() | loadData() | ||
local | |||
if | -- 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 | else | ||
return weapon | -- 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 | end | ||
return "Weapon not found: " .. tostring(weaponTitle or "nil") | |||
end | 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() | |||
---@return table | |||
function WeaponsData. | |||
loadData() | 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 | |||
local | ---Returns a complete wiki table with all weapons and all available fields as columns | ||
for | ---Column order is hardcoded to match Weapons.json structure | ||
table.insert( | ---@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 | end | ||
return | 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 | end | ||
--endregion | --endregion | ||
return WeaponsData | |||
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


