Module:HeroData: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Sur (talk | contribs)
m formatting test
Sur (talk | contribs)
m test
Line 12: Line 12:
end
end


-- returns the table of a specific item
-- returns the table of a specific item, used by external modules
function p.pget_json_item(name)
function p.get_json_item(name)
for i,v in pairs(data) do
for i,v in pairs(data) do
if (v["Name"] == name) then
if (v["Name"] == name) then

Revision as of 02:51, 16 September 2024

Module documentation [edit] [purge]

Overview

This module provides functions to create hero infoboxes, statboxes, or retrieve hero information using the data uploaded to Data:HeroData.json.

Both will automatically be translated depending on the language selected. If called on

  • Page - translates to english
  • Page/en - translates to english
  • Page/es - translates to spanish

See Template:Lang for more

Functions

get_hero_var

Retrieve a hero variable's value, such as Abram's MaxHealth

Parameters

  • hero_name - Name of the hero in english, or key of the hero. Preferred and recommended to use hero key, as its much more efficient. Search for the key in Data:HeroData.json
  • hero_var - Key of the hero's variable, see Data:HeroData.json
  • sig_figs_or_localize - OPTIONAL, # of sig figs to round to if retrieving a float, or "true" if its a string that should be localized. See Localizable values section.

Note: Recommended to use hero key where possible, i.e. hero_atlas as its O(1) time complexity instead of hero name (Abrams) which is O(N).

Examples

From wikitext:
Using hero name in english

{{#invoke:HeroData|get_hero_var|Abrams|MaxHealth}}

Script error: The function "get_hero_var" does not exist.


Using hero key

{{#invoke:HeroData|get_hero_var|hero_atlas|MaxHealth}}

Script error: The function "get_hero_var" does not exist.


{{#invoke:HeroData|get_hero_var|Abrams|FalloffStartRange}}

Script error: The function "get_hero_var" does not exist.


Round to 2 sig figs

{{#invoke:HeroData|get_hero_var|Abrams|FalloffStartRange|2}}

Script error: The function "get_hero_var" does not exist.


{{#invoke:HeroData|get_hero_var|Abrams|WeaponName}}

Script error: The function "get_hero_var" does not exist.


Localize

{{#invoke:HeroData|get_hero_var|Abrams|WeaponName|true}}

Script error: The function "get_hero_var" does not exist.

Notes

Only usable on variables that are integers, strings, or floats (meaning not dictionaries/hashes or arrays/lists).

If using sig_figs parameter, ensure the value is a float.

If using localize parameter, ensure the value is a string. See Data:HeroData.json, you will notice that hero_astro's "Lore" variable has the value "hero_astro_lore". This key is then sent to Module:Lang which checks for it in Data:Lang_en.json (or a different language).

get_list_elem

Retrieve a specified element from a list

Parameters

  • hero_name - Name of the hero in english, or hero key. Preferred and recommended to use hero key, as its much more efficient. Search for the key in Data:HeroData.json
  • hero_var - Key of the hero's variable, see Data:HeroData.json
  • number - Index to retrieve from the list. 1 for 1st element, 2 for 2nd element, etc.
  • localize - OPTIONAL - "true" if its a string that should be localized. See Localizable values section.

Example

From wikitext:
Using hero name in english

{{#invoke:HeroData|get_list_elem|Abrams|WeaponTypes|2}}

Script error: The function "get_list_elem" does not exist.


Using hero key

{{#invoke:HeroData|get_list_elem|hero_atlas|WeaponTypes|2}}

Script error: The function "get_list_elem" does not exist.


{{#invoke:HeroData|get_list_elem|Abrams|WeaponTypes|2|true}}

Script error: The function "get_list_elem" does not exist.

write_infobox

Writes a Template:Infobox_hero template call for a given hero

Parameters

  • hero_name – Name of the hero, in english

Example

From wikitext:

{{#invoke:HeroData|write_infobox|HERO_NAME}}

Which outputs Script error: The function "write_infobox" does not exist.

write_stat_infoboxes

Writes all 3 Template:Infobox_stat template calls (Weapon, Vitality, Spirit) for a given hero

Parameters

  • hero_name – Name of the hero, in english

Example

From wikitext:

{{#invoke:HeroData|write_stat_infoboxes|HERO_NAME}}

Which outputs Script error: The function "write_stat_infoboxes" does not exist.

write_hero_comparison_table

Writes the Hero Comparison table for a specific Level and Spirit Power

Parameters

  • level - Number of Levels / Power Increases
  • spirit_power - Amount of Spirit power

Both parameters are optional, as if both are 0 or not provided, the outputted table will be at base and will also include the level/SS scaling in each cell along with the base value, rather than the scaled value.

Example

From wikitext:

{{#invoke:HeroData|write_hero_comparison_table|LEVEL|SPIRITPOWER}}

Which outputs Script error: The function "write_hero_comparison_table" does not exist.

Localizable values

Localizable values as of writing this:

  • Lore
  • Playstyle
  • Role
  • WeaponDescription
  • WeaponName
  • elements in WeaponTypes
  • elements in RecommendedItems

local p = {};
local data = mw.loadJsonData("Data:HeroData.json")

-- returns the table of a specific item
function get_json_item(name)
	for i,v in pairs(data) do
		if (v["Name"] == name) then
			return v
		end
	end
	return nil
end

-- returns the table of a specific item, used by external modules
function p.get_json_item(name)
	for i,v in pairs(data) do
		if (v["Name"] == name) then
			return v
		end
	end
	return nil
end



--{{#invoke:HeroData|get_hero_stat|HERO_NAME|STAT_NAME}}--
p.get_hero_stat = function(frame)
	local hero_name = frame.args[1]
	local hero_stat_key = frame.args[2]
	
	local hero = get_json_item(hero_name)
	if(hero == nil) then return "Hero Not Found" end
	
	local var_value = hero[hero_stat_key]
	if(var_value == nil) then return 0 end

	return var_value
end

--{{#invoke:HeroData|get_hero_scalar|HERO_NAME|SPIRIT_OR_LEVEL|STAT_NAME}}--
p.get_hero_scalar = function(frame)
	local hero_name = frame.args[1]
	local spirit_or_level = frame.args[2]
	local hero_stat_key = frame.args[3]
	
	--Ensure spirit or level is chosen
	if(not (spirit_or_level == "Spirit" or spirit_or_level == "Level")) then return "SPIRIT_OR_LEVEL must be 'Spirit' or 'Level'" end
	
	local scaling_str = spirit_or_level .. "Scaling" --i.e. SpiritScaling or LevelScaling
	
	local hero = get_json_item(hero_name)
	if(hero == nul) then return "Hero Not Found" end
	
	--Retrieve scaling data
	local hero_scaling = hero[scaling_str]
	if(hero_scaling == nil) then return "" end
	
	--Retrieve scaling value
	local hero_scalar = hero_scaling[hero_stat_key]
	if(hero_scalar == nil) then return "" end
	
	return hero_scalar
end

--{{#invoke:HeroData|write_hero_infobox|HERO_NAME}}--
p.write_hero_infobox = function(frame)
    hero_name = frame.args[1]
    hero = get_json_item(hero_name)
    
    if(hero == nil) then return "Hero Not Found" end
    
    -- Prepare the arguments for the template
    local templateArgs = {}
    
    for stat_name, stat_value in pairs(hero) do
        if (type(stat_value) ~= "table") then  -- skips complex nested tables
            templateArgs[stat_name] = stat_value
        end
    end
    
    -- Use expandTemplate to evaluate the Infobox_hero template
    local templateTitle = mw.title.new("Template:Infobox_hero")
    local expandedTemplate = mw.getCurrentFrame():expandTemplate{ title = templateTitle, args = templateArgs }
    
    return expandedTemplate
end
	



return p