Module:Match/Dota: Difference between revisions

From TwogPedia
(Created page with "local getArgs = require('Module:Arguments').getArgs local cargo = mw.ext.cargo local html = require('Module:MatchHTML') local DotaMatch = {} local VariablesLua = mw.ext.VariablesLua function DotaMatch.main(frame) local args = getArgs(frame) local bestof = args.bestof or 3 local maps = {} p1score = args.p1score or 0 p2score = args.p2score or 0 for i = 1, bestof do if args['map' .. i] ~= nil then local map = mw.text.jsonDecode(args['map' .. i]) -- If no...")
 
mNo edit summary
Line 3: Line 3:
local html = require('Module:MatchHTML')
local html = require('Module:MatchHTML')
local DotaMatch = {}
local DotaMatch = {}
local heroesData = mw.loadData('Module:Data/Dota/Heroes')


local VariablesLua = mw.ext.VariablesLua
local VariablesLua = mw.ext.VariablesLua


function DotaMatch.main(frame)
function DotaMatch.main(map, hover, i)
local args = getArgs(frame)
-- 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 ''))


local bestof = args.bestof or 3
-- Loop through second teams picks
local maps = {}
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))
p1score = args.p1score or 0
hover:node(mw.html.create('div'):wikitext('Map ' .. i .. ' bans'))
p2score = args.p2score or 0
local bans = mw.html.create('div'):addClass('details-game')
 
local p1bans = mw.html.create('div'):addClass('details-picks')
for i = 1, bestof do
local p2bans = mw.html.create('div'):addClass('details-picks')
if args['map' .. i] ~= nil then
local map = mw.text.jsonDecode(args['map' .. i])
-- Loop through first teams bans
-- If no score is manually set in template, then add to score
if map.p1bans then
if args.p1score == nil and args.p2score == nil then _G['p' .. map.winner .. 'score'] = _G['p' .. map.winner .. 'score'] + 1 end
local p1bansSplit = mw.text.split(map.p1bans, ',')
for j = 1, #p1bansSplit do
maps[i] = map
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
end
end
local winner = #maps >= math.ceil(args.bestof / 2) and p1score > p2score and 1 or 2 or 0
-- Start creation of output html
local team1 = html.team(args.p1, p1score)
local team2 = html.team(args.p2, p2score, true)
-- local team1 = mw.html.create('div'):addClass('team')
-- :node(mw.html.create('div'):wikitext(mw.ext.displaytitle.get(p1)):addClass('teamName'))
-- :node(mw.html.create('div'):wikitext(p1score):addClass('score'))
-- local team2 = mw.html.create('div'):addClass('team'):addClass(VariablesLua.var( 'matchList', 0 ) == '1' and ' reverse' or '')
-- :node(mw.html.create('div'):wikitext(mw.ext.displaytitle.get(p2)):addClass('teamName'))
-- :node(mw.html.create('div'):wikitext(p2score):addClass('score'))
local container = mw.html.create('div'):addClass('match'):node(team1):node(team2)
-- 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
-- local hover = mw.html.create('div'):addClass('match-details')
hover:node(bans:node(p1bans):node(p2bans))
local hover = html.teamHover(args.p1, args.p2, p1score, p2score, args.date)
-- Don't add to database if no teams are entered
-- VODs etc
if args.p1 ~= nil and args.p2 ~= nil then
hover:node(mw.html.create('div'):wikitext('VOD links etc'))
-- Create hover div content
-- local header = mw.html.create('div'):addClass('details-header')
return ''
-- local hover1 = mw.html.create('div'):addClass('team')
end
-- :node(mw.html.create('div'):wikitext('[[' .. args.p1 .. ']]'):addClass('teamName'))
 
-- :node(mw.html.create('div'):wikitext(p1score):addClass('score'))
function DotaMatch.picksBans(map)
-- local hover2 = mw.html.create('div'):addClass('team reverse')
if map.id and map.winner and not map.p1picks and not map.p1bans then
-- :node(mw.html.create('div'):wikitext('[[' .. args.p2 .. ']]'):addClass('teamName'))
local tables = 'DotaMaps'
-- :node(mw.html.create('div'):wikitext(p2score):addClass('score'))
local fields = 'winner, p1picks, p2picks, p1bans, p2bans, duration'
local whereStr = 'api_id="' .. map.id .. '" AND winner IS NOT NULL'
-- local date = mw.html.create('div'):addClass('tc'):wikitext(args.date)
-- hover:node(header:node(hover1):node(hover2)):node(date)
frame:callParserFunction{name = '#cargo_store:', args = {_table = 'DotaMatches', p1 = args.p1, p2 = args.p2, p1score = p1score, p2score = p2score, date = args.date, winner = winner, bestof= args.bestof, casters = args.casters, twitch = args.twitch, youtube = args.youtube}}
local tables = 'DotaMatches'
local fields = '_ID'
local cargoArgs = {
local cargoArgs = {
where = '_pageName="' .. mw.title.getCurrentTitle().text .. '"'
where = whereStr
}
}
local results = cargo.query(tables, fields, cargoArgs)
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 #results > 0 and #maps > 0 then
if mapData.__json.picks_bans then
for i = 1, #maps do
local pickBans = mapData.__json.picks_bans
local map = maps[i]
map.p1picks = ''
frame:callParserFunction{name = '#cargo_store:', args = {_table = 'DotaMaps', map=i, matchID = results[1]._ID, duration = map.duration, p1picks = map.p1picks, p2picks = map.p2picks, p1bans = map.p1bans, p2bans = map.p2bans, dire = map.dire, winner = map.winner, dotaID = map.dotaid, vod = map.vod}}
map.p2picks = ''
-- frame:callParserFunction{name = '#cargo_store:', args = {_table = 'DotaMaps', map=i, matchID = results[1]._ID, duration = map.duration, p1picks = map.p1picks, p2picks = map.p2picks, p1bans = map.p1bans, p2bans = map.p2bans, dire = map.dire, winner = map.winner, dotaID = map.dotaid, vod = map.vod}}
map.p1bans = ''
map.p2bans = ''
-- add picks/bans to hover
for j = 1, #pickBans do
hover:node(mw.html.create('div'):wikitext('Map ' .. i .. ' picks'))
local selection = pickBans[j]
local picks = mw.html.create('div'):addClass('details-game')
if selection.is_pick then
local p1picks = mw.html.create('div'):addClass('details-picks')
map['p' .. selection.team + 1 .. 'picks'] = map['p' .. selection.team + 1 .. 'picks'] .. getHero(selection.hero_id) .. ','
local p2picks = mw.html.create('div'):addClass('details-picks')
else
map['p' .. selection.team + 1 .. 'bans'] = map['p' .. selection.team + 1 .. 'bans'] .. getHero(selection.hero_id) .. ','
-- Loop through first teams picks
if map.p1picks then
local p1picksSplit = mw.text.split(map.p1picks, ',')
for j = 1, #p1picksSplit do
local hero = mw.text.trim(p1picksSplit[j])
--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
p1picks:node(mw.html.create('div'):wikitext(hero:sub(1, 1)))
end
end
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 ''))
local duration = mapData.__json.duration
-- Loop through second teams picks
local seconds = duration % 60
if map.p2picks then
local p2picksSplit = mw.text.split(map.p2picks, ',')
for j = 1, #p2picksSplit do
local hero = mw.text.trim(p2picksSplit[j])
--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
p2picks:node(mw.html.create('div'):wikitext(hero:sub(1, 1)))
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
local minutes = (duration - seconds) / 60
if map.p1bans then
if seconds < 10 then seconds = "0" .. tostring(seconds) end
local p1bansSplit = mw.text.split(map.p1bans, ',')
map.duration = minutes .. ':' .. seconds
for j = 1, #p1bansSplit do
local hero = mw.text.trim(p1bansSplit[j])
--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
p1bans:node(mw.html.create('div'):wikitext(hero:sub(1, 1)))
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 = mw.text.trim(p2bansSplit[j])
--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
p2bans:node(mw.html.create('div'):wikitext(hero:sub(1, 1)))
end
end
hover:node(bans:node(p1bans):node(p2bans))
-- VODs etc
hover:node(mw.html.create('div'):wikitext('VOD links etc'))
end
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
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
container:node(hover)
end
 
-- local tables = 'DotaMatches, DotaMaps'
function listToNames(map, list, var)
-- local fields = 'map, duration, p1picks, p2picks, p1bans, p2bans, dire, DotaMaps.winner, dotaID, vod'
local split = mw.text.split(list, ',')
-- local cargoArgs = {
map[var] = ''
-- join = 'DotaMaps.matchID=DotaMatches._ID'
 
-- }
for i = 1, #split do
-- local results = cargo.query(tables, fields, cargoArgs)
map[var] = map[var] .. getHero(mw.text.trim(split[i])) .. ','
end
-- reset value
end
 
return container
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
end


return DotaMatch
return DotaMatch

Revision as of 11:20, 29 September 2023

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 = 'DotaMaps'
		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