add load and save functions to classes
move files update character creater to allowing to stack classes
This commit is contained in:
+29
-5
@@ -1,9 +1,10 @@
|
||||
import logging
|
||||
|
||||
from classes.Player import Player
|
||||
import my_helpers.myPickle as my_pickle
|
||||
from classes.Character.Player import Player
|
||||
|
||||
from classes.MapObject.Home import Home
|
||||
|
||||
import logging, os
|
||||
|
||||
LOGGING = logging.getLogger(__name__)
|
||||
class SavedState:
|
||||
def __init__(self, story_position:str="The Beginning"):
|
||||
@@ -16,7 +17,7 @@ class SavedState:
|
||||
}
|
||||
|
||||
def add_player(self, player:dict):
|
||||
self.player = Player(player["name"], player["type"], player["species"])
|
||||
self.player = Player(player["name"], player["type"], player["species"], player["combineble_species"])
|
||||
|
||||
def get_home(self):
|
||||
return self.home
|
||||
@@ -34,4 +35,27 @@ class SavedState:
|
||||
return self.story["place"]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.__class__.__name__} | Home: {self.home}\nStory: {self.story}"
|
||||
return f"{self.__class__.__name__} | Home: {self.home}\nStory: {self.story}\n\n{self.player}"
|
||||
|
||||
def __repr__(self):
|
||||
return str(vars(self))
|
||||
|
||||
@classmethod
|
||||
def load_from_file(cls, file_options):
|
||||
LOGGING.debug(f"Loading from file: {file_options}")
|
||||
if os.path.exists(file_options["path"]):
|
||||
with open(file_options["path"], "rb") as f:
|
||||
data = my_pickle.load(f, file_options['compression'], None)
|
||||
|
||||
new_class = cls()
|
||||
new_class.__dict__.update(data)
|
||||
LOGGING.debug(f"Loaded {data}", {"loaded_data": new_class})
|
||||
return new_class
|
||||
else:
|
||||
raise FileNotFoundError(f"Could not load map file: {file_options['path']}")
|
||||
|
||||
def save_to_file(self, file_options):
|
||||
LOGGING.debug(f"Saving Object {self.__dict__} To file: {file_options}")
|
||||
os.makedirs(os.path.dirname(file_options["path"]), exist_ok=True)
|
||||
with open(file_options["path"], "wb") as f:
|
||||
my_pickle.dump(self.__dict__, f, file_options['compression'], None)
|
||||
|
||||
Reference in New Issue
Block a user