From 2034591fb615d65b95ab0a68e7764788ce45b6a0 Mon Sep 17 00:00:00 2001 From: "Paul S. Ferrell" Date: Tue, 6 Oct 2009 12:47:36 -0600 Subject: [PATCH] Final (I hope tanks update). --- .gitignore | 1 + ctf/teams.py | 1 - tanks/lib/.actions.py.swp | Bin 20480 -> 0 bytes tanks/lib/Pflanzarr.py | 38 ++++++++++++--------- tanks/www/Config.py | 1 + tanks/www/ctf.css | 3 +- tanks/www/docs.cgi | 2 +- tanks/www/errors.cgi | 69 ++++++++++++++++++++++++++++++++++++++ tanks/www/head.html | 4 +-- tanks/www/links.html | 4 +++ tanks/www/results.cgi | 16 +++++---- tanks/www/style.css | 16 --------- tanks/www/submit.cgi | 7 ++-- tanks/www/submit.html | 7 ++-- 14 files changed, 122 insertions(+), 47 deletions(-) delete mode 100644 tanks/lib/.actions.py.swp create mode 100644 tanks/www/Config.py create mode 100755 tanks/www/errors.cgi create mode 100644 tanks/www/links.html delete mode 100644 tanks/www/style.css 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 420b2285120f511a0a1df93e831a93ff55b14b4d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20480 zcmeI2ZD=J|8OKj;w=dc^+^rxAo+cl1qjztTU2Nk@O5?sjmhP(AtSv6vIGKBHGG;P! zcV=$Vs8tYOyVNg=Ahlmq=!ytdl%iOr3VyICiXU33AN*iJS`@)ns2{Aq=bV|jH)(d0 zP+h5>%l`J>%$(=E{LeGb%sKNcjDK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 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: