Module:Database/data

From Cassette Beasts

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

function species_sort_index(species)
	local i = species.bestiary_index
	if i == nil or i < 0 then
		return 99999
	else
		return i
	end
end

function cmp_species(a, b)
	local ai = species_sort_index(a)
	local bi = species_sort_index(b)
	return ai < bi
end

function cmp_moves(a, b)
	return a.name < b.name
end

local species = mw.text.jsonDecode(mw.title.new('Data:Species'):getContent())
local moves = mw.text.jsonDecode(mw.title.new('Data:Moves'):getContent())

local p = {
	species = {
		by_name = {},
		by_index = {},
		by_tag = {}
	},
	moves = {
		by_name = {},
		by_tag = {}
	}
}

for _,s in ipairs(species) do
	p.species.by_name[s.name] = s
	if s.bestiary_index ~= nil and s.bestiary_index >= 0 then
		p.species.by_index[s.bestiary_index] = s
	end
	for _,tag in ipairs(s.moves.tags) do
		if p.species.by_tag[tag] == nil then
			p.species.by_tag[tag] = {}
		end
		table.insert(p.species.by_tag[tag], s)
	end
end
table.sort(p.species.by_name, cmp_species)
table.sort(p.species.by_index, cmp_species)
for tag, species in pairs(p.species.by_tag) do
	table.sort(species, cmp_species)
end

for _,m in ipairs(moves) do
	p.moves.by_name[m.name] = m
	for _,tag in ipairs(m.tags) do
		if p.moves.by_tag[tag] == nil then
			p.moves.by_tag[tag] = {}
		end
		table.insert(p.moves.by_tag[tag], m)
	end
end
table.sort(p.moves.by_name, cmp_moves)
for tag, move in pairs(p.moves.by_tag) do
	table.sort(moves, cmp_moves)
end

return p