Skip to content

Commit

Permalink
update web stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
eklitzke committed Jul 14, 2012
1 parent 9de24fa commit 1b9b0ca
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bot.py
Expand Up @@ -329,6 +329,6 @@ def on_finish(world, score, moves):
def on_finish(world, score, moves):
print "Moves: %s" % "".join(moves)
print "Score: %d (%d/%d)" % (score, world.lambdas_collected, world.remaining_lambdas)
world.post_score(moves, args.file)
world.post_score(moves, args.file, args.name)
sys.exit(0)
run_bot(the_bot, the_world, args.iterations, on_finish)
3 changes: 2 additions & 1 deletion vis.py
Expand Up @@ -213,7 +213,8 @@ def input_iter():

print 'FINAL SCORE: %d' % my_world.score()
print 'MOVES:', ''.join(moves)
my_world.post_score(moves, args.file)
if not args.use_stdin:
my_world.post_score(moves, args.file, 'human')

if __name__ == "__main__":
main()
43 changes: 31 additions & 12 deletions web/webserver.py
@@ -1,3 +1,4 @@
import json
import optparse
import tornado.ioloop
import tornado.web
Expand All @@ -12,46 +13,63 @@ def create_table():
' filename TEXT NOT NULL,'
' score INTEGER NOT NULL,'
' moves TEXT NOT NULL,'
' final_status TEXT)')
' final_status TEXT,'
' bot_name TEXT)')
conn.cursor().execute(
'CREATE INDEX scores_idx ON scores (score, filename)')
conn.cursor().execute(
'CREATE INDEX moves_idx ON scores (moves, filename)')

class BaseHandler(tornado.web.RequestHandler):


class MainHandler(tornado.web.RequestHandler):
columns = ['id', 'filename', 'score', 'moves', 'final_status', 'bot_name']

@property
def cursor(self):
if not hasattr(self, '_cursor'):
self._cursor = conn.cursor()
return self._cursor

def get(self):
buf = []
def get_scores(self):
for row in self.cursor.execute(
'SELECT * FROM scores ORDER BY score DESC'):
print row
buf.append(','.join(map(str, row)))
'SELECT ' + ', '.join(self.columns) +
' FROM scores ORDER BY id DESC'):
yield dict(zip(self.columns, row))


class MainHandler(BaseHandler):

def get(self):
self.set_header('Content-Type', 'text/plain')
self.write('\n'.join(buf))
self.write(','.join(self.columns) + '\n')
for row in self.get_scores():
vals = [row[k] for k in self.columns]
self.write(','.join(map(str, vals)) + '\n')

def post(self):
filename = self.get_argument('filename')
score = int(self.get_argument('score'))
score = int(float(self.get_argument('score')))
moves = self.get_argument('moves')
final_status = self.get_argument('final_status', None)
bot_name = self.get_argument('bot_name', None)
self.cursor.execute(
'SELECT * FROM scores WHERE filename = ? AND moves = ? LIMIT 1',
(filename, moves))
if self.cursor.fetchone() == None:
self.cursor.execute(
'INSERT INTO scores (filename, score, moves, final_status) '
'VALUES (?, ?, ?, ?)', (filename, score, moves, final_status))
'INSERT INTO scores (filename, score, moves, final_status, bot_name) '
'VALUES (?, ?, ?, ?, ?)', (filename, score, moves, final_status, bot_name))
conn.commit()


class JSONHandler(BaseHandler):

def get(self):
self.set_header('Content-Type', 'application/json')
data = list(self.get_scores())
self.write(json.dumps(data))


if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option('--create-table', action='store_true',
Expand All @@ -65,6 +83,7 @@ def post(self):
else:
application = tornado.web.Application([
(r"/", MainHandler),
(r"/json", JSONHandler),
])
application.listen(opts.port)
tornado.ioloop.IOLoop.instance().start()
3 changes: 2 additions & 1 deletion world.py
Expand Up @@ -341,13 +341,14 @@ def _check_end(self, direction, moved_rocks, new_map):
elif self.in_lift:
self.state = REACHED_LIFT

def post_score(self, moves, filename, final_status=None):
def post_score(self, moves, filename, bot_name=None, final_status=None):
if isinstance(moves, list):
moves = ''.join(moves)
data = {
'filename': filename,
'moves': moves,
'score': self.score(),
'bot_name': bot_name,
}
if final_status:
data['final_status'] = final_status
Expand Down

0 comments on commit 1b9b0ca

Please sign in to comment.