Difference between revisions of "Module:Database/data"

From Cassette Beasts
Line 44: Line 44:
 
if name == "" then
 
if name == "" then
 
species.evolves_to[i] = "Unknown"
 
species.evolves_to[i] = "Unknown"
 +
end
 +
end
 +
for i,habitat in ipairs(species.habitats) do
 +
local pos = string.find(habitat, ",")
 +
if pos ~= nil then
 +
species.habitats[i] = string.sub(pos - 1)
 
end
 
end
 
end
 
end

Revision as of 21:53, 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.sub(species.trimmed_description, 1, 3) == "an " then
		species.trimmed_description = string.sub(species.trimmed_description, 4)
	elseif string.sub(species.trimmed_description, 1, 2) == "a " then
		species.trimmed_description = string.sub(species.trimmed_description, 3)
	end
	
	if species.bestiary_index ~= nil and species.bestiary_index < 0 then
		species.bestiary_index = nil
	end
	
	if species.bestiary_index ~= nil then
		species.index = string.format("%03d", species.bestiary_index)
	else
		species.index = "???"
	end
	
	for i,name in ipairs(species.evolves_from) do
		if name == "" then
			species.evolves_from[i] = "Unknown"
		end
	end
	for i,name in ipairs(species.evolves_to) do
		if name == "" then
			species.evolves_to[i] = "Unknown"
		end
	end
	for i,habitat in ipairs(species.habitats) do
		local pos = string.find(habitat, ",")
		if pos ~= nil then
			species.habitats[i] = string.sub(pos - 1)
		end
	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",
	elemental_type = "Typeless",
	bestiary_bios = {""},
	evolves_from = {},
	evolves_to = {},
	habitats = {},
	moves = { initial = {}, tags = {}, upgrades = {} },
	stats = {
		max_ap = 0,
		max_hp = 0,
		melee_attack = 0,
		melee_defense = 0,
		move_slots = 0,
		ranged_attack = 0,
		ranged_defense = 0,
		record_rate = 0,
		speed = 0
	}
}
normalize_species(db.species.unknown)

for _,s in ipairs(species) do
	normalize_species(s)
	db.species.by_name[s.name] = s
	if s.bestiary_index ~= nil then
		db.species.by_index[s.bestiary_index] = s
		db.species.min_index = math.min(db.species.min_index, s.bestiary_index)
		db.species.max_index = math.max(db.species.max_index, s.bestiary_index)
	end
	for _,tag in ipairs(s.moves.tags) do
		if db.species.by_tag[tag] == nil then
			db.species.by_tag[tag] = {}
		end
		table.insert(db.species.by_tag[tag], s)
	end
end
for tag, species in pairs(db.species.by_tag) do
	table.sort(species, cmp_species)
end

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

return db