able to get compress JSON characters stored as cookies
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/python3
|
||||
import json
|
||||
import random
|
||||
from equipment import *
|
||||
from spells import *
|
||||
@@ -9,8 +10,9 @@ def roll_dice(count, sides):
|
||||
|
||||
# Player Character Classes
|
||||
class Adventurer:
|
||||
def __init__(self, level=1, attributes={}) -> None:
|
||||
def __init__(self, c_id: int, level=1, attributes={}) -> None:
|
||||
# using a get() method to pull an attribute else use a default value, https://python-academy.org/en/handbook/get
|
||||
self.id = c_id
|
||||
self.player_class = None
|
||||
self.level = level
|
||||
self.strength = attributes.get('strength', roll_dice(3,6))
|
||||
@@ -42,7 +44,12 @@ class Adventurer:
|
||||
def __str__(self):
|
||||
return f"{self.player_class}"
|
||||
|
||||
def character_sheet(self):
|
||||
def get_json(self):
|
||||
char_dict = self.__dict__
|
||||
char_json = json.dumps(char_dict)
|
||||
return char_dict
|
||||
|
||||
def vertical_sheet(self):
|
||||
sheet = []
|
||||
sheet.append('{0: <28}'.format(f"| {self.player_class.title()} - Level {self.level}"))
|
||||
for key, val in self.get_attributes().items():
|
||||
@@ -124,13 +131,12 @@ class Fighter(Adventurer):
|
||||
{ "level" : 13, "xp" : 720000, "hit-dice" : 9, "thac0" : 10, "saves" : { "death / poison" : 4, "wands" : 5, "paralysis / petrify" : 6, "breath attack" : 5, "spells / rods / staves" : 8 }},
|
||||
{ "level" : 14, "xp" : 840000, "hit-dice" : 9, "thac0" : 10, "saves" : { "death / poison" : 4, "wands" : 5, "paralysis / petrify" : 6, "breath attack" : 5, "spells / rods / staves" : 8 }}
|
||||
]
|
||||
def __init__(self, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, level, attributes)
|
||||
def __init__(self, c_id, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, c_id, level, attributes)
|
||||
self.player_class = "fighter"
|
||||
self.progression = Fighter.progression
|
||||
self.hp = roll_dice(self.level, 8)
|
||||
self.ac = armor[self.armor]
|
||||
self.vertical_sheet = self.character_sheet()
|
||||
|
||||
class MagicUser(Adventurer):
|
||||
prime_requisite = "intelligence"
|
||||
@@ -167,8 +173,8 @@ class MagicUser(Adventurer):
|
||||
13: { 1: 4, 2: 4, 3: 4, 4: 3, 5: 3, 6: 3 },
|
||||
14: { 1: 4, 2: 4, 3: 4, 4: 4, 5: 3, 6: 3 }
|
||||
}
|
||||
def __init__(self, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, level, attributes)
|
||||
def __init__(self,c_id, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, c_id, level, attributes)
|
||||
self.player_class = "magic user"
|
||||
self.progression = MagicUser.progression
|
||||
self.hp = roll_dice(self.level, 4)
|
||||
@@ -177,8 +183,6 @@ class MagicUser(Adventurer):
|
||||
self.weapons = [ list(filter(lambda d: 'silver dagger' in d['name'],weapons))[0], { "name" : "" } ]
|
||||
self.spells = MagicUser.spells[self.level]
|
||||
self.spell_book = self.select_spells()
|
||||
self.vertical_sheet = self.character_sheet()
|
||||
|
||||
|
||||
class Cleric(Adventurer):
|
||||
prime_requisite = "wisdom"
|
||||
@@ -215,8 +219,8 @@ class Cleric(Adventurer):
|
||||
13: { 1: 5, 2: 5, 3: 4, 4: 4, 5: 4 },
|
||||
14: { 1: 6, 2: 5, 3: 5, 4: 5, 5: 4 }
|
||||
}
|
||||
def __init__(self, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, level, attributes)
|
||||
def __init__(self,c_id, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, c_id, level, attributes)
|
||||
self.player_class = "cleric"
|
||||
self.progression = Cleric.progression
|
||||
self.hp = roll_dice(self.level, 6)
|
||||
@@ -228,7 +232,6 @@ class Cleric(Adventurer):
|
||||
self.weapons = [ random.choice(self.possible_melee_weapons), random.choice(self.possible_weapons) ]
|
||||
self.spells = Cleric.spells[self.level]
|
||||
self.spell_book = self.select_spells()
|
||||
self.vertical_sheet = self.character_sheet()
|
||||
|
||||
class Thief(Adventurer):
|
||||
prime_requisite = "dexterity"
|
||||
@@ -249,14 +252,13 @@ class Thief(Adventurer):
|
||||
{ "level" : 13, "xp" : 640000, "hit-dice" : 9, "thac0" : 12, "saves" : { "death / poison" : 8, "wands" : 9, "paralysis / petrify" : 7, "breath attack" : 10, "spells / rods / staves" : 8 }},
|
||||
{ "level" : 14, "xp" : 760000, "hit-dice" : 9, "thac0" : 12, "saves" : { "death / poison" : 8, "wands" : 9, "paralysis / petrify" : 7, "breath attack" : 10, "spells / rods / staves" : 8 }}
|
||||
]
|
||||
def __init__(self, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, level, attributes)
|
||||
def __init__(self,c_id, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, c_id, level, attributes)
|
||||
self.player_class = "thief"
|
||||
self.progression = Fighter.progression
|
||||
self.hp = roll_dice(self.level, 4)
|
||||
self.armor = random.choice(list(armor.keys()))
|
||||
self.ac = armor[self.armor]
|
||||
self.vertical_sheet = self.character_sheet()
|
||||
|
||||
class Dwarf(Adventurer):
|
||||
prime_requisite = "strength"
|
||||
@@ -275,14 +277,13 @@ class Dwarf(Adventurer):
|
||||
{ "level" : 11, "xp" : 530000, "hit-dice" : 9, "thac0" : 12, "saves" : { "death / poison" : 2, "wands" : 3, "paralysis / petrify" : 4, "breath attack" : 4, "spells / rods / staves" : 6 }},
|
||||
{ "level" : 12, "xp" : 660000, "hit-dice" : 9, "thac0" : 12, "saves" : { "death / poison" : 2, "wands" : 3, "paralysis / petrify" : 4, "breath attack" : 4, "spells / rods / staves" : 6 }}
|
||||
]
|
||||
def __init__(self, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, level, attributes)
|
||||
def __init__(self,c_id, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, c_id, level, attributes)
|
||||
self.player_class = "dwarf"
|
||||
self.progression = Dwarf.progression
|
||||
self.hp = roll_dice(self.level, 8)
|
||||
self.armor = random.choice(list(armor.keys()))
|
||||
self.ac = armor[self.armor]
|
||||
self.vertical_sheet = self.character_sheet()
|
||||
|
||||
class Elf(Adventurer):
|
||||
prime_requisite = "intellgence"
|
||||
@@ -311,8 +312,8 @@ class Elf(Adventurer):
|
||||
9: { 1: 3, 2: 3, 3: 3, 4: 2, 5: 1, 6: '-'},
|
||||
10: { 1: 3, 2: 3, 3: 3, 4: 3, 5: 2, 6: '-'}
|
||||
}
|
||||
def __init__(self, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, level, attributes)
|
||||
def __init__(self,c_id, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, c_id, level, attributes)
|
||||
self.player_class = "elf"
|
||||
self.progression = Elf.progression
|
||||
self.hp = roll_dice(self.level, 6)
|
||||
@@ -320,7 +321,6 @@ class Elf(Adventurer):
|
||||
self.ac = armor[self.armor]
|
||||
self.spells = Elf.spells[self.level]
|
||||
self.spell_book = self.select_spells()
|
||||
self.vertical_sheet = self.character_sheet()
|
||||
|
||||
class Halfling(Adventurer):
|
||||
prime_requisite = "dexterity"
|
||||
@@ -335,11 +335,10 @@ class Halfling(Adventurer):
|
||||
{ "level" : 7, "xp" : 64000, "hit-dice" : 7, "thac0" : 14, "saves" : { "death / poison" : 4, "wands" : 5, "paralysis / petrify" : 6, "breath attack" : 7, "spells / rods / staves" : 8 }},
|
||||
{ "level" : 8, "xp" : 120000, "hit-dice" : 8, "thac0" : 14, "saves" : { "death / poison" : 4, "wands" : 5, "paralysis / petrify" : 6, "breath attack" : 7, "spells / rods / staves" : 8 }},
|
||||
]
|
||||
def __init__(self, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, level, attributes)
|
||||
def __init__(self,c_id, level, attributes={}) -> None:
|
||||
Adventurer.__init__(self, c_id, level, attributes)
|
||||
self.player_class = "halfling"
|
||||
self.progression = Halfling.progression
|
||||
self.hp = roll_dice(self.level, 6)
|
||||
self.armor = random.choice(list(armor.keys()))
|
||||
self.ac = armor[self.armor]
|
||||
self.vertical_sheet = self.character_sheet()
|
||||
|
||||
34
app.py
34
app.py
@@ -1,10 +1,17 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import zlib
|
||||
from main import *
|
||||
from flask import Flask, render_template, redirect, request
|
||||
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")
|
||||
@@ -13,10 +20,23 @@ def index():
|
||||
def party():
|
||||
if request.method == "GET":
|
||||
count = request.args.get("count", default=4, type=int)
|
||||
adv_party = returnParty(count)
|
||||
return render_template("party_sheet.html", adv_party=adv_party, count=count)
|
||||
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')
|
||||
@app.route('/character', methods = ["GET","POST"])
|
||||
def characters():
|
||||
adv_party = returnParty(1)
|
||||
return render_template("party_sheet.html", adv_party=adv_party)
|
||||
if request.method == "GET":
|
||||
c_id = request.args.get("id", default=0, type=int)
|
||||
character = returnCharacter(c_id)
|
||||
return render_template("character.html", character=character)
|
||||
|
||||
BIN
flask_session/2029240f6d1128be89ddc32729463129
Normal file
BIN
flask_session/2029240f6d1128be89ddc32729463129
Normal file
Binary file not shown.
47
main.py
47
main.py
@@ -1,5 +1,7 @@
|
||||
#!/usr/bin/python3
|
||||
from adventurers import *
|
||||
import base64
|
||||
import json
|
||||
import random
|
||||
|
||||
# Player Class Selector
|
||||
@@ -40,19 +42,23 @@ class ClassSelector():
|
||||
return selected_class
|
||||
|
||||
class PartyGenerator():
|
||||
def __init__(self, party_size: int) -> None:
|
||||
def __init__(self, party_size: int, party_level: int) -> None:
|
||||
self.size = party_size
|
||||
self.size = party_size
|
||||
self.level = party_level
|
||||
self.adventurers = []
|
||||
self.adventurer_types = []
|
||||
|
||||
def gen_party(self):
|
||||
c_id = 0
|
||||
while len(self.adventurers) < self.size:
|
||||
new_player = Adventurer()
|
||||
c_id += 1
|
||||
new_player = Adventurer(c_id, self.level)
|
||||
attempts = 0
|
||||
while new_player.player_class not in self.adventurer_types:
|
||||
attempts += 1
|
||||
selected_class = ClassSelector(new_player).selection()
|
||||
new_player = selected_class(new_player.level, new_player.get_attributes())
|
||||
new_player = selected_class(new_player.id, new_player.level, new_player.get_attributes())
|
||||
# i couldnt randomly generate a scenario where a character couldn't be added, but it seems possible, so this is the hard cut off
|
||||
if (new_player.player_class not in self.adventurer_types) or (attempts > 10):
|
||||
self.adventurers.append(new_player)
|
||||
@@ -62,23 +68,44 @@ class PartyGenerator():
|
||||
sheet_string = ""
|
||||
character_sheets = []
|
||||
for c in self.adventurers:
|
||||
character_sheets.append(c.character_sheet())
|
||||
character_sheets.append(c.vertical_sheet())
|
||||
return character_sheets
|
||||
|
||||
def get_json(self):
|
||||
party_list = []
|
||||
for c in self.adventurers:
|
||||
party_list.append(c.get_json())
|
||||
party_json = json.dumps(party_list)
|
||||
return party_json
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.adventurers}"
|
||||
|
||||
def returnParty(party_size):
|
||||
new_party = PartyGenerator(party_size)
|
||||
def returnParty(party_size, party_level):
|
||||
# keep variables within expected ranges
|
||||
if party_size <= 0 or party_size > 5:
|
||||
party_size = 1
|
||||
if party_level < 1 or party_level > 5:
|
||||
party_level = 1
|
||||
# generate an aventuring party per size and level
|
||||
new_party = PartyGenerator(party_size, party_level)
|
||||
# for adventurers select classes
|
||||
new_party.gen_party()
|
||||
# return the created adventurer party
|
||||
return new_party
|
||||
|
||||
def returnCharacter(c_id):
|
||||
for adv in new_party.adventurers:
|
||||
if adv.id == c_id:
|
||||
return adv
|
||||
|
||||
def main():
|
||||
adventurer_party = returnParty(party_size=5)
|
||||
adventurer_party = returnParty(party_size=2, party_level=2)
|
||||
for adv in adventurer_party.adventurers:
|
||||
for line in adv.vertical_sheet:
|
||||
print(f"{line}")
|
||||
print()
|
||||
print(adv.get_json())
|
||||
# for line in adv.vertical_sheet:
|
||||
# print(f"{line}")
|
||||
# print()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
flash
|
||||
flask
|
||||
flask-session
|
||||
|
||||
@@ -2,5 +2,6 @@
|
||||
|
||||
magic_user_spells = {
|
||||
1 : [ "charm person", "detect magic", "floating disc", "hold portal", "light (darkness)", "magic missile", "protection from evil", "read languages", "shield", "sleep", "ventriloquism" ],
|
||||
2 : [ "continual light", "detect evil", "detect invisible", "ESP", "invisibility", "knock", "levitate", "locate object", "mirror image", "phantasmal force", "web", "wizard lock" ]
|
||||
2 : [ "continual light", "detect evil", "detect invisible", "ESP", "invisibility", "knock", "levitate", "locate object", "mirror image", "phantasmal force", "web", "wizard lock" ],
|
||||
3 : [ "clairvoyance", "dispel magic", "fire ball", "fly", "haste", "hold person", "infravision", "invisibility 10'", "lightning bolt", "protection from evil 10'", "protection from normal missiles", "water breathing" ]
|
||||
}
|
||||
|
||||
63
templates/character.html
Normal file
63
templates/character.html
Normal file
@@ -0,0 +1,63 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>D&D Characters</title>
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/bulma.min.css"
|
||||
>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<nav class="navbar" role="navigation" aria-label="main navigation">
|
||||
<div class="navbar-brand">
|
||||
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="navbarBasicExample">
|
||||
<span aria-hidden="true"></span>
|
||||
<span aria-hidden="true"></span>
|
||||
<span aria-hidden="true"></span>
|
||||
<span aria-hidden="true"></span>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div id="navbarBasicExample" class="navbar-menu">
|
||||
<div class="navbar-start">
|
||||
<a class="navbar-item">
|
||||
Characters
|
||||
</a>
|
||||
|
||||
<a class="navbar-item">
|
||||
VTT
|
||||
</a>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="card">
|
||||
<div class="container">
|
||||
<div class="content">
|
||||
<div class="card-content">
|
||||
<div class="content">
|
||||
<h2>Welcome to the Dungeon</h2>
|
||||
<div class="block has-text-centered">
|
||||
<div class="columns">
|
||||
<div class="column" >
|
||||
<pre>{{character.vertical_sheet | join("\n")}}</pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<br>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
@@ -43,35 +43,52 @@
|
||||
<div class="card-content">
|
||||
<div class="content">
|
||||
<h2>Welcome to the Dungeon</h2>
|
||||
<p>How many party members?</p>
|
||||
<div class="block">
|
||||
<div class="columns">
|
||||
<div class="column" >
|
||||
<p>How many party members? {{count}}</p>
|
||||
<div class="buttons">
|
||||
{%for i in range(2,6)%}
|
||||
{% if count|int() == i|int() %}
|
||||
<a class="button is-primary" href="{{ '/party?count=%s'%i }}">{{i}}</a>
|
||||
<a class="button is-primary" href="{{ '/party?count={}&level={}'.format(i,level) }}">{{i}}</a>
|
||||
{% else %}
|
||||
<a class="button" href="{{ '/party?count=%s'%i }}">{{i}}</a>
|
||||
<a class="button" href="{{ '/party?count={}&level={}'.format(i,level) }}">{{i}}</a>
|
||||
{% endif %}
|
||||
{%endfor%}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="column" >
|
||||
<p>What level for the party? {{level}} </p>
|
||||
<div class="buttons">
|
||||
{%for j in range(1,6)%}
|
||||
{% if level|int() == j|int() %}
|
||||
<a class="button is-primary" href="{{ '/party?count={}&level={}'.format(count,j) }}">{{j}}</a>
|
||||
{% else %}
|
||||
<a class="button" href="{{ '/party?count={}&level={}'.format(count,j) }}">{{j}}</a>
|
||||
{% endif %}
|
||||
{%endfor%}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="block has-text-centered">
|
||||
<div class="columns">
|
||||
{%for character in adv_party.adventurers%}
|
||||
<div class="column" >
|
||||
<div class="block has-text-centered">
|
||||
<button class="button is-focused">Select this {{character.player_class.title()}}</button>
|
||||
<a class="button is-focused" href="{{ '/character?id={}'.format(character.id)}}">Select this {{character.player_class.title()}}</a>
|
||||
</div>
|
||||
<pre>{{character.vertical_sheet | join("\n")}}</pre>
|
||||
<pre>{{character.vertical_sheet() | join("\n")}}</pre>
|
||||
</div>
|
||||
{%endfor%}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<br>
|
||||
<div class="block has-text-centered">
|
||||
<button class="button is-primary is-fillwidth">Select this adventuring party!</button>
|
||||
<div>
|
||||
<br>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user