feat: update Player's History

main
Khiem Ton 2 years ago
parent 7a0ad476eb
commit a6865f7a4a
Signed by: th4tkh13m
GPG Key ID: 4D9CF147DCADD05D

3
.idea/.gitignore vendored

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

@ -0,0 +1,13 @@
from ispoof.objects import Pokemon, Raid
from ispoof.lists.playerhistory import PlayerHistory
from sqlalchemy.engine import Engine
from sqlalchemy.exc import ArgumentError
def initialize_database(engine: Engine):
try:
Pokemon.metadata.create_all(engine)
Raid.metadata.create_all(engine)
PlayerHistory.metadata.create_all(engine)
except ArgumentError:
print("Exception")

@ -0,0 +1,39 @@
import sqlalchemy.exc
from geopy.geocoders import Nominatim
from ..core.location import Location
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, DateTime, PrimaryKeyConstraint
from sqlalchemy.orm import Session
from datetime import datetime, timedelta
from math import ceil
Base = declarative_base()
class PlayerHistory(Base):
__tablename__ = "player_history"
time = Column(DateTime)
location = Column(Location)
PrimaryKeyConstraint(time, name="history_pk")
def __init__(self, engine=None):
self.engine = engine
def get_last_activity(self):
time, location = None, None
with Session(self.engine) as session:
result = session.query(PlayerHistory).order_by(PlayerHistory.time.desc()).first()
if result:
time, location = result.time, result.location
session.commit()
return time, location
def add_activity(self, location: Location):
with Session(self.engine) as session:
self.time = datetime.now()
self.location = location
session.add(self)
session.commit()
return True

@ -7,7 +7,8 @@ from typing import List
from sqlalchemy import desc
from datetime import datetime, timedelta
class PokemonList():
class PokemonList:
def __init__(self, engine: Engine):
self.pokemons = None
self.engine = engine

@ -1,9 +0,0 @@
class Object():
def __init__(self, location=None):
self.location = location
def distance_to(self, other):
return self.location.distance(other)
def cooldown_to(self, other):
return self.location.get_cooldown(other) + 1

@ -1,11 +1,17 @@
from geopy.geocoders import Nominatim
from ..core.location import Location
from .object import Object
from ..lists.playerhistory import PlayerHistory
from sqlalchemy.orm import Session
from datetime import datetime, timedelta
from math import ceil
class Player(Object):
def __init__(self):
super().__init__()
self.current_cooldown = 0
class Player():
def __init__(self, engine, location=None, cooldown=0):
self.location = location
self.cooldown = cooldown
self.history = PlayerHistory(engine=engine)
def set_location_with_query(self, query):
loc = Nominatim(user_agent="GetLoc")
@ -13,14 +19,43 @@ class Player(Object):
self.location = Location(get_loc.latitude, get_loc.longitude)
def set_location(self, location, did_activity):
next_cd = self.cooldown_to(location)
if did_activity:
if next_cd + self.current_cooldown >= 120:
self.current_cooldown = 120
else:
self.current_cooldown += next_cd
def get_last_activity(self):
return self.history.get_last_activity()
def set_location(self, location: Location):
self.location = location
def do_activity(self, location: Location):
return self.history.add_activity(location)
def get_location(self):
return self.location
def distance_to(self, other):
return self.location.distance(other)
def get_cooldown(self, location):
time, history_location = self.get_last_activity()
if time and history_location:
cooldown = history_location.get_cooldown(location)
current_cooldown = ceil((time + timedelta(minutes=cooldown) - datetime.now()).total_seconds() / 60)
if current_cooldown < 0:
current_cooldown = 0
else:
current_cooldown = 0
return current_cooldown
def get_current_cooldown(self):
self.cooldown = self.get_cooldown(self.location)
return self.cooldown
def change_gps_by_query(self, query: str, do_activity=False):
self.set_location_with_query(query)
if do_activity:
self.do_activity(self.location)
def change_gps_by_location(self, location: Location, do_activity=False):
self.set_location(location)
if do_activity:
self.do_activity(self.location)

@ -1,10 +1,9 @@
from .object import Object
from datetime import datetime
from sqlalchemy import Column, String, Integer, Boolean, Time, PrimaryKeyConstraint
from sqlalchemy.orm import declarative_base
Base = declarative_base()
class Raid(Object, Base):
class Raid(Base):
__tablename__ = "raid"
name = Column(String(30))

@ -34,7 +34,7 @@ class Scraper():
shiny = data[9].text == "Yes"
start_time = datetime.fromisoformat(data[10].text)
end_time = datetime.fromisoformat(data[11].text)
country = data[12].text
country = data[12].text.strip()
pokemon = Pokemon(name=name, number=number, location=location, cp=cp, level=level, attack=attack,
defense=defense, hp=hp, iv=iv, shiny=shiny, start_time=start_time, end_time=end_time,

@ -17,16 +17,17 @@ if __name__ == "__main__":
except Exception as e:
# traceback.print_exc()
print("Already mounted. Continuing.")
player = Player()
engine = create_engine("sqlite:///data.db")
initialize_database(engine=engine)
player = Player(engine=engine)
query = input("Enter your location: ")
player.set_location_with_query(query)
print(str(player.location))
scraper = Scraper()
engine = create_engine("sqlite:///data.db")
initialize_database(engine=engine)
pokemon_lst = PokemonList(engine=engine)
while True:
pokemons = scraper.get_hundos()
pokemon_lst.insert_to_database(pokemons)
print("Spoof to")
@ -37,7 +38,6 @@ if __name__ == "__main__":
i = int(input("Choose pokemon: "))
pokemon = pokemons[i]
location = pokemon.location
print(pokemon)
device.spoof_gps(location)
did_activity = None
print(pokemon)
@ -46,8 +46,8 @@ if __name__ == "__main__":
if activity in ("y", "n"):
did_activity = activity == "y"
break
player.set_location(location, did_activity)
print(f"Current cooldown: {player.current_cooldown} min")
player.change_gps_by_location(location, did_activity)
print(f"Current cooldown: {player.get_current_cooldown()} min")
while True:
continue_prompt = input("Continue? [Y/N] ").lower()

Loading…
Cancel
Save