diff --git a/league_storage.py b/league_storage.py index 1307e4e..0dcee16 100644 --- a/league_storage.py +++ b/league_storage.py @@ -91,6 +91,9 @@ def state(league_name): return json.load(state_file) def init_league_db(league): + if os.path.exists(os.path.join(data_dir, league_dir, league.name, f"{league.name}.db")): + os.remove(os.path.join(data_dir, league_dir, league.name, f"{league.name}.db")) + conn = create_connection(league.name) player_stats_table_check_string = """ CREATE TABLE IF NOT EXISTS stats ( diff --git a/leagues.py b/leagues.py index 2f87736..f4dd135 100644 --- a/leagues.py +++ b/leagues.py @@ -89,11 +89,13 @@ class league_structure(object): tournaments.append(tourney) return tournaments - def find_team(self, team_name): + def find_team(self, team_search): for subleague in iter(self.league.keys()): for division in iter(self.league[subleague].keys()): - if team_name in self.league[subleague][division]: - return (subleague, division) + for team in self.league[subleague][division]: + if team.name == team_search.name: + return (subleague, division) + return (None, None) def teams_in_league(self): teams = [] @@ -548,6 +550,13 @@ def save_league(this_league): json.dump(league_json_string, league_file, indent=4) league_db.save_league(this_league) +def save_league_as_new(this_league): + league_db.init_league_db(this_league) + with open(os.path.join(data_dir, league_dir, this_league.name, f"{this_league.name}.league"), "w") as league_file: + league_json_string = jsonpickle.encode(this_league.league, keys=True) + json.dump(league_json_string, league_file, indent=4) + league_db.save_league(this_league) + def load_league_file(league_name): if league_db.league_exists(league_name): state = league_db.state(league_name) diff --git a/the_prestige.py b/the_prestige.py index d5071a9..af781ae 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -1123,9 +1123,43 @@ class LeagueForceStopCommand(Command): await msg.channel.send("League halted, boss. We hope you did that on purpose.") return await msg.channel.send("That league either doesn't exist or isn't in the active list. So, huzzah?") - - +class LeagueSwapTeamCommand(Command): + name = "leagueswapteam" + template = "m;leagueswapteam [league name]\n[team to remove]\n[team to add]" + description = "Adds a team to a league, removing the old one in the process. Can only be executed by a league owner, and only before the start of a new season." + + async def execute(self, msg, command): + league_name = command.split("\n")[0].strip() + if league_exists(league_name): + league = leagues.load_league_file(league_name) + if league.day != 1: + await msg.channel.send("That league hasn't finished its current season yet, chief. Either reset it, or be patient.") + return + if (league.owner is not None and msg.author.id in league.owner) or (league.owner is not None and msg.author.id in config()["owners"]): + try: + team_del = get_team_fuzzy_search(command.split("\n")[1].strip()) + team_add = get_team_fuzzy_search(command.split("\n")[2].strip()) + except IndexError: + await msg.channel.send("Three lines, boss. Make sure you give us the team to remove, then the team to add.") + return + + if team_del is None or team_add is None: + await msg.channel.send("We couldn't find one or both of those teams, boss. Try again.") + return + subleague, division = league.find_team(team_del) + if subleague is None or division is None: + await msg.channel.send("That first team isn't in that league, chief. So, that's good, right?") + return + for index in range(0, len(league.league[subleague][division])): + if league.league[subleague][division][index].name == team_del.name: + league.league[subleague][division].pop(index) + league.league[subleague][division].append(team_add) + league.schedule = {} + league.generate_schedule() + leagues.save_league_as_new(league) + await msg.channel.send(embed=league.standings_embed()) + await msg.channel.send("Paperwork signed, stamped, copied, and faxed up to the goddess. Xie's pretty quick with this stuff.") commands = [ IntroduceCommand(), @@ -1160,6 +1194,7 @@ commands = [ LeagueScheduleCommand(), LeagueTeamScheduleCommand(), LeagueRegenerateScheduleCommand(), + LeagueSwapTeamCommand(), LeagueForceStopCommand(), CreditCommand(), RomanCommand(),