async def showdown(fn=None): parse_configs() apply_mods(config.pokemon_mode) original_pokedex = deepcopy(pokedex) original_move_json = deepcopy(all_move_json) driver = webdriver.Chrome() driver.get('https://play.pokemonshowdown.com/') login_browser(driver, config.username, config.password) ps_websocket_client = await PSWebsocketClient.create( config.username, config.password, config.websocket_uri) await ps_websocket_client.login() battles_run = 0 wins = 0 losses = 0 while True: team = load_team(config.team_name) if config.bot_mode == constants.CHALLENGE_USER: await ps_websocket_client.challenge_user(config.user_to_challenge, config.pokemon_mode, team) elif config.bot_mode == constants.ACCEPT_CHALLENGE: await ps_websocket_client.accept_challenge(config.pokemon_mode, team) elif config.bot_mode == constants.SEARCH_LADDER: await ps_websocket_client.search_for_match(config.pokemon_mode, team) else: raise ValueError("Invalid Bot Mode") winner = await pokemon_battle(ps_websocket_client, config.pokemon_mode) if winner == config.username: wins += 1 else: losses += 1 logger.info("W: {}\tL: {}".format(wins, losses)) check_dictionaries_are_unmodified(original_pokedex, original_move_json) if train_in_place == True: train() print('train completed') battles_run += 1 fn(battles_run) if battles_run >= config.run_count: # driver.close() break
async def showdown(): parse_configs() apply_mods(config.pokemon_mode) original_pokedex = deepcopy(pokedex) original_move_json = deepcopy(all_move_json) ps_websocket_client = await PSWebsocketClient.create( config.username, config.password, config.websocket_uri) await ps_websocket_client.login() time.sleep( 10) # prevent race conditions with server login and user challenge battles_run = 0 wins = 0 losses = 0 while True: team = load_team(config.team_name) if config.bot_mode == constants.CHALLENGE_USER: await ps_websocket_client.challenge_user(config.user_to_challenge, config.pokemon_mode, team) elif config.bot_mode == constants.ACCEPT_CHALLENGE: await ps_websocket_client.accept_challenge(config.pokemon_mode, team, config.room_name) elif config.bot_mode == constants.SEARCH_LADDER: await ps_websocket_client.search_for_match(config.pokemon_mode, team) else: raise ValueError("Invalid Bot Mode") winner = await pokemon_battle(ps_websocket_client, config) if winner == config.username: wins += 1 else: losses += 1 logger.info("W: {}\tL: {}".format(wins, losses)) check_dictionaries_are_unmodified(original_pokedex, original_move_json) battles_run += 1 if battles_run >= config.run_count: break
async def showdown(): env = Env() env.read_env() config.log_to_file = env.bool("LOG_TO_FILE", config.log_to_file) config.save_replay = env.bool("SAVE_REPLAY", config.save_replay) logger.setLevel(env("LOG_LEVEL", "DEBUG")) websocket_uri = env("WEBSOCKET_URI", "sim.smogon.com:8000") username = env("PS_USERNAME") password = env("PS_PASSWORD", "") bot_mode = env("BOT_MODE") team_name = env("TEAM_NAME", None) pokemon_mode = env("POKEMON_MODE", constants.DEFAULT_MODE) run_count = int(env("RUN_COUNT", 1)) apply_mods(pokemon_mode) original_pokedex = deepcopy(pokedex) original_move_json = deepcopy(all_move_json) if bot_mode not in constants.BOT_MODES: raise ValueError("{} is not a valid bot mode".format(bot_mode)) ps_websocket_client = await PSWebsocketClient.create( username, password, websocket_uri) await ps_websocket_client.login() team = load_team(team_name) battles_run = 0 wins = 0 losses = 0 while True: if bot_mode == constants.CHALLENGE_USER: user_to_challenge = env("USER_TO_CHALLENGE") await ps_websocket_client.challenge_user(user_to_challenge, pokemon_mode, team) elif bot_mode == constants.ACCEPT_CHALLENGE: await ps_websocket_client.accept_challenge(pokemon_mode, team) elif bot_mode == constants.SEARCH_LADDER: await ps_websocket_client.search_for_match(pokemon_mode, team) else: raise ValueError("Invalid Bot Mode") is_random_battle = "random" in pokemon_mode winner = await pokemon_battle(ps_websocket_client, is_random_battle) if winner == username: wins += 1 else: losses += 1 logger.info("\nW: {}\nL: {}\n".format(wins, losses)) if original_move_json != all_move_json: logger.critical( "Move JSON changed!\nDumping modified version to `modified_moves.json`" ) with open("modified_moves.json", 'w') as f: json.dump(all_move_json, f, indent=4) exit(1) else: logger.debug("Move JSON unmodified!") if original_pokedex != pokedex: logger.critical( "Pokedex JSON changed!\nDumping modified version to `modified_pokedex.json`" ) with open("modified_pokedex.json", 'w') as f: json.dump(pokedex, f, indent=4) exit(1) else: logger.debug("Pokedex JSON unmodified!") battles_run += 1 if battles_run >= run_count: break
async def showdown(accept, agent=None): """ Will run through number of battles specified in config accept: boolean. If accept is true will create an accept_bot agent: specify str agent name (i.e rand_bot) or a DQNAgent. If DQNAgent will choose nn_bot """ if accept: conf = agent.config # conf = create_accept_bot(agent) # accept gary else: await asyncio.sleep( 1) # ensure that challenge bot has time to be created first if agent == "True" or agent == "rand_bot" or agent == "safest": if agent == "True": agent = ENEMY_BOT conf = create_challenge_bot(True, agent) # cbninjask5uber else: if agent == "False": agent = ENEMY_BOT conf = create_challenge_bot(False, agent) # cbninjask5uber config = conf apply_mods(config.pokemon_mode) original_pokedex = deepcopy(pokedex) original_move_json = deepcopy(all_move_json) ps_websocket_client = await PSWebsocketClient.create( config.username, config.password, config.websocket_uri) await ps_websocket_client.login() wins = 0 losses = 0 reward = 0 for _ in range(num_games): if config.team_name != "random": team_name = config.team_name else: team_name = "gen8/ou/" + np.random.choice(POSSIBLE_TEAMS) # print(team_name) team = load_team(team_name) if config.bot_mode == constants.CHALLENGE_USER: await ps_websocket_client.challenge_user(config.user_to_challenge, config.pokemon_mode, team) elif config.bot_mode == constants.ACCEPT_CHALLENGE: await ps_websocket_client.accept_challenge(config.pokemon_mode, team) elif config.bot_mode == constants.SEARCH_LADDER: await ps_websocket_client.search_for_match(config.pokemon_mode, team) else: raise ValueError("Invalid Bot Mode") if type(agent) == str: winner = await pokemon_battle(ps_websocket_client, config.pokemon_mode, config, agent=None) else: winner = await pokemon_battle(ps_websocket_client, config.pokemon_mode, config, agent) if winner == config.username: finalReward = 1 wins += 1 else: finalReward = -.5 losses += 1 if type(agent) != str: # logger.critical("W: {}\tL: {}".format(wins, losses)) reward += agent.previous_reward + finalReward # logger.critical("End Score: {}".format(reward)) # winPercList.append(reward) await agent.step(agent.previous_state, agent.previous_action, finalReward, agent.previous_state, True) else: logger.debug("W: {}\tL: {}".format(wins, losses)) if type(agent) != str: reward = reward / num_games logger.critical("W: {}\tL: {}".format(wins, losses)) logger.critical("End Score: {}".format(reward)) check_dictionaries_are_unmodified(original_pokedex, original_move_json) return reward, wins #winner == config.username