Module:Match/Dota

From TwogPedia
< Module:Match
Revision as of 07:37, 11 October 2023 by Couchor (talk | contribs)

Documentation for this module may be created at Module:Match/Dota/doc

local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo
local html = require('Module:MatchHTML')
local DotaMatch = {}
local heroesData = mw.loadData('Module:Data/Dota/Heroes')

local VariablesLua = mw.ext.VariablesLua

function DotaMatch.main(map, hover, i)
	-- add picks/bans to hover 
	hover:node(mw.html.create('div'):wikitext('Map ' .. i .. ' picks'))
	local picks = mw.html.create('div'):addClass('details-game')
	local p1picks = mw.html.create('div'):addClass('details-picks')
	local p2picks = mw.html.create('div'):addClass('details-picks')
	
	-- Loop through first teams picks
	if map.p1picks then
		local p1picksSplit = mw.text.split(map.p1picks, ',')
		for j = 1, #p1picksSplit do
			local hero = getHero(mw.text.trim(p1picksSplit[j]))
			--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
			p1picks:node(mw.html.create('div'):wikitext('[[File:' .. hero .. '.png|20px|' .. hero .. ']]'))
		end
	end
	
	-- create the duration/winner part
	local duration = mw.html.create('div'):addClass('details-duration')
		:node(mw.html.create('div'):wikitext(map.winner == '1' and 'W' or ''))
		:node(mw.html.create('div'):wikitext(map.duration))
		:node(mw.html.create('div'):wikitext(map.winner == '2' and 'W' or ''))

	-- Loop through second teams picks
	if map.p2picks then
		local p2picksSplit = mw.text.split(map.p2picks, ',')
		for j = 1, #p2picksSplit do
			local hero = getHero(mw.text.trim(p2picksSplit[j]))
			--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
			p2picks:node(mw.html.create('div'):wikitext('[[File:' .. hero .. '.png|20px|' .. hero .. ']]'))
		end
	end
	hover:node(picks:node(p1picks):node(duration):node(p2picks))
	
	hover:node(mw.html.create('div'):wikitext('Map ' .. i .. ' bans'))
	local bans = mw.html.create('div'):addClass('details-game')
	local p1bans = mw.html.create('div'):addClass('details-picks')
	local p2bans = mw.html.create('div'):addClass('details-picks')
	
	-- Loop through first teams bans
	if map.p1bans then
		local p1bansSplit = mw.text.split(map.p1bans, ',')
		for j = 1, #p1bansSplit do
			local hero = getHero(mw.text.trim(p1bansSplit[j]))
			--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
			p1bans:node(mw.html.create('div'):wikitext('[[File:' .. hero .. '.png|20px|' .. hero .. ']]'))
		end
	end
		
	-- Loop through second teams bans
	if map.p2bans then
		local p2bansSplit = mw.text.split(map.p2bans, ',')
		for j = 1, #p2bansSplit do
			local hero = getHero(mw.text.trim(p2bansSplit[j]))
			--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
			p2bans:node(mw.html.create('div'):wikitext('[[File:' .. hero .. '.png|20px|' .. hero .. ']]'))
		end
	end
	
	hover:node(bans:node(p1bans):node(p2bans))
	
	-- VODs etc
	hover:node(mw.html.create('div'):wikitext('VOD links etc'))
	
	return ''
end

function DotaMatch.picksBans(map)
	if map.id and map.winner and not map.p1picks and not map.p1bans then
		local tables = 'Maps_dota'
		local fields = 'winner, p1picks, p2picks, p1bans, p2bans, duration'
		local whereStr = 'api_id="' .. map.id .. '" AND winner IS NOT NULL'
	
		local cargoArgs = {
			where = whereStr
		}
		
		local results = cargo.query(tables, fields, cargoArgs)

		if #results == 0 then
			local mapData = mw.ext.externaldata.getWebData({
				format = 'json',
			    url = 'https://api.opendota.com/api/matches/' .. map.id
			})
	
			if mapData.__json.picks_bans then
				local pickBans = mapData.__json.picks_bans
				map.p1picks = ''
				map.p2picks = ''
				map.p1bans = ''
				map.p2bans = ''
				for j = 1, #pickBans do
					local selection = pickBans[j]
					if selection.is_pick then 
						map['p' .. selection.team + 1 .. 'picks'] = map['p' .. selection.team + 1 .. 'picks'] .. getHero(selection.hero_id) .. ','
					else
						map['p' .. selection.team + 1 .. 'bans'] = 	map['p' .. selection.team + 1 .. 'bans'] .. getHero(selection.hero_id) .. ','
					end
				end
	
				local duration = mapData.__json.duration
				local seconds = duration % 60
				
				local minutes = (duration - seconds) / 60
				if seconds < 10 then seconds = "0" .. tostring(seconds) end
				map.duration = minutes .. ':' .. seconds
			end
		else
			map.p1picks = results[1].p1picks
			map.p2picks = results[1].p2picks
			map.p1bans = results[1].p1bans
			map.p2bans = results[1].p2bans
			map.duration = results[1].duration
		end
	else 
		-- Go through normal picks and bans and turn them into proper hero names
		if map.p1picks then listToNames(map, map.p1picks, "p1picks")end
		if map.p2picks then listToNames(map, map.p2picks, "p2picks")end
		if map.p1bans then listToNames(map, map.p1bans, "p1bans")end
		if map.p2bans then listToNames(map, map.p2bans, "p2bans")end
	end
end

function listToNames(map, list, var)
	local split = mw.text.split(list, ',')
	map[var] = ''

	for i = 1, #split do 
		map[var] = map[var] .. getHero(mw.text.trim(split[i])) .. ','
	end
end

function getHero(idOrName)
	if type(idOrName) == "string" then idOrName = string.lower(idOrName) end
    for _, hero in ipairs(heroesData) do
        if hero.id == idOrName then
            return hero.localized_name
        else 
        	for _, name in ipairs(hero.names) do
	            if name == idOrName then
	                return hero.localized_name
	            end
	        end
        end
    end
    return "Hero not found " .. idOrName
end

return DotaMatch