Module:ChampionFlashcard

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

To edit the documentation or categories for this module, click here.


local util_vars = require('Module:VarsUtil')
local util_cargo = require('Module:CargoUtil')
local util_html = require('Module:HTMLUtil')
local util_table = require('Module:TableUtil')
local util_text = require('Module:TextUtil')
local m_champion = require('Module:Champion')

local lang = mw.getLanguage('en')

local i18n = {
	range = 'Range: ',
	dmg_type = 'Damage Type: ',
	bar_sustained = 'Sustained Damage',
	bar_burst = 'Burst Damage',
	bar_cc = 'CC Level',
	bar_tank = 'Tankiness',
	mid_goal = 'Goal: ',
	mid_strengths = 'Strengths: ',
	mid_weaknesses = 'Weaknesses: ',
	mid_ult = 'Ultimate: ',
	mid_mech = 'Mechanic: ',
	roles = 'Class(es):',
	fighter = 'Fighter',
	support = 'Support', 
	marksman = 'Marksman', 
	mage = 'Mage',
	tank = 'Tank',
	assassin = 'Assassin'
}

local sep = '%s*,%s*'

local BARS = { 'bar_sustained', 'bar_burst', 'bar_cc', 'bar_tank' }
local MIDDLE = { 'mid_goal', 'mid_strengths', 'mid_weaknesses', 'mid_ult', 'mid_mech' }
local ROLES = { 'fighter', 'support', 'marksman', 'mage', 'tank', 'assassin' }

local p = {}
function p.main(frame)
	if frame == mw.getCurrentFrame() then
		args = require('Module:ProcessArgs').merge(true)
	else
		frame = mw.getCurrentFrame()
	end
	local result = p.doQuery(args)
	p.formatResult(result)
	local n = util_vars.setGlobalIndex('flashcard', frame)
	if #result == 1 then
		return p.makeFlashcard(result[1], n)
	else
		return p.makeFlashcards(result, n)
	end
end

function p.doQuery(args)
	local query = {
		tables = { 'ChampionFlashcard', 'InfoboxChampion' },
		fields = {
			'ChampionFlashcard.Champion=Name',
			'ChampionFlashcard.ChampionRange=Range',
			'ChampionFlashcard.DamageType=DamageType',
			'ChampionFlashcard.CCLevel=bar_cc',
			'ChampionFlashcard.BurstLevel=bar_burst',
			'ChampionFlashcard.SustainedLevel=bar_sustained',
			'ChampionFlashcard.TankLevel=bar_tank',
			'ChampionFlashcard.Goal=mid_goal',
			'ChampionFlashcard.Strengths=mid_strengths',
			'ChampionFlashcard.Weaknesses=mid_weaknesses',
			'ChampionFlashcard.Ultimate=mid_ult',
			'ChampionFlashcard.Mechanic=mid_mech',
			'ChampionFlashcard.Classes=Classes',
			'InfoboxChampion.Title=Subtitle'
		},
		where = string.format('(%s) AND ChampionFlashcard.Year="2018"',p.where(args)),
		types = {
			CCLevel = 'Integer',
			BurstLevel = 'Integer',
			SustainedLevel = 'Integer',
			TankLevel = 'Integer'
		},
		join = 'ChampionFlashcard.Champion = InfoboxChampion.Name',
		groupBy = 'ChampionFlashcard.Champion'
	}
	return util_cargo.queryAndCast(query)
end

function p.where(args)
	if args.where then
		return args.where
	elseif args.champion then
		return string.format('ChampionFlashcard.Champion="%s"', m_champion.championname(args.champion))
	elseif args.champions then
		local tbl = {}
		for s in mw.text.gsplit(args.champions,sep) do
			tbl[#tbl+1] = string.format('ChampionFlashcard.Champion="%s"', m_champion.championname(s))
		end
		return table.concat(tbl,' OR ')
	elseif args.alphabet then
		return p.whereAlphabet(
			string.sub(args.alphabet,1,1),
			string.sub(args.alphabet,3,3)
		)
	elseif args.role then
		return string.format('ChampionFlashcard.Roles HOLDS "%s"', args.role)
	else
		error('No valid where condition provided')
	end
end

function p.whereAlphabet(s, e)
	if e == 'Z' then
		return string.format('ChampionFlashcard.Champion > "%s"',s)
	else
		return string.format(
			'ChampionFlashcard.Champion > "%s" AND ChampionFlashcard.Champion < "%s"',
			s,
			util_text.nextLetter(e)
		)
	end
end
	

function p.formatResult(result)
	for i, row in ipairs(result) do
		row.Icon = string.format('[[File:%sSquare.png|link=%s]]', row.Name, row.Name)
		util_table.appendHash(row, util_text.split(lang:lc(row.Classes)))
	end
	return
end

function p.makeFlashcards(result, n)
	local tbl = {}
	for i, row in ipairs(result) do
		tbl[i] = p.makeFlashcard(row, n .. 'and' .. i)
	end
	return util_html.blockBox(tbl, '1em')
end

function p.makeFlashcard(row, n)
	local div = mw.html.create('div'):addClass('flashcard')
	div:node(p.makeFCTop(row, n))
	div:node(p.makeFCMiddle(row, n))
	div:node(p.makeFCDetails(row, n))
	div:allDone()
	return tostring(div)
end

function p.makeFCTop(row, n)
	local tbl = mw.html.create('table'):addClass('flashcard-top')
	tbl:tag('tr')
		:tag('td')
			:attr('rowspan','2')
			:addClass('flashcard-icon')
			:wikitext(row.Icon)
		:done()
		:tag('td')
			:addClass('flashcard-title')
			:tag('div')
				:addClass('flashcard-name')
				:wikitext(row.Name)
			:done()
			:tag('div')
				:wikitext(row.Subtitle)
			:done()
		:done()
		:node(p.makeLinks(row.Name, n))
	:done()
	:tag('tr')
		:tag('td')
			:addClass('flashcard-basics')
			:node(p.makeFCBasics(i18n.range, row.Range))
			:node(p.makeFCBasics(i18n.dmg_type, row.DamageType))
	return tbl
end

function p.makeLinks(name, n)
	local td = mw.html.create('td')
	:addClass('flashcard-links')
	:attr('rowspan','2')
	:tag('div')
		:addClass('flashcard-page')
		:wikitext(string.format('[[%s|Champion Page]]', name))
	:done()
	:tag('div')
		:addClass('flashcard-toggle')
		:tag('span')
			:addClass('_toggler-flashcard' .. n)
			:addClass('_toggle flashcard' .. n)
			:addClass('flashcard-show')
			:wikitext('Show card')
		:done()
		:tag('span')
			:addClass('_toggler-flashcard' .. n)
			:addClass('_toggle flashcard' .. n)
			:addClass('_toggle_inithide')
			:addClass('flashcard-hide')
			:wikitext('Hide card')
		:done()
	:done()
	return td
end

function p.makeFCBasics(key, value)
	local div = mw.html.create('div')
	:tag('span'):addClass('flashcard-basics-header'):wikitext(key):done()
	:wikitext(value)
	return div
end

function p.makeFCBars(row)
	local div = mw.html.create('div'):addClass('flashcard-bars')
	for _, v in ipairs(BARS) do
		div:node(p.makeFCBar(i18n[v], row[v] or '70'))
	end
	return div
end

function p.makeFCBar(key, value)
	local div = mw.html.create('div'):addClass('flashcard-bar')
	div:tag('div')
		:addClass('flashcard-bar-header')
		:wikitext(key)
	div:tag('div')
		:addClass('flashcard-bar-outer')
		:tag('div')
			:addClass('flashcard-bar-inner')
			:css('width', value .. '%')
	return div
end

function p.makeFCMiddle(row, n)
	local div = mw.html.create('div')
		:addClass('flashcard-middle')
		:addClass('_toggle flashcard' .. n)
		:addClass('_toggle_inithide')
	div:node(p.makeFCBars(row))
	div:node(p.makeFCRoles(row))
	return div
end

function p.makeFCDetails(row, n)
	local ul = mw.html.create('ul')
		:addClass('flashcard-details')
		:addClass('_toggle flashcard' .. n)
		:addClass('_toggle_inithide')
	for _, v in ipairs(MIDDLE) do
		if row[v] then
			ul:node(p.makeFCDetail(i18n[v], row[v]))
		end
	end
	return ul
end

function p.makeFCDetail(key, value)
	local li = mw.html.create('li')
	li:tag('span')
		:addClass('flashcard-details-header')
		:wikitext(key)
	li:wikitext(value)
	return li
end

function p.makeFCRoles(row)
	local div = mw.html.create('div'):addClass('flashcard-roles')
	div:tag('div'):addClass('flashcard-roles-header'):wikitext(i18n.roles)
	for _, v in ipairs(ROLES) do
		div:node(p.makeFCRole(v, row[v]))
	end
	return div
end

function p.makeFCRole(role, isActive)
	local div = mw.html.create('div')
		:addClass('flashcard-role')
		:addClass('flashcard-role-' .. role)
	if isActive then
		div:addClass('flashcard-role-active')
	end
	div:tag('div'):addClass('flashcard-role-sprite')
	div:tag('div'):addClass('flashcard-role-name'):wikitext(i18n[role])
	return div
end

return p