This repository has been archived on 2026-06-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
The-Adventure-Game/classes/Map.py
T

53 lines
1.7 KiB
Python

import my_helpers.myPickle as my_pickle
from json import load as json_load
import logging, os
LOGGING = logging.getLogger(__name__)
class Map:
def __init__(self, path):
self.path = path
self.story = {}
def make_file(self, story:dict={}):
for file in os.listdir(self.path):
if file.endswith("json"):
with open(f"{self.path}/{file}", "r") as f:
filename = os.path.splitext(os.path.basename(file))[0]
story[filename] = json_load(f)
self.story = story
def __str__(self):
return f"{self.__class__.__name__} | Story: {self.story}"
def __getitem__(self, key):
return self.story[key]
@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(file_options["path"])
new_class.__dict__.update(data.__dict__)
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} 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, f, file_options['compression'], None)
def by_the_place(self, is_by_a_place, chapter, moves):
LOGGING.debug(is_by_a_place)
if is_by_a_place:
return self[chapter]["Places"][is_by_a_place]
return self[chapter]["World"][moves]