Difference between revisions of "Module:Database/data"

From Cassette Beasts
Line 16: Line 16:
 
function cmp_moves(a, b)
 
function cmp_moves(a, b)
 
return a.name < b.name
 
return a.name < b.name
 +
end
 +
 +
function normalize_species(species)
 +
species.trimmed_description = species.description
 +
if string.substr(species.trimmed_description, 1, 4) == "an " then
 +
species.trimmed_description = string.substr(species.trimmed_description, 4)
 +
elseif string.substr(species.trimmed_description, 1, 3) == "a " then
 +
species.trimmed_description = string.substr(species.trimmed_description, 3)
 +
end
 +
 +
if species.bestiary_index ~= nil and species.bestiary_index >= 0 then
 +
species.index = string.format("%03d", species.bestiary_index)
 +
else
 +
species.index = "???"
 +
end
 
end
 
end
  
Line 21: Line 36:
 
local moves = mw.text.jsonDecode(mw.title.new('Data:Moves'):getContent())
 
local moves = mw.text.jsonDecode(mw.title.new('Data:Moves'):getContent())
  
local p = {
+
local db = {
 
species = {
 
species = {
 
by_name = {},
 
by_name = {},
Line 33: Line 48:
 
by_tag = {}
 
by_tag = {}
 
}
 
}
 +
}
 +
 +
db.species.unknown = {
 +
name = "Unknown",
 +
description = "an unknown species",
 +
trimmed_description = "unknown species",
 +
elemental_type = "Typeless"
 
}
 
}
  
 
for _,s in ipairs(species) do
 
for _,s in ipairs(species) do
 +
normalize_species(s)
 
p.species.by_name[s.name] = s
 
p.species.by_name[s.name] = s
 
if s.bestiary_index ~= nil and s.bestiary_index >= 0 then
 
if s.bestiary_index ~= nil and s.bestiary_index >= 0 then
Line 66: Line 89:
 
end
 
end
  
return p
+
return db

Revision as of 20:29, 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

function normalize_species(species)
	species.trimmed_description = species.description
	if string.substr(species.trimmed_description, 1, 4) == "an " then
		species.trimmed_description = string.substr(species.trimmed_description, 4)
	elseif string.substr(species.trimmed_description, 1, 3) == "a " then
		species.trimmed_description = string.substr(species.trimmed_description, 3)
	end
	
	if species.bestiary_index ~= nil and species.bestiary_index >= 0 then
		species.index = string.format("%03d", species.bestiary_index)
	else
		species.index = "???"
	end
end

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

local db = {
	species = {
		by_name = {},
		by_index = {},
		by_tag = {},
		min_index = 1,
		max_index = 0
	},
	moves = {
		by_name = {},
		by_tag = {}
	}
}

db.species.unknown = {
	name = "Unknown",
	description = "an unknown species",
	trimmed_description = "unknown species",
	elemental_type = "Typeless"
}

for _,s in ipairs(species) do
	normalize_species(s)
	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
		p.species.min_index = math.min(p.species.min_index, s.bestiary_index)
		p.species.max_index = math.max(p.species.max_index, s.bestiary_index)
	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
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
for tag, move in pairs(p.moves.by_tag) do
	table.sort(moves, cmp_moves)
end

return db