mirror of https://github.com/dirtbags/moth.git
Get generated puzzles working
This commit is contained in:
parent
0cd6dc97c5
commit
881e5a46e1
30
puzzles.py
30
puzzles.py
|
@ -55,6 +55,9 @@ class Puzzle:
|
|||
|
||||
super().__init__()
|
||||
|
||||
if not os.path.isdir(path):
|
||||
raise ValueError("No such directory: {}".format(path))
|
||||
|
||||
self._dict = defaultdict(lambda: [])
|
||||
if os.path.isdir(path):
|
||||
self._puzzle_dir = path
|
||||
|
@ -63,17 +66,20 @@ class Puzzle:
|
|||
self.message = bytes(random.choice(messageChars) for i in range(20))
|
||||
self.body = ''
|
||||
|
||||
if not os.path.exists(path):
|
||||
raise ValueError("No puzzle at path: {}".format(path))
|
||||
elif os.path.isdir(path):
|
||||
# A list of temporary files we've created that will need to be deleted.
|
||||
self._temp_files = []
|
||||
|
||||
# Expected format is path/<points_int>.moth
|
||||
pathname = os.path.split(path)[-1]
|
||||
try:
|
||||
self.points = int(pathname)
|
||||
except ValueError:
|
||||
pass
|
||||
raise ValueError("Directory name must be a point value: {}".format(path))
|
||||
files = os.listdir(path)
|
||||
|
||||
self._seed = category_seed * self.points
|
||||
self.rand = random.Random(self._seed)
|
||||
|
||||
if 'puzzle.moth' in files:
|
||||
self._read_config(open(os.path.join(path, 'puzzle.moth')))
|
||||
|
||||
|
@ -82,15 +88,10 @@ class Puzzle:
|
|||
loader = SourceFileLoader('puzzle_mod', os.path.join(path, 'puzzle.py'))
|
||||
puzzle_mod = loader.load_module()
|
||||
if hasattr(puzzle_mod, 'make'):
|
||||
self.body = '# `puzzle.body` was not set by the `make` function'
|
||||
puzzle_mod.make(self)
|
||||
else:
|
||||
raise ValueError("Unacceptable file type for puzzle at {}".format(path))
|
||||
|
||||
self._seed = category_seed * self.points
|
||||
self.rand = random.Random(self._seed)
|
||||
|
||||
# A list of temporary files we've created that will need to be deleted.
|
||||
self._temp_files = []
|
||||
self.body = '# `puzzle.py` does not define a `make` function'
|
||||
|
||||
def cleanup(self):
|
||||
"""Cleanup any outstanding temporary files."""
|
||||
|
@ -214,16 +215,13 @@ class Puzzle:
|
|||
def __getitem__(self, item):
|
||||
return self._dict[item.lower()]
|
||||
|
||||
def make_answer(self, word_count, sep=b' '):
|
||||
def make_answer(self, word_count, sep=' '):
|
||||
"""Generate and return a new answer. It's automatically added to the puzzle answer list.
|
||||
:param int word_count: The number of words to include in the answer.
|
||||
:param str|bytes sep: The word separator.
|
||||
:returns: The answer bytes
|
||||
:returns: The answer string
|
||||
"""
|
||||
|
||||
if type(sep) == str:
|
||||
sep = sep.encode('ascii')
|
||||
|
||||
answer = sep.join(self.rand.sample(self.ANSWER_WORDS, word_count))
|
||||
self['answer'] = answer
|
||||
|
||||
|
|
Loading…
Reference in New Issue