Modul:Property score

-- This uses the form given in https://no.wikipedia.org/w/index.php?title=Modul:Property_map/conf&oldid=19723853
-- It is not updated to latest form

local conf = mw.loadData( 'Module:Property score/conf' )

local h = {}

function h.score( ref )
	local scores = {}
	local good = {}

	for _,v in ipairs( conf.templates ) do
		scores[v] = 0
		good[v] = true
	end

	for _,v in ipairs( conf.map ) do
		local templates = ( v.template and { v.template } )
			or ( v.templates )
			or ( {} )
		local claim = ref.snaks[v.property]
		if claim then
			for _,w in ipairs( templates ) do
				scores[w] = scores[w] and 2 or 1
			end
		end
		if v.required and not claim then
			for _,w in ipairs( templates ) do
				good[w] = false
			end
		end
	end

	for k,_ in pairs( scores ) do
		if not good[k] then
			scores[k] = nil
		end
	end

	return scores
end

function h.winner( scores )
	local t = {}
	local value = -1

	for k,v in pairs( scores ) do
		if v and value < v then
			value = v
		end
	end

	if value == -1 then
		return nil
	end

	for _,v in ipairs( conf.templates ) do
		if v and value == scores[v] then
			table.insert( t, v )
		end
	end

	return unpack( t )
end

return h