This repository has been archived by the owner on Jan 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
111 lines (92 loc) · 3.02 KB
/
bot.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
# coding=utf-8
import time
import random
import traceback
from barbot import constants, logger, barbot
def main():
"""Main function."""
bot = barbot.Bot('barbot.conf')
bot.change_game('towers')
try:
game(bot)
except:
print(traceback.format_exc())
logger.info('Close bot.')
bot.leave_game()
exit()
def game(bot):
"""Start and contol current game."""
logger.info('Enter to towers.')
bot.entry()
logger.info(u'Tower: {}'.format(bot._game._tower))
bot.hero._update_status(bot._game._page)
hp = bot.hero.hp
while not bot.hero.is_tired:
bot.hero._update_status(bot._game._page)
actions = bot.get_actions()
if bot.hero.hp < hp:
if actions['move']['forward']:
action = random.choice(actions['move']['forward'])
elif actions['move']['backward']:
action = random.choice(actions['move']['backward'])
else:
bot.entry()
continue
else:
if bot.hero._class == constants.WARRIOR:
action = choice_warrior_action(actions)
else:
action = choice_medic_action(actions)
if action is None:
bot.entry()
continue
bot.move(action)
logger.info(bot.get_action_log())
print('=' * 60)
hp = bot.hero.hp
time.sleep(random.randint(4, 7))
def choice_warrior_action(actions):
"""Choice the optimal action for warrior."""
if actions['skills'] and any(actions['attack'].values()):
action = actions['skills'][0]
elif actions['attack']:
if actions['attack']['tower']:
action = actions['attack']['tower']
elif actions['attack']['last']:
action = actions['attack']['last']
else:
action = actions['attack']['random']
elif actions['move']:
if actions['move']['forward']:
action = random.choice(actions['move']['forward'])
else:
action = random.choice(actions['move']['backward'])
else:
action = None
return action
def choice_medic_action(actions):
"""Choice the optimal action for medic."""
if actions['heal']:
if actions['heal']['self']:
action = actions['heal']['self']
elif actions['heal']['last']:
action = actions['heal']['last']
else:
action = actions['heal']['new']
elif actions['skills'] and any(actions['burning'].values()):
action = actions['skills'][0]
elif actions['burning']:
if actions['burning']['last']:
action = actions['burning']['last']
else:
action = actions['burning']['random']
elif actions['move']:
if actions['move']['forward']:
action = random.choice(actions['move']['forward'])
else:
action = random.choice(actions['move']['backward'])
else:
action = None
return action
if __name__ == '__main__':
main()