forked from c0de/BaseballBot
Working base version
This commit is contained in:
0
src/main/database_module/__init__.py
Normal file
0
src/main/database_module/__init__.py
Normal file
31
src/main/database_module/database_classes/db_classes.py
Normal file
31
src/main/database_module/database_classes/db_classes.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from sqlalchemy import Column, String, Integer, ForeignKey, Date
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.ext.hybrid import hybrid_property
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from src.main.db_session import DatabaseSession
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
class Play(Base):
|
||||
__tablename__ = 'play'
|
||||
|
||||
play_id = Column(String, nullable=False, primary_key=True)
|
||||
pitch_value = Column(Integer, nullable=True)
|
||||
swing_value = Column(Integer, nullable=False)
|
||||
creation_date = Column(Date, nullable=False)
|
||||
|
||||
guesses = relationship(lambda : Guess)
|
||||
|
||||
class Guess(Base):
|
||||
__FAKE_VALUE__ = -5000
|
||||
|
||||
__tablename__ = 'guess'
|
||||
member_id = Column(String, nullable=False, primary_key=True)
|
||||
play_id = Column(UUID, ForeignKey(Play.play_id), nullable=False, primary_key=True)
|
||||
guessed_number = Column(Integer, nullable=False)
|
||||
member_name = Column(String, nullable=False)
|
||||
difference = Column(Integer)
|
||||
|
||||
play = relationship("Play", back_populates="guesses")
|
85
src/main/database_module/guess_dao.py
Normal file
85
src/main/database_module/guess_dao.py
Normal file
@@ -0,0 +1,85 @@
|
||||
from copy import deepcopy
|
||||
|
||||
from src.main.database_module.database_classes.db_classes import Guess
|
||||
from src.main.db_session import DatabaseSession
|
||||
|
||||
MEMBER_ID = 'member_id'
|
||||
PLAY_ID = 'play_id'
|
||||
GUESSED_NUMBER = 'guessed_number'
|
||||
DIFFERENCE = 'difference'
|
||||
MEMBER_NAME = 'member_name'
|
||||
|
||||
class GuessDAO():
|
||||
db_string = None
|
||||
session = None
|
||||
Session = None
|
||||
engine = None
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def insert(self, guess_info):
|
||||
session = DatabaseSession.session
|
||||
|
||||
guess = Guess(
|
||||
member_id=guess_info[MEMBER_ID],
|
||||
play_id = guess_info[PLAY_ID],
|
||||
guessed_number = guess_info[GUESSED_NUMBER],
|
||||
member_name = guess_info[MEMBER_NAME]
|
||||
)
|
||||
|
||||
existing_guess = self.__convert_all__(session\
|
||||
.query(Guess)\
|
||||
.filter(Guess.member_id == guess_info[MEMBER_ID], Guess.play_id == guess_info[PLAY_ID]))
|
||||
|
||||
if len(existing_guess) == 0:
|
||||
session.add(guess)
|
||||
session.commit()
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
'''
|
||||
Converts the database object into a Dictionary, so that the database object is not passed out of the
|
||||
datastore layer.
|
||||
'''
|
||||
def __convert_all__(self, games):
|
||||
converted_games = []
|
||||
for game in games:
|
||||
game_dict = {}
|
||||
for column in game.__dict__:
|
||||
game_dict[column] = str(getattr(game, column))
|
||||
|
||||
converted_games.append(deepcopy(game_dict))
|
||||
|
||||
return converted_games
|
||||
|
||||
def set_differences(self, pitch_value, play_id):
|
||||
session = DatabaseSession.session
|
||||
games_to_update = self.__convert_all__(session.query(Guess).filter(Guess.play_id == play_id))
|
||||
|
||||
for game in games_to_update:
|
||||
difference = self.calculate_difference(pitch_value, game[GUESSED_NUMBER])
|
||||
session.query(Guess).filter(Guess.member_id == game[MEMBER_ID], Guess.play_id == game[PLAY_ID]).update({Guess.difference: difference})
|
||||
|
||||
session.commit()
|
||||
|
||||
def calculate_difference(self, pitch_value, guess_value):
|
||||
pitched_number = int(pitch_value)
|
||||
possible_value = abs(int(guess_value) - pitched_number)
|
||||
|
||||
if possible_value > 500:
|
||||
return 1000 - possible_value
|
||||
else:
|
||||
return possible_value
|
||||
|
||||
|
||||
def fetch_closest(self, num_to_fetch):
|
||||
session = DatabaseSession.session
|
||||
|
||||
return self.__convert_all__(
|
||||
session\
|
||||
.query(Guess)\
|
||||
.order_by(Guess.difference)\
|
||||
.limit(num_to_fetch)
|
||||
)
|
79
src/main/database_module/play_dao.py
Normal file
79
src/main/database_module/play_dao.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from copy import deepcopy
|
||||
|
||||
from src.main.db_session import DatabaseSession
|
||||
from src.main.database_module.database_classes.db_classes import Play
|
||||
|
||||
PLAY_ID = 'play_id'
|
||||
PITCH_VALUE = 'pitch_value'
|
||||
SWING_VALUE = 'swing_value'
|
||||
CREATION_DATE = 'creation_date'
|
||||
|
||||
class PlayDAO():
|
||||
db_string = None
|
||||
session = None
|
||||
Session = None
|
||||
engine = None
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def insert(self, play_info):
|
||||
session = DatabaseSession.session
|
||||
|
||||
play = Play(
|
||||
play_id = play_info[PLAY_ID],
|
||||
pitch_value = play_info[PITCH_VALUE] if PITCH_VALUE in play_info else None,
|
||||
swing_value = play_info[SWING_VALUE] if SWING_VALUE in play_info else None,
|
||||
creation_date = play_info[CREATION_DATE]
|
||||
)
|
||||
|
||||
session.add(play)
|
||||
session.commit()
|
||||
|
||||
def get_play_by_id(self, input_id):
|
||||
session = DatabaseSession.session
|
||||
return self.__convert_all__(session.query(Play).filter(Play.play_id == input_id))
|
||||
|
||||
'''
|
||||
Checks to see if there is a play that is currently active or not
|
||||
'''
|
||||
def is_active_play(self):
|
||||
return self.get_active_play() != None
|
||||
|
||||
def get_active_play(self):
|
||||
session = DatabaseSession.session
|
||||
plays = self.__convert_all__(session.query(Play).filter(Play.pitch_value == None))
|
||||
|
||||
if len(plays) > 1:
|
||||
raise AssertionError("More than one active play! Can't continue!")
|
||||
elif len(plays) == 0:
|
||||
return None
|
||||
else:
|
||||
return plays[0]
|
||||
|
||||
def resolve_play(self, input_pitch, input_swing):
|
||||
session = DatabaseSession.session
|
||||
active_id = self.get_active_play()
|
||||
|
||||
session\
|
||||
.query(Play)\
|
||||
.filter(Play.pitch_value == None)\
|
||||
.update({Play.pitch_value: input_pitch, Play.swing_value: input_swing})
|
||||
session.commit()
|
||||
|
||||
return active_id
|
||||
|
||||
'''
|
||||
Converts the database object into a Dictionary, so that the database object is not passed out of the
|
||||
datastore layer.
|
||||
'''
|
||||
def __convert_all__(self, plays):
|
||||
converted_plays = []
|
||||
for play in plays:
|
||||
play_dict = {}
|
||||
for column in play.__dict__:
|
||||
play_dict[column] = str(getattr(play, column))
|
||||
|
||||
converted_plays.append(deepcopy(play_dict))
|
||||
|
||||
return converted_plays
|
Reference in New Issue
Block a user