def path_to_unexplored(self, current_room): # id's of rooms passed in. all_rooms = RoomModel.find_all() rooms_dict = {} # make all rooms represent their JSON object, and insert into rooms dict. for i in range(len(all_rooms)): rooms_dict[str(all_rooms[i].json()['id'])] = all_rooms[i].json() visited_rooms = set() queue = [] path = [] room = current_room #id of room queue.append([room]) while len(queue) > 0: current_path = queue.pop(0) prev_room = current_path[-1] if prev_room is not None: if prev_room not in visited_rooms: visited_rooms.add(prev_room) for exit_dir in rooms_dict[str(prev_room)]['exits']: new_path = current_path[:] new_path.append( rooms_dict[str(prev_room)]['exits'][exit_dir]) if rooms_dict[str( prev_room)]['exits'][exit_dir] == '?': path = { "path": self.create_path(rooms_dict, current_path) } return path else: queue.append(new_path) path.append(exit_dir) # no path found. return None return None
def get(self): rooms = RoomModel.find_all() if rooms is None: return {'message': 'No rooms were found.'}, 404 for i in range(len(rooms)): rooms[i] = rooms[i].json() return rooms, 200