Обновлена 15 сентября 2026 г. в 08:55
Итераторы индексов
По нашей игровой логике все пойманные покемоны возвращаются на карту.
Это делается для всех покемонов на карте каждые 60 секунд с помощью
метода respawn(). Мы перебираем покемонов по статусу, используя
функцию итератора индексов Tarantool LINK_NOT_DEFINED(../../../../reference/reference_lua/box/box_index/pairs),
и сбрасываем статус всех пойманных покемонов обратно на «active» с помощью box.space.pokemons:update().
respawn = function(self)fiber.name('Respawn fiber')for _, tuple in box.space.pokemons.index.status:pairs(self.state.CAUGHT) dobox.space.pokemons:update(tuple[self.ID],{{'=', self.STATUS, self.state.ACTIVE}})endend
Для удобства чтения введем именованные поля:
ID = 1,STATUS = 2,
Полная реализация start() теперь выглядит так:
-- create game objectstart = function(self)-- create spaces and indexesbox.once('init', function()box.schema.create_space('pokemons')box.space.pokemons:create_index("primary", {type = 'hash', parts = {1, 'unsigned'}})box.space.pokemons:create_index("status", {type = "tree", parts = {2, 'str'}})end)-- create modelslocal ok_m, pokemon = avro.create(schema.pokemon)local ok_p, player = avro.create(schema.player)if ok_m and ok_p then-- compile modelslocal ok_cm, compiled_pokemon = avro.compile(pokemon)local ok_cp, compiled_player = avro.compile(player)if ok_cm and ok_cp then-- start the gameself.pokemon_model = compiled_pokemonself.player_model = compiled_playerself.respawn()log.info('Started')return trueelselog.error('Schema compilation failed')endelselog.info('Schema creation failed')endreturn falseend