From 51215cba5917f0e607036d3de94a8cbbd9133ca2 Mon Sep 17 00:00:00 2001 From: Sakimori Date: Mon, 22 Feb 2021 14:27:04 -0500 Subject: [PATCH] designed and implemented the One Big League --- database.py | 113 ++++++++++++++++++++++++++++++++++++++++++++ games.py | 10 +++- main_controller.py | 2 +- the-prestige.pyproj | 2 + the_prestige.py | 57 ++++++++++++++++++++++ 5 files changed, 182 insertions(+), 2 deletions(-) diff --git a/database.py b/database.py index 6e89b73..950e7fc 100644 --- a/database.py +++ b/database.py @@ -1,6 +1,7 @@ #handles the database interactions import os, json, datetime, re import sqlite3 as sql +from random import sample data_dir = "data" @@ -71,6 +72,14 @@ def initialcheck(): owner_id integer ); """ + one_big_league_check_string = """ CREATE TABLE IF NOT EXISTS one_big_league ( + counter integer PRIMARY KEY, + team_name text NOT NULL, + teams_beaten_list text, + current_opponent_pool text, + obl_points int DEFAULT 0 + );""" + if conn is not None: c = conn.cursor() c.execute(soulscream_table_check_string) @@ -78,6 +87,7 @@ def initialcheck(): c.execute(player_table_check_string) c.execute(player_stats_table_check_string) c.execute(teams_table_check_string) + c.execute(one_big_league_check_string) conn.commit() conn.close() @@ -297,6 +307,26 @@ def get_all_teams(): conn.close() return None +def get_all_team_names(): + conn = create_connection() + if conn is not None: + c = conn.cursor() + c.execute("SELECT name FROM teams") + team_names = c.fetchall() + team_names_out = [name for (name,) in team_names] + conn.close() + return team_names_out + conn.close() + return None + +def get_filtered_teams(filter_list): + teams_list = get_all_team_names() + out_list = [] + for team in teams_list: + if team not in filter_list: + out_list.append(team) + return out_list + def search_teams(search_string): conn = create_connection() if conn is not None: @@ -327,3 +357,86 @@ def add_stats(player_game_stats_list): c.execute(f"UPDATE stats SET {stat} = ? WHERE name=?",(player_stats_dic[stat],name)) conn.commit() conn.close() + + +def add_team_obl(team): + conn = create_connection() + if conn is not None: + c=conn.cursor() + opponents = sample(get_filtered_teams([team.name]), 5) + c.execute("INSERT INTO one_big_league(team_name, current_opponent_pool) VALUES (?, ?)", (team.name, list_to_newline_string(opponents))) + + conn.commit() + conn.close() + +def save_obl_results(winning_team, losing_team): + conn = create_connection() + if conn is not None: + c=conn.cursor() + c.execute("SELECT teams_beaten_list, current_opponent_pool, obl_points FROM one_big_league WHERE team_name = ?", (winning_team.name,)) + try: + beaten_string, opponents_string, obl_points = c.fetchone() + except TypeError: #add team to OBL + add_team_obl(winning_team) + add_team_obl(losing_team) + else: + beaten_teams = newline_string_to_list(beaten_string) + opponent_teams = newline_string_to_list(opponents_string) + if losing_team.name in opponent_teams: + beaten_teams.append(losing_team.name) + opponent_teams = sample(get_filtered_teams([winning_team.name]), 5) + obl_points += 1 + + c.execute("UPDATE one_big_league SET teams_beaten_list = ?, current_opponent_pool = ?, obl_points = ? WHERE team_name = ?", (list_to_newline_string(beaten_teams), list_to_newline_string(opponent_teams), obl_points, winning_team.name)) + + conn.commit() + conn.close() + return + +def get_obl_stats(team): + conn = create_connection() + if conn is not None: + c=conn.cursor() + opponents_string = None + while opponents_string is None: + c.execute("SELECT teams_beaten_list, current_opponent_pool FROM one_big_league WHERE team_name = ?", (team.name,)) + try: + beaten_string, opponents_string = c.fetchone() + except TypeError: #add team to OBL + add_team_obl(team) + + beaten_teams = newline_string_to_list(beaten_string) + opponent_teams = opponents_string + obl_points = len(beaten_teams) + + teams_list = [name for name, points in obl_leaderboards()] + rank = teams_list.index(team.name) + 1 + + return (obl_points, opponent_teams, rank) + conn.close() + return (None, None) + +def obl_leaderboards(): + conn = create_connection() + if conn is not None: + c=conn.cursor() + c.execute("SELECT team_name, obl_points FROM one_big_league ORDER BY obl_points DESC") + teams_list = c.fetchall() + + return teams_list #element (team_name, obl_points) + conn.close() + return False + +def list_to_newline_string(list): + string = "" + for element in list: + if string != "": + string += "\n" + string += element + return string + +def newline_string_to_list(string): + if string is not None and string != "": + return string.split("\n") + else: + return [] \ No newline at end of file diff --git a/games.py b/games.py index 30df172..283edf3 100644 --- a/games.py +++ b/games.py @@ -626,7 +626,7 @@ class game(object): self.inning += 1 if self.inning > self.max_innings and self.teams["home"].score != self.teams["away"].score: #game over self.over = True - #do the One Big League stuff here + db.save_obl_results(self.teams["home"] if self.teams["home"].score > self.teams["away"].score else self.teams["away"], self.teams["home"] if self.teams["home"].score < self.teams["away"].score else self.teams["away"]) def end_of_game_report(self): @@ -791,3 +791,11 @@ def search_team(search_term): teams.append(team_json) return teams + +def get_filtered_teams(teams_to_remove): + teams = [] + for team_pickle in db.get_all_teams(): + this_team = jsonpickle.decode(team_pickle[0], keys=True, classes=team) + if this_team.name not in teams_to_remove: + teams.append(this_team) + return teams \ No newline at end of file diff --git a/main_controller.py b/main_controller.py index ca693a2..7f5c17f 100644 --- a/main_controller.py +++ b/main_controller.py @@ -236,4 +236,4 @@ def update_loop(): state["update_pause"] -= 1 socketio.emit("states_update", game_states) - time.sleep(8) + time.sleep(1) diff --git a/the-prestige.pyproj b/the-prestige.pyproj index 090ed69..7ad6158 100644 --- a/the-prestige.pyproj +++ b/the-prestige.pyproj @@ -29,6 +29,7 @@ Code + Code @@ -46,6 +47,7 @@ + diff --git a/the_prestige.py b/the_prestige.py index 4787f60..6a497b3 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -1233,6 +1233,60 @@ class LeagueRenameCommand(Command): else: await msg.channel.send("We can't find that league.") +class OBLExplainCommand(Command): + name = "oblhelp" + template = "m;oblhelp" + description = "Explains the One Big League!" + + async def execute(self, msg, command): + await msg.channel.send("""The One Big League, or OBL, is an asynchronous league that includes every team in the simsim's database. To participate, just use the m;oblteam command with your team of choice. No signup is required! This will give a list of five opponents; playing against one of them and winning nets you a point, and will refresh the list with five new opponents. Each meta-season will last for a few weeks, after which the leaderboards are reset to start the race again! + +Look out for the people wrestling emoji, which indicates the potential for a :people_wrestling:Wrassle Match:people_wrestling:, where both teams are on each others' lists and both have the opportunity to score a point. Team rankings and points can also be viewed in the oblteam command, and the overall OBL leaderboard can be checked with the m;oblstandings command. Best of luck!! +""") + +class OBLLeaderboardCommand(Command): + name = "oblstandings" + template = "m;oblstandings" + description = "Displays the 15 teams with the most OBL points in this meta-season." + + async def execute(self, msg, command): + leaders_list = db.obl_leaderboards()[:15] + leaders = {} + rank = 1 + for team, points in leaders_list: + leaders[team] = {"rank" : rank, "points" : points} + rank += 1 + + embed = discord.Embed(color=discord.Color.red(), title="The One Big League") + for team in leaders.keys(): + embed.add_field(name=f"{leaders[team]['rank']}. {team}", value=f"{leaders[team]['points']} points" , inline = False) + await msg.channel.send(embed=embed) + +class OBLTeamCommand(Command): + name = "oblteam" + template = "m;oblteam [team name]" + description = "Displays a team's rank, current OBL points, and current opponent selection." + + async def execute(self, msg, command): + team = get_team_fuzzy_search(command.strip()) + if team is None: + await msg.channel.send("Sorry boss, we can't find that team.") + return + + points, opponents_string, rank = db.get_obl_stats(team) + opponents_list = db.newline_string_to_list(opponents_string) + for index in range(0, len(opponents_list)): + oppteam = get_team_fuzzy_search(opponents_list[index]) + opplist = db.get_obl_stats(oppteam)[1] + if team.name in opplist: + opponents_list[index] = opponents_list[index] + " 🤼" + + embed = discord.Embed(color=discord.Color.red(), title=f"{team.name} in the One Big League") + embed.add_field(name="OBL Points", value=points) + embed.add_field(name="Rank", value=rank) + embed.add_field(name="Opponent Pool", value=opponents_string, inline=False) + await msg.channel.send(embed=embed) + commands = [ IntroduceCommand(), @@ -1256,6 +1310,9 @@ commands = [ StartGameCommand(), StartRandomGameCommand(), StartTournamentCommand(), + OBLExplainCommand(), + OBLTeamCommand(), + OBLLeaderboardCommand(), LeagueClaimCommand(), LeagueAddOwnersCommand(), StartLeagueCommand(),