diff --git a/__pycache__/adventurers.cpython-312.pyc b/__pycache__/adventurers.cpython-312.pyc index 61f9698..494437d 100644 Binary files a/__pycache__/adventurers.cpython-312.pyc and b/__pycache__/adventurers.cpython-312.pyc differ diff --git a/__pycache__/app.cpython-312.pyc b/__pycache__/app.cpython-312.pyc index b4b5c63..7689690 100644 Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ diff --git a/__pycache__/main.cpython-312.pyc b/__pycache__/main.cpython-312.pyc index fbc2f36..cffa1dc 100644 Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ diff --git a/adventurers.py b/adventurers.py index 48fb9c4..b059c14 100755 --- a/adventurers.py +++ b/adventurers.py @@ -10,9 +10,9 @@ def roll_dice(count, sides): # Player Character Classes class Adventurer: - def __init__(self, c_id: int, level=1, attributes={}) -> None: + def __init__(self, c_id: str, 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.identifier = c_id self.player_class = None self.level = level self.strength = attributes.get('strength', roll_dice(3,6)) @@ -117,6 +117,10 @@ class Adventurer: spell_book.append(random_spell) return spell_book + def set_level(self, new_level): + self.level = new_level + + class Fighter(Adventurer): adv_class = "fighter" diff --git a/app.py b/app.py index e9ae2f1..f439c63 100644 --- a/app.py +++ b/app.py @@ -19,39 +19,73 @@ def index(): @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) + stored_adv_party = [] + stored_count = None + stored_level = None + if request.method == "GET": + count = request.args.get("count", default=4, type=int) + level = request.args.get("level", default=1, type=int) + cache = request.args.get("cache", default='true', type=str) + adv_party = returnParty(count, level) + # check for cookies present + if request.cookies: + print('there are cookies') + stored_count = len(request.cookies) + #print(request.cookies) + for c in request.cookies: + if c.startswith('adv'): + cookie_encoded = request.cookies[c] + cookie_compressed = base64.urlsafe_b64decode(cookie_encoded) + character_dict = json.loads(zlib.decompress(cookie_compressed).decode()) + new_char = createCharacterWithDict(character_dict) + stored_level = new_char.level + stored_adv_party.append(new_char) + # if request arguments match the adventure party count & level stored in the cookies, just use the cookies + if cache == 'true': + print('caching is true') + if level != stored_level: + print(f'change party level to {level}') + for adv in stored_adv_party: + print('...editing') + adv.set_level(level) + print(adv.level) + if count == stored_count: + print('counts and levels are the same') + adv_party.set_party(stored_adv_party, count, level) + if count > stored_count: + print('extend the party!') + extension = count - stored_count + more_party_members = returnParty(extension,level).adventurers + extended_party = stored_adv_party + more_party_members + adv_party.set_party(extended_party, count, level) + if count < stored_count: + print('reduce the party!') + reduction = count - stored_count + reduced_party = stored_adv_party[:reduction] + adv_party.set_party(reduced_party, count, level) + + + # generate page + response = make_response(render_template("party_sheet.html", adv_party=adv_party, count=count, level=level, cache=cache)) + # make a cookie for each character + # this took a while to figure out, json 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 = character.identifier + char_json = json.dumps(character.get_json()) + compressed = zlib.compress(char_json.encode()) + cookie_data = base64.urlsafe_b64encode(compressed).decode() + response.set_cookie(cookie_string, cookie_data) + return response @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) + if request.method == "GET": + c_id = str(request.args.get("id", default=0, type=str)) + if request.cookies and c_id: + cookie_encoded = request.cookies[c_id] + cookie_compressed = base64.urlsafe_b64decode(cookie_encoded) + character_dict = json.loads(zlib.decompress(cookie_compressed).decode()) + new_char = createCharacterWithDict(character_dict) + else: + new_char = Adventurer(1,1) + return render_template("character.html", character=new_char) diff --git a/main.py b/main.py index 76a92a5..e05c87d 100755 --- a/main.py +++ b/main.py @@ -43,27 +43,33 @@ class ClassSelector(): class PartyGenerator(): 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 + c_id = 1 while len(self.adventurers) < self.size: - c_id += 1 - new_player = Adventurer(c_id, self.level) + identifier = "adv-" + str(c_id) + new_player = Adventurer(identifier, 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.id, new_player.level, new_player.get_attributes()) + new_player = selected_class(new_player.identifier, 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): + c_id += 1 self.adventurers.append(new_player) self.adventurer_types.append(new_player.player_class) + def set_party(self, adventurers, count, level): + self.size = count + self.level = level + self.adventurers = adventurers + self.adventurer_types = [] + def get_character_sheets(self): sheet_string = "" character_sheets = [] @@ -94,22 +100,31 @@ def returnParty(party_size, party_level): # return the created adventurer party return new_party -def returnCharacter(c_id): +def returnCharacter(identifer): for adv in new_party.adventurers: - if adv.id == c_id: + if adv.identifier == identifier: return adv +def createCharacterWithDict(character_dict): + chosen_class = character_dict['player_class'] + c_id = character_dict['identifier'] + level = character_dict['level'] + adv_dict = Adventurer.get_subclass_dict() + chosen_class = adv_dict[chosen_class] + new_char = chosen_class(c_id=c_id, level=level) + for k, v in character_dict.items(): + setattr(new_char,k, v) + return new_char + + def main(): adventurer_party = returnParty(party_size=2, party_level=2) for adv in adventurer_party.adventurers: print(adv.adv_class) adv_dict = Adventurer.get_subclass_dict() test_class = adv_dict['fighter'] - new_char = test_class(c_id=1,level=1) + new_char = test_class(c_id="adv-1",level=1) print(new_char.adv_class) -# for line in adv.vertical_sheet: -# print(f"{line}") -# print() if __name__ == "__main__": main() diff --git a/templates/character.html b/templates/character.html index 048d536..2042ee2 100644 --- a/templates/character.html +++ b/templates/character.html @@ -10,31 +10,26 @@ -
diff --git a/templates/index.html b/templates/index.html index 197cf6e..158f3d1 100644 --- a/templates/index.html +++ b/templates/index.html @@ -23,7 +23,7 @@