Difference between revisions of "Module:Database/data"

From Cassette Beasts
m (Tomc moved page Module:Species/data to Module:Database/data without leaving a redirect)
Line 1: Line 1:
local s = mw.title.new('Data:Species'):getContent()
+
function species_sort_index(species)
return mw.text.jsonDecode(s)
+
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 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 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 moves do
 +
p.moves.by_name[m.name] = m
 +
for tag in 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

Revision as of 19:06, 21 May 2022

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 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 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 moves do
	p.moves.by_name[m.name] = m
	for tag in 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