Module:AbilityData/hero: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Sur (talk | contribs)
m collapsible thats at least not bugging for write_hero_abilities
Sur (talk | contribs)
m note transclusion message moved to inside the notes
Line 106: Line 106:
local ability_name_localized = lang_module.get_string(ability_key)
local ability_name_localized = lang_module.get_string(ability_key)
local notes_str_localized = dictionary_module.translate('Notes', nil, '')
local notes_str_localized = dictionary_module.translate('Notes', nil, '')
local tab_name = "|-|(" .. ability_num .. ") " .. ability_name_en .. "="
local tab_name = "<h2>" .. ability_name_localized .. "</h2>\n"
--Determine notes link
--Determine notes link
Line 116: Line 116:
notes_source_page = ability_name_en .. '/' .. dictionary_module.translate('Notes', 'en', '')
notes_source_page = ability_name_en .. '/' .. dictionary_module.translate('Notes', 'en', '')
if notes_str_localized=='' then
if notes_str_localized=='' then
notes_untranslated_msg = dictionary_module.translate('NotesNeedsTranslation') .. ' [[Template:Translate]]' .. "<br>"
notes_untranslated_msg = '<br>' .. dictionary_module.translate('NotesNeedsTranslation') .. ' [[Template:Translate]]' .. "<br>"
end
end
else
else
Line 124: Line 124:
--Determine tab content
--Determine tab content
local tab_content = "<i>" .. dictionary_module.translate_embed(
local notes_transcluded_from = "<i>" .. dictionary_module.translate_embed(
'TranscludedNotesFrom',  
'TranscludedNotesFrom',  
'https://en.wikipedia.org/wiki/Help:Transclusion',  
'https://en.wikipedia.org/wiki/Help:Transclusion',  
'[['..notes_source_page..']]',  
'[['..notes_source_page..']]',  
'https://deadlocked.wiki/index.php?title='..util_module.url_encode(notes_source_page)..'&action=edit'
'https://deadlocked.wiki/index.php?title='..util_module.url_encode(notes_source_page)..'&action=edit'
) .. "</i><br>" .. notes_untranslated_msg
) .. "</i>" .. notes_untranslated_msg
local tab_content = tab_content .. ability_module.get_ability_card_from_key(
local notes_section = '{{' .. notes_source_page .. '}}\n' .. notes_transcluded_from
local tab_content = ability_module.get_ability_card_from_key(
hero_key,  
hero_key,  
ability_num,  
ability_num,  
'true',
'true',
'{{'..notes_source_page..'}}'
notes_section
)
)
local html = mw.html.create()
     
ret = ret .. tab_name .. tab_content .. '\n'
    -- Outer div that is the clickable area
    local outerDiv = html:tag('div')
        :addClass('mw-collapsible mw-collapsed')  -- Makes it collapsible and starts in collapsed state
        :css('width', '100%')  -- Adjust width if needed
        :css('cursor', 'pointer')  -- Makes the cursor a pointer for a clickable effect
        :attr('style', 'display:block;')  -- Ensures the div behaves like a block-level element
    -- Text A and the dropdown arrow image with size 35px
    outerDiv:wikitext('Clickable div that expands ' .. '[[File:Dropdown Arrow.png|link=|35px]]')
    -- Inner div that will be revealed when clicked
    local innerDiv = outerDiv:tag('div')
        :addClass('mw-collapsible-content')
        :wikitext(tab_content)
    local tab_wrapper =  tostring(outerDiv)
ret = ret .. tab_wrapper .. '\n'
end
end



Revision as of 23:54, 15 October 2024

Documentation for this module may be created at Module:AbilityData/hero/doc

local ability_module = require("Module:AbilityData")
local dictionary_module = require("Module:Dictionary")
local lang_module = require("Module:Lang")
local util_module = require("Module:Utilities")
local heroes_data = mw.loadJsonData("Data:HeroData.json")
local abilities_data = mw.loadJsonData("Data:AbilityData.json")

local p = {}

function p.get_hero_key(ability_key)
	if type(ability_key) == 'table' and ability_key.args then
		local frame = ability_key
		ability_key = frame.args[1]
	end
	
	local ability_data = abilities_data[ability_key]
	local hero_binds = ability_data["HeroBinds"]
	if hero_binds == nil then return 'Ability ' .. ability_key .. ' is not bound by any heroes' end
	for _, hero_bind_data in ipairs(hero_binds) do
		return hero_bind_data["HeroKey"]
	end
end

function p.get_ability_num(ability_key)
	if type(ability_key) == 'table' and ability_key.args then
		local frame = ability_key
		ability_key = frame.args[1]
	end
	
	local ability_data = abilities_data[ability_key]
	local hero_binds = ability_data["HeroBinds"]
	if hero_binds == nil then return 'Ability ' .. ability_key .. ' is not bound by any heroes' end
	for _, hero_bind_data in ipairs(hero_binds) do
		return hero_bind_data["Slot"]
	end
end

-- Write each of the hero's ability cards in a tab of a tabber
function p.write_hero_abilities(frame)
	local hero_key = frame.args[1]
	
	--Determine the hero's abilities
	local hero_data = heroes_data[hero_key]
	if (hero_data==nil) then return "Hero key "..hero_key.." not found" end
	local abilities_data = hero_data["BoundAbilities"]
	if (abilities_data==nil) then return "Hero key"..hero_key.." has no BoundAbilities" end
	
	local ret = ""
	for ability_num, ability_data in pairs(abilities_data) do
		local ability_key = ability_data["Key"]
		local ability_name_en = ability_data["Name"]
		local ability_name_localized = lang_module.get_string(ability_key)
		local notes_str_localized = dictionary_module.translate('Notes', nil, '')
		local tab_name = "|-|(" .. ability_num .. ") " .. ability_name_en .. "="
		
		--Determine notes link
		local notes_source_page
		local notes_untranslated_msg = ''
		-- if either "Notes" string or ability_name is not able to be localized,
		-- use an english link instead, as transcluded content relies on it
		if (ability_name_localized == '') or (notes_str_localized == '') then
			notes_source_page = ability_name_en .. '/' .. dictionary_module.translate('Notes', 'en', '')
			if notes_str_localized=='' then
				notes_untranslated_msg = dictionary_module.translate('NotesNeedsTranslation') .. ' [[Template:Translate]]' .. "<br>"
			end
		else
			notes_source_page = ability_name_localized .. '/' .. notes_str_localized
		end
		notes_source_page = "User:Sur/" .. notes_source_page --temp
		
		--Determine tab content
		local tab_content = "<i>" .. dictionary_module.translate_embed(
			'TranscludedNotesFrom', 
			'https://en.wikipedia.org/wiki/Help:Transclusion', 
			'[['..notes_source_page..']]', 
			'https://deadlocked.wiki/index.php?title='..util_module.url_encode(notes_source_page)..'&action=edit'
			) .. "</i><br>" .. notes_untranslated_msg
		
		local tab_content = tab_content .. ability_module.get_ability_card_from_key(
			hero_key, 
			ability_num, 
			'true',
			'{{'..notes_source_page..'}}'
			)
		
		ret = ret .. tab_name .. tab_content
	end
	ret = "<tabber>" .. ret .. "</tabber>"
	return frame:preprocess(ret)
end

--collapsible list
function p.write_hero_abilities2(frame)
	local hero_key = frame.args[1]
	
	--Determine the hero's abilities
	local hero_data = heroes_data[hero_key]
	if (hero_data==nil) then return "Hero key "..hero_key.." not found" end
	local abilities_data = hero_data["BoundAbilities"]
	if (abilities_data==nil) then return "Hero key"..hero_key.." has no BoundAbilities" end
	
	local ret = ""
	for ability_num, ability_data in pairs(abilities_data) do
		local ability_key = ability_data["Key"]
		local ability_name_en = ability_data["Name"]
		local ability_name_localized = lang_module.get_string(ability_key)
		local notes_str_localized = dictionary_module.translate('Notes', nil, '')
		local tab_name = "<h2>" .. ability_name_localized .. "</h2>\n"
		
		--Determine notes link
		local notes_source_page
		local notes_untranslated_msg = ''
		-- if either "Notes" string or ability_name is not able to be localized,
		-- use an english link instead, as transcluded content relies on it
		if (ability_name_localized == '') or (notes_str_localized == '') then
			notes_source_page = ability_name_en .. '/' .. dictionary_module.translate('Notes', 'en', '')
			if notes_str_localized=='' then
				notes_untranslated_msg = '<br>' .. dictionary_module.translate('NotesNeedsTranslation') .. ' [[Template:Translate]]' .. "<br>"
			end
		else
			notes_source_page = ability_name_localized .. '/' .. notes_str_localized
		end
		notes_source_page = "User:Sur/" .. notes_source_page --temp
		
		--Determine tab content
		local notes_transcluded_from = "<i>" .. dictionary_module.translate_embed(
			'TranscludedNotesFrom', 
			'https://en.wikipedia.org/wiki/Help:Transclusion', 
			'[['..notes_source_page..']]', 
			'https://deadlocked.wiki/index.php?title='..util_module.url_encode(notes_source_page)..'&action=edit'
			) .. "</i>" .. notes_untranslated_msg
		
		local notes_section =  '{{' .. notes_source_page .. '}}\n' .. notes_transcluded_from
		
		local tab_content = ability_module.get_ability_card_from_key(
			hero_key, 
			ability_num, 
			'true',
			notes_section
			)
			
	    
		ret = ret .. tab_name .. tab_content .. '\n'
	end

	return frame:preprocess(ret)
end

return p