diff --git a/.gitignore b/.gitignore index 15953f7..dd4952d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *~ *.pyc *.dat +*.swp passwd target/ puzzler/ diff --git a/ctf/teams.py b/ctf/teams.py index 41da559..29ed25b 100755 --- a/ctf/teams.py +++ b/ctf/teams.py @@ -18,7 +18,6 @@ teams = {} built = 0 def build_teams(): global teams, built - if not os.path.exists(passwdfn): return if os.path.getmtime(passwdfn) <= built: diff --git a/tanks/lib/.actions.py.swp b/tanks/lib/.actions.py.swp deleted file mode 100644 index 420b228..0000000 Binary files a/tanks/lib/.actions.py.swp and /dev/null differ diff --git a/tanks/lib/Pflanzarr.py b/tanks/lib/Pflanzarr.py index 8f05ed3..4f31ca1 100644 --- a/tanks/lib/Pflanzarr.py +++ b/tanks/lib/Pflanzarr.py @@ -5,6 +5,8 @@ import random import subprocess import xml.sax.saxutils +from urllib import unquote, quote + from PIL import Image, ImageColor, ImageDraw try: @@ -14,6 +16,7 @@ except: path = '/home/pflarr/repos/gctf/' sys.path.append(path) from ctf import teams +teams.build_teams() import Tank @@ -43,7 +46,7 @@ class Pflanzarr: players = [] for p in tmpPlayers: p = unquote(p) - if not (p.startswith('.') or p.endswith('#') or p.endswith('~')) + if not (p.startswith('.') or p.endswith('#') or p.endswith('~'))\ and p in teams.teams: players.append(p) @@ -54,23 +57,27 @@ class Pflanzarr: assert len(players) >= 1, "There must be at least one player." - # The one is added to ensure that there is at least one defaultAI bot. - size = math.sqrt(len(players) + 1) - if int(size) != size: - size = size + 1 + # The one is added to ensure that there is at least one #default bot. + cols = math.sqrt(len(players) + 1) + if int(cols) != cols: + cols = cols + 1 - size = int(size) - if size < 2: - size = 2 + cols = int(cols) + if cols < 2: + cols = 2 - self._board = (size*self.SPACING, size*self.SPACING) + rows = len(players)/cols + if len(players) % cols != 0: + rows = rows + 1 + + self._board = (cols*self.SPACING, rows*self.SPACING) - while len(players) < size**2: + while len(players) < cols*rows: players.append('#default') self._tanks = [] - for i in range(size): - for j in range(size): + for i in range(cols): + for j in range(rows): startX = i*self.SPACING + self.SPACING/2 startY = j*self.SPACING + self.SPACING/2 player = random.choice(players) @@ -78,7 +85,7 @@ class Pflanzarr: if player == '#default': color = '#a0a0a0' else: - color = team.teams[player][1] + color = '#%s' % teams.teams[player][1] tank = Tank.Tank( player, (startX, startY), color, self._board, testMode=True) if player == '#default': @@ -250,7 +257,7 @@ class Pflanzarr: print tank.name, 'has errors' - fileName = os.path.join(self._errorDir, tank.name) + fileName = os.path.join(self._errorDir, quote(tank.name)) file = open(fileName, 'w') for error in tank._program.errors: file.write(error) @@ -351,7 +358,8 @@ class Pflanzarr: self._resultsDir = os.path.join(dir, 'results') self._errorDir = os.path.join(dir, 'errors') self._imageDir = os.path.join(dir, 'frames') - os.mkdir( self._imageDir ) + if not os.path.isdir(self._imageDir): + os.mkdir( self._imageDir ) self._playerDir = os.path.join(dir, 'ai', 'players') def _getDefaultAIs(self, dir, difficulty): diff --git a/tanks/www/Config.py b/tanks/www/Config.py new file mode 100644 index 0000000..4821f03 --- /dev/null +++ b/tanks/www/Config.py @@ -0,0 +1 @@ +DATA_PATH = '/var/lib/tanks/' diff --git a/tanks/www/ctf.css b/tanks/www/ctf.css index ca7c9f6..f3b91d1 100644 --- a/tanks/www/ctf.css +++ b/tanks/www/ctf.css @@ -54,7 +54,7 @@ h1, h2, h3 { letter-spacing: -0.05em; } -code, pre, .readme { +code, pre, .readme, div.errors { color: #fff; background-color: #555; margin: 1em; @@ -97,3 +97,4 @@ dd { fieldset * { margin: 3px; } + diff --git a/tanks/www/docs.cgi b/tanks/www/docs.cgi index 26eb752..8ceb4d0 100755 --- a/tanks/www/docs.cgi +++ b/tanks/www/docs.cgi @@ -18,7 +18,7 @@ except: print open('head.html').read() % "Documentation" print '' print '

Pflanzarr Documentation

' -print 'Submit | Results | Documentation' +print open('links.html').read() print Program.__doc__ print '

Setup Actions:

' diff --git a/tanks/www/errors.cgi b/tanks/www/errors.cgi new file mode 100755 index 0000000..1359cc9 --- /dev/null +++ b/tanks/www/errors.cgi @@ -0,0 +1,69 @@ +#!/usr/bin/python + +print """Content-Type: text/html\n\n""" +print """\n\n""" +import cgi +import cgitb; cgitb.enable() +import os + +import Config + +try: + from urllib.parse import quote +except: + from urllib import quote + +try: + from ctf import teams +except: + import sys + path = '/home/pflarr/repos/gctf/' + sys.path.append(path) + from ctf import teams +teams.build_teams() + +head = open('head.html').read() % "Error Report" +print head +print open('links.html').read() + +def done(): + print '' + sys.exit(0) + +fields = cgi.FieldStorage() +team = fields.getfirst('team', '').strip() +passwd = fields.getfirst('passwd', '').strip() +if team and passwd and \ + team in teams.teams and passwd == teams.teams[team][0]: + path = os.path.join(Config.DATA_PATH, 'errors', quote(team)) + if os.path.isfile(path): + errors = open(path).readlines() + print '

Your latest errors:' + print '

' + if errors: + print '
\n'.join(errors) + else: + print 'There were no errors.' + print '
' + else: + print '

No error file found.' + + done() + +if team and team not in teams.teams: + print '

Invalid team.' + +if team and team in teams.teams and passwd != teams.teams[team][0]: + print '

Invalid password.' + +print ''' +

+
+ Error report request: + Team:
+ Password:
+ +
+
''' + +done() diff --git a/tanks/www/head.html b/tanks/www/head.html index b98713e..f2b66ea 100644 --- a/tanks/www/head.html +++ b/tanks/www/head.html @@ -1,5 +1,5 @@ - ' - %s" + + %s diff --git a/tanks/www/links.html b/tanks/www/links.html new file mode 100644 index 0000000..1615bf7 --- /dev/null +++ b/tanks/www/links.html @@ -0,0 +1,4 @@ +Documentation | +Results | +Submit | +My Errors diff --git a/tanks/www/results.cgi b/tanks/www/results.cgi index 4901ac4..a17ee77 100755 --- a/tanks/www/results.cgi +++ b/tanks/www/results.cgi @@ -3,15 +3,17 @@ import cgitb; cgitb.enable() import os +import Config + print """Content-Type: text/html\n\n""" print """\n\n""" head = open('head.html').read() % "Pflanzarr Results" print head print "

Results

" -print 'Submit | Results | Documentation' +print open('links.html').read() try: - winner = open(os.path.join('data', 'winner')).read() + winner = open(os.path.join(Config.DATA_PATH, 'winner')).read() except: winner = "No winner yet." @@ -19,9 +21,9 @@ print "

Last Winner: ", winner, '

' print "

Results so far:

" try: - games = os.listdir(os.path.join('data', 'results')) + games = os.listdir(os.path.join('results')) except: - print '

The data directory does not exist.' + print '

The results directory does not exist.' games = [] if not games: @@ -30,7 +32,7 @@ gameNums = [] for game in games: try: num = int(game) - path = os.path.join( 'data', "results", game, 'results.html') + path = os.path.join( 'results', game, 'results.html') if os.path.exists( path ): gameNums.append( int(num) ) else: @@ -43,5 +45,5 @@ gameNums.sort(reverse=True) for num in gameNums: print '

%d - ' % num, - print 'v' % num, - print 'r' % num + print 'v' % num, + print 'r' % num diff --git a/tanks/www/style.css b/tanks/www/style.css deleted file mode 100644 index ac787f0..0000000 --- a/tanks/www/style.css +++ /dev/null @@ -1,16 +0,0 @@ -body { background-color : #000000; - color : #E0E0E0; - } - -table { - border : 2px solid #00EE00; - border-collapse : collapse; - margin : 3px; - } - -table td { border : 1px solid #00BB00; - padding-left: 3px; - padding-right: 3px; - text-align: left; - vertical-align: top; - } diff --git a/tanks/www/submit.cgi b/tanks/www/submit.cgi index 01ad8c8..9d57907 100755 --- a/tanks/www/submit.cgi +++ b/tanks/www/submit.cgi @@ -4,6 +4,8 @@ import cgi import cgitb; cgitb.enable() import os +import Config + try: from urllib.parse import quote except: @@ -16,13 +18,14 @@ except: path = '/home/pflarr/repos/gctf/' sys.path.append(path) from ctf import teams +teams.build_teams() print """Content-Type: text/html\n\n""" print """\n\n""" head = open('head.html').read() % "Submission Results" print head print "

Results

" -print 'Submit | Results | Documentation' +print open('links.html').read() def done(): print '' @@ -45,7 +48,7 @@ if team not in teams.teams: if passwd != teams.teams[team][0]: print '

Invalid password.'; done() -path = os.path.join('data/ai/players', encode(team) ) +path = os.path.join(Config.DATA_PATH, 'ai/players', encode(team) ) file = open(path, 'w') file.write(code) file.close() diff --git a/tanks/www/submit.html b/tanks/www/submit.html index 86cc13c..f538d5d 100644 --- a/tanks/www/submit.html +++ b/tanks/www/submit.html @@ -6,8 +6,11 @@

Program Submission

-

Submit | Results | Documentation - +

+ Documentation | + Results | + Submit | + My Errors

Your program: