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
 
(33 intermediate revisions by the same user not shown)
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo
local cargo = mw.ext.cargo
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
-- add picks/bans to hover
function DotaMatch.main(map, hover, i)
local mapHeader = mw.html.create('div'):wikitext('Map ' .. i .. ' '):attr('style', 'font-size: 0.9rem;')
-- VODs etc
if map.yt then
mapHeader:wikitext('[[File:Youtube.png|20x20px|https://www.youtube.com/watch?v=' .. map.yt .. ']]')
end
if map.id then
mapHeader:wikitext('[[File:Dotabuff.png|20x20px|https://www.dotabuff.com/matches/' .. map.id .. ']]')
mapHeader:wikitext('[[File:Datdota.png|20x20px|https://www.datdota.com/matches/' .. map.id .. ']]')
mapHeader:wikitext('[[File:Stratz.png|20x20px|https://stratz.com/matches/' .. map.id .. ']]')
end


function DotaMatch.main(frame)
hover:node(mapHeader)
local args = getArgs(frame)
local dire = map.dire or 'unknown'
 
local picks = mw.html.create('div'):addClass('details-game')
local bestof = args.bestof or 3
local p1picks = mw.html.create('div'):addClass(dire == '1' and 'details-picks dire' or 'details-picks radiant')
local maps = {}
local p2picks = mw.html.create('div'):addClass(dire == '2' and 'details-picks dire' or 'details-picks radiant')
local iconSize = '28px'
-- Loop through first teams picks
if map.p1picks then
local p1picksSplit = mw.text.split(map.p1picks, ',')
local p1playersSplit = map.p1players and mw.text.split(map.p1players, ',') or {}
for j = 1, #p1picksSplit do
local hero = getHero(mw.text.trim(p1picksSplit[j]))
local title = hero
if p1playersSplit[j] then title = title .. ' - ' .. (string.find(p1playersSplit[j], 'People/') and string.gsub(p1playersSplit[j], 'People/', '') or 'Api ID: ' .. p1playersSplit[j]) end
--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
p1picks:node(mw.html.create('div'):wikitext('[[File:' .. hero .. '.png|' .. iconSize .. '|' .. title .. '|link=]]'))
end
end
p1score = args.p1score or 0
-- create the duration/winner part
p2score = args.p2score or 0
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'):node(mw.html.create('abbr'):attr('title', 'Map duration'):wikitext(map.duration)))
:node(mw.html.create('div'):wikitext(map.winner == '2' and 'W' or ''))


for i = 1, bestof do
-- Loop through second teams picks
if args['map' .. i] ~= nil then
if map.p2picks then
local map = mw.text.jsonDecode(args['map' .. i])
local p2picksSplit = mw.text.split(map.p2picks, ',')
-- If no score is manually set in template, then add to score
local p2playersSplit = map.p2players and mw.text.split(map.p2players, ',') or {}
if args.p1score == nil and args.p2score == nil then _G['p' .. map.winner .. 'score'] = _G['p' .. map.winner .. 'score'] + 1 end
for j = 1, #p2picksSplit do
local hero = getHero(mw.text.trim(p2picksSplit[j]))
maps[i] = map
local title = hero
if p2playersSplit[j] then title = title .. ' - ' .. (string.find(p2playersSplit[j], 'People/') and string.gsub(p2playersSplit[j], 'People/', '') or 'Api ID: ' .. p2playersSplit[j]) end
--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
p2picks:node(mw.html.create('div'):wikitext('[[File:' .. hero .. '.png|' .. iconSize .. '|' .. title .. '|link=]]'))
end
end
end
end
hover:node(picks:node(p1picks):node(duration):node(p2picks))


local winner = #maps >= math.ceil(args.bestof / 2) and p1score > p2score and 1 or 2 or 0
    if map.p1bans or map.p2bans then
 
local bans = mw.html.create('div'):addClass('details-game')
-- Start creation of output html
local p1bans = mw.html.create('div'):addClass('details-picks'):addClass( dire == '1' and 'details-picks dire' or 'details-picks radiant')
local team1 = html.team(args.p1, p1score)
local p2bans = mw.html.create('div'):addClass('details-picks'):addClass( dire == '2' and 'details-picks dire' or 'details-picks radiant')
local team2 = html.team(args.p2, p2score, true)
-- local team1 = mw.html.create('div'):addClass('team')
-- Loop through first teams bans
-- :node(mw.html.create('div'):wikitext(mw.ext.displaytitle.get(p1)):addClass('teamName'))
if map.p1bans then
-- :node(mw.html.create('div'):wikitext(p1score):addClass('score'))
local p1bansSplit = mw.text.split(map.p1bans, ',')
-- local team2 = mw.html.create('div'):addClass('team'):addClass(VariablesLua.var( 'matchList', 0 ) == '1' and ' reverse' or '')
for j = 1, #p1bansSplit do
-- :node(mw.html.create('div'):wikitext(mw.ext.displaytitle.get(p2)):addClass('teamName'))
local hero = getHero(mw.text.trim(p1bansSplit[j]))
-- :node(mw.html.create('div'):wikitext(p2score):addClass('score'))
--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
p1bans:node(mw.html.create('div'):wikitext('[[File:' .. hero .. '.png|' .. iconSize .. '|' .. hero .. '|link=]]'))
end
end
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|' .. iconSize .. '|' .. hero .. '|link=]]'))
end
end
-- local hover = mw.html.create('div'):addClass('match-details')
   
local hover = html.teamHover(args.p1, args.p2, p1score, p2score, args.date)
hover:node(bans:node(mw.html.create('div'):addClass('w-100'):wikitext('Bans')):node(mw.html.create('div'):addClass('w-100'):node(p1bans):node(p2bans)))
end
-- Don't add to database if no teams are entered
return ''
if args.p1 ~= nil and args.p2 ~= nil then
end
-- Create hover div content
 
-- local header = mw.html.create('div'):addClass('details-header')
function DotaMatch.picksBans(maps, p1_api, p2_api, hover, results, frame)
-- local hover1 = mw.html.create('div'):addClass('team')
if #maps > 0 then
-- :node(mw.html.create('div'):wikitext('[[' .. args.p1 .. ']]'):addClass('teamName'))
local isNotTranscluded = not VariablesLua.varexists('transclude') or VariablesLua.var('transclude') == '0'
-- :node(mw.html.create('div'):wikitext(p1score):addClass('score'))
 
-- local hover2 = mw.html.create('div'):addClass('team reverse')
for i = 1, #maps do
-- :node(mw.html.create('div'):wikitext('[[' .. args.p2 .. ']]'):addClass('teamName'))
local map = maps[i]
-- :node(mw.html.create('div'):wikitext(p2score):addClass('score'))
-- local date = mw.html.create('div'):addClass('tc'):wikitext(args.date)
-- Go through normal picks and bans and turn them into proper hero names
if map.p1picks then listToNames(map, map.p1picks, "p1picks") end
-- hover:node(header:node(hover1):node(hover2)):node(date)
if map.p2picks then listToNames(map, map.p2picks, "p2picks") end
if map.p1bans then listToNames(map, map.p1bans, "p1bans") end
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}}
if map.p2bans then listToNames(map, map.p2bans, "p2bans") end
local tables = 'DotaMatches'
-- Only enter to db if page is not being transcluded
local fields = '_ID'
if isNotTranscluded and #results > 0 then
local cargoArgs = {
frame:callParserFunction{name = '#cargo_store:', args = {_table = 'Maps_Dota', map=i, api_id = map.id, matchID = results[1]._ID, duration = map.duration, p1picks = map.p1picks, p1players = map.p1players, p2picks = map.p2picks, p2players = map.p2players, p1bans = map.p1bans, p2bans = map.p2bans, dire = map.dire, fp = map.fp, winner = map.winner, p1kills = map.p1kills, p2kills = map.p2kills, yt = map.yt, vod = map.vod}}
where = '_pageName="' .. mw.title.getCurrentTitle().text .. '"'
}
local results = cargo.query(tables, fields, cargoArgs)
if #results > 0 and #maps > 0 then
for i = 1, #maps do
local map = maps[i]
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}}
-- 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}}
-- 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 = 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
-- 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 = 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
if map.p1bans then
local p1bansSplit = mw.text.split(map.p1bans, ',')
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
DotaMatch.main(map, hover, i)
end
end
   
end
end
container:node(hover)
return map
end
-- local tables = 'DotaMatches, DotaMaps'
 
-- local fields = 'map, duration, p1picks, p2picks, p1bans, p2bans, dire, DotaMaps.winner, dotaID, vod'
function listToNames(map, list, var)
-- local cargoArgs = {
local split = mw.text.split(list, ',')
-- join = 'DotaMaps.matchID=DotaMatches._ID'
map[var] = ''
-- }
 
-- local results = cargo.query(tables, fields, cargoArgs)
for i = 1, #split do
map[var] = map[var] .. getHero(mw.text.trim(split[i])) .. ','
-- reset value
end
map[var] = removeEndComma(map[var])
return container
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
 
function removeEndComma(inputString)
    -- Use the string.match function to check if the string ends with a comma
    if inputString:match(",$") then
        -- If it does, use string.sub to remove the last character (the comma)
        return inputString:sub(1, -2)
    else
        -- If not, return the input string as it is
        return inputString
    end
end
end


return DotaMatch
return DotaMatch

Latest revision as of 19:26, 11 March 2024

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

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

local VariablesLua = mw.ext.VariablesLua
-- add picks/bans to hover 
function DotaMatch.main(map, hover, i)
	local mapHeader = mw.html.create('div'):wikitext('Map ' .. i .. ' '):attr('style', 'font-size: 0.9rem;')
	-- VODs etc
	if map.yt then
		mapHeader:wikitext('[[File:Youtube.png|20x20px|https://www.youtube.com/watch?v=' .. map.yt .. ']]')
	end
	if map.id then
		mapHeader:wikitext('[[File:Dotabuff.png|20x20px|https://www.dotabuff.com/matches/' .. map.id .. ']]')
		mapHeader:wikitext('[[File:Datdota.png|20x20px|https://www.datdota.com/matches/' .. map.id .. ']]')
		mapHeader:wikitext('[[File:Stratz.png|20x20px|https://stratz.com/matches/' .. map.id .. ']]')
	end

	hover:node(mapHeader)
	local dire = map.dire or 'unknown'
	local picks = mw.html.create('div'):addClass('details-game')
	local p1picks = mw.html.create('div'):addClass(dire == '1' and 'details-picks dire' or 'details-picks radiant')
	local p2picks = mw.html.create('div'):addClass(dire == '2' and 'details-picks dire' or 'details-picks radiant')
	local iconSize = '28px'
	-- Loop through first teams picks
	if map.p1picks then
		local p1picksSplit = mw.text.split(map.p1picks, ',')
		local p1playersSplit = map.p1players and mw.text.split(map.p1players, ',') or {}
		for j = 1, #p1picksSplit do
			local hero = getHero(mw.text.trim(p1picksSplit[j]))
			local title = hero
			if p1playersSplit[j] then title = title .. ' - ' .. (string.find(p1playersSplit[j], 'People/') and string.gsub(p1playersSplit[j], 'People/', '') or 'Api ID: ' .. p1playersSplit[j]) end
			--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
			p1picks:node(mw.html.create('div'):wikitext('[[File:' .. hero .. '.png|' .. iconSize .. '|' .. title .. '|link=]]'))
		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'):node(mw.html.create('abbr'):attr('title', 'Map duration'):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, ',')
		local p2playersSplit = map.p2players and mw.text.split(map.p2players, ',') or {}
		for j = 1, #p2picksSplit do
			local hero = getHero(mw.text.trim(p2picksSplit[j]))
			local title = hero
			if p2playersSplit[j] then title = title .. ' - ' .. (string.find(p2playersSplit[j], 'People/') and string.gsub(p2playersSplit[j], 'People/', '') or 'Api ID: ' .. p2playersSplit[j]) end
			--p1picks:wikitext('[[File:' .. w .. '-icon.png|20px]]')
			p2picks:node(mw.html.create('div'):wikitext('[[File:' .. hero .. '.png|' .. iconSize .. '|' .. title .. '|link=]]'))
		end
	end
	hover:node(picks:node(p1picks):node(duration):node(p2picks))

    if map.p1bans or map.p2bans then
	local bans = mw.html.create('div'):addClass('details-game')
	local p1bans = mw.html.create('div'):addClass('details-picks'):addClass( dire == '1' and 'details-picks dire' or 'details-picks radiant')
	local p2bans = mw.html.create('div'):addClass('details-picks'):addClass( dire == '2' and 'details-picks dire' or 'details-picks radiant')
	
	-- 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|' .. iconSize .. '|' .. hero .. '|link=]]'))
		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|' .. iconSize .. '|' .. hero .. '|link=]]'))
		end
	end
	
    
	hover:node(bans:node(mw.html.create('div'):addClass('w-100'):wikitext('Bans')):node(mw.html.create('div'):addClass('w-100'):node(p1bans):node(p2bans)))
	end
	
	return ''
end

function DotaMatch.picksBans(maps, p1_api, p2_api, hover, results, frame)
	if #maps > 0 then
		local isNotTranscluded = not VariablesLua.varexists('transclude') or VariablesLua.var('transclude') == '0'

		for i = 1, #maps do
			local map = maps[i]
			
			-- 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
			
			-- Only enter to db if page is not being transcluded
			if isNotTranscluded and #results > 0 then
				frame:callParserFunction{name = '#cargo_store:', args = {_table = 'Maps_Dota', map=i, api_id = map.id, matchID = results[1]._ID, duration = map.duration, p1picks = map.p1picks, p1players = map.p1players, p2picks = map.p2picks, p2players = map.p2players, p1bans = map.p1bans, p2bans = map.p2bans, dire = map.dire, fp = map.fp, winner = map.winner, p1kills = map.p1kills, p2kills = map.p2kills, yt = map.yt, vod = map.vod}}
			end

			DotaMatch.main(map, hover, i)
		end
    	
	end
	return map
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
	map[var] = removeEndComma(map[var])
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

function removeEndComma(inputString)
    -- Use the string.match function to check if the string ends with a comma
    if inputString:match(",$") then
        -- If it does, use string.sub to remove the last character (the comma)
        return inputString:sub(1, -2)
    else
        -- If not, return the input string as it is
        return inputString
    end
end

return DotaMatch