Allow for random seeds

This commit is contained in:
Neale Pickett 2018-10-10 15:26:44 +00:00
parent d8a26fa30e
commit 6a9182ac9c
1 changed files with 23 additions and 8 deletions

View File

@ -21,8 +21,16 @@ import mothballer
sys.dont_write_bytecode = True # Don't write .pyc files sys.dont_write_bytecode = True # Don't write .pyc files
def get_seed(request):
seedstr = request.match_info.get("seed")
if seedstr == "random":
return random.getrandbits(32)
else:
return int(seedstr)
async def handle_puzzlelist(request): async def handle_puzzlelist(request):
seed = int(request.match_info.get("seed")) seed = get_seed(request)
puzzles = { puzzles = {
"__devel__": [[0, ""]], "__devel__": [[0, ""]],
} }
@ -40,7 +48,7 @@ async def handle_puzzlelist(request):
async def handle_puzzle(request): async def handle_puzzle(request):
seed = int(request.match_info.get("seed")) seed = get_seed(request)
category = request.match_info.get("category") category = request.match_info.get("category")
points = int(request.match_info.get("points")) points = int(request.match_info.get("points"))
cat = moth.Category(request.app["puzzles_dir"].joinpath(category), seed) cat = moth.Category(request.app["puzzles_dir"].joinpath(category), seed)
@ -58,7 +66,7 @@ async def handle_puzzle(request):
async def handle_puzzlefile(request): async def handle_puzzlefile(request):
seed = int(request.match_info.get("seed")) seed = get_seed(request)
category = request.match_info.get("category") category = request.match_info.get("category")
points = int(request.match_info.get("points")) points = int(request.match_info.get("points"))
filename = request.match_info.get("filename") filename = request.match_info.get("filename")
@ -78,7 +86,7 @@ async def handle_puzzlefile(request):
async def handle_mothballer(request): async def handle_mothballer(request):
seed = int(request.match_info.get("seed")) seed = get_seed(request)
category = request.match_info.get("category") category = request.match_info.get("category")
try: try:
@ -106,9 +114,16 @@ async def handle_index(request):
<body> <body>
<h1>Dev Server</h1> <h1>Dev Server</h1>
<p> <p>
You need to provide the contest seed in the URL. You need to provide the contest seed in the URL.
If you don't have a contest seed in mind, If you don't have a contest seed in mind,
why not try <a href="{seed}/">{seed}</a>? why not try <a href="{seed}/">{seed}</a>?
</p>
<p>
If you are chaotic,
you could even take your chances with a
<a href="random/">random seed</a> for every HTTP request.
This means generated files will get a different seed than the puzzle itself!
</p>
</body> </body>
</html> </html>
""".format(seed=seed) """.format(seed=seed)