/
webControl.py
executable file
·491 lines (423 loc) · 17.5 KB
/
webControl.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.common.keys import Keys
from random import random
import pokemon as poke
import battleControl as battle
global username
global password
global showdown
global driver
global wait
global debug_flag
username = 'SutadasutoIA'
password = 'Stardust_1'
showdown = 'http://play.pokemonshowdown.com'
wait = 10
debug_flag = True
def prepareBrowser():
global driver
global wait
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 120)
driver.implicitly_wait(2)
driver.get(showdown)
return driver
def debug(s):
if debug_flag:
print(s)
def login():
wait.until(EC.element_to_be_clickable((By.NAME, 'login')))
driver.find_element_by_name('login').click()
userFlag = False
while userFlag is False:
userFlag = True
try:
driver.find_element_by_name('username').send_keys(username)
except:
userFlag = False
driver.find_element_by_css_selector('button[type=submit]').click()
driver.implicitly_wait(2)
passFlag = False
while passFlag is False:
passFlag = True
try:
driver.find_element_by_name('password').send_keys(password)
except:
passFlag = False
driver.find_element_by_css_selector('button[type=submit]').click()
logFlag = False
while logFlag is False:
try:
if driver.find_element_by_class_name('username').text == username:
debug("Logged in.")
logFlag = True
except:
null = 0
def load_team(name=None):
if name is None:
name = 'MyTeam'
debug("Loading team")
team = open('team.txt', 'r')
driver.find_element_by_css_selector('.mainmenu2').click()
wait.until(EC.element_to_be_clickable((By.NAME, 'newTop')))
driver.find_element_by_name('newTop').click()
driver.find_element_by_css_selector('.teambuilderformatselect').click()
driver.find_element_by_css_selector('ul.popupmenu:nth-child(1) > li:nth-child(13) > button:nth-child(1)').click()
driver.find_element_by_name('import').click()
driver.find_element_by_css_selector('.teamedit > textarea.textbox').send_keys(Keys.CONTROL + 'a')
driver.find_element_by_css_selector('.teamedit > textarea.textbox').send_keys(team.readlines())
driver.find_element_by_name('saveImport').click()
driver.find_element_by_css_selector('input.textbox.teamnameedit').send_keys(Keys.CONTROL + 'a')
time.sleep(1)
driver.find_element_by_css_selector('input.textbox.teamnameedit').send_keys(name)
time.sleep(1)
driver.find_element_by_name('closeRoom').click()
def join_battle(user=None):
if user is None:
wait.until(EC.element_to_be_clickable((By.NAME, 'format')))
driver.find_element_by_name('format').click()
debug("Looking for Battle Spot")
driver.find_element(By.XPATH, "//*[@name='selectFormat'][@value='gen7battlespotsingles']").click()
debug("Search for battle")
wait.until(EC.element_to_be_clickable((By.NAME, 'search')))
driver.find_element_by_name('search').click()
else:
wait.until(EC.element_to_be_clickable((By.NAME, 'finduser')))
driver.find_element_by_name('finduser').click()
driver.find_element_by_name('data').send_keys(user)
driver.find_element_by_css_selector('p.buttonbar:nth-child(2) > button:nth-child(1)').click()
debug("Requesting challenge vs " + user)
wait.until(EC.element_to_be_clickable((By.NAME, 'challenge')))
driver.find_element_by_name('challenge').click()
driver.implicitly_wait(1)
driver.find_element_by_css_selector('.challenge > form:nth-child(1) > p:nth-child(2) ' +
'> button:nth-child(2)').click()
driver.implicitly_wait(1)
#driver.find_element_by_css_selector('ul.popupmenu:nth-child(1) > li:nth-child(14) ' +
# '> button:nth-child(1)').click()
driver.find_element(By.XPATH, "//*[@name='selectFormat'][@value='gen7battlespotsingles']").click()
driver.implicitly_wait(1)
driver.find_element_by_name('makeChallenge').click()
print('Waiting for opponent')
def select_pokemon():
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.switchmenu > button:nth-child(1)')))
debug("Battle Found")
debug("Selecting Pokemon")
driver.find_element_by_css_selector('.switchmenu > button:nth-child(1)').click()
driver.find_element_by_css_selector('.switchmenu > button:nth-child(2)').click()
driver.find_element_by_css_selector('.switchmenu > button:nth-child(3)').click()
def select_attack(attack):
while True:
try:
ls = driver.find_elements_by_name('chooseMove')
break
except NoSuchElementException:
null = 0
for element in ls:
value = element.get_attribute("value")
if int(value) == attack:
element.click()
return 0
try:
null = ls[10]
except IndexError:
null = 0
def changeTo(pokemon=None):
if pokemon is None:
ls = driver.find_elements_by_name('chooseSwitch')
ls[0].click()
else:
ls = driver.find_elements_by_name('chooseSwitch')
for option in ls:
if option.text.lower().split('-')[0] == pokemon.lower().split('-')[0]:
option.click()
return 0
print('Hay algo inusual en el programa. Se cambia al primer pokemon disponible')
try:
ls[0].click()
except IndexError:
null = 0
def getFoeName(team):
moreThanOneMega = ["charizard", "mewtwo"]
foe = driver.find_elements_by_css_selector('.lstatbar > strong')
if len(foe) == 1:
name = " ".join(foe[0].text.split(" ")[:-1]).lower()
for poke in team:
if len(poke.split(name)) == 2:
name = poke
break
icons = driver.find_elements_by_css_selector(".lstatbar>strong>img")
if len(icons) > 0:
if str(icons[-1].get_attribute("alt")) == "mega":
if name in moreThanOneMega:
picons = driver.find_elements_by_css_selector("div.rightbar>div.trainer>div.teamicons>span.picon")
for picon in picons:
title = picon.get_attribute("title").lower()
if "(active)" in title:
if "mega-x" in title:
name += "-mega-x"
break
elif "mega-y" in title:
name += "-mega-y"
break
else:
name += "-mega"
return name
else:
return 'Foe pokemon not found!'
def myPokemonName(team):
moreThanOneMega = ["charizard", "mewtwo"]
myPokemon = driver.find_elements_by_css_selector('.rstatbar > strong')
if len(myPokemon) == 1:
name = " ".join(myPokemon[0].text.split(" ")[:-1]).lower()
for poke in team:
if len(poke.split(name)) == 2:
name = poke
break
icons = driver.find_elements_by_css_selector(".rstatbar>strong>img")
if len(icons) > 0:
if str(icons[-1].get_attribute("alt")) == "mega":
if name in moreThanOneMega:
picons = driver.find_elements_by_css_selector("div.leftbar>div.trainer>div.teamicons>span.picon")
for picon in picons:
title = picon.get_attribute("title").lower()
if "(active)" in title:
if "mega-x" in title:
name += "-mega-x"
break
elif "mega-y" in title:
name += "-mega-y"
break
else:
name += "-mega"
return name
else:
return 'Foe pokemon not found!'
def myHP():
try:
myPokeHP = driver.find_element(By.CSS_SELECTOR, '.whatdo > small')
percentage = myPokeHP.text
percentage = percentage.split(' ')[1]
HP = percentage.split('/')[0]
return HP
except:
return 'My HP not found!'
def foeHP():
try:
foePokeHP = driver.find_element(By.CSS_SELECTOR, '.lstatbar > div:nth-child(2) > div:nth-child(3)')
HPpixels = foePokeHP.get_attribute('style')
HPpixels = HPpixels.split(" ")[1]
HP = float(HPpixels[0:-3])
return round(HP * 100/151, 1)
except:
return 'HP not found!'
def analyzeCurrentStatus(myPokemon, foePokemon):
stats = ["atk", "def", "spa", "spd", "spe", "accuracy", "evasion"]
hs = ["psn", "par", "slp", "brn", "frz"]
vs = ["confused", "focus energy", "embargo", "taunt", "leech seed"]
terrains = ["electric terrain", "psychic terrain", "grassy terrain", "misty terrain"]
backgrounds = ["tailwind", "foe's tailwind", "trick room"]
weathers = ["rain", "sandstorm", "sun", "hail"]
inGameMoves = driver.find_elements_by_name('chooseMove')
for move in inGameMoves:
if move.is_displayed():
text = str(move.text).split('\n')
name = text[0]
type = text[1]
currentPP = text[2].split("/")[0]
for attack in myPokemon.list_attacks:
if name.lower().replace("-"," ").replace("'","") == attack.name:
attack.atype = type.lower()
attack.pp = int(currentPP)
myBoosts = [1] * 7
foeBoosts = [1] * 7
myStats = driver.find_elements_by_css_selector('div.statbar.rstatbar>div.hpbar>div.status')
myStats = str(myStats[0].text).lower()
foeStats = driver.find_elements_by_css_selector('div.statbar.lstatbar>div.hpbar>div.status')
foeStats = str(foeStats[0].text).lower()
myHardStatus = "healthy"
for status in hs:
if myStats.startswith(status):
myHardStatus = status
break
foeHardStatus = "healthy"
for status in hs:
if foeStats.startswith(status):
foeHardStatus = status
break
myVolatileStatus = []
for status in vs:
if status in myStats:
myVolatileStatus.append(status)
foeVolatileStatus = []
for status in vs:
if status in foeStats:
foeVolatileStatus.append(status)
list = myStats.split(" ")
for stat in stats:
if stat in list:
i = list.index(stat)
boost = list[i-1]
boost = boost.split("\xd7")[0]
myBoosts[stats.index((stat))] = float(boost)
list = foeStats.split(" ")
for stat in stats:
if stat in list:
i = list.index(stat)
boost = list[i - 1]
boost = boost.split("\xd7")[0]
foeBoosts[stats.index((stat))] = float(boost)
myPokemon.boosts = myBoosts
myPokemon.hard_status = myHardStatus
myPokemon.volatile_status = myVolatileStatus
foePokemon.boosts = foeBoosts
foePokemon.hard_status = foeHardStatus
foePokemon.volatile_status = foeVolatileStatus
battleField = driver.find_elements_by_css_selector("div.weather")
elements = battleField[-1].text.lower().split("\n")
for element in elements:
for terrain in terrains:
if element.startswith(terrain):
print("We are in " + terrain)
break
for background in backgrounds:
if element.startswith(background):
print("We are in " + background)
for weather in weathers:
if element.startswith(weather):
print("We are under " + weather)
break
def battleActive():
return len(driver.find_elements_by_name('instantReplay')) < 1
def fainted():
hpBar = driver.find_elements_by_css_selector('.rstatbar')
return not len(hpBar) > 0
def oneBattle(pokedex, attacks, jugadores):
history = driver.find_elements_by_class_name('battle-history')
player1 = history[0].text.lower()
player2 = history[1].text.lower()
team1 = str(player1).split('\n')[1].split(' / ')
team2 = str(player2).split('\n')[1].split(' / ')
turn = 0
expectedTurn = 1
print('Waiting for opponent...')
while turn != expectedTurn:
history = driver.find_elements_by_class_name('battle-history')
try:
turn = int(history[-1].text.split(" ")[1])
except ValueError:
turn = -1
time.sleep(0.1)
expectedTurn += 1
time.sleep(0.1)
while battleActive():
foe = getFoeName(team2)
poke.appendPokemon(foe, jugadores.list_rival, pokedex)
rival = poke.foePokemon(foe, jugadores)
myPkm = myPokemonName(team1)
active = poke.myPokemon(myPkm, jugadores)
mHP = myHP()
rival.hp = foeHP()
analyzeCurrentStatus(active, rival)
print('\n\nFoe: ' + rival.name)
print('HP: ' + str(rival.hp) + '%')
print('Status: ' + rival.hard_status)
print('Conditions: ' + str(rival.volatile_status))
print('Boosts: ' + str(rival.boosts))
print('My pokemon: ' + active.name)
print('HP: ' + mHP)
print('Status: ' + active.hard_status)
print('Conditions: ' + str(active.volatile_status))
print('Boosts: ' + str(active.boosts))
decision = battle.decideAction(active, rival, jugadores)
if decision > 0:
try:
select_attack(decision)
except:
print('Hay algo inusual en el programa. Se usa el primer ataque disponible')
decision = 1
select_attack(decision)
print(active.name + ' uses ' + active.list_attacks[decision - 1].name)
active.list_attacks[decision - 1].make_attack()
print(active.list_attacks[decision - 1].name + ': ' +
str(active.list_attacks[decision - 1].pp) + '/' + str(active.list_attacks[decision - 1].max_pp))
else:
pokemonSelected = jugadores.list_pokemon[-1*decision - 1].name
print('Change to ' + pokemonSelected)
active = poke.myPokemon(pokemonSelected, jugadores)
changeTo(pokemonSelected)
myPkm = pokemonSelected
print('Waiting for opponent...')
while turn != expectedTurn:
flag = fainted()
if flag:
print(myPkm + ' fainted!')
try:
jugadores.list_pokemon.pop(jugadores.list_pokemon.index(active))
except:
null = 0
print('Quedan vivos: ')
for pokemon in jugadores.list_pokemon:
print(pokemon.name)
choiceIndex = -1 * battle.cambio(active, rival, jugadores)
choiceName = jugadores.list_pokemon[choiceIndex - 1].name
print('Elige a: ' + choiceName)
changeTo(choiceName)
while True:
if not fainted():
break
if not battleActive():
break
history = driver.find_elements_by_class_name('battle-history')
try:
turn = int(history[-1].text.split(" ")[1])
except ValueError:
turn = -1
time.sleep(0.1)
expectedTurn += 1
time.sleep(0.1)
try:
driver.find_element_by_name('showChat').click()
except:
null = 0
log = driver.find_elements_by_css_selector('.battle-history')
attacksUsed = []
for line in log:
event = line.text
parts = event.split('The opposing ')
if len(parts) == 2:
pair = parts[1]
poke_atk = pair[0:-1].lower().split(' used ')
if len(poke_atk) == 2:
attacksUsed.append(poke_atk)
for line in attacksUsed:
name = line[0]
attack = line[1]
pokemon = poke.foePokemon(name, jugadores)
pokemon.learn_attack(poke.get_this_attack(attack.replace("'",""), attacks))
time.sleep(1)
try:
driver.find_element_by_name('hideChat').click()
except:
null = 0
time.sleep(3)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.messagebar > p:nth-child(1)')))
try:
while True:
message = driver.find_element_by_css_selector('.messagebar > p:nth-child(1)')
result = message.text
if result[-15:] == 'won the battle!':
break
except:
result = 'unknown'
if result == 'SutadasutoIA won the battle!':
return []
driver.find_element_by_name('closeRoom').click()
driver.quit()
return jugadores.list_rival