parent
7a0ad476eb
commit
a6865f7a4a
@ -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
|
@ -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,26 +1,61 @@
|
|||||||
from geopy.geocoders import Nominatim
|
from geopy.geocoders import Nominatim
|
||||||
from ..core.location import Location
|
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):
|
class Player():
|
||||||
super().__init__()
|
|
||||||
self.current_cooldown = 0
|
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):
|
def set_location_with_query(self, query):
|
||||||
loc = Nominatim(user_agent="GetLoc")
|
loc = Nominatim(user_agent="GetLoc")
|
||||||
get_loc = loc.geocode(query)
|
get_loc = loc.geocode(query)
|
||||||
|
|
||||||
self.location = Location(get_loc.latitude, get_loc.longitude)
|
self.location = Location(get_loc.latitude, get_loc.longitude)
|
||||||
|
|
||||||
def set_location(self, location, did_activity):
|
def get_last_activity(self):
|
||||||
next_cd = self.cooldown_to(location)
|
return self.history.get_last_activity()
|
||||||
if did_activity:
|
|
||||||
if next_cd + self.current_cooldown >= 120:
|
def set_location(self, location: Location):
|
||||||
self.current_cooldown = 120
|
|
||||||
else:
|
|
||||||
self.current_cooldown += next_cd
|
|
||||||
self.location = location
|
self.location = location
|
||||||
|
|
||||||
|
def do_activity(self, location: Location):
|
||||||
|
return self.history.add_activity(location)
|
||||||
|
|
||||||
def get_location(self):
|
def get_location(self):
|
||||||
return self.location
|
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)
|
||||||
|
|
||||||
|
Loading…
Reference in new issue