-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
150 lines (115 loc) · 4.39 KB
/
Copy pathClient.py
File metadata and controls
150 lines (115 loc) · 4.39 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import webbrowser
import websocket
import ssl
import time
import _thread
import ujson as json
import random
from enum import Enum
import requests
from Battle import Battle
from RAIchu_Enums import MoveType, AIType, ChallengeStatus
from Battle_Resources import Battle_Resources
def on_message(ws, message):
global battle_tag
global username
global logged_in
global battles
global challenged
global BATTLE_STATUS
print(message)
if logged_in and Battle.tag in message:
battle_tag = message.split('\n')[0]
battle_id = battle_tag.split(Battle.tag)[1]
if len(battle_id) == 9 and not '|error|[Invalid choice] There\'s nothing to choose' in message:
print('BATTLE:', battle_id)
if battle_id not in battles.keys():
new_battle = Battle(battle_id)
battles[battle_id] = new_battle
elif '|choice|move batonpass|' in message or '|choice||move uturn' in message:
battles[battle_id].move_required = MoveType.BATTLE_SWITCH
#This message will only occur when challenging random players and not specific users
elif (username + '\'s rating:') in message and battle_tag in message:
ws.send('|/leave ' + battle_tag)
battles[battle_id].reset_battle_info()
ws.send('|/battle!')
battles[battle_id].update_battle_info(message)
elif '|challstr|' in message:
challstr = message[10:]
print(challstr)
logged_in = login(challstr, ws)
elif BATTLE_STATUS == ChallengeStatus.ACCEPT_CHALLENGE and (not challenged) and '|updatechallenge' in message:
challenged = True
print('--------------------------------')
def login(challstr, ws):
global username
f = open('login.txt', 'r').readlines()
username = f[0][:-1]
password = f[1]
r = requests.post('https://play.pokemonshowdown.com/action.php', data={'act': 'login', 'name': username, 'pass' : password, 'challstr' : challstr})
print(r.status_code, r.reason)
assertion = r.text.split('"assertion":"')[1][:-2]
ws.send('|/trn ' + username + ',0,' + assertion)
if r.status_code == 200:
return True
return False
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
global logged_in
logged_in = False
def run(*args):
global in_battle
global battle_info
global move_required
global NUM_BATTLES
global BATTLE_STATUS
global CHALLENGE_USER
global logged_in
global challenged
challenged = False
done = False
while not logged_in:
time.sleep(1)
for i in range(NUM_BATTLES):
time.sleep(5)
if BATTLE_STATUS == ChallengeStatus.RANDOM:
ws.send('|/battle!')
elif BATTLE_STATUS == ChallengeStatus.SEND_CHALLENGE:
ws.send('|/challenge ' + CHALLENGE_USER +', gen7randombattle')
for i in range(100000):
if (not done) and BATTLE_STATUS == ChallengeStatus.ACCEPT_CHALLENGE and challenged:
done = True
ws.send('|/accept '+ CHALLENGE_USER)# +', gen7randombattle')
time.sleep(1)
time.sleep(1)
ws.close()
print("thread terminating...")
_thread.start_new_thread(run, ())
if __name__ == "__main__":
global random_move
global manual
global battles
global BATTLE_STATUS
global CHALLENGE_USER
global BATTLE_TIMER
manual = False
random_move = True
NUM_BATTLES = 1
TAG = 'battle-gen7randombattle-'
AI = AIType.MINIMAX
DATA_DIRECTORY = 'Battle_data'
CHALLENGE_USER = ''
BATTLE_STATUS = ChallengeStatus.RANDOM
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://sim2.psim.us/showdown/websocket",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
battles = {}
Battle.initialize(ws, AI,TAG, DATA_DIRECTORY)
Battle_Resources.initialize(DATA_DIRECTORY)
ws.run_forever()