From 3343f622089659dc95c6413d2bbe14200ea12e38 Mon Sep 17 00:00:00 2001 From: "Paul S. Ferrell" Date: Wed, 7 Oct 2009 13:18:30 -0600 Subject: [PATCH] Added 'fake' to .gitignore. Removed ctf.teams dependency from Pflanzarr --- .gitignore | 1 + tanks/lib/Pflanzarr.py | 54 ++++++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index dd4952d..5439071 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ *.dat *.swp passwd +fake target/ puzzler/ ctf.tce diff --git a/tanks/lib/Pflanzarr.py b/tanks/lib/Pflanzarr.py index 4f31ca1..c908815 100644 --- a/tanks/lib/Pflanzarr.py +++ b/tanks/lib/Pflanzarr.py @@ -9,19 +9,12 @@ from urllib import unquote, quote from PIL import Image, ImageColor, ImageDraw -try: - from ctf import teams -except: - import sys - path = '/home/pflarr/repos/gctf/' - sys.path.append(path) - from ctf import teams -teams.build_teams() - import Tank class Pflanzarr: + TEAMS_FILE = '/var/lib/ctf/passwd' + FRAME_DELAY = 15 SPACING = 150 @@ -42,12 +35,14 @@ class Pflanzarr: if not os.path.exists(self._gameDir): os.mkdir(self._gameDir) + colors = self._getColors() + tmpPlayers = os.listdir(self._playerDir) players = [] for p in tmpPlayers: p = unquote(p) if not (p.startswith('.') or p.endswith('#') or p.endswith('~'))\ - and p in teams.teams: + and p in colors: players.append(p) AIs = {} @@ -73,7 +68,7 @@ class Pflanzarr: self._board = (cols*self.SPACING, rows*self.SPACING) while len(players) < cols*rows: - players.append('#default') + players.append(None) self._tanks = [] for i in range(cols): @@ -82,13 +77,13 @@ class Pflanzarr: startY = j*self.SPACING + self.SPACING/2 player = random.choice(players) players.remove(player) - if player == '#default': + if player == None: color = '#a0a0a0' else: - color = '#%s' % teams.teams[player][1] + color = tank = Tank.Tank( player, (startX, startY), color, self._board, testMode=True) - if player == '#default': + if player == None: tank.program(random.choice(defaultAIs)) else: tank.program(AIs[player]) @@ -156,10 +151,12 @@ class Pflanzarr: if tank in kills[tank]: kills[tank].remove(tank) - self._saveResults(kills) for tank in self._tanks: self._outputErrors(tank) self._makeMovie() + # This needs to go after _makeMovie; the web scripts look for these + # files to see if the game has completed. + self._saveResults(kills) def _killTanks(self, tanks, reason): for tank in tanks: @@ -219,7 +216,8 @@ class Pflanzarr: html.append('') - if winner.name != '#default': + # Write a blank file if the winner is a default tank.. + if winner.name != None: winnerFile.write(tanks[0].name) winnerFile.close() @@ -250,7 +248,7 @@ class Pflanzarr: def _outputErrors(self, tank): """Output errors for each team.""" - if tank.name == '#default': + if tank.name == None: return if tank._program.errors: @@ -378,7 +376,27 @@ class Pflanzarr: defaultAIs.append( file.read() ) return defaultAIs - + + def _getColors(self): + """Get the team colors from the passwd file. The passwd file location + is set by self.TEAMS_FILE. Returns a dictionary of players->color""" + errorColor = '#ffffff' + + try: + file = open(self.TEAMS_FILE) + except: + return {}.fromkeys(players, errorColor) + + colors = {} + for line in file: + try: + team, passwd, color = map(unquote, line.split('\t')) + colors[team] = '#%s' % color + except: + colors[team] = errorColor + + return teams + def _getGameNum(self): """Figure out what game number this is from the past games played."""