Module:Event bonus cards

From Sekaipedia
Revision as of 04:07, 18 April 2022 by ChaoticShadow (talk | contribs)

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 cargoQuery(where)
	local tables = 'Cards'
	local fields = [[
		_pageName,
		rarity,
		thumbnail,
		thumbnail_trained,
		card_name,
		card_character
	]]
	local args = {
        where = where,
        orderBy = 'Cards.rarity DESC, Cards.card_id DESC',
        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 getTable(data)
	local datatable = DatatableBuilder.new()
	datatable
		:setColumns{
			{
				name = 'id',
				header = 'ID',
				dataFields = { 'card_id' }
			},
			{
				name = 'thumbnail',
				header = 'Thumbnail',
				dataFields = { 'thumbnail' },
				func = formatThumbnail,
				sortable = false
			},
			{
				name = 'name',
				header = 'Card name',
				dataFields = { 'cardPageName', 'card_name' },
				func = formatPageLink
			},
		}
		:setData( data )

	return datatable
end

function p.main(frame)
	local args = getArgs(frame)
	
	local characters = args['characters'] or VariablesLua.var('characters')
	local characterList = {}
	
	local attribute = args['attribute'] or VariablesLua.var('attribute')
	local maxDate   = args['max date'] or VariablesLua.var('start')
	
	if maxDate then
		maxDate = mw.text.split(maxDate, ',')[1]
	end
	
	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 = 	"(Cards.attribute = '" .. attribute .. "')" ..
		' AND ' ..
		"(Cards.date < '" .. maxDate .. "')" ..
		' AND ' ..
		'(' .. characterWheres .. ')'
	
	local results = cargoQuery(wheres)
	
	return getTable(results):tostring() .. wheres
end

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