forked from SMU-SIS/tictactoe
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.py
107 lines (88 loc) · 3.01 KB
/
main.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
"""`main` is the top level module for your Bottle application.
Loads the Bottle framework and adds a custom error
handler.
"""
# import the Bottle framework
from bottle import Bottle
from google.appengine.api import users
from bottle import route, request, run, redirect
import json
import urllib
from google.appengine.api import urlfetch
from google.appengine.ext import ndb
class Bot(ndb.Model):
name = ndb.StringProperty()
language = ndb.StringProperty()
code = ndb.StringProperty()
class GameResult(ndb.Model):
bot1 = ndb.KeyProperty(kind=Bot)
bot2 = ndb.KeyProperty(kind=Bot)
created = ndb.DateTimeProperty(auto_now_add=True)
result = ndb.IntegerProperty()
def add_result(bot1,bot2,result):
gr = GameResult(bot1=bot1, bot2=bot2, result=result)
gr.put()
def list_results():
results = []
score = {}
for gr in GameResult.all():
results.append({"bot1":gr.bot1.name, "bot2":gr.bot2.name,"result":gr.result})
if score.has_key(gr.bot1.key().id()):
score[gr.bot1.key().id()] += gr.result
else:
score[gr.bot1.key().id()] = gr.result
if score.has_key(gr.bot2.key().id()):
score[gr.bot2.key().id()] -=gr.result
else:
score[gr.bot2.key().id()] = -gr.result
return (results, score)
def verify_service(requestJSON, url):
params = urllib.urlencode({'jsonrequest': requestJSON})
deadline = 10
result = urlfetch.fetch(url=url,
payload=params,
method=urlfetch.POST,
deadline=deadline,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
return result.content
# Run the Bottle wsgi application. We don't need to call run() since our
# application is embedded within an App Engine WSGI application server.
bottle = Bottle()
@bottle.route('/api/currentplayer')
def currentplayer():
d = {"name":str(users.get_current_user())}
return json.dumps(d)
def verify(problem, tests, url):
d = {"tests":tests, "solution":problem}
requestJSON = json.dumps(d)
result = verify_service(requestJSON,url)
return result
@bottle.route('/api/post_bot')
def use_verify_service():
name = request.params.get('name')
language = request.params.get('language')
code = request.params.get('code')
new_bot = Bot(name = name,
language = language,
code = code)
new_bot.put()
# result = json.dumps({"name":name,"language":langauge,"code":code})
return 'true'
@bottle.route('/api/use_verify_service')
def use_verify_service():
url = "http://162.222.183.53/python"
problem = request.params.get('problem')
tests = request.params.get('tests')
result = verify(problem, tests, url)
return result
@bottle.route('/api/get_bots')
def use_verify_service():
bots = Bot.query().fetch(100)
result = []
for bot in bots:
result.append(bot.to_dict())
return json.dumps(result)
@bottle.error(404)
def error_404(error):
"""Return a custom 404 error."""
return 'Sorry, Nothing at this URL.'