async def battle_tag(message: str, usage: Usage): """ Main in fuction. Filter every message sent by server and launch corresponding function. :param websocket: Websocket stream. :param message: Message received from server. Format : room|message|... :param usage: Bot usage : standby, challenging owner, searching random battles. """ lines = message.splitlines() battle = check_battle(BATTLES, lines[0].split('|')[0].split('>')[1]) sender = Sender() for line in lines[1:]: current = line.split('|') if len(current) < 2: continue _, command, *other = current try: match command: case 'init': # Creation de la bataille battle = Battle(lines[0].split("|")[0].split(">")[1]) BATTLES.append(battle) await sender.send(battle.battletag, 'Hi') await sender.send(battle.battletag, '/timer on') case 'player' if other[1] == USERNAME: # Récupérer l'id joueur du bot battle.player_id = other[0] battle.turn += int(other[0].split('p')[1]) - 1 case 'request' if other[0] != '': # Maj team bot if len(other[0]) == 1: await battle.req_loader(other[1].split('\n')[1]) else: await battle.req_loader(other[0]) case 'teampreview': # Selection d'ordre des pokemons await battle.make_team_order() case 'turn': # Phase de reflexion await battle.make_action() case 'callback' if other[0] == 'trapped': await battle.make_move(make_best_move(battle)) case 'poke' if battle.player_id not in other[0]: name, variant, level = re.match(r'(.*?)\|(.*?), (?:L(\d+), )?.*', '|'.join(other[0:2])).groups() battle.update_enemy(name, '100/100', variant, level if level else 100) case 'win': await sender.send(battle.battletag, 'wp') await sender.leaving(battle.battletag) BATTLES.remove(battle) if usage == Usage.SEARCH: log_battle_result(USERNAME in other[0]) case 'error': raise ValueError(*other) case _: # Send to battlelog parser. battlelog_parsing(battle, command, other) except Exception as exception: await sender.send(battle.battletag, 'Sorry, I crashed.') await sender.forfeit(battle.battletag) time.sleep(2) raise exception
def major_actions(battle: Battle, command: str, split_line: list[str]): match command: case "move": pass case "switch" if battle.player_id not in split_line[0]: # Enemy pokemon has switched in regex = re.compile(r'p\da: (.*?)\|(.*?), (?:L(\d+), )?.*') name, variant, level = regex.match('|'.join(split_line[0:2])).groups() battle.update_enemy(name, split_line[2], variant, level if level else '100') case "swap": pass case "detailschange": pass case "cant": pass case "faint": pass case _: pass
def major_actions(battle: Battle, split_line): if split_line[0] == "move": pass elif split_line[0] == "switch": if battle.player_id not in split_line[1]: battle.update_enemy(split_line[2].split(',')[0], split_line[2].split(',')[1].split('L')[1], split_line[3]) elif split_line[0] == "swap": pass elif split_line[0] == "detailschange": pass elif split_line[0] == "cant": pass elif split_line[0] == "faint": pass elif split_line[0] == "poke": if battle.player_id not in split_line[1]: pkm = split_line[2].split(', ') battle.update_enemy( pkm[0], pkm[1][1:] if len(pkm) > 1 and 'L' in pkm[1] else '100', 100) else: pass
def minor_actions(battle: Battle, split_line): if split_line[0] == "-fail": pass elif split_line[0] == "-damage": pass elif split_line[0] == "-heal": pass elif split_line[0] == "-status": if battle.player_id in split_line[1]: battle.update_status(battle.bot_team.active(), split_line[2]) else: battle.update_status(battle.enemy_team.active(), split_line[2]) elif split_line[0] == "-curestatus": if battle.player_id in split_line[1]: battle.update_status(battle.bot_team.active()) else: battle.update_status(battle.enemy_team.active()) elif split_line[0] == "-cureteam": pass elif split_line[0] == "-boost": if battle.player_id in split_line[1]: battle.set_buff(battle.bot_team.active(), split_line[2], int(split_line[3])) else: battle.set_buff(battle.enemy_team.active(), split_line[2], int(split_line[3])) elif split_line[0] == "-unboost": if battle.player_id in split_line[1]: battle.set_buff(battle.bot_team.active(), split_line[2], -int(split_line[3])) else: battle.set_buff(battle.enemy_team.active(), split_line[2], -int(split_line[3])) elif split_line[0] == "-weather": pass elif split_line[0] == "-fieldstart": pass elif split_line[0] == "-fieldend": pass elif split_line[0] == "-sidestart": if "Reflect" in split_line[2] or "Light Screen" in split_line[2]: battle.screens[split_line[2].split(":")[1].lower().replace( " ", "")] = True print("** " + battle.screens) elif split_line[0] == "-sideend": if "Reflect" in split_line[2] or "Light Screen" in split_line[2]: battle.screens[split_line[2].split(":")[1].lower().replace( " ", "")] = False print("** " + battle.screens) elif split_line[0] == "-crit": pass elif split_line[0] == "-supereffective": pass elif split_line[0] == "-resisted": pass elif split_line[0] == "-immune": pass elif split_line[0] == "-item": if battle.player_id in split_line[1]: battle.bot_team.active().item = split_line[2].lower().replace( " ", "") else: battle.enemy_team.active().item = split_line[2].lower().replace( " ", "") elif split_line[0] == "-enditem": if battle.player_id not in split_line[1]: battle.bot_team.active().item = None else: battle.enemy_team.active().item = None elif split_line[0] == "-ability": pass elif split_line[0] == "-endability": pass elif split_line[0] == "-transform": pass elif split_line[0] == "-mega": pass elif split_line[0] == "-activate": pass elif split_line[0] == "-hint": pass elif split_line[0] == "-center": pass elif split_line[0] == "-message": pass else: pass
async def battle_tag(websocket, message, usage): """ Main in fuction. Filter every message sent by server and launch corresponding function. :param websocket: Websocket stream. :param message: Message received from server. Format : room|message1|message2. :param usage: 0: Only recieving. 1 Challenging Synedh. 2 searching random battles. """ global battles lines = message.splitlines() battle = check_battle(battles, lines[0].split("|")[0].split(">")[1]) for line in lines[1:]: try: current = line.split('|') if current[1] == "init": # Creation de la bataille battle = Battle(lines[0].split("|")[0].split(">")[1]) battles.append(battle) await senders.sendmessage(websocket, battle.battletag, "Hi") await senders.sendmessage(websocket, battle.battletag, "/timer on") elif current[1] == "player" and len( current) > 3 and current[3].lower() == "suchtestbot": # Récupérer l'id joueur du bot battle.player_id = current[2] battle.turn += int(current[2].split('p')[1]) - 1 elif current[1] == "request": if current[2] == '': continue # Maj team bot if len(current[2]) == 1: try: await battle.req_loader(current[3].split('\n')[1], websocket) except KeyError as e: print(e) print(current[3]) else: await battle.req_loader(current[2], websocket) elif current[1] == "teampreview": # Selection d'ordre des pokemons await battle.make_team_order(websocket) elif current[1] == "turn": # Phase de reflexion await battle.make_action(websocket) elif current[1] == "callback" and current[2] == "trapped": await battle.make_move(websocket) elif current[1] == "win": await senders.sendmessage(websocket, battle.battletag, "wp") await senders.leaving(websocket, battle.battletag) battles.remove(battle) if usage == 2: with open("log.txt", "r+") as file: line = file.read().split('/') file.seek(0) if "suchtestbot" in current[2].lower(): file.write( str(int(line[0]) + 1) + "/" + line[1] + "/" + str(int(line[2]) + 1)) else: file.write(line[0] + "/" + str(int(line[1]) + 1) + "/" + str(int(line[2]) + 1)) elif current[1] == "c": # This is a message pass else: # Send to battlelog parser. battlelog_parsing(battle, current[1:]) except IndexError: pass
def minor_actions(battle: Battle, command: str, split_line: list[str]): match command: case "-fail": pass case "-damage" if battle.player_id not in split_line[0]: name = re.match(r'p\da: (.*)', split_line[0]).group(1) battle.update_enemy(name, split_line[1]) case "-heal": pass case "-status": battle.update_status(battle.get_team(split_line[0]).active(), split_line[1]) case "-curestatus": battle.update_status(battle.get_team(split_line[0]).active()) case "-cureteam": pass case "-boost": battle.set_buff(battle.get_team(split_line[0]).active(), split_line[1], int(split_line[2])) case "-unboost": battle.set_buff(battle.get_team(split_line[0]).active(), split_line[1], - int(split_line[2])) case "-weather": battle.weather = split_line[0] case "-fieldstart": battle.fields.append(split_line[0]) print("** " + battle.fields) case "-fieldend": battle.fields.remove(split_line[0]) print("** " + battle.fields) case "-sidestart": battle.side_condition.append(split_line[1]) print("** " + battle.side_condition) case "-sideend": battle.side_condition.remove(split_line[1]) print("** " + battle.side_condition) case "-crit": pass case "-supereffective": pass case "-resisted": pass case "-immune": pass case "-item": battle.get_team(split_line[0]).active().item = split_line[1].lower().replace(" ", "") case "-enditem": battle.get_team(split_line[0]).active().item = None case "-ability": pass case "-endability": pass case "-transform": pass case "-mega": pass case "-activate": pass case "-hint": pass case "-center": pass case "-message": pass case _: pass