forked from ScottehMax/pyMon
/
battle.py
302 lines (226 loc) · 10.6 KB
/
battle.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
import json
import random
import utils
import battleutils
class BattleHandler:
def __init__(self, ch):
self.ch = ch
self.battles = {}
def handle(self, msg):
battle_id = msg.pop(0)[1:-1].split('\n')[0]
if battle_id not in self.battles:
print battle_id, 'is new battle'
self.new_battle(battle_id)
try:
if msg[0][0] != '|':
msg[0] = '|' + msg[0]
for part in '|'.join(msg).split('\n'):
if part != '|':
self.battles[battle_id].handle(part.split('|')[1:])
except IndexError:
err = 'list index out of range'
def handle_challenge(self, msg):
info = json.loads(msg[2])['challengesFrom']
for who, battle_format in info.iteritems():
print who, battle_format
if battle_format in ['randombattle', 'challengecup1vs1']:
self.ch.send_msg('', '/utm')
self.ch.send_msg('', '/accept %s' % who)
def new_battle(self, id):
adapter = BattleAdapter({'id': id, 'ch': self.ch, 'bh': self})
self.battles[id] = adapter
class BattleAdapter:
def __init__(self, args):
self.id = args['id']
self.ch = args['ch']
self.bh = args['bh']
self.rqid = 0
self.lastmove, self.waiting = None, False # Bleugh
self.battle = battleutils.Battle(id=self.id)
def pick_alive_mons(self, sidedata):
# This returns a list of the *INDEXES* of any alive mons
alive_mons = []
for i in range(6):
if sidedata['pokemon'][i]['condition'] != '0 fnt':
alive_mons.append(i + 1)
return alive_mons
def pick_best_switch(self, sidedata):
alive_mons = []
for i in range(6):
if sidedata['pokemon'][i]['condition'] != '0 fnt':
alive_mons.append(i + 1)
for mon in [x for x in sidedata['pokemon']]:
print mon
def name_alive_mons(self, sidedata):
alive_mons = []
for i in range(6):
if sidedata['pokemon'][i]['condition'] != '0 fnt':
alive_mons.append(sidedata['pokemon'][i]['details'].split(', ')[0])
return ', '.join(alive_mons)
def best_move(self, active, types, target):
movesinfo = self.pick_move(active)
print movesinfo
try:
movelist = [x.get('id') for x in movesinfo]
except Exception as e:
print e
movelist = movesinfo
print movelist
scores = [battleutils.calculate_move_score(active, types, target, x) for x in movelist]
best_score = max(scores)
best_move = movelist[scores.index(best_score)]
if best_score >= 120:
print 'Best score is %s - score %s' % (best_move, best_score)
return '/move ' + best_move
else:
b_switch = self.best_switch(active, self.sidedata, target)
if b_switch != '/switch 1':
b_mon_switch_id = b_switch.split('/switch ')[1]
b_mon_switch = self.sidedata.get('pokemon')[int(b_mon_switch_id) - 1]
b_mon_species = b_mon_switch.get('details').split(', ')[0]
print 'Score not good enough. switching to %s' % b_mon_species
return b_switch
else:
print 'No good other options. using %s' % best_move
return '/move ' + best_move
def best_switch(self, active, sidedata, target):
print 'call to switch'
highest_score = 0
alive_mon_indexes = self.pick_alive_mons(sidedata)
side_mon_info = sidedata.get('pokemon')
# Loops through all mons, finds which has the highest score, and switches
print alive_mon_indexes
best_mon_index = alive_mon_indexes[0]
for i in alive_mon_indexes:
mon = side_mon_info[i - 1]
mon_info = mon.get('details').split(', ')
mon_species = utils.condense(mon_info[0])
mon_level = mon_info[1][1:]
try:
mon_types = battleutils.POKEDEX.get(mon_species).get('types')
except Exception as e:
print e
mon_types = ['Normal']
movelist = mon.get('moves')
scores = [battleutils.calculate_move_score(active, mon_types, target, x) for x in movelist]
print movelist
print scores
best_score = max(scores)
if best_score > highest_score:
print 'best score %s is higher than highest score %s!' % (best_score, highest_score)
highest_score = best_score
best_move = movelist[scores.index(best_score)]
print 'best move is %s!' % best_move
best_mon = mon_species
best_mon_index = i
result = '/switch ' + str(best_mon_index)
print result
return result
def pick_move(self, active):
# Might add the main eval here later on
possiblemoves = []
if active is not None and 'trapped' in active:
return [active['moves'][0]['move']]
for move in active['moves']:
if move.get('pp') > 0 and not move.get('disabled'):
possiblemoves.append(move)
print possiblemoves
return possiblemoves
def handle(self, msg):
# Commenting out until I decide whether to properly integrate triggers in battle rooms with regular triggers.
# self.ch.handle(msg, self.id)
if msg[0] == 'init':
self.ch.send_msg(self.id, '/timer')
elif msg[0] == 'c':
# These are hardcoded.
if utils.condense(msg[1]) == 'scotteh':
if msg[2] == '!alive':
self.ch.send_msg(self.id, self.name_alive_mons(self.sidedata))
elif msg[2] == '!moves':
if self.active is not None:
self.ch.send_msg(self.id, ', '.join(self.pick_move(self.active)))
elif msg[2] == '!item':
self.ch.send_msg(self.id, self.sidedata['pokemon'][0]['item'])
elif msg[2].startswith('!custom'):
self.ch.send_msg(self.id, msg[2][8:])
elif msg[2].startswith('.eval'):
expression = msg[2][6:]
try:
response = eval(expression)
self.ch.send_msg(self.id, response)
except Exception as e:
self.ch.send_msg(self.id, e)
elif msg[0] == 'player':
# Sets p1 and p2 in a new player object
if msg[1] == 'p1':
if msg[2] == self.ch.user:
# This is me
self.me = 'p1'
else:
self.me = 'p2'
self.battle.p1 = battleutils.Player(id=msg[1], username=utils.condense(msg[2]))
else:
self.battle.p2 = battleutils.Player(id=msg[1], username=utils.condense(msg[2]))
elif msg[0] == 'poke':
if msg[1] == 'p1':
self.battle.add_to_team(self.battle.p1, msg[2])
else:
self.battle.add_to_team(self.battle.p2, msg[2])
elif msg[0] == 'switch' or msg[0] == 'drag':
# New mon was sent out
if msg[1][0:2] != self.me:
if self.waiting:
self.waiting = False
self.ch.send_msg(self.id, '/switch %s|%s' % (random.choice(self.pick_alive_mons(self.sidedata)),
self.rqid))
mon_info = msg[2].split(', ')
mon_species = utils.condense(mon_info[0])
mon_level = int(mon_info[1][1:])
print 'Encountered a level %s %s' % (mon_level, mon_species)
self.active_opponent = battleutils.Pokemon(species=mon_species, level=mon_level)
else:
mon_info = msg[2].split(', ')
mon_species = utils.condense(mon_info[0])
self.types = battleutils.POKEDEX[mon_species]['types']
elif msg[0] == 'request':
self.canMegaEvo = False
request = json.loads(msg[1])
self.rqid = request.get('rqid')
self.active = request.get('active', [None])[0] # Note: check if active has multiple elements for doubles
self.sidedata = request.get('side')
self.player = self.sidedata.get('id')
self.canMegaEvo = self.sidedata.get('pokemon')[0].get('canMegaEvo')
elif msg[0] == 'turn':
# move_msg = '/move %s' % random.choice(self.pick_move(self.active))
# move_msg = '/move %s' % self.best_move(self.active, self.types, self.active_opponent)
move_msg = self.best_move(self.active, self.types, self.active_opponent)
print 'move_msg is %s' % move_msg
if move_msg.startswith('/move'):
message = (move_msg + '|' if not self.canMegaEvo else move_msg + ' mega |') + str(self.rqid)
self.ch.send_msg(self.id, message)
else:
self.ch.send_msg(self.id, move_msg)
elif msg[0] == 'teampreview':
self.ch.send_msg(self.id, "/team %s|%s" % (random.randint(1, 6), self.rqid))
elif msg[0] == 'faint' and msg[1][0:2] == self.player:
if self.lastmove in {'U-turn', 'Volt Switch'}:
# The opponent needs to send out their 'mon first
self.waiting = True
else:
mon_choice = self.best_switch(self.active, self.sidedata, self.active_opponent)
self.ch.send_msg(self.id, '%s|%s' % (mon_choice, self.rqid))
elif self.waiting and msg[0] == 'switch' and msg[1][0:2] != self.player:
# Opponent has sent out their 'mon
self.waiting = False
self.ch.send_msg(self.id, '/switch %s|%s' % (random.choice(self.pick_alive_mons(self.sidedata)), self.rqid))
elif msg[0] == 'win':
self.ch.send_msg(self.id, '/leave')
elif msg[0] == 'deinit':
del self.bh.battles[self.id]
elif msg[0] == '-crit' and msg[1][0:2] == self.player:
self.ch.send_msg(self.id, 'wow stop hacking anytime')
elif msg[0] == 'move':
self.lastmove = msg[2]
if msg[1][0:2] == self.player:
if msg[2] in {'U-turn', 'Volt Switch', 'Baton Pass', 'Parting Shot'}: # Horrible hack
self.ch.send_msg(self.id, '/switch %s' % random.choice(self.pick_alive_mons(self.sidedata)[1:]))