2 # -*- coding: utf-8 -*-
4 # Summary: a bot for playing TBP Hunt (http://spider.eecs.umich.edu/~tbp/michelle/michelle.php)
6 # Author-email: "Elson Liu" <eyliu.dev@umich.edu>
39 "Content-Type": "application/x-www-form-urlencoded",
40 "User-Agent": "TauBetaPyBot (Watson; JamesOS 1.0)",
41 "Accept": "text/plain"
62 COLOR_RED
="\x1b[1;31m"
63 COLOR_GREEN
="\x1b[1;32m"
64 COLOR_YELLOW
="\x1b[1;33m"
65 COLOR_WHITE
="\x1b[1;37m"
75 print "%s%s%s" % (c
, txt
, COLOR_NONE
)
81 csvfile
= file("stats-%d.csv" % int(time
.time()), "w")
86 csvfile
.write(",".join(a
) + "\n")
89 a
= [str(time
.time()),]
91 a
.append(str(player
.get(k
,"NA")))
93 a
.append(str(enemy
.get(k
,"NA")))
94 csvfile
.write(",".join(a
) + "\n")
98 cprint(COLOR_GREEN
, "logging in...")
99 user
= raw_input("Username: ")
100 pw
= getpass
.getpass("Password: ")
101 # user = "username" # hardcode username and password for testing
108 response
, content
= fetch_page(None,body
)
109 if "set-cookie" in response
:
110 headers
["Cookie"] = response
["set-cookie"]
113 parse(content
,None,body
)
118 cprint(COLOR_GREEN
, "exploring...")
119 query
= {"action": "explore"}
121 body
= {"exploreS": "Explore"}
122 response
, content
= fetch_page(query
, body
)
126 body
= {"finishexploreS": "What Did I Find?!"}
127 response
, content
= fetch_page(query
, body
)
128 parse(content
, query
)
132 cprint(COLOR_GREEN
, "napping...")
137 response
, content
= fetch_page(query
)
142 cprint(COLOR_GREEN
, "fighting...")
146 response
, content
= fetch_page(query
)
151 cprint(COLOR_GREEN
, "fleeing...")
155 response
, content
= fetch_page(query
)
160 cprint(COLOR_GREEN
, "casting withstand...")
162 "baction": "Withstand"
164 response
, content
= fetch_page(query
)
169 cprint(COLOR_GREEN
, "doing nothing...")
173 response
, content
= fetch_page(query
)
177 def fetch_page(query
,body
=None):
180 "http://spider.eecs.umich.edu/~tbp/michelle/michelle.php",
181 urllib
.urlencode(query
)
184 url
= "http://spider.eecs.umich.edu/~tbp/michelle/michelle.php"
185 http
= httplib2
.Http()
187 response
, content
= http
.request(url
, "POST", headers
=headers
,
188 body
=urllib
.urlencode(body
))
190 response
, content
= http
.request(url
, "GET", headers
=headers
)
191 if "set-cookie" in response
:
192 print "Cookie: %s" % response
["set-cookie"]
193 return response
, content
196 def parse(page
,query
,body
=None):
197 tree
= lxml
.html
.document_fromstring(page
)
198 if body
!= None and "loginS" in body
or query
!= None:
199 for attribute
in attributes
:
200 search
= "".join(["//span[@id='",attribute
,"']"])
201 player
[attribute
] = float(tree
.xpath(search
)[0].text_content().strip())
202 if body
!= None and "finishexploreS" in body
or query
!= None:
203 notification
= tree
.xpath("//p[@class='notify']")
205 if notification
!= []:
206 state
["event"] = notification
[0].text_content().strip()
207 if query
!= None and "baction" in query
:
208 for attribute
in eattributes
:
209 search
= "".join(["//span[@id='",attribute
,"']"])
210 results
= tree
.xpath(search
)
212 enemy
[attribute
] = results
[0].text_content().strip()
213 bnotifications
= tree
.xpath("//p[@class='fightnotify']")
214 mnotifications
= tree
.xpath("//p[@class='enchantnotify']")
217 for bnotify
in bnotifications
:
218 bevent
= bnotify
.text_content().strip()
219 print " Battle event: %s" % bevent
220 bevents
.append(bevent
)
221 for mnotify
in mnotifications
:
222 mevent
= mnotify
.text_content().strip()
223 print " Magical event: %s" % mevent
224 mevents
.append(mevent
)
225 state
["bevents"] = bevents
226 state
["mevents"] = mevents
229 cprint(COLOR_YELLOW
, state
["event"])
235 _xp
= player
["experience"]
236 _nxp
= player
["neededexperience"]
237 _hp
= player
["health"]
238 _mhp
= player
["maxhealth"]
239 _int
= player
["integrity"]
240 _mint
= player
["maxintegrity"]
241 _gold
= player
["gold"]
243 cprint(COLOR_NONE
, "level %4d, xp %d / %d, health %d / %d (%d%%), integrity %d / %d, gold %d" %
244 (_l
, _xp
, _nxp
, _hp
, _mhp
, int(100*_hp
/_mhp
), _int
, _mint
, _gold
,))
249 parser
= optparse
.OptionParser()
250 parser
.add_option('-n', action
="store", dest
="limit",
251 help="number of iterations", default
=1000)
252 parser
.add_option('-c', action
="store_true", dest
="color",
253 help="enable ANSI color escape codes", default
=False)
254 options
, args
= parser
.parse_args()
255 limit
= int(options
.limit
)
256 enable_colors(bool(options
.color
))
259 print "Number of rounds: %d" % limit
261 for n
in xrange(limit
):
262 cprint(COLOR_WHITE
,"\nROUND %d" % (n
+1,))
265 if "event" in state
and state
["event"].find("assailed") > -1:
269 while float(player
["health"]) / float(player
["maxhealth"]) > 0.6 and \
270 state
["event"].find("defeated") < 0 and \
271 state
["event"].find("Rebecca") < 0:
272 percentage
= player
["health"] / player
["maxhealth"]
274 if "bevents" in state
:
275 for bevent
in state
["bevents"]:
276 if bevent
.find("taking aim") > -1:
277 aim_round
= battle_round
278 cprint(COLOR_RED
,"Danger, Will Robinson! Rogue Nerf Bow and Arrow is taking aim!")
280 if state
["event"].find("Launcher") > -1:
283 elif battle_round
== aim_round
:
289 time
.sleep(wait_time
)
292 if float(player
["health"]) / float(player
["maxhealth"]) <= 0.6 or \
293 state
["event"].find("Rebecca") > -1 or \
294 state
["event"].find("Launcher") > -1:
298 if float(player
["health"]) / float(player
["maxhealth"]) <= 0.6 or \
299 float(player
["integrity"]) / float(player
["maxintegrity"]) <= 0.8:
302 if int(player
["health"]) <= 0:
304 cprint(COLOR_RED
, "TauBetaPyBot has died! :-( Sleeping for %d seconds..." % (death_timeout
,))
305 time
.sleep(death_timeout
)
309 if __name__
== '__main__':
312 except KeyboardInterrupt:
313 cprint(COLOR_RED
, "Bye, bye.")