Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Last active October 31, 2015 14:37
Show Gist options
  • Save cocodrips/72e89887d39d81960e1f to your computer and use it in GitHub Desktop.
Save cocodrips/72e89887d39d81960e1f to your computer and use it in GitHub Desktop.
CodeRunner2015 予選B 33位でした〜
import urllib2
token = ""
def send(url):
return urllib2.urlopen(url.format(token)).read()
def get(url):
return urllib2.urlopen(url).read()
import coderunner
import json
import time
url_enter = "http://game.coderunner.jp/enter?token={}"
url_info = "http://game.coderunner.jp/infoJson?token={}"
url_room = "http://game.coderunner.jp/roomJson?rid={}"
LONGEST = 2.8
GIVEUP = 10000000
class Runner():
def __init__(self):
self.last_post = 0
def solve(self):
with open("log/info.log", 'a') as info_f, open("log/normal.log", 'a') as log_f:
while True:
self.get_info(info_f, log_f)
try:
print "POST", time.time() - self.last_post
print coderunner.send(url_enter)
self.last_post = time.time()
except:
tt = time.time()
print tt - self.last_post
time.sleep(max(1 - (tt - self.last_post), 0.01))
def get_info(self, f, log_f):
while True:
while True:
try:
data = coderunner.send(url_info)
break
except:
time.sleep(0.5)
f.write(data + '\n')
try:
data_j = json.loads(data)
except:
return
# print json.dumps(data_j, indent=4)
"""
Infomation
"""
print "=====", data_j['hps']
print "E", data_j['hp'], '/', data_j['maxhp']
print "M", str(data_j['power']).ljust(13), data_j['damage']
for i in xrange(len(data_j['friend'])):
print i, str(data_j['friend'][i]['power']).ljust(13), data_j['friend'][i]['damage'], "Id:", data_j['friend'][i]['id']
"""
Other
"""
log_f.write(json.dumps(data_j['log']))
next = pow(data_j['power'], 0.5)
need = pow(data_j['hp'], 0.5)
current_time = time.time()
current_wait = current_time - self.last_post
if (data_j['hps'] and (data_j['hp'] * 2) < data_j['hps'][0]):
# if (data_j['power'] > pow(LONGEST * 1000, 2) ): # (_ _)
# return
self.sleep(1, 'next---')
continue
if need < next:
if next < 998:
print "next---"
continue
else:
return
if not data_j['hps']:
"""
Last
"""
if need - next < 2000:
self.sleep(1)
else:
self.sleep((need - next) / 2000, "(half)")
else:
"""
No last
"""
if need - next > 3000:
self.sleep(LONGEST, "long")
elif need - next < 1500:
self.sleep(1.0)
elif need - next < 2000:
self.sleep((need - next - 500) / 1000)
else:
self.sleep((need - next) / 2000)
try:
data = coderunner.send(url_info)
data_j = json.loads(data)
except:
print "info err"
if (data_j['hp'] > GIVEUP):
return
#
# if (data_j['hp'] < GIVEUP and data_j['power'] > GIVEUP):
# return
def sleep(self, t, dest=""):
print "Time:", t, dest
if t > 2:
while t > 0.1:
if (t < 1):
time.sleep(t)
return
time.sleep(1)
t -= 1
try:
data = coderunner.send(url_info)
data_j = json.loads(data)
print "Current power:", data_j['hp']
if data_j['hp'] < data['power']:
return
except:
print "json err"
return
else:
time.sleep(t)
if __name__ == '__main__':
runner = Runner()
runner.solve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment