Skip to content

Commit

Permalink
#16: Added a hacky approach to directory configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
ecdavis committed Sep 10, 2016
1 parent d245ff1 commit 54f6f3a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
50 changes: 42 additions & 8 deletions spacegame/config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
import os.path


UNIVERSE_PATH = os.path.abspath("data/universe/")
UNIVERSE_FILE = os.path.join(UNIVERSE_PATH, "universe.json")
STAR_SYSTEM_PATH = os.path.join(UNIVERSE_PATH, "star_systems")
CELESTIAL_PATH = os.path.join(UNIVERSE_PATH, "celestials")
USER_DIR_PATH = "data/users/"
USER_FILE_PATH = USER_DIR_PATH + "%s.user.json"
PLAYER_DIR_PATH = "data/players/"
PLAYER_FILE_PATH = PLAYER_DIR_PATH + "%s.mobile.json"
class _PathConfig(object):
def __init__(self, data_path):
self.data_path = os.path.abspath(data_path)

@property
def data_dir(self):
return self.data_path

@property
def universe_dir(self):
return os.path.join(self.data_dir, "universe")

@property
def universe_file(self):
return os.path.join(self.universe_dir, "universe.json")

@property
def star_system_dir(self):
return os.path.join(self.universe_dir, "star_systems")

@property
def celestial_dir(self):
return os.path.join(self.universe_dir, "celestials")

@property
def user_dir(self):
return os.path.join(self.data_dir, "users")

@property
def user_file_pattern(self):
return os.path.join(self.user_dir, "%s.user.json")

@property
def player_dir(self):
return os.path.join(self.data_dir, "players")

@property
def player_file_pattern(self):
return os.path.join(self.player_dir, "%s.mobile.json")


path = _PathConfig("data")
16 changes: 8 additions & 8 deletions spacegame/core/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,32 @@ def save_data(self):


def user_exists(user_uuid):
return os.path.exists(config.USER_FILE_PATH % util.uuid_to_base32(user_uuid))
return os.path.exists(config.path.user_file_pattern % util.uuid_to_base32(user_uuid))


def load_user(user_uuid):
return storage.load_file(config.USER_FILE_PATH % util.uuid_to_base32(user_uuid), User)
return storage.load_file(config.path.user_file_pattern % util.uuid_to_base32(user_uuid), User)


def save_user(user):
storage.save_object(config.USER_FILE_PATH % util.uuid_to_base32(user.uuid), user)
storage.save_object(config.path.user_file_pattern % util.uuid_to_base32(user.uuid), user)


def player_name_exists(player_name):
for filename in os.listdir(config.PLAYER_DIR_PATH):
p = storage.load_file(os.path.join(config.PLAYER_DIR_PATH, filename), mobile.Mobile)
for filename in os.listdir(config.path.player_dir):
p = storage.load_file(os.path.join(config.path.player_dir, filename), mobile.Mobile)
if p.name == player_name:
return True
return False


def player_exists(player_uuid):
return os.path.exists(config.PLAYER_FILE_PATH % util.uuid_to_base32(player_uuid))
return os.path.exists(config.path.player_file_pattern % util.uuid_to_base32(player_uuid))


def load_player(player_uuid):
return storage.load_file(config.PLAYER_FILE_PATH % util.uuid_to_base32(player_uuid), mobile.Mobile)
return storage.load_file(config.path.player_file_pattern % util.uuid_to_base32(player_uuid), mobile.Mobile)


def save_player(player):
storage.save_object(config.PLAYER_FILE_PATH % util.uuid_to_base32(player.uuid), player)
storage.save_object(config.path.player_file_pattern % util.uuid_to_base32(player.uuid), player)
24 changes: 12 additions & 12 deletions spacegame/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

def check_and_create_directories():
ensure_dirs_exist = [
config.UNIVERSE_PATH,
config.STAR_SYSTEM_PATH,
config.CELESTIAL_PATH,
config.USER_DIR_PATH,
config.PLAYER_DIR_PATH
config.path.universe_dir,
config.path.star_system_dir,
config.path.celestial_dir,
config.path.user_dir,
config.path.player_dir
]
for d in ensure_dirs_exist:
directory = os.path.abspath(d)
Expand All @@ -22,7 +22,7 @@ def check_and_create_directories():


def check_and_create_universe():
if os.path.exists(config.UNIVERSE_FILE):
if os.path.exists(config.path.universe_file):
logging.debug("Universe exists, skipping creation.")
return
u = universe.Universe()
Expand All @@ -39,18 +39,18 @@ def check_and_create_universe():


def load_universe():
u = persist.load_universe(config.UNIVERSE_FILE)
for s in persist.load_star_systems(config.STAR_SYSTEM_PATH):
u = persist.load_universe(config.path.universe_file)
for s in persist.load_star_systems(config.path.star_system_dir):
u.add_star_system(s)
for c in persist.load_celestials(config.CELESTIAL_PATH):
for c in persist.load_celestials(config.path.celestial_dir):
u.add_celestial(c)
return u


def save_universe(u):
persist.save_celestials(config.CELESTIAL_PATH, u)
persist.save_star_systems(config.STAR_SYSTEM_PATH, u)
persist.save_universe(config.UNIVERSE_FILE, u)
persist.save_celestials(config.path.celestial_dir, u)
persist.save_star_systems(config.path.star_system_dir, u)
persist.save_universe(config.path.universe_file, u)


def main():
Expand Down

0 comments on commit 54f6f3a

Please sign in to comment.