Module:Event bonus cards: Difference between revisions

From Sekaipedia
Content added Content deleted
(remove acquire)
(added thumbnail trained)
Line 47: Line 47:
Cards.rarity,
Cards.rarity,
Cards.thumbnail,
Cards.thumbnail,
Cards.thumbnail_trained,
Cards.card_name,
Cards.card_name,
Cards.card_character,
Cards.card_character,
Line 190: Line 191:
func = formatThumbnail,
func = formatThumbnail,
sortable = false,
sortable = false,
},
{
name = 'thumbnail trained',
header = '',
dataFields = { 'Cards.thumbnail_trained' },
func = formatThumbnail,
sortable = false,
default = ''
},
},
{
{

Revision as of 23:30, 20 April 2022

To generate {{Event bonus cards}}, invoke using the main function.


local getArgs = require('Module:Arguments').getArgs
local DatatableBuilder = require('Module:DatatableBuilder')
local utils   = require('Module:Utilities')
local cargo        = mw.ext.cargo
local VariablesLua = mw.ext.VariablesLua

local p = {}

local function formatList(list)
	if #list < 2 then
		return list[1]
	end
	
	local ul = mw.html.create('ul')
	
	for i=1,#list do
		ul:tag('li')
			:wikitext(list[i])
	end
	
	return tostring(ul)
end

local function formatThumbnail(thumbnail)
	return string.format('[[File:%s|64px]]', thumbnail)
end

local function formatAcquire(acquire)
	local list = utils.splitWithDelim(',')(acquire)
	
	return formatList(list)
end

local function formatPageLink(page, name)
	if page and name then
		return string.format('[[%s|%s]]', page, name)
	end
	
	return nil
end

local function cargoQuery(where)
	local tables = 'Cards, card_skills'
	local fields = [[
		Cards._pageName = cardPageName,
		Cards.card_id,
		Cards.rarity,
		Cards.thumbnail,
		Cards.thumbnail_trained,
		Cards.card_name,
		Cards.card_character,
		Cards.attribute,
		Cards.rarity,
		Cards.status,
		card_skills.skill_type,
	]]
	local args = {
        where = where,
        join = 'Cards._pageID = card_skills._pageID',
        orderBy = 'Cards.rarity DESC, Cards.card_id ASC',
        groupBy = 'Cards._pageID'
    }
    
    return cargo.query( tables, fields, args )
end

local function getVSMapping()
	local virtualSingers = {
		'Hatsune Miku',
		'Kagamine Rin',
		'Kagamine Len',
		'Megurine Luka',
		'MEIKO',
		'KAITO'
	}
	local units = { 
		'Leo/need',
		'MORE MORE JUMP!',
		'Vivid BAD SQUAD',
		'Wonderlands×Showtime',
		'25-ji, Nightcord de.'
	}
	
	local mapping = {}
	
	for _, character in ipairs(virtualSingers) do
		for _, unit in ipairs(units) do
			local key = string.format(
				'%s (%s)',
				character,
				unit
			)
			
			mapping[key] = {
				['character'] = character,
				['support unit'] = unit
			}
		end
	end
	
	for _,character in ipairs(virtualSingers) do
		local key = string.format(
			'%s (VIRTUAL SINGER)',
			character
		)
		
		mapping[key] = {
			['character'] = character,
			['support unit'] = ''
		}
	end
	
	return mapping
end

local function getWhereFor50Bonus(characters, attribute, maxDateTime, eventId)
	local maxDate
	if maxDateTime then
		maxDate = mw.text.split(maxDateTime, ',')[1]
	end
	
	local characterList = {}
	if characters then
		characterList = utils.splitWithDelim(';')(characters)
	end
	
	local vsMapping = getVSMapping()
	
	local characterWheres = {}
	
	for _,character in ipairs(characterList) do
		local vals = vsMapping[character] or
			{ ['character'] = character }
		
		local where = '('
		
		where = where .. string.format(
			"Cards.card_character = '%s'",
			vals['character']
		)
		
		if vals['support unit'] then
			where = where .. string.format(
				" AND Cards.support_unit = '%s'",
				vals['support unit']
			)
		end
		
		where = where .. ')'
		
		table.insert(characterWheres, where)
	end
	
	characterWheres = table.concat(characterWheres, ' OR ')
	
	local wheres = 	string.format(
		"Cards.attribute = '%s' AND Cards.date <= '%s' AND (%s)",
		attribute,
		maxDate,
		characterWheres
	)
	
	if tonumber(eventId) >= 36 then
		wheres = wheres .. 
			string.format(
				" AND NOT (Cards.rarity = '4' AND Cards.event_id = %s)",
				eventId
			)
	end
	
	return wheres
end

local function getWhereFor70Bonus(eventId)
	return ''
end

local function getTable(data)
	local datatable = DatatableBuilder.new()
	datatable
		:setColumns{
			{
				name = 'id',
				header = 'ID',
				dataFields = { 'Cards.card_id' }
			},
			{
				name = 'thumbnail',
				header = 'Thumbnail',
				dataFields = { 'Cards.thumbnail' },
				func = formatThumbnail,
				sortable = false,
			},
			{
				name = 'thumbnail trained',
				header = '',
				dataFields = { 'Cards.thumbnail_trained' },
				func = formatThumbnail,
				sortable = false,
				default = ''
			},
			{
				name = 'name',
				header = 'Card name',
				dataFields = { 'cardPageName', 'Cards.card_name' },
				func = formatPageLink
			},
			{
				name = 'rarity',
				header = 'Rarity',
				dataFields = { 'Cards.rarity' }
			},
			{
				name = 'status',
				header = 'Status',
				dataFields = { 'Cards.status' }
			},
			{
				name = 'skill',
				header = 'Skill',
				dataFields = { 'card_skills.skill_type' }
			},
		}
		:setData( data )

	return datatable
end

function p.main(frame)
	local args = getArgs(frame)
	
	local bonus = args['bonus']
	
	local characters = args['characters'] or VariablesLua.var('characters')
	local attribute  = args['attribute'] or VariablesLua.var('attribute')
	local maxDate    = args['max date'] or VariablesLua.var('start date')
	local eventId    = args['event id'] or VariablesLua.var('event id')
	
	local wheres
	if bonus == '50%' then
		wheres = getWhereFor50Bonus(characters, attribute, maxDate, eventId)
	elseif bonus == '70%' then
		wheres = getWhereFor70Bonus(eventId)
	else
		error("A bonus must be provided")
	end
	
	local results = cargoQuery(wheres)
	
	return getTable(results):tostring()
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.