Difference between revisions of "Module:Database/data"
From Cassette Beasts
Line 25: | Line 25: | ||
by_name = {}, | by_name = {}, | ||
by_index = {}, | by_index = {}, | ||
− | by_tag = {} | + | by_tag = {}, |
+ | min_index = 1, | ||
+ | max_index = 0 | ||
}, | }, | ||
moves = { | moves = { | ||
Line 37: | Line 39: | ||
if s.bestiary_index ~= nil and s.bestiary_index >= 0 then | if s.bestiary_index ~= nil and s.bestiary_index >= 0 then | ||
p.species.by_index[s.bestiary_index] = s | p.species.by_index[s.bestiary_index] = s | ||
+ | p.species.min_index = min(p.species.min_index, s.bestiary_index) | ||
+ | p.species.max_index = max(p.species.max_index, s.bestiary_index) | ||
end | end | ||
for _,tag in ipairs(s.moves.tags) do | for _,tag in ipairs(s.moves.tags) do | ||
Line 45: | Line 49: | ||
end | end | ||
end | end | ||
− | |||
− | |||
for tag, species in pairs(p.species.by_tag) do | for tag, species in pairs(p.species.by_tag) do | ||
table.sort(species, cmp_species) | table.sort(species, cmp_species) | ||
Line 60: | Line 62: | ||
end | end | ||
end | end | ||
− | |||
for tag, move in pairs(p.moves.by_tag) do | for tag, move in pairs(p.moves.by_tag) do | ||
table.sort(moves, cmp_moves) | table.sort(moves, cmp_moves) |
Revision as of 19:16, 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 = {},
min_index = 1,
max_index = 0
},
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
p.species.min_index = min(p.species.min_index, s.bestiary_index)
p.species.max_index = 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 p