1
0
Fork 0

add in progress and finished states, better report

This commit is contained in:
Lili (Tlapka) 2021-03-18 14:21:08 +01:00
parent bc8d679703
commit 91eb876a83
1 changed files with 21 additions and 13 deletions

View File

@ -53,7 +53,8 @@ class Game:
self.level = level_number self.level = level_number
self.branch = '' self.branch = ''
self.game_in_progress = True self.game_in_progress = False
self.game_finished = False
self.game_end_time = 0 self.game_end_time = 0
def read_mapping(self, file): def read_mapping(self, file):
@ -64,10 +65,11 @@ class Game:
def start_game(self): def start_game(self):
"""Start a new game, if there isn't one already in progress.""" """Start a new game, if there isn't one already in progress."""
if (self.level != 0): if (self.game_in_progress or self.game_finished):
return False return False
self.game_in_progress = True
self.level = 1 self.level = 1
self.level_log.append("level1") #add level 1 self.level_log.append("level1") # add level 1 to log
start_time = time.time() start_time = time.time()
subprocess.run(["vagrant", "up"], cwd=self.game_directory, subprocess.run(["vagrant", "up"], cwd=self.game_directory,
env=dict(os.environ, ANSIBLE_ARGS='--tags \"setup\"')) env=dict(os.environ, ANSIBLE_ARGS='--tags \"setup\"'))
@ -82,13 +84,14 @@ class Game:
"""Mark game as not in progress and log end time, if on the last level. """Mark game as not in progress and log end time, if on the last level.
Return false if prerequisites were not met, true otherwise.""" Return false if prerequisites were not met, true otherwise."""
if (self.next_level_exists() or not self.game_in_progress):
if (not self.next_level_exists() and self.game_in_progress): return False
else:
self.solving_times.append(int(time.time() - self.level_start_time)) self.solving_times.append(int(time.time() - self.level_start_time))
self.game_end_time = time.time() self.game_end_time = time.time()
self.game_in_progress = False self.game_in_progress = False
self.game_finished = True
return True return True
return False
def next_level_exists(self): def next_level_exists(self):
"""Return true if next level exists.""" """Return true if next level exists."""
@ -128,7 +131,7 @@ class Game:
next_branch_input == '' is understood as no branch selected, a level next_branch_input == '' is understood as no branch selected, a level
without possible branching.""" without possible branching."""
next_branch = "" next_branch = ""
if (self.level == 0): if (not self.game_in_progress):
raise NoLevelFoundError("Can't continue, (S)tart the game first!") raise NoLevelFoundError("Can't continue, (S)tart the game first!")
if (not self.next_level_exists()): if (not self.next_level_exists()):
raise NoLevelFoundError("No next level found! Perhaps you already finished the game?") raise NoLevelFoundError("No next level found! Perhaps you already finished the game?")
@ -161,6 +164,8 @@ class Game:
self.level_log = [] self.level_log = []
self.game_start_time = 0 self.game_start_time = 0
self.level_start_time = 0 self.level_start_time = 0
self.game_in_progress = False
self.game_finished = False
self.level = 0 self.level = 0
self.branch = '' self.branch = ''
@ -196,10 +201,10 @@ class Game:
def print_info(self): def print_info(self):
"""Print info about the game in a human-readable way.""" """Print info about the game in a human-readable way."""
if (self.level == 0): if (not self.game_in_progress and not self.game_finished):
print("Game is not yet started.") print("Game is not yet started.")
else: else:
if (self.game_in_progress): if (self.game_in_progress): #in progress implies not finished
if (self.branch): if (self.branch):
print("Game in progress. Level:{} Branch:{}".format(self.level, self.branch)) print("Game in progress. Level:{} Branch:{}".format(self.level, self.branch))
else: else:
@ -383,12 +388,15 @@ def game_loop():
print("Error encountered: {}".format(err)) print("Error encountered: {}".format(err))
elif ((command == "f") or (command == "finish")): elif ((command == "f") or (command == "finish")):
if (game.finish_game()) if (game.finish_game()):
print("Game finished, total time logged!") print("Game finished, total time logged!")
elif (not game.game_in_progress) elif (not game.game_in_progress and not game.game_finished):
print("Game was already marked as finished earlier.") print("Can't finish game, game was not started yet.")
elif (not game.game_in_progress and game.game_finished):
print("Can't finish game, game was already finished earlier.")
else: else:
print("Could not finish game. Make sure you are on the last level!") print("Could not finish game.")
print("Make sure you are on the last level!")
elif ((command == "i") or (command == "info")): elif ((command == "i") or (command == "info")):
game.print_info() game.print_info()
elif ((command == "h") or (command == "help")): elif ((command == "h") or (command == "help")):