Prepare to put on device

This commit is contained in:
Neale Pickett 2009-09-25 13:27:37 -06:00
parent dbd9aea6f1
commit ba19575d62
19 changed files with 69 additions and 27 deletions

View File

@ -96,6 +96,7 @@ class Manager:
if now > self.last_beat + pulse: if now > self.last_beat + pulse:
for game in list(self.games): for game in list(self.games):
game.heartbeat(now) game.heartbeat(now)
self.last_beat = now
for event in self.timers: for event in self.timers:
when, cb = event when, cb = event
if now >= when: if now >= when:
@ -373,6 +374,7 @@ class TurnBasedGame(Game):
Game.__init__(self, manager, players) Game.__init__(self, manager, players)
def heartbeat(self, now=None): def heartbeat(self, now=None):
print('heart', self)
if now and (now - self.began > self.game_timeout): if now and (now - self.began > self.game_timeout):
self.running = False self.running = False

View File

@ -88,7 +88,7 @@ def incdict(dict, key, amt=1):
dict[key] = dict.get(key, 0) + amt dict[key] = dict.get(key, 0) + amt
class Storage: class Storage:
def __init__(self, fn): def __init__(self, fn='/var/lib/ctf/points.dat'):
self.points_by_team = {} self.points_by_team = {}
self.points_by_cat = {} self.points_by_cat = {}
self.points_by_cat_team = {} self.points_by_cat_team = {}

View File

@ -13,7 +13,7 @@ def makesock(host):
def submit(cat, team, score, sock=None): def submit(cat, team, score, sock=None):
if not sock: if not sock:
sock = makesock('cfl-sunray1') sock = makesock('localhost')
begin = time.time() begin = time.time()
mark = int(begin) mark = int(begin)
req = points.encode_request(1, mark, cat, team, score) req = points.encode_request(1, mark, cat, team, score)

View File

@ -13,7 +13,7 @@ class MyHandler(asyncore.dispatcher):
asyncore.dispatcher.__init__(self) asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_DGRAM) self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
self.bind(('', port)) self.bind(('', port))
self.store = points.Storage('scores.dat') self.store = points.Storage()
self.acked = set() self.acked = set()
self.outq = [] self.outq = []

View File

@ -8,6 +8,7 @@ import re
import sys import sys
import pointscli import pointscli
import teams import teams
import http.cookies
from urllib.parse import quote, unquote from urllib.parse import quote, unquote
## ##
@ -27,7 +28,7 @@ def dbg(*vals):
points_by_cat = {} points_by_cat = {}
points_by_team = {} points_by_team = {}
try: try:
for line in open('puzzler.dat'): for line in open('/var/lib/ctf/puzzler.dat'):
cat, team, pts = [unquote(v) for v in line.strip().split('\t')] cat, team, pts = [unquote(v) for v in line.strip().split('\t')]
pts = int(pts) pts = int(pts)
points_by_cat[cat] = max(points_by_cat.get(cat, 0), pts) points_by_cat[cat] = max(points_by_cat.get(cat, 0), pts)
@ -36,20 +37,33 @@ except IOError:
pass pass
f = cgi.FieldStorage() c = http.cookies.SimpleCookie(os.environ.get('HTTP_COOKIE', ''))
try:
team = c['team'].value
passwd = c['passwd'].value
except KeyError:
team, passwd = None, None
f = cgi.FieldStorage()
cat = f.getfirst('c') cat = f.getfirst('c')
points = f.getfirst('p') points = f.getfirst('p')
team = f.getfirst('t') team = f.getfirst('t', team)
passwd = f.getfirst('w') passwd = f.getfirst('w', passwd)
key = f.getfirst('k') key = f.getfirst('k')
verboten = ['key', 'index.html'] verboten = ['key', 'index.html']
def start_html(title): def start_html(title):
print('''Content-type: text/html print('Content-type: text/html')
if team or passwd:
<?xml version="1.0" encoding="UTF-8"?> c = http.cookies.SimpleCookie()
if team:
c['team'] = team
if passwd:
c['passwd'] = passwd
print(c)
print()
print('''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
@ -111,9 +125,9 @@ def show_puzzles(cat, cat_dir):
print('<p>None (someone is slacking)</p>') print('<p>None (someone is slacking)</p>')
end_html() end_html()
def show_puzzle(cat, points, points_dir, team, passwd): def show_puzzle(cat, points, points_dir, team='', passwd=''):
# Show puzzle in cat for points # Show puzzle in cat for points
start_html('%s for %s' % (cat, points)) start_html('%s for %s points' % (cat, points))
fn = os.path.join(points_dir, 'index.html') fn = os.path.join(points_dir, 'index.html')
if os.path.exists(fn): if os.path.exists(fn):
print('<div class="readme">') print('<div class="readme">')
@ -139,10 +153,11 @@ def win(cat, team, points):
start_html('Winner!') start_html('Winner!')
points = int(points) points = int(points)
f = open('puzzler.dat', 'a') f = open('puzzler.dat', 'a')
pointscli.submit(cat, team, points)
fcntl.lockf(f, fcntl.LOCK_EX) fcntl.lockf(f, fcntl.LOCK_EX)
f.write('%s\t%s\t%d\n' % (quote(cat), quote(team), points)) f.write('%s\t%s\t%d\n' % (quote(cat), quote(team), points))
pointscli.submit(cat, team, points) print('<p>%d points for %s.</p>' % (points, team))
print('<p>%d points for %s.</p>' % (team, points)) print('<p>Back to <a href="puzzler.cgi?c=%s">%s</a>.</p>' % (cat, cat))
end_html() end_html()
def main(): def main():
@ -168,16 +183,24 @@ def main():
else: else:
show_puzzle(cat, points, points_dir, team, passwd) show_puzzle(cat, points, points_dir, team, passwd)
else: else:
thekey = open('%s/key' % points_dir).read().strip() try:
thekey = open('%s/key' % points_dir, encoding='utf-8').read().strip()
except IOError:
# If there's no key, this can never be solved.
thekey = False
if not teams.chkpasswd(team, passwd): if not teams.chkpasswd(team, passwd):
start_html('Wrong password') start_html('Wrong password')
end_html() end_html()
elif key != thekey: elif key != thekey:
show_puzzle(cat, points, points_dir) show_puzzle(cat, points, points_dir, team, passwd)
elif points_by_team.get((team, cat)): elif int(points) in points_by_team.get((team, cat), set()):
start_html('Greedy greedy') start_html('Greedy greedy')
end_html() end_html()
else: else:
win(cat, team, points) win(cat, team, points)
main() main()
# Local Variables:
# mode: python
# End:

View File

@ -1 +1 @@
2 4 6 8 10 _ 1 10 11 101 110 _ _

View File

@ -1 +1 @@
12 111 1000

View File

@ -1 +1 @@

View File

@ -0,0 +1,7 @@
<pre>
C: 00 f1 00 b4 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64
S: 00 bf 00 f1 02 68 69
C: 00 f3 00 bf 0b 68 6f 77 20 61 72 65 20 79 6f 75
S: 00 ca 00 f3 0d 6e 6f 74 20 62 61 64 2c 20 79 6f 75 3f
C: __ __ __ __ __ 62 65 65 6e 20 77 6f 72 73 65
</pre>

1
puzzles/sequence/500/key Normal file
View File

@ -0,0 +1 @@
01 00 00 ca 0a

View File

@ -1 +1 @@
66 70 72 74 76 _ 66 67 70 71 72 73 74 75 76 77 _

View File

@ -51,7 +51,7 @@ class IdiotBot(threading.Thread):
return self.move return self.move
def run(self): def run(self):
c = Client(('localhost', 5388)) c = Client(('cfl', 5388))
c.debug = False c.debug = False
#print('lobby', c.command('^', 'lobby')) #print('lobby', c.command('^', 'lobby'))
c.command('login', self.team, 'furble') c.command('login', self.team, 'furble')
@ -60,7 +60,7 @@ class IdiotBot(threading.Thread):
ret = c.command(move) ret = c.command(move)
if ret == ['WIN']: if ret == ['WIN']:
print('%s wins' % self.team) print('%s wins' % self.team)
amt = random.uniform(0.1, 1.2) amt = random.uniform(0.1, 2.6)
if c.debug: if c.debug:
print(c, 'sleep %f' % amt) print(c, 'sleep %f' % amt)
time.sleep(amt) time.sleep(amt)

View File

@ -1,27 +1,36 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
import fcntl import fcntl
import time
import os
from urllib.parse import quote, unquote from urllib.parse import quote, unquote
house = 'dirtbags' house = 'dirtbags'
passwdfn = '/var/lib/ctf/passwd'
teams = None teams = None
built = 0
def build_teams(): def build_teams():
global teams global teams, built
modt = os.path.getmtime(passwdfn)
if modt <= built:
return
teams = {} teams = {}
try: try:
f = open('passwd') f = open(passwdfn)
for line in f: for line in f:
line = line.strip() line = line.strip()
team, passwd = [unquote(v) for v in line.strip().split('\t')] team, passwd = [unquote(v) for v in line.strip().split('\t')]
teams[team] = passwd teams[team] = passwd
except IOError: except IOError:
pass pass
built = time.time()
def validate(team): def validate(team):
if teams is None: build_teams()
build_teams()
def chkpasswd(team, passwd): def chkpasswd(team, passwd):
validate(team) validate(team)