-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame_control.py
More file actions
496 lines (452 loc) · 20.9 KB
/
game_control.py
File metadata and controls
496 lines (452 loc) · 20.9 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
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
492
493
494
495
496
import json
import random
import time
import protocol.login as login
import protocol.request_parser as rp
import protocol.senders as sender
from ai.chooser import Chooser
from ai.chooser_type import Difficulty
from ai.damage_tracker import DamageTracker
from model.field import BattleFieldSingle
from model.field_type import Field
from model.stats_type import StatsType
from model.status import Status
from model.status_type import StatusType
from protocol.data_source import DatabaseDataSource
from protocol.enemy_updater import update_enemy_move, update_enemy_pokemon
import model.setup_logger
import logging
logger = logging.getLogger("GameLoop")
class GameLoop:
"""Main control class"""
def __init__(self, ws, user_name, password, sex, gen, difficulty, mode, opponent_name):
self.ws = ws
self.user_name = user_name
self.password = password
self.sex = sex
self.gen = gen
self.mode = mode
self.opponent_name = opponent_name
self.battle_field = BattleFieldSingle(None, None, {}, {})
with open("standard_answers", "r") as file:
self.standard_answers = file.readlines()
self.db = DatabaseDataSource()
self.damage_tracker = DamageTracker()
self.last_move = ""
self.counter = 0
self.chooser = Chooser(difficulty)
self.bot_volatile = []
self.oppo_volatile = []
self.mul_stats_bot = {
StatsType.Atk: 0,
StatsType.Def: 0,
StatsType.Spa: 0,
StatsType.Spd: 0,
StatsType.Spe: 0,
StatsType.Accuracy: 0,
StatsType.Evasion: 0
}
self.mul_stats_oppo = {
StatsType.Atk: 0,
StatsType.Def: 0,
StatsType.Spa: 0,
StatsType.Spd: 0,
StatsType.Spe: 0,
StatsType.Accuracy: 0,
StatsType.Evasion: 0
}
# Dict of handlers for the challenge section
self.handler_challenge = {
"challstr": self._handle_challstr,
"updateuser": self._handle_update_user,
"updatesearch": self._handle_update_search
}
# Dict of handlers of the battle section
self.handler_battle = {
"init": self._handle_init,
"player": self._handle_player,
"-damage": self._handle_damage,
"switch": self._handle_switch,
"move": self._handle_move,
"request": self._handle_request,
"teampreview": self._handle_team_preview,
"turn": self._handle_turn,
"callaback": self._handle_callback,
"win": self._handle_win,
"c": self._handle_chat,
"faint": self._handle_faint,
"-heal": self._handle_heal,
"-status": self._handle_status,
"-curestatus": self._handle_curestatus,
"-boost": self._handle_boost,
"-unboost": self._handle_unboost,
"-weather": self._handle_weather,
"-fieldstart": self._handle_field_start,
"-fieldend": self._handle_field_end,
"-start": self._handle_start_vol,
"-end": self._handle_end_vol
}
async def handle_message(self, message):
"""Main method of the class that dispatches and invokes the correct method
:param message: The message received from the ws
:return:
"""
splitted = message.split("|")
if "battle" not in splitted[0]:
function = self.handler_challenge.get(splitted[1], self._handle_place_holder)
await function(splitted)
else:
lines = message.splitlines()
for line in lines[1:]:
if line == '':
continue
current = line.split("|")
function = self.handler_battle.get(current[1], self._handle_place_holder)
await function(current)
async def _handle_place_holder(self, string_tab):
"""Placeholder function in case that the key is not in dict then do nothing.
:param string_tab:
:return:
"""
pass
async def _handle_challstr(self, string_tab):
"""Challstr message handler, when we receive challstr we can do the login procedure
:param string_tab: message
:return:
"""
logging.info("Login Successful")
await login.log_in(self.ws, self.user_name, self.password, self.sex, string_tab[2], string_tab[3])
async def _handle_update_user(self, string_tab):
"""When we receive an updateuser means that we're ready to fight an opponent, so send the challenge
:param string_tab:
:return:
"""
if self.user_name in string_tab[2]:
if self.mode == "challenging":
await sender.challenge(self.ws, self.opponent_name, "gen"+str(self.gen)+"randombattle")
elif self.mode == "searching":
await sender.searching(self.ws, "gen"+str(self.gen)+"randombattle")
async def _handle_update_search(self, string_tab):
"""Method that handles the creation of battle room and saves the room id and prints the link to watch the battle
:param string_tab:
:return:
"""
to_parse = json.loads(string_tab[2])
if to_parse["games"]:
for k in to_parse["games"]:
if "battle" in k:
self.battle_field.room_name = k
print("If you want to follow the match go to this link https://play.pokemonshowdown.com/{}".format(
self.battle_field.room_name))
async def _handle_init(self, current):
"""Method that handles the first message of the communication with the showdown server
:param current:
:return:
"""
logging.info("NEW BATTLE STARTING")
num_answer = random.randint(0, len(self.standard_answers) - 1)
await sender.sender(self.ws, self.battle_field.room_name, self.standard_answers[num_answer])
time.sleep(3)
await sender.sender(self.ws, self.battle_field.room_name, "Send go {difficulty} if you want to change "
"difficulty. Available difficulties are: easy, "
"normal, hard!\n/timer on")
print("-----------------------------------------------------------------------")
async def _handle_player(self, current):
"""Method that handles the player message that saves our id and the opponent id
:param current:
:return:
"""
if len(current) > 3 and current[3].lower() == self.user_name:
self.battle_field.player_id = current[2]
if "2" in current[2]:
self.battle_field.turn_number = 1
else:
self.battle_field.turn_number = 0
async def _handle_damage(self, current):
"""Method that handles the -damage message and updates the current hp of the pokemons
:param current:
:return:
"""
if self.battle_field.player_id in current[2]:
if 'fnt' not in current[3]:
actual = int(current[3].split(" ")[0].split("/")[0].strip())
total = int(current[3].split(" ")[0].split("/")[1].strip())
damage_perc = round(actual / total)
self.damage_tracker.add_damage(self.battle_field.active_pokemon_oppo, damage_perc,
self.battle_field.active_pokemon_bot, self.last_move)
else:
if 'fnt' not in current[3]:
damage_perc = int(current[3].split("/")[0].strip())
self.damage_tracker.add_damage(self.battle_field.active_pokemon_bot, damage_perc,
self.battle_field.active_pokemon_oppo, self.last_move)
async def _handle_switch(self, current):
"""Method that handle the switch message and updates the current status of the battlefield
:param current:
:return:
"""
if self.battle_field.player_id not in current[2]:
name = current[2].split(":")[1].strip()
splitted = current[3].split(",")
# Parsing of details
if len(splitted) == 3:
level = int(splitted[1].replace("L", "").strip())
gender = current[3].split(",")[2].strip()
elif len(splitted) == 2 and "L" in splitted[1]:
level = int(splitted[1].replace("L", "").strip())
gender = ""
elif len(splitted) == 2 and "M" in splitted[1] or "F" in splitted[1]:
level = 80
gender = splitted[1].strip()
else:
level = 80
gender = ""
if self.battle_field.active_pokemon_oppo:
for stats in self.battle_field.active_pokemon_oppo.stats.mul_stats:
self.battle_field.active_pokemon_oppo.stats.mul_stats[stats] = 0
update_enemy_pokemon(self.battle_field, self.db, name, level, gender)
for key in self.mul_stats_oppo:
self.mul_stats_oppo[key] = 0
self.oppo_volatile.clear()
self.battle_field.active_pokemon_oppo.volatile_status.clear()
else:
for key in self.mul_stats_bot:
self.mul_stats_bot[key] = 0
self.bot_volatile.clear()
async def _handle_move(self, current):
"""Method that handles the message move and updates the current moveset of the enemy
:param current:
:return:
"""
if self.battle_field.player_id not in current[2]:
move_name = current[3].strip()
self.last_move = move_name
logging.info("{} received {} from {}".format(self.battle_field.active_pokemon_bot, move_name,
self.battle_field.active_pokemon_oppo))
update_enemy_move(self.battle_field, self.db, move_name)
async def _handle_request(self, current):
"""Key method that handles the parsing of our team and saves the id of the next request
:param current:
:return:
"""
if "forceSwitch" in current[2]:
self.battle_field.turn_number = json.loads(current[2])["rqid"]
index = self.chooser.choose_switch(self.battle_field)
await sender.sendswitch(self.ws, self.battle_field.room_name, index,
self.battle_field.turn_number)
return
if "wait" in current[2]:
self.battle_field.turn_number = json.loads(current[2])["rqid"]
return
if current[2] == '':
return
if len(current[2]) == 1:
try:
# Populate the team
# await battle.req_loader(current[3].split('\n')[1], self.ws)
active, bench, number = rp.parse_and_set(current[2], self.db)
self.battle_field.turn_number = number
self.battle_field.active_pokemon_bot = active
self.battle_field.all_pkmns_bot = bench
self.battle_field.bench_selector_side[1] = bench
self.battle_field.active_selector_side[1] = active
for element in self.bot_volatile:
Status.add_volatile_status(element, self.battle_field.active_pokemon_bot)
for key in self.mul_stats_bot:
self.battle_field.active_pokemon_bot.stats.mul_stats[key] = self.mul_stats_bot[key]
print(self.battle_field.active_pokemon_bot.to_string())
except KeyError as e:
print(e)
print(current[3])
else:
# Populate team
active, bench, number = rp.parse_and_set(current[2], self.db)
self.battle_field.active_pokemon_bot = active
self.battle_field.all_pkmns_bot = bench
self.battle_field.turn_number = number
self.battle_field.bench_selector_side[1] = bench
self.battle_field.active_selector_side[1] = active
for element in self.bot_volatile:
Status.add_volatile_status(element, self.battle_field.active_pokemon_bot)
for key in self.mul_stats_bot:
self.battle_field.active_pokemon_bot.stats.mul_stats[key] = self.mul_stats_bot[key]
print(self.battle_field.active_pokemon_bot.to_string())
if number < 3:
logger.info("Team: {}".format(list(map(lambda x: x[1], self.battle_field.all_pkmns_bot.items()))))
async def _handle_team_preview(self, current):
"""Method that handles the team preview message
:param current:
:return:
"""
pass
async def _handle_turn(self, current):
"""Method that handles the turn message which means that we can do a move, so we call the ai module
:param current:
:return:
"""
# An action is a move or a switch
move, is_move = self.chooser.choose_move(self.battle_field)
if is_move:
await sender.sendmove(self.ws, self.battle_field.room_name, move, self.battle_field.turn_number,
self.battle_field.active_pokemon_bot.can_mega,
self.battle_field.active_pokemon_bot.moves[move].is_Z)
else:
await sender.sendswitch(self.ws, self.battle_field.room_name, move, self.battle_field.turn_number)
print(self.battle_field.active_pokemon_oppo.oppo_to_string())
print("-----------------------------------------------------------------------")
async def _handle_callback(self, current):
"""Method that handles the trapped state of the pokemon that cannot switch so he must do a move
:param current:
:return:
"""
if current[2] == "trapped":
move = self.chooser.choose_move(self.battle_field, True)
await sender.sendmove(self.ws, self.battle_field.room_name, move, self.battle_field.turn_number)
async def _handle_win(self, current):
"""Method that handles the win of the bot
:param current:
:return:
"""
await sender.sendmessage(self.ws, self.battle_field.room_name, "Well done, have a nice day!")
await sender.leaving(self.ws, self.battle_field.room_name)
if self.user_name in current[2]:
logger.info("BATTLE WON!")
else:
logger.info("BATTLE LOST!")
exit(1)
async def _handle_chat(self, current):
"""Method that replies to the chat"""
if self.user_name not in current[2]:
if "go" in current[3]:
try:
new_difficulty = Difficulty[current[3].split(" ")[1].capitalize()]
self.chooser.difficulty = new_difficulty
logger.info("Difficulty is now set on {}".format(new_difficulty.name))
await sender.sender(self.ws, self.battle_field.room_name, "Difficulty is now set on: {}".format(
new_difficulty.name))
except:
await sender.sender(self.ws, self.battle_field.room_name, "That difficulty is not supported "
"yet!\nTry easy, normal or hard")
else:
num_answer = random.randint(0, len(self.standard_answers) - 1)
await sender.sender(self.ws, self.battle_field.room_name, self.standard_answers[num_answer])
async def _handle_faint(self, current):
"""Method that handles the fainting of a pokemon
:param current:
:return:
"""
"""faint"""
if self.battle_field.player_id not in current[1]:
logger.info("{} fainted".format(self.battle_field.active_pokemon_bot))
Status.apply_non_volatile_status(StatusType.Fnt, self.battle_field.active_pokemon_bot)
else:
logger.info("{} fainted".format(self.battle_field.active_pokemon_oppo))
Status.apply_non_volatile_status(StatusType.Fnt, self.battle_field.active_pokemon_oppo)
async def _handle_heal(self, current):
"""Method that handles the heal message and updates the hp of the pokemons"""
"""-heal"""
if self.battle_field.player_id in current[2]:
self.battle_field.update_heal(1, int(current[3].split("/")[0]))
else:
# TODO: The opponent is in %
self.battle_field.update_heal(2, int(current[3].split("/")[0]))
async def _handle_status(self, current):
"""Method that updates the status of the active pokemons
:param current:
:return:
"""
"""-status"""
if self.battle_field.player_id in current[2]:
self.battle_field.update_status(1, current[3])
else:
self.battle_field.update_status(2, current[3])
async def _handle_curestatus(self, current):
"""Method that cures the status of the active pokemon
:param current:
:return:
"""
"""-curestatus"""
if self.battle_field.player_id in current[2]:
self.battle_field.update_status(1)
else:
self.battle_field.update_status(2)
async def _handle_boost(self, current):
"""Method that handles the boost of the active pokemons' stats
:param current:
:return:
"""
"""-boost"""
if self.battle_field.player_id in current[2]:
self.battle_field.update_buff(1, current[3], int(current[4]))
self.mul_stats_bot[StatsType[current[3].strip().capitalize()]] += int(current[4])
else:
self.battle_field.update_buff(2, current[3], int(current[4]))
self.mul_stats_oppo[StatsType[current[3].strip().capitalize()]] += int(current[4])
async def _handle_unboost(self, current):
"""Method that handles the boost of the negative pokemons' stats
:param current:
:return:
"""
"""-unboost"""
if self.battle_field.player_id in current[2]:
self.battle_field.update_buff(1, current[3], - int(current[4]))
self.mul_stats_bot[StatsType[current[3].strip().capitalize()]] -= int(current[4])
else:
self.battle_field.update_buff(2, current[3], - int(current[4]))
self.mul_stats_oppo[StatsType[current[3].strip().capitalize()]] -= int(current[4])
async def _handle_weather(self, current):
"""Method that handles the weather message and updates the battlefield
:param current:
:return:
"""
"""-weather"""
if current[2] == 'none':
logger.info("Weather set to Normal")
self.battle_field.update_weather("Normal")
else:
logger.info("Weather set to {}".format(current[2]))
self.battle_field.update_weather(current[2])
async def _handle_field_start(self, current):
"""Method that handles the set of the terrain
:param current:
:return:
"""
"""-fieldstart"""
terrain = current[2].split(":")[1].strip().split(" ")[0]
logger.info("Terrain set to {}".format(terrain))
self.battle_field.update_field(Field[terrain])
async def _handle_field_end(self, current):
"""Method that resets the terrain
:param current:
:return:
"""
"""-fieldend"""
logger.info("Terrain set to Normal")
self.battle_field.update_field(Field.Normal)
async def _handle_start_vol(self, current):
"""|-start|p2a: Toxicroak|move: Taunt"""
if "move" in current[3]:
vol_status = current[3].split(":")[1].strip().replace(" ", "")
else:
vol_status = current[3].strip().capitalize().replace(" ", "")
if self.battle_field.player_id in current[2]:
Status.add_volatile_status(StatusType[vol_status],
self.battle_field.active_pokemon_bot)
self.bot_volatile.append(StatusType[vol_status])
else:
Status.add_volatile_status(StatusType[vol_status],
self.battle_field.active_pokemon_oppo)
self.oppo_volatile.append(StatusType[vol_status])
async def _handle_end_vol(self, current):
"""|-end|p2a: Toxicroak|move: Taunt"""
if "move" in current[3]:
vol_status = current[3].split(":")[1].strip()
else:
vol_status = current[3].strip().capitalize()
if self.battle_field.player_id in current[2]:
Status.remove_volatile_status(StatusType[vol_status],
self.battle_field.active_pokemon_bot)
self.bot_volatile.remove(StatusType[vol_status])
else:
Status.remove_volatile_status(StatusType[vol_status],
self.battle_field.active_pokemon_oppo)
self.oppo_volatile.remove(StatusType[vol_status])