Merge branch 'master' of cfl:/var/projects/gctf

This commit is contained in:
Curt Hash 2009-10-08 11:12:13 -06:00
commit 91ee387600
12 changed files with 144 additions and 32 deletions

View File

@ -142,6 +142,8 @@ class FlagServer(asynchat.async_chat):
self.inbuf.append(data) self.inbuf.append(data)
def set_flag(self, team): def set_flag(self, team):
if not self.cat:
return
self.flag = team self.flag = team
self.submitter.set_flag(self.cat, team) self.submitter.set_flag(self.cat, team)
f = open(os.path.join(flags_dir, self.cat), 'w') f = open(os.path.join(flags_dir, self.cat), 'w')

View File

@ -18,4 +18,4 @@ target:
$(MAKE) -C daemons TARGET=$(TARGET) install $(MAKE) -C daemons TARGET=$(TARGET) install
clean: clean:
rm -rf target rm -rf target pwnables.tce

View File

@ -0,0 +1 @@
/var/lib/cat/flag

View File

@ -24,7 +24,7 @@ target:
$(INSTALL) -d target/usr/lib/www/tanks/ $(INSTALL) -d target/usr/lib/www/tanks/
$(INSTALL) www/* target/usr/lib/www/tanks/ $(INSTALL) www/* target/usr/lib/www/tanks/
ln -s /var/lib/tanks target/usr/lib/www/tanks/results ln -s /var/lib/tanks/results target/usr/lib/www/tanks/results
$(INSTALL) -d target/usr/lib/python2.6/site-packages/tanks/ $(INSTALL) -d target/usr/lib/python2.6/site-packages/tanks/
$(INSTALL) lib/* target/usr/lib/python2.6/site-packages/tanks/ $(INSTALL) lib/* target/usr/lib/python2.6/site-packages/tanks/

View File

@ -201,16 +201,25 @@ class Pflanzarr:
break break
winner = random.choice(winners) winner = random.choice(winners)
html = ['<html><body>', html = ['<html>',
'<head><title>Game %d results</title>',
'<link href="../ctf.css" rel="stylesheet" type="text/css">',
'</head>',
'<body>',
'<table><tr><th>Team<th>Kills<th>Cause of Death'] '<table><tr><th>Team<th>Kills<th>Cause of Death']
for tank in tanks: for tank in tanks:
if tank is winner: if tank is winner:
rowStyle = 'style="color:red;"' rowStyle = 'style="font-weight:bold; '\
'background-color:%s"' % tank._color
else: else:
rowStyle = '' rowStyle = 'style="background-color:%s"' % tank._color
if name:
name = xml.sax.saxutils.escape(tank.name)
else:
name = '#default'
html.append('<tr %s><td>%s<td>%d<td>%s' % html.append('<tr %s><td>%s<td>%d<td>%s' %
(rowStyle, (rowStyle,
xml.sax.saxutils.escape(tank.name), name,
len(kills[tank]), len(kills[tank]),
xml.sax.saxutils.escape(tank.deathReason))) xml.sax.saxutils.escape(tank.deathReason)))

View File

@ -75,7 +75,7 @@ class Tank(object):
else: else:
self._tAngle = tAngle self._tAngle = tAngle
self._color = color self.color = color
# You can't fire until fireReady is 0. # You can't fire until fireReady is 0.
self._fireReady = self.FIRE_RATE self._fireReady = self.FIRE_RATE
@ -466,7 +466,7 @@ class Tank(object):
# The base body rectangle. # The base body rectangle.
for poly in gm.displacePoly(hood, self.pos, self._limits): for poly in gm.displacePoly(hood, self.pos, self._limits):
d.polygon( poly, fill=self._color ) d.polygon( poly, fill=self.color )
# The treads # The treads
for poly in gm.displacePoly(tread1, self.pos, self._limits) + \ for poly in gm.displacePoly(tread1, self.pos, self._limits) + \
@ -475,7 +475,7 @@ class Tank(object):
# The turret circle # The turret circle
for poly in gm.displacePoly(self.body, self.pos, self._limits): for poly in gm.displacePoly(self.body, self.pos, self._limits):
d.ellipse( poly, fill=self._color, outline='black') d.ellipse( poly, fill=self.color, outline='black')
self._drawLaser(d) self._drawLaser(d)
@ -491,7 +491,7 @@ class Tank(object):
if self._fired: if self._fired:
laser = gm.rotatePoly( self.laser, self._angle + self._tAngle ) laser = gm.rotatePoly( self.laser, self._angle + self._tAngle )
for poly in gm.displacePoly(laser, self.pos, self._limits): for poly in gm.displacePoly(laser, self.pos, self._limits):
drawing.polygon(poly, fill=self._color) drawing.polygon(poly, fill=self.color)
self._fired = False self._fired = False
@ -522,7 +522,7 @@ class Tank(object):
if self._sensorState[i]: if self._sensorState[i]:
color = '#000000' color = '#000000'
else: else:
color = self._color color = self.color
r, angle, width, tAttached = self._sensors[i] r, angle, width, tAttached = self._sensors[i]
r = int(r) r = int(r)

View File

@ -1,25 +1,91 @@
#! /usr/bin/python #! /usr/bin/python
import time
import optparse import optparse
import shutil
import time
import asyncore
import asynchat
from tanks import Pflanzarr from tanks import Pflanzarr
T = 60*5 T = 60*5
MAX_HIST = 30
HIST_STEP = 100
key = 'tanks:::2bac5e912ff2e1ad559b177eb5aeecca'
parser = optparse.OptionParser('DATA_DIR easy|medium|hard MAX_TURNS') class Flagger(asynchat.async_chat):
opts, args = parser.parse_args() """Use to connect to flagd and submit the current flag holder."""
if (len(args) != 3) or (args[1] not in ('easy', 'medium', 'hard')):
parser.error('Wrong number of arguments')
try:
turns = int(args[2])
except:
parser.error('Invalid number of turns')
while True: def __init__(self, addr, auth):
start = time.time() asynchat.async_chat.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((addr, 6668))
self.push(auth + '\n')
self.flag = None
def handle_read(self):
msg = self.recv(4096)
raise ValueError("Flagger died: %r" % msg)
def handle_error(self):
# If we lose the connection to flagd, nobody can score any
# points. Terminate everything.
asyncore.close_all()
asynchat.async_chat.handle_error(self)
def set_flag(self, team):
if team:
eteam = team
else:
eteam = ''
self.push(eteam + '\n')
self.flag = team
def run_tanks():
p = Pflanzarr.Pflanzarr(args[0], args[1]) p = Pflanzarr.Pflanzarr(args[0], args[1])
p.run(turns) p.run(turns)
diff = time.time() - start path = os.path.join(args[0], 'results')
if diff - T > 0: files = os.listdir(path)
time.sleep( diff - T ) gameNums = []
for file in files:
try:
gameNums.append( int(file) )
except:
continue
gameNums.sort(reverse=True)
highest = gameNums[0]
for num in gameNums:
if highest - MAX_HIST > num and not (num % HIST_STEP == 0):
shutil.rmtree(os.path.join(path, num))
try:
winner = open('/var/lib/tanks/winner').read().strip()
except:
winner = None
flagger.set_flag(winner)
def main():
parser = optparse.OptionParser('DATA_DIR easy|medium|hard MAX_TURNS')
opts, args = parser.parse_args()
if (len(args) != 3) or (args[1] not in ('easy', 'medium', 'hard')):
parser.error('Wrong number of arguments')
try:
turns = int(args[2])
except:
parser.error('Invalid number of turns')
flagger = Flagger('localhost', key)
lastrun = 0
while True:
asyncore.loop(60, count=1)
now = time.time()
if now - lastrun >= 60:
run_tanks()
lastrun = now
if __name__ == '__main__':
main()

View File

@ -28,22 +28,28 @@ except:
if not games: if not games:
print "<p>No games have occurred yet." print "<p>No games have occurred yet."
gameNums = [] gameNums = []
for game in games: for game in games:
try: try:
num = int(game) gameNums.append( int(game) )
path = os.path.join( 'results', game, 'results.html')
if os.path.exists( path ):
gameNums.append( int(num) )
else:
continue
except: except:
continue continue
gameNums.sort(reverse=True) gameNums.sort(reverse=True)
# Don't include games that haven't completed
i = 0
num = str(gameNums[i])
for i in range(len(gameNums)):
path = os.path.join( 'results', str(gameNums[i]), 'results.html') )
if os.path.exists( path ):
break
gameNums = gameNums[i:]
for num in gameNums: for num in gameNums:
print '<p>%d - ' % num, print '<p>%d - ' % num,
print '<a href="results/%d/game.avi">v</a>' % num, print '<a href="results/%d/game.avi">v</a>' % num,
print '<a href="results/%d/results.html">r</a>' % num print '<a href="results/%d/results.html">r</a>' % num
print '</body></html>'

20
tanksFlagger/Makefile Normal file
View File

@ -0,0 +1,20 @@
FAKE = fakeroot -s fake -i fake
INSTALL = $(FAKE) install -o 100
all: tanksFlagger.tce
push: tanksFlagger.tce
netcat -l -q 0 -p 3333 < tanksFlagger.tce
tanksFlagger.tce: target
$(FAKE) sh -c 'cd target && tar -czf - .' > $@
target:
$(INSTALL) -d target/var/service/tanksFlagger
$(INSTALL) run report_score.py target/var/service/tanksFlagger/
$(INSTALL) -d target/var/service/tanksFlagger/log/
$(INSTALL) log.run target/var/service/tanksFlagger/log/run
clean:
rm -rf target tanksFlagger.tce fake

3
tanksFlagger/log.run Executable file
View File

@ -0,0 +1,3 @@
#! /bin/sh
exec logger -t tanksFlagger

5
tanksFlagger/run Executable file
View File

@ -0,0 +1,5 @@
#! /bin/sh
[ -f /var/lib/ctf/disabled/tanks ] && exit 0
exec envuidgid ctf python3 report_score.py 2>&1