diff --git a/games.py b/games.py index a077faf..f2b0368 100644 --- a/games.py +++ b/games.py @@ -107,20 +107,45 @@ class team(object): self.score = 0 self.slogan = None + def find_player(self, name): + for index in range(0,len(self.lineup)): + if self.lineup[index].name == name: + return (self.lineup[index], index, self.lineup) + for index in range(0,len(self.rotation)): + if self.rotation[index].name == name: + return (self.rotation[index], index, self.rotation) + else: + return (None, None, None) + def swap_player(self, name): - if len(self.lineup) > 1: - for index in range(0,len(self.lineup)): - if self.lineup[index].name == name: - if self.add_pitcher(self.lineup[index]): - self.lineup.pop(index) - return True - if len(self.rotation) > 1: - for index in range(0,len(self.rotation)): - if self.rotation[index].name == name: - if self.add_lineup(self.rotation[index])[0]: - self.rotation.pop(index) - return True + this_player, index, roster = self.find_player(name) + if this_player is not None and len(roster) > 1: + if roster == self.lineup: + if self.add_pitcher(this_player): + roster.pop(index) + return True + else: + if self.add_lineup(this_player)[0]: + self.rotation.pop(index) + return True return False + + def delete_player(self, name): + this_player, index, roster = self.find_player(name) + if this_player is not None and len(roster) > 1: + roster.pop(index) + return True + else: + return False + + def slide_player(self, name, new_spot): + this_player, index, roster = self.find_player(name) + if this_player is not None and new_spot < len(roster): + roster.pop(index) + roster.insert(new_spot-1, this_player) + return True + else: + return False def add_lineup(self, new_player): if len(self.lineup) < 20: @@ -735,7 +760,7 @@ def search_team(search_term): return None teams.append(team_json) - return teams + return teams def base_string(base): if base == 1: diff --git a/the_prestige.py b/the_prestige.py index 239595b..cfce42d 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -295,8 +295,8 @@ class CreditCommand(Command): await msg.channel.send("Our avatar was graciously provided to us, with permission, by @HetreaSky on Twitter.") class SwapPlayerCommand(Command): - name = "swap" - template = """m;swap + name = "swapsection" + template = """m;swapsection [team name] [player name]""" description = "Swaps a player from lineup to rotation, or from rotation to lineup. Requires team ownership." @@ -308,16 +308,98 @@ class SwapPlayerCommand(Command): if team is None: await msg.channel.send("Can't find that team, boss. Typo?") return - elif owner_id != msg.author.id or msg.author.id not in config()["owners"]: + elif owner_id != msg.author.id and msg.author.id not in config()["owners"]: await msg.channel.send("You're not authorized to mess with this team. Sorry, boss.") return elif not team.swap_player(player_name): - await msg.channel.send("Either we can't find that player, or they're your last member of that side of the roster. Can't field an empty lineup, chief.") + await msg.channel.send("Either we can't find that player, you've got no space on the other side, or they're your last member of that side of the roster. Can't field an empty lineup, and we *do* have rules, chief.") return else: await msg.channel.send(embed=build_team_embed(team)) games.update_team(team) - await msg.channel.send("Paperwork signed, stamped, and copied.") + await msg.channel.send("Paperwork signed, stamped, copied, and faxed up to the goddess. Xie's pretty quick with this stuff.") + +class MovePlayerCommand(Command): + name = "move" + template = """m;move + [team name] + [player name] + [new lineup/rotation position number] (indexed with 1 being the top)""" + description = "Moves a player in your lineup or rotation. Requires team ownership." + + async def execute(self, msg, command): + team_name = command.split("\n")[1].strip() + player_name = command.split("\n")[2].strip() + team, owner_id = games.get_team_and_owner(team_name) + try: + new_pos = int(command.split("\n")[3].strip()) + except ValueError: + await msg.channel.send("Hey, quit being cheeky. We're just trying to help. Third line has to be a natural number, boss.") + return + if owner_id != msg.author.id and msg.author.id not in config()["owners"]: + await msg.channel.send("You're not authorized to mess with this team. Sorry, boss.") + return + elif not team.slide_player(player_name, new_pos): + await msg.channel.send("You either gave us a number that was bigger than your current roster, or we couldn't find the player on the team. Try again.") + return + else: + await msg.channel.send(embed=build_team_embed(team)) + games.update_team(team) + await msg.channel.send("Paperwork signed, stamped, copied, and faxed up to the goddess. Xie's pretty quick with this stuff.") + +class AddPlayerCommand(Command): + name = "addplayer" + template = """m;addplayer pitcher (or m;addplayer batter) + [team name] + [player name]""" + description = "Recruits a new player to your team, as either a pitcher or a batter. Requires team ownership." + + async def execute(self, msg, command): + team_name = command.split("\n")[1].strip() + player_name = command.split("\n")[2].strip() + team, owner_id = games.get_team_and_owner(team_name) + if owner_id != msg.author.id and msg.author.id not in config()["owners"]: + await msg.channel.send("You're not authorized to mess with this team. Sorry, boss.") + return + + new_player = games.player(ono.get_stats(player_name)) + + if "batter" in command.split("\n")[0]: + if not team.add_lineup(new_player)[0]: + await msg.channel.send("Too many batters 🎶") + return + elif "pitcher" in command.split("\n")[0]: + if not team.add_pitcher(new_player): + await msg.channel.send("8 pitchers is quite enough, we think.") + return + + await msg.channel.send(embed=build_team_embed(team)) + games.update_team(team) + await msg.channel.send("Paperwork signed, stamped, copied, and faxed up to the goddess. Xie's pretty quick with this stuff.") + +class DeletePlayerCommand(Command): + name = "removeplayer" + template = """m;removeplayer + [team name] + [player name]""" + + async def execute(self, msg, command): + team_name = command.split("\n")[1].strip() + player_name = command.split("\n")[2].strip() + team, owner_id = games.get_team_and_owner(team_name) + if owner_id != msg.author.id and msg.author.id not in config()["owners"]: + await msg.channel.send("You're not authorized to mess with this team. Sorry, boss.") + return + + if not team.delete_player(player_name): + await msg.channel.send("We've got bad news: that player isn't on your team. The good news is that... that player isn't on your team?") + return + + else: + await msg.channel.send(embed=build_team_embed(team)) + games.update_team(team) + await msg.channel.send("Paperwork signed, stamped, copied, and faxed up to the goddess. Xie's pretty quick with this stuff.") + class HelpCommand(Command): name = "help" @@ -386,6 +468,9 @@ commands = [ SaveTeamCommand(), ImportCommand(), SwapPlayerCommand(), + MovePlayerCommand(), + AddPlayerCommand(), + DeletePlayerCommand(), DeleteTeamCommand(), ShowTeamCommand(), ShowAllTeamsCommand(),