Skip to content

Commit

Permalink
Merge pull request #12 from Coffee-fueled-deadlines/Development
Browse files Browse the repository at this point in the history
Development -> Main
  • Loading branch information
Coffee-fueled-deadlines committed Sep 6, 2024
2 parents 9d178dc + 98d7606 commit 8dff75e
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
87 changes: 87 additions & 0 deletions PyDnD/Player.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

# Built-in/Generic Imports
import json
from uuid import uuid4
import warnings

Expand Down Expand Up @@ -469,6 +470,92 @@ def levelDown(self):
"""This will be removed in version 1.1.0"""
self.leveling_system.levelDown()

# Serialization/Deserialization
# Json
def serialize_to_json(self, filepath):
"""
Serializes the Player object, including the inventory, into a JSON file.
Args:
filepath (str): The file path where the JSON will be saved.
"""
player_data = {
'uid': str(self.uid),
'name': self.name,
'age': self.age,
'gender': self.gender,
'description': self.description,
'biography': self.biography,
'alignment': self.alignment,
'level': self.level,
'experience': self.experience,
'nextLvlExperience': self.nextLvlExperience,
'wealth': self.wealth,
'strength': self.strength,
'dexterity': self.dexterity,
'constitution': self.constitution,
'wisdom': self.wisdom,
'intelligence': self.intelligence,
'charisma': self.charisma,
'hp': self.hp,
'mp': self.mp,
'skillpoints': self.skillpoints,
'featpoints': self.featpoints,
'inventory': self.inventory.items, # Assuming inventory is a list of items
}

with open(filepath, 'w') as json_file:
json.dump(player_data, json_file, indent=4)

print(f"Player data serialized to {filepath}")

@staticmethod
def deserialize_from_json(filepath):
"""
Deserializes the Player object from a JSON file.
Args:
filepath (str): The file path where the JSON is stored.
Returns:
Player: The reconstructed Player object.
"""
with open(filepath, 'r') as json_file:
player_data = json.load(json_file)

# Create a new Player object and populate its fields
player = Player(
name=player_data.get('name'),
age=player_data.get('age'),
gender=player_data.get('gender'),
alignment=player_data.get('alignment'),
description=player_data.get('description'),
biography=player_data.get('biography'),
level=player_data.get('level'),
wealth=player_data.get('wealth'),
strength=player_data.get('strength'),
dexterity=player_data.get('dexterity'),
constitution=player_data.get('constitution'),
wisdom=player_data.get('wisdom'),
intelligence=player_data.get('intelligence'),
charisma=player_data.get('charisma'),
hp=player_data.get('hp'),
mp=player_data.get('mp'),
inventory_size=10 # Set a default or modify based on the data if needed
)

# Set experience and skill/feat points
player._experience = player_data.get('experience')
player.skillpoints = player_data.get('skillpoints')
player.featpoints = player_data.get('featpoints')

# Reconstruct the inventory
for item in player_data.get('inventory', []):
player.add_item_to_inventory(item)

print(f"Player data deserialized from {filepath}")
return player

# Static Helper Methods
@staticmethod
def _validate_string(value, name):
Expand Down
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ print(f"Items in inventory: {theBagMan.get_inventory()}") # Bye bye

`Roll` and `Dice` are two new Modules in the PyDnD package. The `Roll` module makes use of the `Dice` module, meaning that generally you won't or shouldn't interact directly with the `Dice` module itself. Below are some examples of how to roll different types of dice.

Examples
### Examples
```python
from PyDnD import Roll

Expand All @@ -235,4 +235,43 @@ print( f"Strength Stat is: {strength_stat}" )
# Lets tell the roller
strength_stat = Roll.roll(num_dice=4, sides=6, drop_lowest=1, return_rolls=True)
print( f"Strength Stat is: {strength_stat[0]} and the rolls where: {strength_stat[1]}" )
```
***

## Serialization/Deserialization (JSON)

PyDnD now supports JSON Serialization and Deserialization (This means that you can easily export your character to and from a .json file). Below are some examples of how to do Serialize a character:

### Examples
```python
# Example of JSON Serialization

from PyDnD import Player

# Lets make a new Character
newPlayer = Player(name='Thor Odinson', age='34', gender='Male', description='Looks like a pirate angel', biography='Born on Asgard, God of Thunder')

# Lets display some info
print( "Name:" + newPlayer.name )
print( "Age: " +newPlayer.age)
print( "Gender:" + newPlayer.gender )
print( "Description: " + newPlayer.description )
print( "Biography: " + newPlayer.biography )

# Lets save this character to a .json file
newPlayer.serialize_to_json("./path/to/newPlayer.json")
```

Below is an example of deserialization from that same file:

```python
# Lets load our player from json
newPlayer = Player().deserialize_from_json("./path/to/newPlayer.json")

# Lets display some info
print( "Name:" + newPlayer.name )
print( "Age: " +newPlayer.age)
print( "Gender:" + newPlayer.gender )
print( "Description: " + newPlayer.description )
print( "Biography: " + newPlayer.biography )
```

0 comments on commit 8dff75e

Please sign in to comment.