✨
This commit is contained in:
142
main.py
142
main.py
@@ -1,72 +1,72 @@
|
||||
import flask
|
||||
import string
|
||||
import json
|
||||
|
||||
BASE64 = string.digits + string.ascii_letters + "-_"
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
with open("questions.json", "r", encoding="utf-8") as file_pointer:
|
||||
QUESTIONS = json.load(file_pointer)
|
||||
|
||||
def encode_state(game_encoding):
|
||||
characters = [game_encoding[i*6:i*6+6] for i in range(0,6)]
|
||||
return ''.join(
|
||||
[BASE64[sum([2**j for j in range(6) if not i[j]])] for i in characters]
|
||||
)
|
||||
|
||||
|
||||
def decode_state(game_state):
|
||||
bit_string = ''.join([format(BASE64.index(s), '06b')[::-1] for s in game_state])
|
||||
return [x != "1" for x in bit_string]
|
||||
|
||||
@app.route('/')
|
||||
def main_page():
|
||||
game_encoding = flask.request.args.get("game_state")
|
||||
if game_encoding is None:
|
||||
game_encoding = "000000"
|
||||
game_state = decode_state(game_encoding)
|
||||
return flask.render_template("main_board.html", categories=QUESTIONS["categories"], game_state=game_encoding, draw_cells=game_state)
|
||||
|
||||
@app.route('/question')
|
||||
def question():
|
||||
game_encoding = flask.request.args.get("game_state")
|
||||
question = flask.request.args.get("question")
|
||||
if game_encoding is None or question is None:
|
||||
return flask.redirect("/")
|
||||
|
||||
game_state = decode_state(game_encoding)
|
||||
game_state[int(question)] = False
|
||||
game_encoding = encode_state(game_state)
|
||||
question_text = QUESTIONS["questions"][int(question)%6][int(question)//6][0]
|
||||
|
||||
if question_text == "":
|
||||
return flask.redirect("/?game_state="+game_encoding)
|
||||
elif question_text == "$":
|
||||
return flask.render_template("question picture.html", question=int(question), game_state=game_encoding)
|
||||
|
||||
|
||||
return flask.render_template("question.html", question=int(question), question_text=question_text, game_state=game_encoding)
|
||||
|
||||
@app.route('/answer')
|
||||
def answer():
|
||||
game_encoding = flask.request.args.get("game_state")
|
||||
question = flask.request.args.get("question")
|
||||
if game_encoding is None or question is None:
|
||||
return flask.redirect("/")
|
||||
|
||||
answer_text = QUESTIONS["questions"][int(question)%6][int(question)//6][1]
|
||||
|
||||
if answer_text == "":
|
||||
return flask.redirect("/?game_state="+game_encoding)
|
||||
elif answer_text == "$":
|
||||
return flask.render_template("answer picture.html", question=int(question), game_state=game_encoding)
|
||||
|
||||
return flask.render_template("answer.html", question=int(question), answer_text=answer_text, game_state=game_encoding)
|
||||
|
||||
@app.route('/static/<path:path>')
|
||||
def flag_image(path):
|
||||
return flask.send_from_directory("static", path)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import flask
|
||||
import string
|
||||
import json
|
||||
|
||||
BASE64 = string.digits + string.ascii_letters + "-_"
|
||||
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
with open("questions.json", "r", encoding="utf-8") as file_pointer:
|
||||
QUESTIONS = json.load(file_pointer)
|
||||
|
||||
def encode_state(game_encoding):
|
||||
characters = [game_encoding[i*6:i*6+6] for i in range(0,6)]
|
||||
return ''.join(
|
||||
[BASE64[sum([2**j for j in range(6) if not i[j]])] for i in characters]
|
||||
)
|
||||
|
||||
|
||||
def decode_state(game_state):
|
||||
bit_string = ''.join([format(BASE64.index(s), '06b')[::-1] for s in game_state])
|
||||
return [x != "1" for x in bit_string]
|
||||
|
||||
@app.route('/')
|
||||
def main_page():
|
||||
game_encoding = flask.request.args.get("game_state")
|
||||
if game_encoding is None:
|
||||
game_encoding = "000000"
|
||||
game_state = decode_state(game_encoding)
|
||||
return flask.render_template("main_board.html", categories=QUESTIONS["categories"], game_state=game_encoding, draw_cells=game_state)
|
||||
|
||||
@app.route('/question')
|
||||
def question():
|
||||
game_encoding = flask.request.args.get("game_state")
|
||||
question = flask.request.args.get("question")
|
||||
if game_encoding is None or question is None:
|
||||
return flask.redirect("/")
|
||||
|
||||
game_state = decode_state(game_encoding)
|
||||
game_state[int(question)] = False
|
||||
game_encoding = encode_state(game_state)
|
||||
question_text = QUESTIONS["questions"][int(question)%6][int(question)//6][0]
|
||||
|
||||
if question_text == "":
|
||||
return flask.redirect("/?game_state="+game_encoding)
|
||||
elif question_text == "$":
|
||||
return flask.render_template("question picture.html", question=int(question), game_state=game_encoding)
|
||||
|
||||
|
||||
return flask.render_template("question.html", question=int(question), question_text=question_text, game_state=game_encoding)
|
||||
|
||||
@app.route('/answer')
|
||||
def answer():
|
||||
game_encoding = flask.request.args.get("game_state")
|
||||
question = flask.request.args.get("question")
|
||||
if game_encoding is None or question is None:
|
||||
return flask.redirect("/")
|
||||
|
||||
answer_text = QUESTIONS["questions"][int(question)%6][int(question)//6][1]
|
||||
|
||||
if answer_text == "":
|
||||
return flask.redirect("/?game_state="+game_encoding)
|
||||
elif answer_text == "$":
|
||||
return flask.render_template("answer picture.html", question=int(question), game_state=game_encoding)
|
||||
|
||||
return flask.render_template("answer.html", question=int(question), answer_text=answer_text, game_state=game_encoding)
|
||||
|
||||
@app.route('/static/<path:path>')
|
||||
def flag_image(path):
|
||||
return flask.send_from_directory("static", path)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host='0.0.0.0', port=8001)
|
||||
Reference in New Issue
Block a user