more style fixes
This commit is contained in:
parent
b02028036f
commit
e4a0b0323b
80
assistant.py
80
assistant.py
|
@ -22,7 +22,7 @@ class Game:
|
||||||
and it will save loading times and solving times of the player."""
|
and it will save loading times and solving times of the player."""
|
||||||
|
|
||||||
def __init__(self, mapping_file, game_directory,
|
def __init__(self, mapping_file, game_directory,
|
||||||
level_number=0, levelBranch=''):
|
level_number=0, level_branch=''):
|
||||||
"""Create an object of class Game.
|
"""Create an object of class Game.
|
||||||
|
|
||||||
The game itself is NOT started at this point. To start it, call
|
The game itself is NOT started at this point. To start it, call
|
||||||
|
@ -51,7 +51,7 @@ class Game:
|
||||||
self.level_start_time = 0
|
self.level_start_time = 0
|
||||||
|
|
||||||
self.level = level_number
|
self.level = level_number
|
||||||
self.branch = ''
|
self.branch = level_branch
|
||||||
|
|
||||||
self.game_in_progress = False
|
self.game_in_progress = False
|
||||||
self.game_finished = False
|
self.game_finished = False
|
||||||
|
@ -86,12 +86,11 @@ class Game:
|
||||||
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 (self.next_level_exists() or not self.game_in_progress):
|
||||||
return False
|
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
|
||||||
self.game_finished = True
|
return True
|
||||||
return True
|
|
||||||
|
|
||||||
def next_level_exists(self):
|
def next_level_exists(self):
|
||||||
"""Return true if next level exists."""
|
"""Return true if next level exists."""
|
||||||
|
@ -131,25 +130,24 @@ 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 (not self.game_in_progress):
|
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?")
|
||||||
if (self.next_level_is_forked()):
|
if self.next_level_is_forked():
|
||||||
next_branch = self.next_level_branch_check(next_branch_input)
|
next_branch = self.next_level_branch_check(next_branch_input)
|
||||||
# throws NoLevelFoundError
|
# throws NoLevelFoundError
|
||||||
elif (next_branch_input != ""):
|
elif next_branch_input != "":
|
||||||
raise NoLevelFoundError("Next level has no branch, but branch was given.")
|
raise NoLevelFoundError("Next level has no branch, but branch was given.")
|
||||||
self.solving_times.append(int(time.time() - self.level_start_time))
|
self.solving_times.append(int(time.time() - self.level_start_time))
|
||||||
self.level += 1
|
self.level += 1
|
||||||
self.branch = next_branch
|
self.branch = next_branch
|
||||||
self.levelname = "level" + str(self.level) + self.branch
|
level_name = "level" + str(self.level) + self.branch
|
||||||
self.level_log.append(self.levelname)
|
self.level_log.append(level_name)
|
||||||
self.boxes = self.level_mapping["level" + str(self.level)][self.levelname]
|
boxes = self.level_mapping["level" + str(self.level)][level_name]
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
subprocess.run(["vagrant", "up"] + self.boxes + ["--provision"],
|
subprocess.run(["vagrant", "up"] + boxes + ["--provision"], cwd=self.game_directory,
|
||||||
cwd=self.game_directory,
|
env=dict(os.environ, ANSIBLE_ARGS='--tags \"' + level_name + '\"'))
|
||||||
env=dict(os.environ, ANSIBLE_ARGS='--tags \"' + self.levelname + '\"'))
|
|
||||||
end_time = time.time()
|
end_time = time.time()
|
||||||
load_time = int(end_time - start_time)
|
load_time = int(end_time - start_time)
|
||||||
self.load_times.append(load_time)
|
self.load_times.append(load_time)
|
||||||
|
@ -185,7 +183,7 @@ class Game:
|
||||||
|
|
||||||
If concise is True, prints only numbers and letters."""
|
If concise is True, prints only numbers and letters."""
|
||||||
|
|
||||||
if (load_time < 60):
|
if load_time < 60:
|
||||||
if concise:
|
if concise:
|
||||||
print("{}s".format(load_time))
|
print("{}s".format(load_time))
|
||||||
else:
|
else:
|
||||||
|
@ -201,11 +199,11 @@ 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 (not self.game_in_progress and not self.game_finished):
|
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): # in progress implies not finished
|
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:
|
||||||
print("Game in progress. Level:{}".format(self.level))
|
print("Game in progress. Level:{}".format(self.level))
|
||||||
|
@ -223,7 +221,7 @@ class Game:
|
||||||
print("")
|
print("")
|
||||||
print("Loading times:")
|
print("Loading times:")
|
||||||
for i in range(len(self.load_times)):
|
for i in range(len(self.load_times)):
|
||||||
if (i == 0):
|
if i == 0:
|
||||||
print("Setup + ", end="")
|
print("Setup + ", end="")
|
||||||
print("{}: ".format(self.level_log[i]), end="")
|
print("{}: ".format(self.level_log[i]), end="")
|
||||||
self.print_time(self.load_times[i], True)
|
self.print_time(self.load_times[i], True)
|
||||||
|
@ -263,8 +261,8 @@ def check_prerequisites():
|
||||||
print("NOK, Python not found.")
|
print("NOK, Python not found.")
|
||||||
if found:
|
if found:
|
||||||
version_number = version.stdout.split(" ", 1)[1].split(".")
|
version_number = version.stdout.split(" ", 1)[1].split(".")
|
||||||
if (version_number[0] == "3"):
|
if version_number[0] == "3":
|
||||||
if (int(version_number[1]) > 7):
|
if int(version_number[1]) > 7:
|
||||||
print("OK, Python version higher than 3.7.")
|
print("OK, Python version higher than 3.7.")
|
||||||
else:
|
else:
|
||||||
print("NOK, Python version lower than 3.7.")
|
print("NOK, Python version lower than 3.7.")
|
||||||
|
@ -281,8 +279,8 @@ def check_prerequisites():
|
||||||
print("NOK, Vagrant not found.")
|
print("NOK, Vagrant not found.")
|
||||||
if found:
|
if found:
|
||||||
version_number = version.stdout.split(" ", 1)[1].split(".")
|
version_number = version.stdout.split(" ", 1)[1].split(".")
|
||||||
if (version_number[0] == "2"):
|
if version_number[0] == "2":
|
||||||
if (int(version_number[1]) > 1):
|
if int(version_number[1]) > 1:
|
||||||
print("OK, Vagrant version higher than 2.2.")
|
print("OK, Vagrant version higher than 2.2.")
|
||||||
else:
|
else:
|
||||||
print("NOK, Vagrant version lower than 2.2.")
|
print("NOK, Vagrant version lower than 2.2.")
|
||||||
|
@ -301,7 +299,7 @@ def check_prerequisites():
|
||||||
print("If you are on Linux, you don't have VirtualBox installed, NOK.")
|
print("If you are on Linux, you don't have VirtualBox installed, NOK.")
|
||||||
if found:
|
if found:
|
||||||
version_number = version.stdout.split(".")
|
version_number = version.stdout.split(".")
|
||||||
if (int(version_number[0]) > 5):
|
if int(version_number[0]) > 5:
|
||||||
print("OK, VirtualBox version higher than 5 detected.")
|
print("OK, VirtualBox version higher than 5 detected.")
|
||||||
else:
|
else:
|
||||||
print("NOK, VirtualBox version lower than 6 detected.")
|
print("NOK, VirtualBox version lower than 6 detected.")
|
||||||
|
@ -330,30 +328,30 @@ def game_loop():
|
||||||
print("Welcome to the adaptive game assistant.")
|
print("Welcome to the adaptive game assistant.")
|
||||||
print("Basic commands are:")
|
print("Basic commands are:")
|
||||||
print("(S)tart, (N)ext, (H)elp, (C)heck, (E)xit")
|
print("(S)tart, (N)ext, (H)elp, (C)heck, (E)xit")
|
||||||
while(True):
|
while True:
|
||||||
print("Waiting for next input:")
|
print("Waiting for next input:")
|
||||||
command = input()
|
command = input()
|
||||||
command = command.lower()
|
command = command.lower()
|
||||||
if ((command == "a") or (command == "abort")):
|
if command in ("a", "abort"):
|
||||||
print("Aborting game, deleting all VMs.")
|
print("Aborting game, deleting all VMs.")
|
||||||
game.abort_game()
|
game.abort_game()
|
||||||
print("Game aborted, progress reset, VMs deleted.")
|
print("Game aborted, progress reset, VMs deleted.")
|
||||||
elif ((command == "e") or (command == "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.")
|
||||||
game.abort_game()
|
game.abort_game()
|
||||||
print("Exiting...")
|
print("Exiting...")
|
||||||
return
|
return
|
||||||
elif ((command == "s") or (command == "start")):
|
elif command in ("s", "start"):
|
||||||
print("Trying to start the game.")
|
print("Trying to start the game.")
|
||||||
print("The initial setup may take a while, up to 20 minutes.")
|
print("The initial setup may take a while, up to 20 minutes.")
|
||||||
if (game.start_game()):
|
if game.start_game():
|
||||||
print("If you do not see any error messages above,")
|
print("If you do not see any error messages above,")
|
||||||
print("then the game was started succesfully!")
|
print("then the game was started succesfully!")
|
||||||
print("You can start playing now.")
|
print("You can start playing now.")
|
||||||
else:
|
else:
|
||||||
print("Game was not started, it's already in progress!")
|
print("Game was not started, it's already in progress!")
|
||||||
print("To start over, please run `abort` first.")
|
print("To start over, please run `abort` first.")
|
||||||
elif ((command == "n") or (command == "next")):
|
elif command in ("n", "next"):
|
||||||
try:
|
try:
|
||||||
if game.level == 0:
|
if game.level == 0:
|
||||||
print("Can't continue, (S)tart the game first!")
|
print("Can't continue, (S)tart the game first!")
|
||||||
|
@ -370,7 +368,7 @@ def game_loop():
|
||||||
game.next_level()
|
game.next_level()
|
||||||
print("Level deployed.")
|
print("Level deployed.")
|
||||||
print("If you don't see any errors above, you can continue playing.")
|
print("If you don't see any errors above, you can continue playing.")
|
||||||
if (game.level == 5):
|
if game.level == 5:
|
||||||
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 probably finished the game!")
|
print("No next levels found -- you probably finished the game!")
|
||||||
|
@ -382,13 +380,13 @@ def game_loop():
|
||||||
# print("Game was already marked as finished earlier.")
|
# print("Game was already marked as finished earlier.")
|
||||||
# else:
|
# else:
|
||||||
# print("Could not finish game, but there are no other levelse either.")
|
# print("Could not finish game, but there are no other levelse either.")
|
||||||
# print("This situation should not happen.") # TODO: look at this again later
|
# print("This situation should not happen.") # TODO: look at this again
|
||||||
# print("Remember to save your gamefile idk")
|
# print("Remember to save your gamefile idk")
|
||||||
except NoLevelFoundError as err:
|
except NoLevelFoundError as err:
|
||||||
print("Error encountered: {}".format(err))
|
print("Error encountered: {}".format(err))
|
||||||
|
|
||||||
elif ((command == "f") or (command == "finish")):
|
elif command in ("f", "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 and not game.game_finished):
|
elif (not game.game_in_progress and not game.game_finished):
|
||||||
print("Can't finish game, game was not started yet.")
|
print("Can't finish game, game was not started yet.")
|
||||||
|
@ -397,11 +395,11 @@ def game_loop():
|
||||||
else:
|
else:
|
||||||
print("Could not finish game.")
|
print("Could not finish game.")
|
||||||
print("Make sure you are on the last level!")
|
print("Make sure you are on the last level!")
|
||||||
elif ((command == "i") or (command == "info")):
|
elif command in ("i", "info"):
|
||||||
game.print_info()
|
game.print_info()
|
||||||
elif ((command == "h") or (command == "help")):
|
elif command in ("h", "help"):
|
||||||
print_help()
|
print_help()
|
||||||
elif ((command == "c") or (command == "check")):
|
elif command in ("c", "check"):
|
||||||
check_prerequisites()
|
check_prerequisites()
|
||||||
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