-
Notifications
You must be signed in to change notification settings - Fork 3
/
BotWarsHandler.py
executable file
·137 lines (114 loc) · 4.39 KB
/
BotWarsHandler.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python
#
# This file is part of the BotWarsServer program.
# Copyright (C) 2013 Rajat Khanduja, Suhail Sherif
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This file contains the functions required to handle the incoming requests.
import time, json, urlparse, os
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from base64 import b64decode
import logging
from judge import judge, NoSuchProblemException
import BotWarsDb as db
TEMP_DIR = "/tmp/BOTWARS_TMP"
# Return strings
INVALID_PROBLEM = "Invalid Problem number"
INVALID_AUTHENTICATION = "Invalid Authentication. Please check your username and password again."
FILEDATA_MISSING = "Error receiving file content. Please check if you are choosing the correct file."
class BotwarsHandler (BaseHTTPRequestHandler):
# self._evaluator
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_POST(self):
''' Respond to POST request.'''
# Extract and print the contents of the POST
logging.debug('Received post request at '+self.path)
recvTime = int(time.time())
length = int(self.headers['Content-Length'])
post_data = urlparse.parse_qs(self.rfile.read(length).decode('utf-8'))
teamname = post_data['teamname'][0]
teampass = post_data['teampassword'][0]
if self.path=='/submit':
problemNo = post_data['problemnumber'][0]
filename = post_data['filename'][0]
try:
filedata = b64decode(post_data['filedata'][0])
except Exception as e:
logging.info ('Caught exception %s', e)
self.do_HEAD()
self.wfile.write(FILEDATA_MISSING)
return
logging.debug('Post request details :\t' +
'teamname = %s ;;' +
'teampass = %s ;;' +
'problemNo = %s ;;' +
'filename = %s ;;', teamname, teampass, problemNo, filename)
if not db.authenticate(teamname, teampass):
self.do_HEAD()
self.wfile.write(INVALID_AUTHENTICATION)
return
# Check if the folder for the user exists
folder = os.path.join(TEMP_DIR, teamname, problemNo)
if not os.path.exists(folder):
os.makedirs(folder)
# Create filename using the folder information
filename = os.path.join(folder, filename)
# Dump data into file
self.dumpFile(filename, filedata)
score = 0
# Judge solution
try:
score, errors = judge (problemNo, filename)
except NoSuchProblemException as e:
self.do_HEAD()
self.wfile.write(INVALID_PROBLEM)
logging.debug(str(e))
return
# Rename file so as to be able to keep older versions.
newfilename = filename + str(recvTime)
os.rename(filename, newfilename)
db.updateSubmissions(teamname, problemNo, recvTime, newfilename, score, errors)
# Return evaluation results
self.do_HEAD()
self.wfile.write("Score earned for solution : " + str(score))
if errors:
self.wfile.write("\nErrors\n" + errors)
return
elif self.path == '/leaderboard':
self.do_HEAD()
leaderboard = db.getLeaderboard()
self.wfile.write(leaderboard)
return
elif self.path == '/score':
if not db.authenticate(teamname, teampass):
self.do_HEAD()
self.wfile.write(INVALID_AUTHENTICATION)
return
self.do_HEAD()
scores = db.getScores(teamname)
self.wfile.write(scores)
def do_GET(self):
logging.debug('Received get request at '+self.path)
self.do_HEAD()
leaderboard = db.getLeaderboard().replace("\n", "<BR />").replace("\t", " ")
self.wfile.write(leaderboard)
return
def dumpFile(self, filename, filedata):
fp = open (filename,'w')
fp.write(filedata)
fp.close()