58 lines
2.3 KiB
Python
58 lines
2.3 KiB
Python
#!/usr/bin/python3
|
|
import zlib
|
|
from main import *
|
|
from adventurers import *
|
|
from flask import Flask, render_template, redirect, request, make_response
|
|
from flask_session import Session
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Configure session
|
|
app.config["SESSION_PERMANENT"] = False
|
|
app.config["SESSION_TYPE"] = "filesystem"
|
|
Session(app)
|
|
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return render_template("index.html")
|
|
|
|
@app.route('/party', methods = ["GET","POST"])
|
|
def party():
|
|
if request.method == "GET":
|
|
count = request.args.get("count", default=4, type=int)
|
|
level = request.args.get("level", default=1, type=int)
|
|
adv_party = returnParty(count, level)
|
|
resp = make_response(render_template("party_sheet.html", adv_party=adv_party, count=count, level=level))
|
|
# make a cookie for each character
|
|
# this took a while to figure out, json alone was too large, b64 encoded json was too large, but it turns out you can compress json as a cookie
|
|
for character in adv_party.adventurers:
|
|
cookie_string = str(character.id)
|
|
char_json = json.dumps(character.get_json())
|
|
compressed = zlib.compress(char_json.encode())
|
|
cookie_data = base64.urlsafe_b64encode(compressed).decode()
|
|
resp.set_cookie(cookie_string, cookie_data)
|
|
return resp
|
|
# return render_template("party_sheet.html", adv_party=adv_party, count=count, level=level)
|
|
|
|
@app.route('/character', methods = ["GET","POST"])
|
|
def characters():
|
|
if request.method == "GET":
|
|
c_id = str(request.args.get("id", default=0, type=int))
|
|
cookie_encoded = request.cookies[c_id]
|
|
cookie_compressed = base64.urlsafe_b64decode(cookie_encoded)
|
|
character_dict = json.loads(zlib.decompress(cookie_compressed).decode())
|
|
chosen_class = character_dict['player_class']
|
|
c_id = character_dict['id']
|
|
level = character_dict['level']
|
|
adv_dict = Adventurer.get_subclass_dict()
|
|
chosen_class = adv_dict[chosen_class]
|
|
# note, i wonder if i can find a way to use the dict in a one liner, like from hw1
|
|
# makes a new character of the correct player class
|
|
new_char = chosen_class(c_id=c_id, level=level)
|
|
# loads in all the values from the dict
|
|
for k, v in character_dict.items():
|
|
# https://realpython.com/ref/builtin-functions/setattr/
|
|
setattr(new_char,k, v)
|
|
return render_template("character.html", character=new_char)
|