forked from pmariglia/foul-play
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
110 lines (84 loc) · 3.75 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import asyncio
import json
from copy import deepcopy
from environs import Env
import constants
import config
from config import init_logging
import logging
from teams import load_team
from showdown.run_battle import pokemon_battle
from showdown.websocket_client import PSWebsocketClient
from data import all_move_json
from data import pokedex
from data.mods.apply_mods import apply_mods
logger = logging.getLogger(__name__)
def parse_configs():
env = Env()
env.read_env()
config.battle_bot_module = env("BATTLE_BOT", 'safest')
config.save_replay = env.bool("SAVE_REPLAY", config.save_replay)
config.use_relative_weights = env.bool("USE_RELATIVE_WEIGHTS", config.use_relative_weights)
config.gambit_exe_path = env("GAMBIT_PATH", config.gambit_exe_path)
config.search_depth = int(env("MAX_SEARCH_DEPTH", config.search_depth))
config.greeting_message = env("GREETING_MESSAGE", config.greeting_message)
config.battle_ending_message = env("BATTLE_OVER_MESSAGE", config.battle_ending_message)
config.websocket_uri = env("WEBSOCKET_URI", "sim.smogon.com:8000")
config.username = env("PS_USERNAME")
config.password = env("PS_PASSWORD", "")
config.bot_mode = env("BOT_MODE")
config.team_name = env("TEAM_NAME", None)
config.pokemon_mode = env("POKEMON_MODE", constants.DEFAULT_MODE)
config.run_count = int(env("RUN_COUNT", 1))
if config.bot_mode == constants.CHALLENGE_USER:
config.user_to_challenge = env("USER_TO_CHALLENGE")
init_logging(env("LOG_LEVEL", "DEBUG"))
def check_dictionaries_are_unmodified(original_pokedex, original_move_json):
# The bot should not modify the data dictionaries
# This is a "just-in-case" check to make sure and will stop the bot if it mutates either of them
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!")
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()
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)
battles_run += 1
if battles_run >= config.run_count:
break
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(showdown())