mirror of https://github.com/dirtbags/moth.git
some updates to package-puzzles
This commit is contained in:
parent
1ba99db98f
commit
36c93973cc
|
@ -12,15 +12,15 @@ import markdown
|
||||||
import random
|
import random
|
||||||
import zipfile
|
import zipfile
|
||||||
|
|
||||||
messageChars = b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
import puzzles
|
||||||
|
|
||||||
def djb2hash(buf):
|
|
||||||
h = 5381
|
|
||||||
for c in buf:
|
|
||||||
h = ((h * 33) + c) & 0xffffffff
|
|
||||||
return h
|
|
||||||
|
|
||||||
def write_kv_pairs(ziphandle, filename, kv):
|
def write_kv_pairs(ziphandle, filename, kv):
|
||||||
|
""" Write out a sorted map to file
|
||||||
|
:param ziphandle: a zipfile object
|
||||||
|
:param filename: The filename to write within the zipfile object
|
||||||
|
:param kv: the map to write out
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
filehandle = io.StringIO()
|
filehandle = io.StringIO()
|
||||||
for key in sorted(kv.keys()):
|
for key in sorted(kv.keys()):
|
||||||
if type(kv[key]) == type([]):
|
if type(kv[key]) == type([]):
|
||||||
|
@ -31,53 +31,7 @@ def write_kv_pairs(ziphandle, filename, kv):
|
||||||
filehandle.seek(0)
|
filehandle.seek(0)
|
||||||
ziphandle.writestr(filename, filehandle.read())
|
ziphandle.writestr(filename, filehandle.read())
|
||||||
|
|
||||||
class Puzzle:
|
if __name__ == '__main__':
|
||||||
def __init__(self, stream):
|
|
||||||
self.message = bytes(random.choice(messageChars) for i in range(20))
|
|
||||||
self.fields = {}
|
|
||||||
self.answers = []
|
|
||||||
self.hashes = []
|
|
||||||
|
|
||||||
body = []
|
|
||||||
header = True
|
|
||||||
for line in stream:
|
|
||||||
if header:
|
|
||||||
line = line.strip()
|
|
||||||
if not line.strip():
|
|
||||||
header = False
|
|
||||||
continue
|
|
||||||
key, val = line.split(':', 1)
|
|
||||||
key = key.lower()
|
|
||||||
val = val.strip()
|
|
||||||
self._add_field(key, val)
|
|
||||||
else:
|
|
||||||
body.append(line)
|
|
||||||
self.body = ''.join(body)
|
|
||||||
|
|
||||||
def _add_field(self, key, val):
|
|
||||||
if key == 'answer':
|
|
||||||
h = djb2hash(val.encode('utf8'))
|
|
||||||
self.answers.append(val)
|
|
||||||
self.hashes.append(h)
|
|
||||||
else:
|
|
||||||
self.fields[key] = val
|
|
||||||
|
|
||||||
def publish(self):
|
|
||||||
obj = {
|
|
||||||
'author': self.fields['author'],
|
|
||||||
'hashes': self.hashes,
|
|
||||||
'body': markdown.markdown(self.body),
|
|
||||||
}
|
|
||||||
return obj
|
|
||||||
|
|
||||||
def secrets(self):
|
|
||||||
obj = {
|
|
||||||
'answers': self.answers,
|
|
||||||
'summary': self.fields['summary'],
|
|
||||||
}
|
|
||||||
return obj
|
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='Build a puzzle category')
|
parser = argparse.ArgumentParser(description='Build a puzzle category')
|
||||||
parser.add_argument('seed', help='contest seed')
|
parser.add_argument('seed', help='contest seed')
|
||||||
parser.add_argument('puzzledir', nargs='+', help='Directory of puzzle source')
|
parser.add_argument('puzzledir', nargs='+', help='Directory of puzzle source')
|
||||||
|
@ -114,7 +68,7 @@ for puzzledir in args.puzzledir:
|
||||||
filename = os.path.basename(puzzlePath)
|
filename = os.path.basename(puzzlePath)
|
||||||
points, ext = os.path.splitext(filename)
|
points, ext = os.path.splitext(filename)
|
||||||
points = int(points)
|
points = int(points)
|
||||||
puzzle = Puzzle(open(puzzlePath))
|
puzzle = puzzles.Puzzle(open(puzzlePath))
|
||||||
puzzles[points] = puzzle
|
puzzles[points] = puzzle
|
||||||
|
|
||||||
# build mapping, answers, and summary
|
# build mapping, answers, and summary
|
||||||
|
@ -123,10 +77,6 @@ for puzzledir in args.puzzledir:
|
||||||
summary = {}
|
summary = {}
|
||||||
for points in sorted(puzzles):
|
for points in sorted(puzzles):
|
||||||
puzzle = puzzles[points]
|
puzzle = puzzles[points]
|
||||||
print(args.seed)
|
|
||||||
print(categoryname)
|
|
||||||
print(points)
|
|
||||||
|
|
||||||
hashmap = hashlib.sha1(args.seed.encode('utf-8'))
|
hashmap = hashlib.sha1(args.seed.encode('utf-8'))
|
||||||
hashmap.update(categoryname.encode('utf-8'))
|
hashmap.update(categoryname.encode('utf-8'))
|
||||||
hashmap.update(str(points).encode('utf-8'))
|
hashmap.update(str(points).encode('utf-8'))
|
||||||
|
@ -142,11 +92,11 @@ for puzzledir in args.puzzledir:
|
||||||
# write out puzzles
|
# write out puzzles
|
||||||
for points in sorted(puzzles):
|
for points in sorted(puzzles):
|
||||||
puzzle = puzzles[points]
|
puzzle = puzzles[points]
|
||||||
puzzledir = os.path.join(categoryname, "content", mapping[points])
|
puzzledir = os.path.join(categoryname, 'content', mapping[points])
|
||||||
# test write
|
# build/write json
|
||||||
write_kv_pairs(zf, os.path.join(puzzledir, 'summary.txt'), summary)
|
ziphandle.writestr(filename, filehandle.read())
|
||||||
# build json
|
zf.writestr(os.path.join(puzzledir, 'puzzle.json'), \
|
||||||
|
json.dumps(puzzle.publish()))
|
||||||
|
# write associated files
|
||||||
|
|
||||||
#vim:py
|
#vim:py
|
||||||
|
|
Loading…
Reference in New Issue