Module:Sandbox/LVL: Difference between revisions

Jump to navigation Jump to search
LVL (talk | contribs)
No edit summary
LVL (talk | contribs)
No edit summary
Line 1: Line 1:
local p = {};
local p = {};


-- Load the data from the wiki's JSON pages
-- Loads the necessary data from the wiki's JSON pages.
local heroes_data = mw.loadJsonData("Data:HeroData.json")
local heroes_data = mw.loadJsonData("Data:HeroData.json")
local soul_unlocks_data = mw.loadJsonData("Data:SoulUnlockData.json")
local soul_unlocks_data = mw.loadJsonData("Data:SoulUnlockData.json")


-- A mapping to make the table headers more readable
-- Defines human-readable names for the internal property keys used in headers.
local level_scaling_key_to_name = {
local level_scaling_key_to_name = {
BulletDamage = "Bullet Damage",
BulletDamage = "Bullet Damage",
Line 14: Line 14:
}
}


-- Data for special alternate fire modes not found in HeroData.json
-- Provides data for special alternate fire modes that are not present in HeroData.json.
local alt_fires = {
local alt_fires = {
Shiv = {base = 55.2, level = 2.64, suffix = "alt fire 12 pellets"},
Shiv = {base = 55.2, level = 2.64, suffix = "alt fire 12 pellets"},
Line 21: Line 21:
}
}


-- This is the main function that will be called from your sandbox page.
--[[
-- It generates the HTML for the input box and the interactive table.
-- @function p.write_power_increase_table
-- @description Generates a responsive and readable table showing hero stat scaling based on boons.
--              Includes a compact summary view and an expandable, detailed view for all levels.
--              Can optionally wrap the entire output in a theme-aware collapsible section.
-- @param {Frame} frame The MediaWiki frame object.
-- @param {string} frame.args[1] The internal property name to display (e.g., 'BulletDamage').
-- @param {string} [frame.args.title] An optional title. If provided, the table is wrapped in a collapsible section.
-- @return {string} The complete HTML for the table or collapsible section.
--]]
p.write_power_increase_table = function(frame)
p.write_power_increase_table = function(frame)
-- Calculate the maximum number of boons by counting entries with "PowerIncrease"
-- Read and validate the arguments passed from the #invoke call.
local property = frame.args[1]
local title = frame.args.title
 
if not property or property == '' then
return '<strong class="error">Error: Missing required property argument in #invoke call. Example: {{#invoke:LVL/Sandbox|write_power_increase_table|BulletDamage}}</strong>'
end
 
local name = level_scaling_key_to_name[property]
 
if not name then
return '<strong class="error">Error: Invalid property argument "' .. tostring(property) .. '". Must be one of: BulletDamage, MaxHealth, TechPower, LightMeleeDamage, HeavyMeleeDamage.</strong>'
end
-- Create an ordered list of soul requirements for each power increase.
local pi_souls_list = {}
local pi_souls_list = {}
for _, v in ipairs(soul_unlocks_data) do
for _, v in ipairs(soul_unlocks_data) do
Line 33: Line 55:
local max_boons = #pi_souls_list
local max_boons = #pi_souls_list
-- Filter out any disabled heroes
-- Define the milestones to display in the summary table.
local milestones = {10, 20, max_boons}
-- Filter out disabled heroes from the data.
local filtered_heroes = {}
local filtered_heroes = {}
for _, hero in pairs(heroes_data) do
for _, hero in pairs(heroes_data) do
Line 41: Line 66:
end
end
local property = frame.args[1] -- The stat we are calculating, e.g., "BulletDamage"
-- Sort the hero list alphabetically by name for consistent ordering.
local name = level_scaling_key_to_name[property]
table.sort(filtered_heroes, function(a, b)
return a.Name < b.Name
-- Create a unique ID for the table so the JavaScript can find it
end)
local table_id = "scaling-table-" .. property
-- Create the table element with standard wiki classes and our unique ID
-- Begin building the main data table.
local create_table = mw.html.create("table"):addClass("wikitable sortable"):attr("id", table_id)
local create_table = mw.html.create("table"):addClass("wikitable sortable")
-- Create the header row for the table
-- Create the header row for the summary table.
local header_row = mw.html.create("tr")
local header_row = mw.html.create("tr")
header_row
header_row
Line 56: Line 80:
:tag("th"):wikitext("Base " .. name):done()
:tag("th"):wikitext("Base " .. name):done()
:tag("th"):wikitext("Increase per Boon"):done()
:tag("th"):wikitext("Increase per Boon"):done()
-- The header for the "Total" column includes a span that will be updated by JavaScript
 
:tag("th"):wikitext("Total (at <span class=\"boon-count-display\">0</span> Boons)"):done()
-- Dynamically create milestone headers with both boon count and soul amount.
for _, count in ipairs(milestones) do
local header_text = count .. " Boons"
if count == max_boons then header_text = header_text .. " (Max)" end
local required_souls = pi_souls_list[count]
local souls_formatted = string.format("%.1fK", (required_souls + 400) / 1000)
local soul_display = frame:expandTemplate{ title = "Souls", args = {souls_formatted} }
local full_header = header_text .. "<br /><small>(" .. soul_display .. ")</small>"
header_row:tag("th"):wikitext(full_header):done()
end
header_row:tag("th"):wikitext("Full Details"):done()
create_table:node(header_row)
create_table:node(header_row)
-- Loop through each hero to create a row for them in the table
-- A helper function that generates the HTML for a single hero's row and its collapsible details.
for _, hero in ipairs(filtered_heroes) do
local function addHeroRows(hero_data, is_alt_fire)
local base = (hero[property] or 0)
local base, level, suffix
local level_scaling = hero["LevelScaling"] and hero["LevelScaling"][property] or 0
local hero_icon_wikitext = frame:expandTemplate{ title = "HeroIcon", args = {hero_data["Name"]}}
local level = level_scaling
local shots = (hero["BulletsPerShot"] or 1)
-- Determine if this is a main weapon or an alt-fire and set the correct data.
local burst = (hero["BulletsPerBurst"] or 1)
if is_alt_fire then
local bullet_mult = shots * burst
base = hero_data.alt_fire.base
local hero_icon = frame:expandTemplate{ title = "HeroIcon", args = {hero["Name"]}}
level = hero_data.alt_fire.level
local hero_suffix = ""
suffix = " (" .. hero_data.alt_fire.suffix .. ")"
else
-- Special handling for bullet damage with multiple pellets/bursts
base = (hero_data[property] or 0)
if (property == "BulletDamage" and bullet_mult > 1) then
level = (hero_data["LevelScaling"] and hero_data["LevelScaling"][property] or 0)
if (not hero["HitOnceAcrossAllBullets"]) then
suffix = ""
base = base * bullet_mult
level = level * bullet_mult
local shots = (hero_data["BulletsPerShot"] or 1)
local burst = (hero_data["BulletsPerBurst"] or 1)
if (property == "BulletDamage" and (shots * burst) > 1) then
if (not hero_data["HitOnceAcrossAllBullets"]) then
base = base * shots * burst
level = level * shots * burst
end
if (shots > 1) then suffix = suffix .. " (" .. shots .. " pellets)" end
if (burst > 1) then suffix = suffix .. " (" .. burst .. " bullets)" end
end
end
end


if (shots > 1) then hero_suffix = hero_suffix .. " (" .. shots .. " pellets)" end
-- Build the main summary row with the hero's info and milestone values.
if (burst > 1) then hero_suffix = hero_suffix .. " (" .. burst .. " bullets)" end
local summary_row = mw.html.create("tr")
end
local hero_row = mw.html.create("tr")
local hero_cell_content = hero_icon_wikitext .. suffix
-- Add the data-base and data-level attributes to the row for the JavaScript to read
summary_row:tag("td"):wikitext(hero_cell_content):done()
hero_row
summary_row
:attr("data-base", base)
:attr("data-level", level)
:tag("td"):wikitext(hero_icon .. " " .. hero["Name"] .. hero_suffix):done()
:tag("td"):wikitext(string.format("%.2f", base)):done()
:tag("td"):wikitext(string.format("%.2f", base)):done()
:tag("td"):wikitext("+" .. string.format("%.2f", level)):done()
:tag("td"):wikitext("+" .. string.format("%.2f", level)):done()
-- Add a placeholder cell for the total, with a class for JS to find
:tag("td"):addClass("total-value"):wikitext(string.format("%.2f", base)):done()
create_table:node(hero_row)
for _, count in ipairs(milestones) do
local total = base + level * count
summary_row:tag("td"):wikitext(string.format("%.2f", total)):done()
end
 
-- Create the main container for the collapsible "Full Details" section.
local details_id = "boon-details-" .. hero_data.Name .. "-" .. property .. (is_alt_fire and "-alt" or "")
local collapsible_container = mw.html.create("div")
:addClass("mw-collapsible mw-collapsed")
:attr("id", details_id)
:attr("style", "display: flex; flex-direction: column; align-items: flex-end; gap: 5px;")
-- If the hero has a special alt-fire, create a separate row for it
-- Style the "Show All" toggle to look like a theme-aware button.
if (property == "BulletDamage" and alt_fires[hero["Name"]] ~= nil) then
collapsible_container:tag("div")
local alt_fire = alt_fires[hero["Name"]]
    :addClass("mw-collapsible-toggle")
local alt_base = alt_fire["base"]
    :attr("style", "display: inline-block; padding: 2px 8px; border: 1px solid currentColor; border-radius: 1px; text-decoration: none;")
local alt_level = alt_fire["level"]
    :wikitext("Show All")
local alt_suffix = " (''" .. alt_fire["suffix"] .. "'')"
local alt_fire_row = mw.html.create("tr")
-- Create the container for the expanded content.
local content_div = mw.html.create("div"):addClass("mw-collapsible-content")
-- Generate the three-column layout by distributing rows into mini-tables.
local column1_rows, column2_rows, column3_rows = {}, {}, {}
local rows_per_column = math.ceil(max_boons / 3)
for i = 1, max_boons do
local total = base + level * i
local required_souls = pi_souls_list[i]
local souls_formatted = string.format("%.1fK", (required_souls + 400) / 1000)
local soul_display = frame:expandTemplate{ title = "Souls", args = {souls_formatted} }
-- Add data attributes for the alt-fire row as well
local row_html = string.format("<tr><td>%s</td><td style='text-align:right;'>%.2f</td></tr>", soul_display, total)
alt_fire_row
:attr("data-base", alt_base)
:attr("data-level", alt_level)
:tag("td"):wikitext(hero_icon .. " ''↳ Alt-fire''" .. alt_suffix):done()
:tag("td"):wikitext(string.format("%.2f", alt_base)):done()
:tag("td"):wikitext("+" .. string.format("%.2f", alt_level)):done()
:tag("td"):addClass("total-value"):wikitext(string.format("%.2f", alt_base)):done()
create_table:node(alt_fire_row)
if i <= rows_per_column then
table.insert(column1_rows, row_html)
elseif i <= rows_per_column * 2 then
table.insert(column2_rows, row_html)
else
table.insert(column3_rows, row_html)
end
end
end
local table_style = "border:none; background:transparent; margin:0 10px; min-width:150px;"
local table1 = "<table style='" .. table_style .. "'>" .. table.concat(column1_rows) .. "</table>"
local table2 = "<table style='" .. table_style .. "'>" .. table.concat(column2_rows) .. "</table>"
local table3 = "<table style='" .. table_style .. "'>" .. table.concat(column3_rows) .. "</table>"
-- Use a flexbox container to display the mini-tables side-by-side.
local flex_container_style = "display:flex; justify-content:space-around; padding:10px 0;"
content_div:wikitext("<div style='" .. flex_container_style .. "'>" .. table1 .. table2 .. table3 .. "</div>")
collapsible_container:node(content_div)
summary_row:tag("td"):node(collapsible_container)
create_table:node(summary_row)
end
end
-- Create a container div to hold both the input controls and the table
-- Iterate through each hero to generate their main row and an alt-fire row if applicable.
local container = mw.html.create("div"):addClass("scaling-calculator-container")
for _, hero in ipairs(filtered_heroes) do
addHeroRows(hero, false)
if (property == "BulletDamage" and alt_fires[hero["Name"]] ~= nil) then
local alt_fire_hero_data = {
Name = hero.Name,
alt_fire = alt_fires[hero.Name]
}
addHeroRows(alt_fire_hero_data, true)
end
end
local input_id = "boon-input-" .. property
-- If a title was provided, wrap the completed table in a theme-aware collapsible container.
if title and title ~= '' then
-- Create the HTML for the input box and its label
local wrapper = mw.html.create('table')
local input_controls = mw.html.create("div"):addClass("boon-input-controls")
:addClass('mw-collapsible mw-collapsed wikitable')
input_controls
:css('width', '100%')
:tag("label"):attr("for", input_id):wikitext("Number of Boons: "):done()
:css('margin-bottom', '1em')
:tag("input")
 
:attr("type", "number")
local header_row_wrapper = wrapper:tag('tr')
:attr("id", input_id)
 
:attr("value", 0)
header_row_wrapper:tag('th')
:attr("min", 0)
:css('text-align', 'center')
:attr("max", max_boons) -- Use the max_boons we calculated
:wikitext('<span style="font-size: larger; font-weight: bold;">' .. title .. '</span><span class="mw-collapsible-toggle mw-collapsible-toggle-right">[Show]</span>')
:css("width", "60px")
 
:done()
local content_row_wrapper = wrapper:tag('tr')
 
-- Add the input controls and the table to the container
content_row_wrapper:tag('td')
container:node(input_controls)
:addClass('mw-collapsible-content')
container:node(create_table)
:node(create_table)
-- Return the complete HTML for the component
return tostring(wrapper)
return frame:extensionTag{ name = 'nowiki', content = tostring(container) }
else
-- If no title is provided, return the table as before.
return tostring(create_table)
end
end
end


return p
return p