finish hint giving, add better logs
This commit is contained in:
parent
542860fede
commit
678d59f856
|
@ -177,7 +177,7 @@ class Game:
|
||||||
|
|
||||||
def log_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
|
||||||
"""Log 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, 'a') as f:
|
||||||
yaml.dump(self, f)
|
yaml.dump(self, f)
|
||||||
|
|
||||||
# # # METHODS THAT OUTPUT INTO STDOUT # # #
|
# # # METHODS THAT OUTPUT INTO STDOUT # # #
|
||||||
|
|
|
@ -32,4 +32,8 @@ class HintGiver:
|
||||||
def is_hint_name(self, level, branch, hint_name):
|
def is_hint_name(self, level, branch, hint_name):
|
||||||
if hint_name in self.hints[level][level+branch]:
|
if hint_name in self.hints[level][level+branch]:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def log_to_file(self, filename):
|
||||||
|
with open(filename, 'a') as f:
|
||||||
|
yaml.dump(self, f)
|
51
assistant.py
51
assistant.py
|
@ -79,6 +79,15 @@ def check_prerequisites():
|
||||||
else:
|
else:
|
||||||
print("NOK, VirtualBox version lower than 6 detected.")
|
print("NOK, VirtualBox version lower than 6 detected.")
|
||||||
|
|
||||||
|
def write_log(filename, game, hint_giver):
|
||||||
|
try:
|
||||||
|
game.log_to_file(filename)
|
||||||
|
hint_giver.log_to_file(filename)
|
||||||
|
except OSError:
|
||||||
|
print("Error encountered while saving game data:")
|
||||||
|
print("Error number: {}, Error text: {}".format(err.errno, err.strerror))
|
||||||
|
print("Double check that location {} for the file exists and can be written to.".format(filename))
|
||||||
|
|
||||||
def game_loop():
|
def game_loop():
|
||||||
"""Interactively assist the player with playing the game.
|
"""Interactively assist the player with playing the game.
|
||||||
|
|
||||||
|
@ -163,7 +172,7 @@ def game_loop():
|
||||||
print("This is the last level of the game.")
|
print("This is the last level of the game.")
|
||||||
else:
|
else:
|
||||||
print("No next levels found -- you finished the game!")
|
print("No next levels found -- you finished the game!")
|
||||||
print("Make sure to run (F)inish and (D)ump before exiting.")
|
print("Make sure to run (F)inish and (L)og your progress before exiting.")
|
||||||
except NoLevelFoundError as err:
|
except NoLevelFoundError as err:
|
||||||
print("Error encountered: {}".format(err))
|
print("Error encountered: {}".format(err))
|
||||||
|
|
||||||
|
@ -191,23 +200,39 @@ def game_loop():
|
||||||
confirmation = confirmation.lower()
|
confirmation = confirmation.lower()
|
||||||
if (confirmation == "yes"):
|
if (confirmation == "yes"):
|
||||||
print("Overwriting file...")
|
print("Overwriting file...")
|
||||||
game.log_to_file("logs/game_log.yml")
|
write_log("logs/game_log.yml", game, hint_giver)
|
||||||
else:
|
else:
|
||||||
print("File not overwritten.")
|
print("File not overwritten.")
|
||||||
else:
|
else:
|
||||||
print("Writing file...")
|
print("Writing file...")
|
||||||
game.log_to_file("logs/game_log.yml")
|
write_log("logs/game_log.yml", game, hint_giver)
|
||||||
elif command in ("t", "hint"):
|
elif command in ("t", "hint"):
|
||||||
if hint_giver.show_taken_hints("level" + str(game.level), game.branch) != []:
|
if not game.game_in_progress:
|
||||||
print("Hints taken in current level:")
|
print("Game not started, can't give hints.")
|
||||||
for hint in hint_giver.show_taken_hints("level" + str(game.level), game.branch):
|
else:
|
||||||
print("{}: {}".format(hint, hint_giver.take_hint(level, branch, hint)))
|
if hint_giver.show_taken_hints("level" + str(game.level), game.branch) != []:
|
||||||
print("Choose which hint to take:")
|
print("Hints taken in current level:")
|
||||||
print("0: (cancel, take no hint)")
|
for hint in hint_giver.show_taken_hints("level" + str(game.level), game.branch):
|
||||||
i = 1
|
print("{}: {}".format(hint, hint_giver.take_hint("level" + str(game.level), game.branch, hint)))
|
||||||
for hint in hint_giver.show_possible_hints("level" + str(game.level), game.branch):
|
print("Choose which hint to take:")
|
||||||
print("{}: {}".format(i, hint))
|
print("0: (cancel, take no hint)")
|
||||||
i += 1
|
i = 1
|
||||||
|
possible_hints = hint_giver.show_possible_hints("level" + str(game.level), game.branch)
|
||||||
|
for hint in possible_hints:
|
||||||
|
print("{}: {}".format(i, hint))
|
||||||
|
i += 1
|
||||||
|
hint = input()
|
||||||
|
try:
|
||||||
|
hint = int(hint)
|
||||||
|
if hint == 0:
|
||||||
|
print("0 selected, no hint taken.")
|
||||||
|
elif hint >= i:
|
||||||
|
print("Number too high selected, no hint taken.")
|
||||||
|
else:
|
||||||
|
print("{}: {}".format(hint, hint_giver.take_hint(
|
||||||
|
"level" + str(game.level), game.branch, possible_hints[hint-1])))
|
||||||
|
except ValueError:
|
||||||
|
print("Invalid input, no hint taken.")
|
||||||
else:
|
else:
|
||||||
print("Unknown command. Enter another command or try (H)elp.")
|
print("Unknown command. Enter another command or try (H)elp.")
|
||||||
|
|
||||||
|
|
Reference in New Issue