Module:DataValidation

From Granblue Fantasy Wiki
Jump to navigation Jump to search

Used to compare data between different parts of the site.


local p = {}

local util = require('Module:Util')

function p.fetchCharacters(frame)
	local argList = {
		'%PAGE%',
		'name',
		'jpname',
		'jptitle',
		'jpva',
		'va',
		'rarity',
		'type',
		'element',
		'race',
		'weapon',
		'charid',
		'da_rate',
		'ta_rate',
		'id',
		'release_date',
		'gender',
		'obtain',
		'join',
		'join_weapon',
	}
	local params = {
		category = 'Characters',
		include = '{Character}:' .. table.concat(argList, ':'),
		mode = 'userformat',
		secseparators = '====',
		multisecseparators = '===='
	}
	local data = frame:callParserFunction('#dpl:', params)
	argList[1] = 'link'
	return p.indexDPLTable(frame, argList, data)
end

function p.indexDPLTable(frame, argList, data)
	local pagenames = {}
	pagenames[frame:getTitle()] = true
	local pf = frame:getParent()
	while (pf ~= nil) and (pf ~= pf:getParent()) do
		pagenames[pf:getTitle()] = true
		pf = pf:getParent()
	end
	local result = {}
	local ix = 1
	local rows = util.string.split(data, '====')
	for rowix,row in pairs(rows) do
		local found_value = false
		local args = {}
		local i = 0
		local sargs = util.string.split(row, '\n|')
		for argix,arg in pairs(sargs) do
			i = i+1
			if (arg ~= '') and (arg ~= nil) then
				local key = argList[i]
				args[key] = arg
				found_value = true
			end
		end
		ix = ix + 1
		args.ix = ix
		if found_value then
			if pagenames[args['name']] ~= nil then
				args['name'] = args['link']
			end
			table.insert(result, args)
		end
	end
	return result
end

function p.compareValues(tbl, objA, objB, prop, lowercase)
		local valA = '???'
		if objA[prop] ~= nil then
			valA = objA[prop]
		end
		table.insert(tbl, '<td>[['..valA..']]</td>')
		
		local valB = '???'
		if (objB ~= nil) and (objB[prop] ~= nil) then
			valB = objB[prop]
		end
		
		local diff
		if lowercase then
			diff = (string.lower(valA) ~= string.lower(valB))
		else
			diff = (valA ~= valB)
		end
		
		if diff then
			table.insert(tbl, '<td style="background-color: red;">[['..valB..']]</td>')
		else
			table.insert(tbl, '<td>[['..valB..']]</td>')
		end

end

function p.makeCharacterReport(frame) 
	local ModuleCharacterTier = require('Module:CharacterTier')
	local temp = p.fetchCharacters(frame)
	-- index by link
	local characters = {}
	for key,value in pairs(temp) do
		characters[value.link] = value
	end
	local temp = ModuleCharacterTier.fetchCharacters(frame, {})
	local chartiers = {}
	for key,value in pairs(temp) do
		chartiers[value.link] = value
	end
	
	local result = {}
	table.insert(result, '<table class="wikitable sortable jquery-tablesorter" style="white-space: nowrap;">')
  	table.insert(result, '<tr>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">id</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">cid</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">date</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">gender</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">obtain</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">join</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">join_weapon</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">wpns</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">da</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">ta</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">jpname</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">jptitle</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">jpva</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">link</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">c.name</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">ct.name</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">c.r</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">ct.r</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">c.el</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">ct.el</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">c.ty</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">ct.ty</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">c.ra</th>')
    table.insert(result, '<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">ct.ra</th>')
  	table.insert(result, '</tr>')
	for link,character in pairs(characters) do
		local chartier = chartiers[link]
		
		table.insert(result, '<tr>')
		
		if character.id ~= nil then
			table.insert(result, '<td>'..character.id..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		
		if character.charid ~= nil then
			table.insert(result, '<td>'..character.charid..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		
		if character.release_date ~= nil then
			table.insert(result, '<td>'..character.release_date..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		
		if character.gender ~= nil then
			table.insert(result, '<td>'..character.gender..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		
		if character.obtain ~= nil then
			table.insert(result, '<td>'..character.obtain..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		
		if character.join ~= nil then
			table.insert(result, '<td>'..character.join..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		
		if character.join_weapon ~= nil then
			table.insert(result, '<td>'..character.join_weapon..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		
		if character.weapon ~= nil then
			table.insert(result, '<td>'..character.weapon..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end

		if character.da_rate ~= nil then
			table.insert(result, '<td>'..character.da_rate..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		
		if character.ta_rate ~= nil then
			table.insert(result, '<td>'..character.ta_rate..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		
		if character.jpname ~= nil then
			table.insert(result, '<td>'..character.jpname..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		if character.jptitle ~= nil then
			table.insert(result, '<td>'..character.jptitle..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		if character.jpva ~= nil then
			table.insert(result, '<td>'..character.jpva..'</td>')
		else
			table.insert(result, '<td style="background-color: red;">???</td>')
		end
		
		table.insert(result, '<td>[['..character.link..']]</td>')
		
		p.compareValues(result, character, chartier, 'name', false)
		p.compareValues(result, character, chartier, 'rarity', true)
		p.compareValues(result, character, chartier, 'element', true)
		p.compareValues(result, character, chartier, 'type', true)
		p.compareValues(result, character, chartier, 'race', true)

		table.insert(result, '</tr>')
	end
	table.insert(result, '</table>')
	
	return table.concat(result, '\n')
end

return p