Module:Database/data

From Cassette Beasts

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

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

local stats = {"Accuracy", "Evasion", "Melee Attack", "Melee Defence", "Ranged Attack", "Ranged Defence", "Speed"}
local extra_status_descriptions = {
	["double speed"] = "Characters with Double Speed have their Speed stat raised by 100%.",
}
for _,stat in ipairs(stats) do
	extra_status_descriptions[string.lower(stat) .. " down"] = "Characters with "..stat.." Down have their "..stat.." stat lowered by one third."
	extra_status_descriptions[string.lower(stat) .. " up"] = "Characters with "..stat.." Up have their "..stat.." stat raised by one half."
end
local skip_status_effects = {
	["apple tree"] = true,
	["inflated"] = true,
	["deflated"] = true
}

local db = {
	species = {
		by_name = {},
		by_index = {},
		unnumbered = {},
		by_tag = {},
		by_type = {},
		min_index = 1,
		max_index = 120
	},
	moves = {
		by_name = {},
		by_tag = {},
		by_type = {}
	},
	types = {
		by_name = {},
		ordered = {}
	},
	status_effects = {
		by_name = {}
	}
}

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
	}
}

db.moves.unknown = {
	accuracy = 100,
	ap_cost = 0,
	can_be_copied = true,
	category = "Miscellaneous",
	crit_damage_percent = 150,
	crit_rate = 0.0625,
	default_target = "DEFAULT_TARGET_ENEMY",
	description = "Unknown move.",
	elemental_type = "Typeless",
	is_passive_only = false,
	name = "Unknown",
	physicality = "MELEE",
	power = 0,
	priority = 0,
	tags = {},
	target_type = "TARGET_ONE",
	camouflage = ""
}

db.types.typeless = {
	inflicts = {},
	name = "Typeless",
	palette = {"000", "fff", "fff", "fff", "fff"},
	receives = {},
	sort_order = -99
}
db.types.by_name.typeless = db.types.typeless

db.status_effects.unknown = {
	name = "Unknown",
	description = "Unknown status effect.",
	has_duration = true,
	is_buff = false,
	is_debuff = false,
	is_removable = false
}

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)
	if ai == bi then
		return string.lower(a.name) < string.lower(b.name)
	end
	return ai < bi
end

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

function cmp_types(a, b)
	if a.sort_order == b.sort_order then
		return string.lower(a.name) < string.lower(b.name)
	end
	return a.sort_order < b.sort_order
end

function cmp_status_effects(a, b)
	return string.lower(a.name) < string.lower(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,evo in ipairs(species.evolves_to) do
		if evo.evolved_form == "" then
			evo.evolved_form = "Unknown"
		end
		evo.unevolved_form = species.name
	end
	for i,habitat in ipairs(species.habitats) do
		habitat = string.gsub(habitat, "[\"“”]", "")
		local pos = string.find(habitat, ",")
		if pos ~= nil then
			habitat = string.sub(habitat, 1, pos - 1)
		end
		species.habitats[i] = habitat
	end
	
	table.insert(species.moves.tags, "any")
	species.moves.core_tags = {}
	for _,tag in ipairs(species.moves.tags) do
		table.insert(species.moves.core_tags, tag)
	end
	table.insert(species.moves.tags, string.lower(species.elemental_type))
end

function find_evo(evo_from, evo_to)
	for _,evo in pairs(evo_from.evolves_to) do
		if evo.evolved_form == evo_to.name then
			return evo
		end
	end
	return nil
end

function late_fix_species(species)
	local new_evo_from = {}
	for _,evo_from in ipairs(species.evolves_from) do
		local evo_from_species = db.species.by_name[string.lower(evo_from)]
		if evo_from_species ~= nil then
			local evo = find_evo(evo_from_species, species)
			if evo ~= nil then
				table.insert(new_evo_from, evo)
			end
		end
	end
	species.evolves_from = new_evo_from
end

function normalize_status_effect(status_effect)
	if status_effect.description == "" and extra_status_descriptions[string.lower(status_effect.name)] then
		status_effect.description = extra_status_descriptions[string.lower(status_effect.name)]
	end
	if status_effect.is_buff and not status_effect.is_debuff then
		status_effect.kind = "Buff"
	elseif status_effect.is_debuff and not status_effect.is_buff then
		status_effect.kind = "Debuff"
	elseif string.match(status_effect.name, " Coating$") then
		status_effect.kind = "Transmutation"
	else
		status_effect.kind = "Misc"
	end
	status_effect.moves = {}
	status_effect.reactions = {}
end

normalize_species(db.species.unknown)
normalize_status_effect(db.status_effects.unknown)

for _,s in ipairs(species) do
	normalize_species(s)
	db.species.by_name[string.lower(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)
	else
		table.insert(db.species.unnumbered, s)
	end
	
	if db.species.by_type[string.lower(s.elemental_type)] == nil then
		db.species.by_type[string.lower(s.elemental_type)] = {}
	end
	table.insert(db.species.by_type[string.lower(s.elemental_type)], s)
	
	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
table.sort(db.species.unnumbered, cmp_species)
for _,species in pairs(db.species.by_tag) do
	table.sort(species, cmp_species)
end
for _,species in pairs(db.species.by_type) do
	table.sort(species, cmp_species)
end
for _,species in pairs(db.species.by_name) do
	late_fix_species(species)
end

for _,m in ipairs(moves) do
	db.moves.by_name[string.lower(m.name)] = m
	
	if db.moves.by_type[string.lower(m.elemental_type)] == nil then
		db.moves.by_type[string.lower(m.elemental_type)] = {}
	end
	table.insert(db.moves.by_type[string.lower(m.elemental_type)], m)
	
	m.camouflage = ""
	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)
		if tag == "camouflage" then
			m.camouflage = string.lower(m.elemental_type)
		end
	end
end
for _,moves in pairs(db.moves.by_type) do
	table.sort(moves, cmp_moves)
end
for _,moves in pairs(db.moves.by_tag) do
	table.sort(moves, cmp_moves)
end

for _,t in ipairs(types) do
	db.types.by_name[string.lower(t.name)] = t
	table.insert(db.types.ordered, t)
end
table.sort(db.types.ordered, cmp_types)

for _,s in ipairs(status_effects) do
	if not skip_status_effects[string.lower(s.name)] then
		normalize_status_effect(s)
		db.status_effects.by_name[string.lower(s.name)] = s
	end
end

for _,move in pairs(db.moves.by_name) do
	for _,status_name in pairs(move.status_effects) do
		local status_effect = db.status_effects.by_name[string.lower(status_name)]
		if status_effect then
			table.insert(status_effect.moves, move.name)
		end
	end
end
for _,t in pairs(db.types.by_name) do
	for _,reaction in pairs(t.inflicts) do
		for _,status_pair in pairs(reaction.statuses) do
			local status_effect = db.status_effects.by_name[string.lower(status_pair[1])]
			if status_effect then
				table.insert(status_effect.reactions, reaction)
			end
		end
	end
end

return db