70 lines
2.0 KiB
Python
70 lines
2.0 KiB
Python
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"):
|
|
self.home = Home()
|
|
|
|
self.story = {
|
|
"chapter": story_position,
|
|
"story_moves": 0,
|
|
"place": None
|
|
}
|
|
|
|
# Objects
|
|
def add_player(self, player:dict):
|
|
self.player = Player(player["name"], player["type"], player["species"], player["combineble_species"])
|
|
|
|
def get_home(self):
|
|
return self.home
|
|
|
|
def get_player(self):
|
|
return self.player
|
|
|
|
def get_player_species(self, species:str):
|
|
return self.player.get_species(species)
|
|
|
|
def get_player_combineble(self, species:str):
|
|
return self.player.get_combineble_species(species)
|
|
|
|
# Functions
|
|
def get_chapter(self):
|
|
return self.story["chapter"]
|
|
|
|
def get_story_moves(self):
|
|
return self.story["story_moves"]
|
|
|
|
def get_place(self):
|
|
return self.story["place"]
|
|
|
|
def __str__(self):
|
|
return f"{self.__class__.__name__}\n Home: {self.home}\n Story: {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(f'{file_options["path"]}/{file_options["name"]}', "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)
|