Module:Heroes

From Predecessor Wiki
Jump to navigation Jump to search
local p = {}

local heroes = mw.loadData('Module:Heroes/data')
local getArgs = require('Module:Arguments').getArgs
local getter = require('Module:Heroes/getter')
local builder = require("Module:SimpleHTMLBuilder")
local IL = require('Module:ImageLink')
local lib = require("Module:Feature")

function p.get(frame)
    local args = getArgs(frame)
    
    local get = require ('Module:Heroes/getter')
    args['heroname'] = args['heroname'] or args[1]
    args['datatype'] = args['datatype'] or args[2]
    args['output'] = args['output'] or args[3] or nil
    
    local resultGet = get[args['datatype']]
    local result
    if resultGet then
    	result = resultGet(args['heroname'])
    end

    if args['output'] ~= nil and type(result) == "table" then
        if args['output'] == "csv" then
            return lib.tbl_concat{result}
        elseif args['output'] == "custom" then 
            return frame:preprocess(lib.tbl_concat{result, prepend = args['prepend'], append = args['append'], separator = args['separator'], index = args["index"]})
        elseif args['output'] == "template" then 
            return frame:preprocess(lib.tbl_concat{result, prepend = "{{" .. args['t_name'] .. "|", append = "}}", separator = args['separator']})
        elseif args['output'] == "category" then 
            return frame:preprocess(lib.tbl_concat{result, prepend = "[[Category:" .. (args['prepend'] or ""), append = (args['append'] or "") .. "]]", separator = args['separator'] or " "})
        end
    elseif result == nil then
        return ""
    else
        return result
    end
end

local stat_radar = 
[[
<div id="stat-radar-container"
   data-ability_power='%s'
   data-durability='%s'
   data-basic_attack='%s'
   data-mobility='%s'>
</div>
]]
function p.createStatCanvas(ability_power, durability, basic_attack, mobility)
    local html = stat_radar:format(ability_power, durability, basic_attack, mobility)
    return html
end

function p.getInfobox(frame)
	local args = getArgs(frame)
	local hero = args['hero'] or args[1]
	
	local getter_classes = getter.classes(hero)
	local param_classes = ''
	local getter_roles = getter.roles(hero)
	local param_roles = ''
	
	if getter_classes then
		for _,class in pairs(getter_classes) do
			param_classes = param_classes .. class .. ', '
		end
		param_classes = param_classes:sub(1, -3)
	end
	if getter_roles then
		for _,role in pairs(getter_roles) do
			param_roles = param_roles .. '{{Role|' .. role .. '}}<br>'
		end
		param_roles = param_roles:sub(1, -5)
	end
	
	local getter_difficulty = getter.difficulty(hero)
	local param_difficulty = ''
	if getter_difficulty then
		param_difficulty = '{{Icon|' .. getter_difficulty .. '}} ' .. getter_difficulty
	end
	
	return frame:expandTemplate{title = "Hero/infobox", args = {
		-- Main
		['title'] = hero,
		['image'] = getter.image(hero),
		['icon'] = getter.icon(hero),
		-- Information
		['difficulty'] = param_difficulty,
		['classes'] = param_classes,
		['roles'] = param_roles,
		['range_type'] = getter.range_type(hero),
		-- Cost
		['amber'] = getter.amber(hero),
		['platinum'] = getter.platinum(hero),
		-- Statistics
		['statistics'] = p.createStatCanvas(getter.ability_power(hero) or 0, getter.durability(hero) or 0, getter.basic_attack(hero) or 0, getter.mobility(hero) or 0),
		['ability_power'] = getter.ability_power(hero),
		['durability'] = getter.durability(hero),
		['basic_attack'] = getter.basic_attack(hero),
		['mobility'] = getter.mobility(hero),
	}}
end

function p.getRecommended(frame)
	local args = getArgs(frame)
	local hero = args['hero'] or args[1]
	
	local tabber = '<tabber>\n'
	
	if getter.support(hero) then
		local support = lib.tbl_concat{getter.support(hero), prepend = "{{i|", append = "|showlabel=false|size=42px}}", separator = ' '}
		tabber = tabber .. '|-|Support=\n' .. support
	end
	if getter.carry(hero) then
		local carry = lib.tbl_concat{getter.carry(hero), prepend = "{{i|", append = "|showlabel=false|size=42px}}", separator = ' '}
		tabber = tabber .. '|-|Carry=\n' .. carry
	end
	if getter.midlane(hero) then
		local midlane = lib.tbl_concat{getter.midlane(hero), prepend = "{{i|", append = "|showlabel=false|size=42px}}", separator = ' '}
		tabber = tabber .. '|-|Midlane=\n' .. midlane
	end
	if getter.offlane(hero) then
		local offlane = lib.tbl_concat{getter.offlane(hero), prepend = "{{i|", append = "|showlabel=false|size=42px}}", separator = ' '}
		tabber = tabber .. '|-|Offlane=\n' .. offlane
	end
	if getter.jungle(hero) then
		local jungle = lib.tbl_concat{getter.jungle(hero), prepend = "{{i|", append = "|showlabel=false|size=42px}}", separator = ' '}
		tabber = tabber .. '|-|Jungle=\n' .. jungle
	end
	
	tabber = tabber .. '</tabber>'
	return frame:preprocess(tabber)
end

function p.getCategories(frame)
	local args = getArgs(frame)
	local hero = args['hero'] or args[1]
	return frame:expandTemplate{title = "Hero/categories", args = { hero }}
end

function p.getStats(frame)
	local args = getArgs(frame)
	local hero = args['hero'] or args[1]
	return frame:expandTemplate{title = "Hero/statistics", args = { hero }}
end

function p.generateRows(frame)
	local output = {}
	for k,v in lib.pairsByAlphabeticalKeys(heroes) do
		local row = builder.create('tr')
		local cellHero = builder.create('td')
		cellHero
			:attr('data-sort-value', k)
			:node(IL.hero({['hero'] = k, ['size'] = '30px'}))
			:done()
		
		local cellRangeType = builder.create('td')
		local range_type = ''
		if getter.range_type(k) then
			range_type = getter.range_type(k)
		end
		cellRangeType
			:wikitext(range_type)
			:done()
		
		local cellDifficulty = builder.create('td')
		local difficulty = ''
		if getter.difficulty(k) then
			difficulty = '{{Icon|' .. getter.difficulty(k) .. '}} ' .. getter.difficulty(k)
		end
		cellDifficulty
			:wikitext(frame:preprocess(difficulty))
			:done()
			
		local cellClasses = builder.create('td')
		local classes = {}
		if getter.classes(k) then
			for i,v in pairs(getter.classes(k)) do
				table.insert(classes, v)
			end
		end
		cellClasses
			:wikitext(table.concat(classes, '<br>'))
			:done()
			
		local cellRoles = builder.create('td')
		local roles = lib.tbl_concat{getter.roles(k), prepend = "{{ri|", append = "}}", separator = '<br>'}
		cellRoles
			:attr('data-sort-value', table.concat(getter.roles(k), ','))
			:wikitext(frame:preprocess(roles))
			:done()
		
		local cellPlatinum = builder.create('td')
		local platinum = '{{Icon|platinum}} ' .. tostring(getter.platinum(k))
		cellPlatinum
			:wikitext(frame:preprocess(platinum))
			:done()
		
		local cellAmber = builder.create('td')
		local amber = '{{Icon|amber}} ' .. tostring(getter.amber(k))
		cellAmber
			:wikitext(frame:preprocess(amber))
			:done()
		
		local cellDate = builder.create('td')
		cellDate
			:wikitext(tostring(getter.released(k)))
			:done()
			
		row
			:node(cellHero)
			:node(cellRangeType)
			:node(cellDifficulty)
			:node(cellClasses)
			:node(cellRoles)
			:node(cellPlatinum)
			:node(cellAmber)
			:node(cellDate)
			:done()
		table.insert(output, tostring(row))
	end
	
	return table.concat(output, '\n')
end

function p.getList(frame)
	return builder.create('div')
		:addClass('mw-collapsible')
		:wikitext('<h2>List of Heroes</h2>')
		:tag('div')
			:addClass('mw-collapsible-content')
			:wikitext(frame:expandTemplate{title = 'Hero/list'})
			:done()
		:done()
end
function p.buildList(frame)
	local list = ''
	for k,v in lib.pairsByAlphabeticalKeys(heroes) do
		list = list .. tostring(_buildListItem(k))
	end
	return list
end

function _buildListItem(hero)
	local search = hero
	local range = ''
	local difficulty = ''
	local role = ''
	
	if getter.range_type(hero) then
		range = getter.range_type(hero) 
	end
	if getter.difficulty(hero) then
		difficulty = getter.difficulty(hero)
	end
	if getter.roles(hero) then
		role = lib.tbl_concat{getter.roles(hero), separator = ','}
	end
	
	return builder.create('div')
		:addClass('module-heroes__icon')
		:cssText('display: inline-block; margin: 1px;')
		:attr('data-search', search)
		:attr('data-range', range)
		:attr('data-difficulty', difficulty)
		:attr('data-role', role)
		:node(IL.hero({['hero'] = hero, ['showlabel'] = 'false', ['size'] = '40px'}))
end

function p.getAbilities(frame)
	local args = getArgs(frame)
	local hero = args['hero'] or args[1]
	
	local abilities = builder.create('div')
	local LMB = getter.LMB(hero, args['LMB'])
	local RMB = getter.RMB(hero, args['RMB'])
	local Q = getter.Q(hero, args['Q'])
	local E = getter.E(hero, args['E'])
	local R = getter.R(hero, args['R'])
	local Passive = getter.Passive(hero, args['Passive'])
	
	abilities
		:addClass('abilities')
		:wikitext(tostring(Passive) .. tostring(LMB) .. tostring(RMB) .. tostring(Q) .. tostring(E) .. tostring(R))
		:done()
	return abilities
end

-- Credit to http://lua-users.org/wiki/SimpleRound
local function round(num) -- round to 1 decimal
  return tonumber(string.format("%." .. 1 .. "f", num))
end

return p