From bfb6d83d060fc8a312feeb0ca17b5c0d30bd24b8 Mon Sep 17 00:00:00 2001 From: Khiem Ton Date: Fri, 24 Mar 2023 22:33:36 +0700 Subject: [PATCH] feat: save what Pokemon has been visited --- ispoof/lists/pokemonlist.py | 26 ++++++++++++++++++-------- ispoof/objects/pokemon.py | 7 +++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/ispoof/lists/pokemonlist.py b/ispoof/lists/pokemonlist.py index e14789e..d495413 100644 --- a/ispoof/lists/pokemonlist.py +++ b/ispoof/lists/pokemonlist.py @@ -19,10 +19,10 @@ class PokemonList: def sort_by_name(self, reverse=False): with Session(self.engine) as session: if reverse: - result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta)\ + result = session.query(Pokemon).where(Pokemon.visited.is_(False), Pokemon.end_time > datetime.now() + self.timedelta)\ .order_by(desc(Pokemon.name)) else: - result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta)\ + result = session.query(Pokemon).where(Pokemon.visited.is_(False), Pokemon.end_time > datetime.now() + self.timedelta)\ .order_by(Pokemon.name) session.commit() session.close() @@ -31,10 +31,10 @@ class PokemonList: def sort_by_cp(self, reverse=False): with Session(self.engine) as session: if reverse: - result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) \ + result = session.query(Pokemon).where(Pokemon.visited.is_(False), Pokemon.end_time > datetime.now() + self.timedelta) \ .order_by(desc(Pokemon.cp)) else: - result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) \ + result = session.query(Pokemon).where(Pokemon.visited.is_(False), Pokemon.end_time > datetime.now() + self.timedelta) \ .order_by(Pokemon.cp) session.commit() session.close() @@ -43,10 +43,10 @@ class PokemonList: def sort_by_level(self, reverse=False): with Session(self.engine) as session: if reverse: - result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) \ + result = session.query(Pokemon).where(Pokemon.visited.is_(False), Pokemon.end_time > datetime.now() + self.timedelta) \ .order_by(desc(Pokemon.level)) else: - result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) \ + result = session.query(Pokemon).where(Pokemon.visited.is_(False), Pokemon.end_time > datetime.now() + self.timedelta) \ .order_by(Pokemon.level) session.commit() session.close() @@ -54,14 +54,14 @@ class PokemonList: def sort_by_distance(self, location: Location, reverse=False): with Session(self.engine) as session: - result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) + result = session.query(Pokemon).where(Pokemon.visited.is_(False), Pokemon.end_time > datetime.now() + self.timedelta) session.commit() session.close() return sorted(result.all(), key=lambda pokemon: location.distance(pokemon.location), reverse=reverse) def search_by_name(self, query: str): with Session(self.engine) as session: - result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) + result = session.query(Pokemon).where(Pokemon.visited.is_(False), Pokemon.end_time > datetime.now() + self.timedelta) session.commit() session.close() return list(filter(lambda pokemon: partial_ratio(query.lower(), pokemon.name) >= 80, result.all())) @@ -73,3 +73,13 @@ class PokemonList: session.commit() session.close() + def visit_pokemon(self, pokemon: Pokemon): + with Session(self.engine) as session: + new_pokemon = session.query(Pokemon).where(Pokemon.name == pokemon.name, + Pokemon.location == pokemon.location, + Pokemon.start_time == pokemon.start_time, + Pokemon.end_time == pokemon.end_time).one() + new_pokemon.visited = True + session.commit() + session.close() + diff --git a/ispoof/objects/pokemon.py b/ispoof/objects/pokemon.py index 2cb5b1e..05e43a3 100644 --- a/ispoof/objects/pokemon.py +++ b/ispoof/objects/pokemon.py @@ -1,7 +1,7 @@ from datetime import datetime from sqlalchemy import Column, String, Integer, Boolean, DateTime, PrimaryKeyConstraint from ispoof.spoofer.location import Location -from sqlalchemy.orm import declarative_base +from sqlalchemy.orm import declarative_base, Session Base = declarative_base() @@ -21,6 +21,7 @@ class Pokemon(Base): start_time = Column(DateTime) end_time = Column(DateTime) country = Column(String(2)) + visited = Column(Boolean, default=False) PrimaryKeyConstraint(name, location, start_time, end_time, name="pokemon_pk", sqlite_on_conflict='IGNORE') def __init__(self, name, number, location, @@ -41,9 +42,7 @@ class Pokemon(Base): self.end_time = end_time self.country = country - super(Pokemon, self).__init__() - - def is_dispawned(self): + def is_despawned(self): if datetime().now() > self.end_time: return True return False