1
0
Fork 0

rename dumps to logs

This commit is contained in:
Lili (Tlapka) 2021-03-22 17:05:00 +01:00
parent 43ebb0795d
commit 6eec74d007
2 changed files with 18 additions and 16 deletions

View File

@ -149,14 +149,15 @@ class Game:
def abort_game(self): def abort_game(self):
"""Abort game and reset attributes to their default state. """Abort game and reset attributes to their default state.
If a 'dumps' subfolder exists, also dumps the current game log If a /logs subfolder exists, also logs the current game log
to a file before aborting.""" to a file before aborting."""
try: try:
if os.path.isdir("dumps"): if os.path.isdir("logs"):
self.dump_to_file("dumps/aborted_game" + str(time.time())) self.log_to_file("logs/aborted_game" + str(time.time()))
except OSError as err: except OSError as err:
# print("Failed to save game log.") # print("Failed to save game log.")
# print("Error number: {}, Error text: {}".format(err.errno, err.strerror)) # print("Error number: {}, Error text: {}".format(err.errno, err.strerror))
pass
subprocess.run(["vagrant", "destroy", "-f"], cwd=self.game_directory) subprocess.run(["vagrant", "destroy", "-f"], cwd=self.game_directory)
self.load_times = [] self.load_times = []
self.solving_times = [] self.solving_times = []
@ -174,8 +175,8 @@ class Game:
with open(filename) as f: with open(filename) as f:
self.level_mapping = yaml.load(f, Loader=yaml.FullLoader) self.level_mapping = yaml.load(f, Loader=yaml.FullLoader)
def dump_to_file(self, filename): # raise OSError when file can't be opened def log_to_file(self, filename): # raise OSError when file can't be opened
"""Dump the current game state and logs into a YAML file.""" """Log the current game state and logs into a YAML file."""
with open(filename, 'w') as f: with open(filename, 'w') as f:
yaml.dump(self, f) yaml.dump(self, f)

View File

@ -15,7 +15,7 @@ def print_help():
print("(N)ext - advances to the next level. Asks for branch when applicable.") print("(N)ext - advances to the next level. Asks for branch when applicable.")
print("(F)inish - when on the last level, finishes the game and logs end time.") print("(F)inish - when on the last level, finishes the game and logs end time.")
print("(I)nfo - displays info about current run - Level number and name.") print("(I)nfo - displays info about current run - Level number and name.")
print("(D)ump - dump (save) the information about the game into a file.") print("(L)og - log (save) the information about the game into a file.")
print("Helper functions:") print("Helper functions:")
print("(H)elp - explains all commands on the screen.") print("(H)elp - explains all commands on the screen.")
print("(C)heck - checks if prerequisites to run the game are installed.") print("(C)heck - checks if prerequisites to run the game are installed.")
@ -92,19 +92,19 @@ def game_loop():
(N)ext - advances to the next level. Asks for branch when applicable. (N)ext - advances to the next level. Asks for branch when applicable.
(F)inish - when on the last level, finishes the game and logs end time. (F)inish - when on the last level, finishes the game and logs end time.
(I)nfo - displays info about current run - Level number and name. (I)nfo - displays info about current run - Level number and name.
(D)ump - dump (save) the information about the game into a file. (L)og - log (save) the information about the game into a file.
Helper functions: Helper functions:
(H)elp - explains all commands on the screen. (H)elp - explains all commands on the screen.
(C)heck - checks if prerequisites to run the game are installed. (C)heck - checks if prerequisites to run the game are installed.
""" """
if not os.path.isdir("dumps"): if not os.path.isdir("logs"):
try: try:
os.mkdir("dumps") os.mkdir("logs")
except FileExistsError: except FileExistsError:
pass #directory already exists, all OK pass #directory already exists, all OK
except OSError as err: except OSError as err:
print("Error encountered while creating the /dumps subfolder for save data:") print("Error encountered while creating the /logs subfolder for save data:")
print("Error number: {}, Error text: {}".format(err.errno, err.strerror)) print("Error number: {}, Error text: {}".format(err.errno, err.strerror))
print("If not fixed, saving game data might not be possible.") print("If not fixed, saving game data might not be possible.")
try: try:
@ -182,19 +182,20 @@ def game_loop():
print_help() print_help()
elif command in ("c", "check"): elif command in ("c", "check"):
check_prerequisites() check_prerequisites()
elif command in ("d", "dump"): elif command in ("l", "log"):
if os.path.exists("dumps/game_dump.yml"): if os.path.exists("logs/game_log.yml"):
print("It appears that there is already a game dump.") print("It appears that there is already a saved game log.")
print("Write 'yes' to overwrite it.") print("Write 'yes' to overwrite it.")
confirmation = input() confirmation = input()
confirmation = confirmation.lower() confirmation = confirmation.lower()
if (confirmation == "yes"): if (confirmation == "yes"):
print("Overwriting dump file...") print("Overwriting file...")
game.dump_to_file("dumps/game_dump.yml") game.log_to_file("logs/game_log.yml")
else: else:
print("File not overwritten.") print("File not overwritten.")
else: else:
game.dump_to_file("dumps/game_dump.yml") print("Writing file...")
game.log_to_file("logs/game_log.yml")
else: else:
print("Unknown command. Enter another command or try (H)elp.") print("Unknown command. Enter another command or try (H)elp.")