diff --git a/constants/exceptions.py b/constants/exceptions.py index b586ed1..32210fb 100644 --- a/constants/exceptions.py +++ b/constants/exceptions.py @@ -101,4 +101,7 @@ class missingReportInfoException(Exception): pass class invalidUserException(Exception): + pass + +class wrongChannelException(Exception): pass \ No newline at end of file diff --git a/constants/fokabotCommands.py b/constants/fokabotCommands.py index aab4d94..6a97b2b 100644 --- a/constants/fokabotCommands.py +++ b/constants/fokabotCommands.py @@ -758,6 +758,17 @@ def report(fro, chan, message): return False def multiplayer(fro, chan, message): + def getMatchIDFromChannel(chan): + if not chan.lower().startswith("#multi_"): + raise exceptions.wrongChannelException() + parts = chan.lower().split("_") + if len(parts) < 2 or not parts[1].isdigit(): + raise exceptions.wrongChannelException() + matchID = int(parts[1]) + if matchID not in glob.matches.matches: + raise exceptions.matchNotFoundException() + return matchID + def mpMake(): if len(message) < 2: raise exceptions.invalidArgumentsException("Wrong syntax: !mp make ") @@ -779,11 +790,23 @@ def multiplayer(fro, chan, message): glob.matches.disposeMatch(myToken.matchID) return "Multiplayer match #{} disposed successfully".format(myToken.matchID) + def mpLock(): + matchID = getMatchIDFromChannel(chan) + glob.matches.matches[matchID].isLocked = True + return "This match has been locked" + + def mpUnlock(): + matchID = getMatchIDFromChannel(chan) + glob.matches.matches[matchID].isLocked = False + return "This match has been unlocked" + try: subcommands = { "make": mpMake, "clear": mpClose, - "join": mpJoin + "join": mpJoin, + "lock": mpLock, + "unlock": mpUnlock } requestedSubcommand = message[0].lower().strip() if requestedSubcommand not in subcommands: @@ -791,6 +814,10 @@ def multiplayer(fro, chan, message): return subcommands[requestedSubcommand]() except exceptions.invalidArgumentsException as e: return str(e) + except exceptions.wrongChannelException: + return "This command only works in multiplayer chat channels" + except exceptions.matchNotFoundException: + return "Match not found" except: raise diff --git a/objects/match.py b/objects/match.py index 7f07471..73285d3 100644 --- a/objects/match.py +++ b/objects/match.py @@ -56,6 +56,7 @@ class match: self.seed = 0 self.matchDataCache = bytes() self.isTourney = isTourney + self.isLocked = False # if True, users can't change slots/teams. Used in tourney matches # Create all slots and reset them self.slots = [] @@ -487,6 +488,10 @@ class match: :param newSlotID: slot id of new slot :return: """ + # Make sure the match is not locked + if self.isLocked: + return + # Make sure the user is in room oldSlotID = self.getUserSlotID(userID) if oldSlotID is None: @@ -637,6 +642,10 @@ class match: :param userID: id of user :return: """ + # Make sure the match is not locked + if self.isLocked: + return + # Make sure the user is in room slotID = self.getUserSlotID(userID) if slotID is None: