better logging and aborting
This commit is contained in:
parent
678d59f856
commit
2a0a088998
|
@ -147,17 +147,7 @@ class Game:
|
||||||
self.level_start_time = time.time()
|
self.level_start_time = time.time()
|
||||||
|
|
||||||
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 /logs subfolder exists, also logs the current game log
|
|
||||||
to a file before aborting."""
|
|
||||||
try:
|
|
||||||
if os.path.isdir("logs"):
|
|
||||||
self.log_to_file("logs/aborted_game" + str(time.time()))
|
|
||||||
except OSError as err:
|
|
||||||
# print("Failed to save game log.")
|
|
||||||
# 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 = []
|
||||||
|
|
|
@ -34,6 +34,10 @@ class HintGiver:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def restart_game(self):
|
||||||
|
self.total_hints_taken = 0
|
||||||
|
self.hints_taken = {}
|
||||||
|
|
||||||
def log_to_file(self, filename):
|
def log_to_file(self, filename):
|
||||||
with open(filename, 'a') as f:
|
with open(filename, 'a') as f:
|
||||||
yaml.dump(self, f)
|
yaml.dump(self, f)
|
66
assistant.py
66
assistant.py
|
@ -5,6 +5,7 @@ from adaptive_game_module.hint_giver import HintGiver
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
def print_help():
|
def print_help():
|
||||||
"""Print list of arguments that game_loop accepts."""
|
"""Print list of arguments that game_loop accepts."""
|
||||||
|
@ -88,6 +89,34 @@ def write_log(filename, game, hint_giver):
|
||||||
print("Error number: {}, Error text: {}".format(err.errno, err.strerror))
|
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))
|
print("Double check that location {} for the file exists and can be written to.".format(filename))
|
||||||
|
|
||||||
|
def give_hint(game, hint_giver):
|
||||||
|
if not game.game_in_progress:
|
||||||
|
print("Game not started, can't give hints.")
|
||||||
|
return
|
||||||
|
if hint_giver.show_taken_hints("level" + str(game.level), game.branch) != []:
|
||||||
|
print("Hints taken in current level:")
|
||||||
|
for hint in hint_giver.show_taken_hints("level" + str(game.level), game.branch):
|
||||||
|
print("{}: {}".format(hint, hint_giver.take_hint("level" + str(game.level), game.branch, hint)))
|
||||||
|
print("Choose which hint to take:")
|
||||||
|
print("0: (cancel, take no hint)")
|
||||||
|
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.")
|
||||||
|
|
||||||
def game_loop():
|
def game_loop():
|
||||||
"""Interactively assist the player with playing the game.
|
"""Interactively assist the player with playing the game.
|
||||||
|
|
||||||
|
@ -133,8 +162,16 @@ def game_loop():
|
||||||
command = input()
|
command = input()
|
||||||
command = command.lower()
|
command = command.lower()
|
||||||
if command in ("a", "abort"):
|
if command in ("a", "abort"):
|
||||||
|
try:
|
||||||
|
if os.path.isdir("logs"):
|
||||||
|
write_log("logs/aborted_game" + str(time.time()), game, hint_giver)
|
||||||
|
except OSError as err:
|
||||||
|
# print("Failed to save game log.")
|
||||||
|
# print("Error number: {}, Error text: {}".format(err.errno, err.strerror))
|
||||||
|
pass
|
||||||
print("Aborting game, deleting all VMs.")
|
print("Aborting game, deleting all VMs.")
|
||||||
game.abort_game()
|
game.abort_game()
|
||||||
|
hint_giver.restart_game()
|
||||||
print("Game aborted, progress reset, VMs deleted.")
|
print("Game aborted, progress reset, VMs deleted.")
|
||||||
elif command in ("e", "exit"):
|
elif command in ("e", "exit"):
|
||||||
print("Going to exit assistant, abort game and delete VMs.")
|
print("Going to exit assistant, abort game and delete VMs.")
|
||||||
|
@ -200,6 +237,8 @@ def game_loop():
|
||||||
confirmation = confirmation.lower()
|
confirmation = confirmation.lower()
|
||||||
if (confirmation == "yes"):
|
if (confirmation == "yes"):
|
||||||
print("Overwriting file...")
|
print("Overwriting file...")
|
||||||
|
with open("logs/game_log.yml", 'w'):
|
||||||
|
pass
|
||||||
write_log("logs/game_log.yml", game, hint_giver)
|
write_log("logs/game_log.yml", game, hint_giver)
|
||||||
else:
|
else:
|
||||||
print("File not overwritten.")
|
print("File not overwritten.")
|
||||||
|
@ -207,32 +246,7 @@ def game_loop():
|
||||||
print("Writing file...")
|
print("Writing file...")
|
||||||
write_log("logs/game_log.yml", game, hint_giver)
|
write_log("logs/game_log.yml", game, hint_giver)
|
||||||
elif command in ("t", "hint"):
|
elif command in ("t", "hint"):
|
||||||
if not game.game_in_progress:
|
give_hint(game, hint_giver)
|
||||||
print("Game not started, can't give hints.")
|
|
||||||
else:
|
|
||||||
if hint_giver.show_taken_hints("level" + str(game.level), game.branch) != []:
|
|
||||||
print("Hints taken in current level:")
|
|
||||||
for hint in hint_giver.show_taken_hints("level" + str(game.level), game.branch):
|
|
||||||
print("{}: {}".format(hint, hint_giver.take_hint("level" + str(game.level), game.branch, hint)))
|
|
||||||
print("Choose which hint to take:")
|
|
||||||
print("0: (cancel, take no hint)")
|
|
||||||
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