Module:Summon

From Granblue Fantasy Wiki
Jump to navigation Jump to search

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

local p = {}

local util = require('Module:Util')

function p.renderList(frame)
	mw.log('render start: ' .. os.clock())
	local filter = {}
	for ix,fil in pairs({'id','name','link','rarity','element','race','obtain','categories','notcategoryregexp'}) do
		if frame.args[fil] and (string.len(frame.args[fil]) > 0) then
			filter[fil] = frame.args[fil]
		end
	end

	local fields = {
		'%PAGE%',
		'id'
	}
	if frame.args.fields ~= nil then
		for w in (frame.args.fields):gmatch('[^,]+') do
			table.insert(fields, w)
		end
	end

	mw.log('load data: ' .. os.clock())
	local summons = p.fetchSummons(frame, filter, fields)
	
	mw.log('expanding templates: ' .. os.clock())
	local result = {}
	local template = frame.args.template
	for key,summon in pairs(summons) do
		for ix,arg in pairs(frame.args) do
			if summon[ix] == nil then
				summon[ix] = arg
			end
		end
		table.insert(result, frame:expandTemplate{title = template, args = summon})
		--table.insert(result, '')
	end
	mw.log('done: ' .. os.clock())
	return table.concat(result, '')
end

function p.fetchPageById(frame)
	local fields = {
		'%PAGE%',
		'id'
	}
	local id = frame.args.id
	if (id == nil) then
		return ''
	end

	local argList = fields
	local params = {
		category = 'Summons',
		include = '{Summon}:' .. table.concat(argList, ':'),
		notnamespace = 'User|Template',
		mode = 'userformat',
		secseparators = '====',
		multisecseparators = '====',
		skipthispage = 'no',
		includematch = '/id\\s*=\\s*' .. id .. '/',
	}
	local temp = frame:callParserFunction('#dpl:', params)
	argList[1] = 'link'
	local data = p.indexDPLTable(frame, argList, temp)

	for key,value in pairs(data) do
		if value['link'] ~= nil then
			return value['link']
		end
	end
	return ''
end

function p.fetchSummons(frame, filter, fields)
	mw.log('Module:Summon: fetchSummons start: ' .. os.clock())
	local argList = fields
	local params = {
		category = 'Summons',
		include = '{Summon}:' .. table.concat(argList, ':'),
		uses = 'Template:Summon',
		mode = 'userformat',
		notnamespace = 'User|Template',
		secseparators = '====',
		multisecseparators = '====',
		skipthispage = 'no',
	}
	if (filter ~= nil) then
		if filter.notcategoryregexp ~= nil then
			params.notcategoryregexp = filter.notcategoryregexp
		end
		
		if filter.link ~= nil then
			params.title = filter.link
		elseif filter.categories ~= nil then
			params.category = filter.categories
		else
			local match = {}
			for key,part in pairs(filter) do
				table.insert(match, key..'\\s*=\\s*'..part)
			end
			if #match > 0 then
				params.includematch = '/^(?=.*\\b' .. table.concat(match, '\\b)(?=.*\\b') .. '\\b).*$/is'
			end
		end
	end
	mw.logObject(params)
	mw.log('Module:Summon: fetchSummons dpl start: ' .. os.clock())
	local data = frame:callParserFunction('#dpl:', params)
	mw.log('Module:Summon: fetchSummons dpl end / index start: ' .. os.clock())
	argList[1] = 'link'
	local result = p.indexDPLTable(frame, argList, data)
	mw.log('Module:Summon: fetchSummons index end / end: ' .. os.clock())
	return result
end

function p.indexDPLTable(frame, argList, data)
	mw.log('Module:Summon: indexDPLTable start: ' .. os.clock())
	local result = {}
	local ix = 0
	local rows = util.string.split(data, '====')
	for rowix,row in pairs(rows) do
		local args = {}
		local i = 0
		sargs = util.string.split(row, '\n|')
		for argix,arg in pairs(sargs) do
			i = i+1
			if arg ~= '' then
				local key = argList[i]
				args[key] = arg
			end
		end
		if args.link ~= nil then
			ix = ix + 1
			args.ix = ix
			
			-- some housework
			if args.race == nil then
				args.race = 'none'
			end
			if args.link == nil then
				args.link = args.name
			end
			if (args.id ~= nil) then
				args.short_id = string.sub(args.id, 3, 3) .. string.sub(args.id, 5, 7)
			else
				args.short_id = nil
			end
			table.insert(result, args)
		end
	end

	if (frame.args.sortby ~= nil) then
		if (frame.args.sortby == 'journal') then
			table.sort(result, function(a,b) 
				return a.id < b.id
			end)
		else
			table.sort(result, function(a,b) 
				return a.link < b.link
			end)
		end
	else
		table.sort(result, function(a,b) 
			return a.link < b.link
		end)
	end

	mw.log('Module:Summon: indexDPLTable end: ' .. os.clock())
	return result
end

function p.test()
	mw.log(os.clock())
	local frame = mw.getCurrentFrame()
	mw.log(os.clock())
	local items = p.fetchSummons(frame, nil, { '%PAGE%','id','name','rarity','element','race','release_date','obtain','join'})
	mw.log(os.clock())
	local count = 0
    for k,v in pairs(items) do 
    	if v.id ~= nil then
    		mw.log(v.id .. ' ' .. v.short_id .. ' ' .. v.link)
		else
    		mw.log('nil nil ' .. v.link)
		end
		mw.logObject(v)
    	count = count + 1 
    end
	mw.log(os.clock())
    return count
end

function p.test2()
	mw.log(os.clock())
	local frame = mw.getCurrentFrame()
	mw.log(os.clock())
	local summons = p.fetchSummons(frame, {categories='SSR Summons', notcategoryregexp='(Light|Dark) Summons'}, { '%PAGE%','id','name','rarity','element','call_base','call_mlb','call_flb','aura1','aura2','aura3','hp1','hp2','hp3','atk1','atk2','atk3','obtain','subaura1','subaura2','subaura3'})
	mw.log(os.clock())
	local result = {}
	for key,summon in pairs(summons) do
		mw.log(os.clock()..':')
		mw.logObject(summon)
		--table.insert(result, frame:expandTemplate{title = 'Template:SummonListGeneral/Row', args = summon})
		--table.insert(result, '')
	end
	mw.log(os.clock())
	result = table.concat(result, '')
	mw.log(os.clock())
	return result
end

return p