Revert "Add a few things for new CyFi theme"

This reverts commit 9be4387316.
This commit is contained in:
John Donaldson 2020-02-28 22:49:48 +00:00
parent 66d90dbb04
commit f3beab4e89
1 changed files with 59 additions and 49 deletions

View File

@ -67,27 +67,7 @@ class MothRequestHandler(http.server.SimpleHTTPRequestHandler):
cat = moth.Category(catpath, self.seed) cat = moth.Category(catpath, self.seed)
puzzle = cat.puzzle(points) puzzle = cat.puzzle(points)
return puzzle return puzzle
def send_json(self, obj):
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(obj).encode("utf-8"))
def handle_register(self):
# Everybody eats when they come to my house
ret = {
"status": "success",
"data": {
"short": "You win",
"description": "Welcome to the development server, you wily hacker you"
}
}
self.send_json(ret)
endpoints.append(('/{seed}/register', handle_register))
def handle_answer(self): def handle_answer(self):
for f in ("cat", "points", "answer"): for f in ("cat", "points", "answer"):
@ -103,12 +83,17 @@ class MothRequestHandler(http.server.SimpleHTTPRequestHandler):
if self.req.get("answer") in puzzle.answers: if self.req.get("answer") in puzzle.answers:
ret["data"]["description"] = "Answer is correct" ret["data"]["description"] = "Answer is correct"
self.send_json(ret) self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(ret).encode("utf-8"))
endpoints.append(('/{seed}/answer', handle_answer)) endpoints.append(('/{seed}/answer', handle_answer))
def puzzlelist(self): def handle_puzzlelist(self):
puzzles = {} puzzles = {
"__devel__": [[0, ""]],
}
for p in self.server.args["puzzles_dir"].glob("*"): for p in self.server.args["puzzles_dir"].glob("*"):
if not p.is_dir() or p.match(".*"): if not p.is_dir() or p.match(".*"):
continue continue
@ -118,28 +103,13 @@ class MothRequestHandler(http.server.SimpleHTTPRequestHandler):
puzzles[catName].append([0, ""]) puzzles[catName].append([0, ""])
if len(puzzles) <= 1: if len(puzzles) <= 1:
logging.warning("No directories found matching {}/*".format(self.server.args["puzzles_dir"])) logging.warning("No directories found matching {}/*".format(self.server.args["puzzles_dir"]))
self.send_response(200)
return puzzles self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(puzzles).encode("utf-8"))
# XXX: Remove this (redundant) when we've upgraded the bundled theme (probably v3.6 and beyond)
def handle_puzzlelist(self):
self.send_json(self.puzzlelist())
endpoints.append(('/{seed}/puzzles.json', handle_puzzlelist)) endpoints.append(('/{seed}/puzzles.json', handle_puzzlelist))
def handle_state(self):
resp = {
"config": {
"devel": True,
},
"puzzles": self.puzzlelist(),
"messages": "<p><b>[MOTH Development Server]</b> Participant broadcast messages would go here.</p>",
}
self.send_json(resp)
endpoints.append(('/{seed}/state', handle_state))
def handle_puzzle(self): def handle_puzzle(self):
puzzle = self.get_puzzle() puzzle = self.get_puzzle()
@ -150,7 +120,10 @@ class MothRequestHandler(http.server.SimpleHTTPRequestHandler):
obj["logs"] = puzzle.logs obj["logs"] = puzzle.logs
obj["format"] = puzzle._source_format obj["format"] = puzzle._source_format
self.send_json(obj) self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(obj).encode("utf-8"))
endpoints.append(('/{seed}/content/{cat}/{points}/puzzle.json', handle_puzzle)) endpoints.append(('/{seed}/content/{cat}/{points}/puzzle.json', handle_puzzle))
@ -178,7 +151,7 @@ class MothRequestHandler(http.server.SimpleHTTPRequestHandler):
try: try:
catdir = self.server.args["puzzles_dir"].joinpath(category) catdir = self.server.args["puzzles_dir"].joinpath(category)
mb = mothballer.package(category, str(catdir), self.seed) mb = mothballer.package(category, catdir, self.seed)
except Exception as ex: except Exception as ex:
logging.exception(ex) logging.exception(ex)
self.send_response(200) self.send_response(200)
@ -196,11 +169,48 @@ class MothRequestHandler(http.server.SimpleHTTPRequestHandler):
def handle_index(self): def handle_index(self):
seed = random.getrandbits(32) seed = random.getrandbits(32)
self.send_response(307) body = """<!DOCTYPE html>
self.send_header("Location", "%s/" % seed) <html>
self.send_header("Content-Type", "text/html") <head>
<title>Dev Server</title>
<script>
// Skip trying to log in
sessionStorage.setItem("id", "devel-server")
</script>
</head>
<body>
<h1>Dev Server</h1>
<p>
Pick a seed:
</p>
<ul>
<li><a href="{seed}/">{seed}</a>: a special seed I made just for you!</li>
<li><a href="random/">random</a>: will use a different seed every time you load a page (could be useful for debugging)</li>
<li>You can also hack your own seed into the URL, if you want to.</li>
</ul>
<p>
Puzzles can be generated from Python code: these puzzles can use a random number generator if they want.
The seed is used to create these random numbers.
</p>
<p>
We like to make a new seed for every contest,
and re-use that seed whenever we regenerate a category during an event
(say to fix a bug).
By using the same seed,
we make sure that all the dynamically-generated puzzles have the same values
in any new packages we build.
</p>
</body>
</html>
""".format(seed=seed)
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.end_headers() self.end_headers()
self.wfile.write("Your browser was supposed to redirect you to <a href=\"%s/\">here</a>." % seed) self.wfile.write(body.encode('utf-8'))
endpoints.append((r"/", handle_index)) endpoints.append((r"/", handle_index))