def set_current_drafter(): query_results = sql_lib.execute_query("select * from draft;") draft_order = query_results[0][1] current_user = query_results[0][2] snake = query_results[0][3] # Handle the snake draft scenario if snake and current_user == draft_order[-1]: # Reverse the draft order logging.info("Reversing the draft order for snake") draft_order_reverse = draft_order[::-1] draft_order_reverse = json.dumps(draft_order_reverse).replace( "'", "\"").replace("[", "{").replace("]", "}") sql_lib.execute_query( f"update draft set draft_order='{draft_order_reverse}';", update=True) # Current user stays the same - snake return # Core draft indexing for index in range(len(draft_order)): if draft_order[index] == current_user: current_user = draft_order[index + 1] sql_lib.execute_query( f"update draft set current_drafter={current_user};", update=True) logging.info(f"Current user set: {current_user}") return
def make_selection(user, message): logging.info(f"Received message: {message} from {user}") query_results = sql_lib.execute_query("select * from draft;") current_user = query_results[0][2] participants = query_results[0][0] team_count = sql_lib.execute_query( "select count(*) from teams where season='2021';")[0][0] # Don't let a user draft out of turn if user != current_user: logging.info("User out of turn - returning message") return templates.out_of_turn.format( get_username_by_id(current_user, participants)) # Parse out a valid team selection = message.split("draft ")[1] teams = [team.upper() for team in get_teams()] if check_team_draft_status(selection.upper(), Config['season']): # TODO: Add draft status method logging.info("Selection has already been taken - returning message") return templates.selection_taken.format(selection.title()) if selection.upper() in teams: logging.info(f"Valid selection of: {selection.upper()}") logging.info("Logging user selection in database") draft_team(get_username_by_id(current_user, participants), selection.upper(), Config['season'], team_count + 1) # Send a message to the group that the pick has been made # Use this variable to hold the user who picked - current user will now be the next up # Reallyyyy need to refactor this at some point pick_made_by = get_username_by_id(current_user, participants) # Set the next drafter - update current_user variable to reflect this set_current_drafter() current_user = sql_lib.execute_query( "select current_drafter from draft;")[0][0] ack_message = templates.draft_acknowledgment.format( team_count + 1, pick_made_by, selection.title(), get_username_by_id(current_user, participants)) # End the draft if all teams are drafted if team_count + 1 >= int(Config["num_teams"]): logging.info("End of draft! Returning message") sql_lib.execute_query(f"update draft set active={False};") return templates.end_message.format(Config['season']) logging.info("Successful pick made - returning message") return ack_message else: logging.info("Invalid selection received - returning message") return templates.draft_failure.format(selection)
def init_draft(participants, draft_order=None, snake=True): current_drafter = draft_order[0] participants_string = json.dumps(participants).replace("'", "\"") draft_order_string = json.dumps(draft_order).replace("'", "\"").replace( "[", "{").replace("]", "}") query = f"insert into draft(participants, draft_order, current_drafter, snake, season, active) values ('{participants_string}', '{draft_order_string}', '{current_drafter}', {snake}, '{Config['season']}', {True});" sql_lib.execute_query(query, update=True) welcome_message = templates.draft_welcome_message.format( Config['season'], get_username_by_id(draft_order[0], participants), get_username_by_id(draft_order[1], participants), get_username_by_id(draft_order[2], participants), get_username_by_id(draft_order[3], participants), get_username_by_id(draft_order[0], participants)) return welcome_message
def get_draft_object(): """Get the Draft object from pickle file.""" try: result = sql_lib.execute_query("SELECT * FROM draft;") return pickle.loads(result[0][0]) except Exception as err: logging.error(f"Error unpickling object from database: {err}")
def get_teams(): """Get a list of teams from the database.""" result = sql_lib.execute_query("SELECT team FROM seasons;") teams_list = [] if result: for res in result: teams_list.append(res[0]) return teams_list
def teams_drafted(season): """Get a list of teams drafted so far.""" result = sql_lib.execute_query( f"SELECT team FROM teams WHERE season='{season}';") teams_list = [] drafted_string = "" if result: for res in result: teams_list.append(res[0]) for team in teams_list: drafted_string += f"{team}\n" return templates.teams_drafted.format(drafted_string)
def get_teams(): # TODO: Refactor this to at least be a dict of lists so we aren't hard-coding so much jack_teams = [team[0].title() for team in sql_lib.execute_query( f"SELECT team FROM teams WHERE owner='Jack Francis' and season='{Config['season']}';")] jordan_teams = [team[0].title() for team in sql_lib.execute_query( f"SELECT team FROM teams WHERE owner='Jordan Holland' and season='{Config['season']}';")] nathan_teams = [team[0].title() for team in sql_lib.execute_query( f"SELECT team FROM teams WHERE owner='Nathan Lee' and season='{Config['season']}';")] patrick_teams = [team[0].title() for team in sql_lib.execute_query( f"SELECT team FROM teams WHERE owner='Patrick Cooper' and season='{Config['season']}';")] all_of_us = [jack_teams, jordan_teams, nathan_teams, patrick_teams] for big_team in all_of_us: for i, team in enumerate(big_team): # Title uppercases the first letter, so the e gets capitalized in 49ers if team == 'San Francisco 49Ers': big_team[i] = 'San Francisco 49ers' # Discrepancy to look at later. I had to change my team mapping to Washington for ESPN, I believe if team == 'Washington Football Team': big_team[i] = 'Washington' return jack_teams, jordan_teams, nathan_teams, patrick_teams
def pickle_draft_object(instance): """Save the Draft object to a pickle file.""" try: pickle_string = pickle.dumps(instance) if not sql_lib.execute_query("SELECT * FROM draft;"): operation = "INSERT INTO draft VALUES(%s)" else: operation = "UPDATE draft SET object=%s" # Use the connection directly here to use the %s syntax with sql_lib._get_sql_connection().cursor() as cur: cur.execute(operation, (pickle_string)) except Exception as err: logging.error(f"Error pickling Draft object to database: {err}")
def check_team_draft_status(team, season): """Check if a team has already been drafted this year.""" query = f"SELECT * FROM teams WHERE team='{team}' AND season='{season}';" return True if sql_lib.execute_query(query) else False
def draft_team(user, team, season, position): """Enter the draft selection in the database.""" _ = sql_lib.execute_query( f"INSERT INTO teams VALUES ('{user}', '{team}', '{season}', {position});", update=True)
def draft_active(): result = sql_lib.execute_query("select snake from draft;") if result: return result[0][0]