2021-03-16 14:11:29 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-03-30 19:08:48 +02:00
|
|
|
from adaptive_game_module.adaptive_game import Game, NoLevelFoundError
|
2021-03-23 17:40:33 +01:00
|
|
|
from adaptive_game_module.hint_giver import HintGiver
|
2021-03-29 20:11:03 +02:00
|
|
|
from adaptive_game_module.level_selector import LevelSelector
|
2021-03-30 19:08:48 +02:00
|
|
|
from adaptive_game_module.flag_checker import FlagChecker
|
2021-03-22 13:09:27 +01:00
|
|
|
|
2021-03-16 14:11:29 +01:00
|
|
|
import subprocess
|
|
|
|
import os
|
2021-04-08 17:17:14 +02:00
|
|
|
import sys
|
2021-03-24 17:53:56 +01:00
|
|
|
import time
|
2021-03-16 14:11:29 +01:00
|
|
|
|
|
|
|
def print_help():
|
|
|
|
"""Print list of arguments that game_loop accepts."""
|
|
|
|
print("Functions that control the game:")
|
2021-03-18 12:07:16 +01:00
|
|
|
print("(A)bort - destroys all VMs, resets progress to level 0.")
|
|
|
|
print("(E)xit - aborts run, then exits the assistant.")
|
|
|
|
print("(S)tart - starts a new run of the adaptive game, if one isn't in progress.")
|
2021-03-30 13:41:14 +02:00
|
|
|
print("(N)ext - advances to the next level.")
|
2021-03-18 12:07:16 +01:00
|
|
|
print("(F)inish - when on the last level, finishes the game and logs end time.")
|
2021-03-25 13:57:36 +01:00
|
|
|
print("hin(T) - ask for hints, read previously given hints.")
|
2021-03-30 13:41:14 +02:00
|
|
|
print("(I)nfo - displays info about current game - levels traversed, times...")
|
2021-03-22 17:05:00 +01:00
|
|
|
print("(L)og - log (save) the information about the game into a file.")
|
2021-03-16 14:11:29 +01:00
|
|
|
print("Helper functions:")
|
2021-03-18 12:07:16 +01:00
|
|
|
print("(H)elp - explains all commands on the screen.")
|
|
|
|
print("(C)heck - checks if prerequisites to run the game are installed.")
|
2021-03-16 14:11:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def check_prerequisites():
|
|
|
|
"""Check for the right version of required tools and print the result.
|
|
|
|
|
|
|
|
VirtualBox check fails on windows even when VirtualBox is present."""
|
|
|
|
print("checking Python version:")
|
2021-04-08 17:17:14 +02:00
|
|
|
if sys.version_info[0] == 3:
|
|
|
|
if sys.version_info[1] >= 7:
|
|
|
|
print("OK, Python version higher than 3.7.")
|
2021-03-16 14:11:29 +01:00
|
|
|
else:
|
2021-04-08 17:17:14 +02:00
|
|
|
print("NOK, Python version lower than 3.7.")
|
|
|
|
print("Can't check Vagrant and VirtualBox versions.")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
print("NOK, Python version lower than 3.") # would sooner crash lol
|
2021-03-16 14:11:29 +01:00
|
|
|
|
|
|
|
found = True
|
|
|
|
print("checking Vagrant version:")
|
|
|
|
try:
|
|
|
|
version = subprocess.run(["vagrant", "--version"], capture_output=True, text=True)
|
|
|
|
except FileNotFoundError:
|
|
|
|
found = False
|
|
|
|
print("NOK, Vagrant not found.")
|
2021-04-08 19:33:46 +02:00
|
|
|
try:
|
|
|
|
if found:
|
|
|
|
version_number = version.stdout.split(" ", 1)[1].split(".")
|
|
|
|
if version_number[0] == "2":
|
|
|
|
if int(version_number[1]) > 1:
|
|
|
|
print("OK, Vagrant version higher than 2.2.")
|
|
|
|
else:
|
|
|
|
print("NOK, Vagrant version lower than 2.2.")
|
2021-03-16 14:11:29 +01:00
|
|
|
else:
|
2021-04-08 19:33:46 +02:00
|
|
|
print("NOK, Vagrant 2 not detected.")
|
|
|
|
except IndexError:
|
|
|
|
print("Vagrant was found, but the version string couldn't be parsed.")
|
|
|
|
print("The string in question: {}".format(version.stdout))
|
2021-03-16 14:11:29 +01:00
|
|
|
|
2021-04-08 17:17:14 +02:00
|
|
|
found = False
|
2021-03-16 14:11:29 +01:00
|
|
|
print("checking Virtualbox version:")
|
|
|
|
try:
|
2021-04-08 17:17:14 +02:00
|
|
|
version = subprocess.run(["VBoxManage", "--version"], capture_output=True, text=True)
|
|
|
|
found = True
|
2021-03-16 14:11:29 +01:00
|
|
|
except FileNotFoundError:
|
2021-04-08 17:17:14 +02:00
|
|
|
pass # try no caps
|
|
|
|
if not found:
|
|
|
|
try:
|
2021-04-08 17:47:50 +02:00
|
|
|
version = subprocess.run(["vboxmanage", "--version"], capture_output=True, text=True)
|
2021-04-08 17:17:14 +02:00
|
|
|
found = True
|
|
|
|
except FileNotFoundError:
|
|
|
|
print("Virtualbox not found.")
|
|
|
|
print("If you are on Windows, this is probably OK.")
|
|
|
|
print("(You can double check VirtualBox version yourself to be sure)")
|
|
|
|
print("If you are on Linux, you don't have VirtualBox installed, NOK.")
|
2021-03-16 14:11:29 +01:00
|
|
|
if found:
|
|
|
|
version_number = version.stdout.split(".")
|
2021-03-18 14:48:45 +01:00
|
|
|
if int(version_number[0]) > 5:
|
2021-03-16 14:11:29 +01:00
|
|
|
print("OK, VirtualBox version higher than 5 detected.")
|
|
|
|
else:
|
|
|
|
print("NOK, VirtualBox version lower than 6 detected.")
|
|
|
|
|
2021-03-30 19:08:48 +02:00
|
|
|
def write_log(filename, game, hint_giver, flag_checker):
|
2021-03-30 13:41:14 +02:00
|
|
|
"""Write log from `game` and `hint_giver` to file `filename`
|
|
|
|
Calls logging methods, which are just yaml dumps."""
|
2021-03-24 17:37:22 +01:00
|
|
|
try:
|
|
|
|
game.log_to_file(filename)
|
|
|
|
hint_giver.log_to_file(filename)
|
2021-03-30 19:08:48 +02:00
|
|
|
flag_checker.log_to_file(filename)
|
2021-03-24 17:37:22 +01:00
|
|
|
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))
|
|
|
|
|
2021-03-24 17:53:56 +01:00
|
|
|
def give_hint(game, hint_giver):
|
2021-03-30 13:41:14 +02:00
|
|
|
"""Recap previously given hints, and give player another hint if they want."""
|
2021-03-24 17:53:56 +01:00
|
|
|
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)))
|
2021-03-30 16:31:24 +02:00
|
|
|
print("Choose which hint to take (Write a number):")
|
2021-03-24 17:53:56 +01:00
|
|
|
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.")
|
|
|
|
|
2021-03-29 20:11:03 +02:00
|
|
|
def starting_quiz(level_selector):
|
2021-03-30 13:41:14 +02:00
|
|
|
"""Ask the player a few questions, saving known tools to level selector."""
|
2021-03-29 20:11:03 +02:00
|
|
|
print("Please answer 'yes' if you have ever used a tool or skill before,")
|
|
|
|
print("or 'no' if you haven't.")
|
|
|
|
for tool in level_selector.tool_list:
|
|
|
|
unanswered = True
|
|
|
|
while (unanswered):
|
|
|
|
print("Are you familiar with {}?".format(tool))
|
|
|
|
command = input()
|
|
|
|
command = command.lower()
|
|
|
|
if command in ("y", "yes"):
|
|
|
|
level_selector.add_known_tool(tool)
|
|
|
|
unanswered = False
|
|
|
|
elif command in ("n", "no"):
|
|
|
|
unanswered = False
|
|
|
|
elif command in ("exit", "stop"):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
print("Answer has to be 'yes' or 'no' (or 'exit')")
|
|
|
|
unanswered = True
|
|
|
|
return True
|
|
|
|
|
|
|
|
def start_game(game, level_selector):
|
2021-03-30 13:41:14 +02:00
|
|
|
"""Start the game through the game object after doing a starting quiz."""
|
2021-03-29 20:11:03 +02:00
|
|
|
print("Before the game starts, please fill in a little quiz.")
|
|
|
|
print("It will help better decide what levels you will play.")
|
|
|
|
confirmation = starting_quiz(level_selector)
|
|
|
|
if not confirmation:
|
|
|
|
print("Game not started, quiz not filled in.")
|
|
|
|
return
|
|
|
|
print("Quiz filled in!")
|
|
|
|
print("Starting the game.")
|
|
|
|
print("The initial setup may take a while, up to 20 minutes.")
|
|
|
|
if game.start_game():
|
|
|
|
print("If you do not see any error messages above,")
|
|
|
|
print("then the game was started succesfully!")
|
|
|
|
print("You can start playing now.")
|
|
|
|
else:
|
|
|
|
print("Game was not started, it's already in progress!")
|
|
|
|
print("To start over, please run `abort` first.")
|
|
|
|
|
2021-03-30 19:08:48 +02:00
|
|
|
def abort_game(game, hint_giver, flag_checker):
|
2021-03-30 13:41:14 +02:00
|
|
|
"""Abort the game and reset all progress, log current game in a file."""
|
2021-03-30 13:32:17 +02:00
|
|
|
try:
|
|
|
|
if os.path.isdir("logs"):
|
2021-03-30 19:08:48 +02:00
|
|
|
write_log("logs/aborted_game" + str(time.time()), game, hint_giver, flag_checker)
|
2021-03-30 13:32:17 +02:00
|
|
|
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.")
|
|
|
|
game.abort_game()
|
|
|
|
hint_giver.restart_game()
|
|
|
|
print("Game aborted, progress reset, VMs deleted.")
|
|
|
|
|
2021-03-30 19:08:48 +02:00
|
|
|
def player_logging(game, hint_giver, flag_checker):
|
2021-03-30 13:41:14 +02:00
|
|
|
"""Player-initiated log of the game. Always saves to logs/game_log.yml"""
|
2021-03-30 13:32:17 +02:00
|
|
|
if os.path.exists("logs/game_log.yml"):
|
|
|
|
print("It appears that there is already a saved game log.")
|
|
|
|
print("Write 'yes' to overwrite it.")
|
|
|
|
confirmation = input()
|
|
|
|
confirmation = confirmation.lower()
|
|
|
|
if (confirmation == "yes"):
|
|
|
|
print("Overwriting file...")
|
|
|
|
with open("logs/game_log.yml", 'w'):
|
|
|
|
pass
|
2021-03-30 19:08:48 +02:00
|
|
|
write_log("logs/game_log.yml", game, hint_giver, flag_checker)
|
2021-03-30 13:32:17 +02:00
|
|
|
else:
|
|
|
|
print("File not overwritten.")
|
|
|
|
else:
|
|
|
|
print("Writing file...")
|
2021-03-30 19:08:48 +02:00
|
|
|
write_log("logs/game_log.yml", game, hint_giver, flag_checker)
|
2021-03-30 13:32:17 +02:00
|
|
|
|
|
|
|
def finish_game(game):
|
2021-03-30 13:41:14 +02:00
|
|
|
"""Mark game as finished, inform player if that's impossible."""
|
2021-03-30 13:32:17 +02:00
|
|
|
if game.finish_game():
|
|
|
|
print("Game finished, total time saved!")
|
|
|
|
elif (not game.game_in_progress and not game.game_finished):
|
|
|
|
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:
|
|
|
|
print("Could not finish game.")
|
|
|
|
print("Make sure you are on the last level!")
|
2021-03-29 20:11:03 +02:00
|
|
|
|
2021-03-30 19:08:48 +02:00
|
|
|
def try_next_level(game, level_selector):
|
|
|
|
if game.next_level_exists():
|
|
|
|
print("Going to set up level {}".format(game.level + 1))
|
|
|
|
if game.next_level_is_forked():
|
|
|
|
next_level = level_selector.next_level(game.level, game.running_time())
|
|
|
|
print("Setting up next level: {}".format(next_level))
|
|
|
|
game.next_level(next_level)
|
|
|
|
else:
|
|
|
|
game.next_level()
|
|
|
|
print("Level deployed.")
|
|
|
|
print("If you don't see any errors above, you can continue playing.")
|
|
|
|
if game.level == 5: # TODO: maybe remove hardcode
|
|
|
|
print("This is the last level of the game.")
|
|
|
|
else:
|
|
|
|
print("No next levels found -- you finished the game!")
|
|
|
|
print("Make sure to run (F)inish and (L)og your progress before exiting.")
|
|
|
|
|
|
|
|
def check_flag(level, flag_checker):
|
|
|
|
print("To continue, please enter the flag you found:")
|
|
|
|
print("(case sensitive)")
|
|
|
|
flag = input()
|
|
|
|
if flag_checker.check_flag("level"+str(level), flag):
|
|
|
|
print("Flag is correct!")
|
|
|
|
return True
|
|
|
|
print("Flag is incorrect.")
|
|
|
|
print("Double check your spelling, or use hints if you don't know how to proceed.")
|
|
|
|
return False
|
|
|
|
|
2021-03-16 14:11:29 +01:00
|
|
|
def game_loop():
|
|
|
|
"""Interactively assist the player with playing the game.
|
|
|
|
|
|
|
|
Transform inputs from user into method calls.
|
2021-03-18 14:25:12 +01:00
|
|
|
|
2021-03-16 14:11:29 +01:00
|
|
|
Possible inputs
|
|
|
|
---------------
|
|
|
|
Functions that control the game:
|
2021-03-18 12:07:16 +01:00
|
|
|
(A)bort - destroys all VMs, resets progress to level 0.
|
|
|
|
(E)xit - aborts run, then exits the assistant.
|
|
|
|
(S)tart - starts a new run of the adaptive game, if one isn't in progress.
|
2021-03-30 13:41:14 +02:00
|
|
|
(N)ext - advances to the next level.
|
2021-03-25 13:57:36 +01:00
|
|
|
hin(T) - ask for hints, read previously given hints.
|
2021-03-18 12:07:16 +01:00
|
|
|
(F)inish - when on the last level, finishes the game and logs end time.
|
2021-03-30 13:41:14 +02:00
|
|
|
(I)nfo - displays info about current game - levels traversed, times...
|
2021-03-22 17:05:00 +01:00
|
|
|
(L)og - log (save) the information about the game into a file.
|
2021-03-16 14:11:29 +01:00
|
|
|
Helper functions:
|
2021-03-18 12:07:16 +01:00
|
|
|
(H)elp - explains all commands on the screen.
|
|
|
|
(C)heck - checks if prerequisites to run the game are installed.
|
2021-03-16 14:11:29 +01:00
|
|
|
"""
|
2021-03-22 17:05:00 +01:00
|
|
|
if not os.path.isdir("logs"):
|
2021-03-22 16:59:09 +01:00
|
|
|
try:
|
2021-03-22 17:05:00 +01:00
|
|
|
os.mkdir("logs")
|
2021-03-22 16:59:09 +01:00
|
|
|
except FileExistsError:
|
|
|
|
pass #directory already exists, all OK
|
|
|
|
except OSError as err:
|
2021-03-22 17:05:00 +01:00
|
|
|
print("Error encountered while creating the /logs subfolder for save data:")
|
2021-03-22 16:59:09 +01:00
|
|
|
print("Error number: {}, Error text: {}".format(err.errno, err.strerror))
|
|
|
|
print("If not fixed, saving game data might not be possible.")
|
|
|
|
try:
|
2021-03-25 13:57:36 +01:00
|
|
|
game = Game("resources/levels.yml", "../game")
|
2021-03-22 16:59:09 +01:00
|
|
|
except OSError as err:
|
|
|
|
print("Error encountered while setting up the game object.")
|
|
|
|
print("Error number: {}, Error text: {}".format(err.errno, err.strerror))
|
2021-03-25 13:57:36 +01:00
|
|
|
print("(Most likely, `resources/levels.yml` file couldn't be read.")
|
|
|
|
print("Make sure it is in the folder, and readable.")
|
|
|
|
try:
|
|
|
|
hint_giver = HintGiver("resources/hints.yml")
|
|
|
|
except OSError as err:
|
|
|
|
print("Error encountered while setting up the hint giver.")
|
|
|
|
print("Error number: {}, Error text: {}".format(err.errno, err.strerror))
|
|
|
|
print("(Most likely, `resources/hints.yml` file couldn't be read.")
|
2021-03-22 16:59:09 +01:00
|
|
|
print("Make sure it is in the folder, and readable.")
|
2021-03-29 20:11:03 +02:00
|
|
|
try:
|
|
|
|
level_selector = LevelSelector("resources/tools.yml", "resources/level_requirements.yml")
|
|
|
|
except OSError as err:
|
|
|
|
print("Error encountered while setting up the level selector.")
|
|
|
|
print("Error number: {}, Error text: {}".format(err.errno, err.strerror))
|
|
|
|
print("(Most likely, `resources/level_requirements.yml` file couldn't be read.")
|
|
|
|
print("Make sure it is in the folder, and readable.")
|
2021-03-30 19:08:48 +02:00
|
|
|
try:
|
|
|
|
flag_checker = FlagChecker("resources/level_keys.yml")
|
|
|
|
except OSError as err:
|
|
|
|
print("Error encountered while setting up the flag checker.")
|
|
|
|
print("Error number: {}, Error text: {}".format(err.errno, err.strerror))
|
|
|
|
# print("(Most likely, `resources/level_requirements.yml` file couldn't be read.")
|
|
|
|
# print("Make sure it is in the folder, and readable.")
|
|
|
|
|
|
|
|
|
2021-03-16 14:11:29 +01:00
|
|
|
print("Welcome to the adaptive game assistant.")
|
|
|
|
print("Basic commands are:")
|
|
|
|
print("(S)tart, (N)ext, (H)elp, (C)heck, (E)xit")
|
2021-03-18 14:48:45 +01:00
|
|
|
while True:
|
2022-01-25 14:50:17 +01:00
|
|
|
try:
|
|
|
|
print("Waiting for your input:")
|
|
|
|
command = input()
|
|
|
|
command = command.lower()
|
|
|
|
if command in ("a", "abort", "(a)bort"):
|
|
|
|
abort_game(game, hint_giver, flag_checker)
|
|
|
|
elif command in ("e", "exit"):
|
|
|
|
abort_game(game, hint_giver, flag_checker)
|
|
|
|
print("Exiting...")
|
|
|
|
return
|
|
|
|
elif command in ("s", "start", "(s)tart"):
|
|
|
|
start_game(game, level_selector)
|
|
|
|
elif command in ("n", "next", "(n)ext"):
|
|
|
|
try:
|
|
|
|
if game.level == 0:
|
|
|
|
print("Can't continue, (S)tart the game first!")
|
|
|
|
elif game.level == 5:
|
|
|
|
print("Can't continue, you are on the last level!")
|
|
|
|
print("Make sure to run (F)inish and (L)og your progress before exiting.")
|
|
|
|
else:
|
|
|
|
if check_flag(game.level, flag_checker):
|
|
|
|
try_next_level(game, level_selector)
|
|
|
|
except NoLevelFoundError as err:
|
|
|
|
print("Error encountered: {}".format(err))
|
|
|
|
elif command in ("f", "finish", "(f)inish"):
|
|
|
|
if (not game.game_in_progress and not game.game_finished):
|
|
|
|
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.")
|
2021-03-30 19:08:48 +02:00
|
|
|
else:
|
|
|
|
if check_flag(game.level, flag_checker):
|
2022-01-25 14:50:17 +01:00
|
|
|
finish_game(game)
|
|
|
|
elif command in ("i", "info", "information", "(i)nfo", "(i)nformation"):
|
|
|
|
game.print_info()
|
|
|
|
elif command in ("h", "help", "(h)elp"):
|
|
|
|
print_help()
|
|
|
|
elif command in ("c", "check", "(c)heck"):
|
|
|
|
check_prerequisites()
|
|
|
|
elif command in ("l", "log", "(l)og"):
|
|
|
|
player_logging(game, hint_giver, flag_checker)
|
|
|
|
elif command in ("t", "hint", "hin(t)"):
|
|
|
|
give_hint(game, hint_giver)
|
2021-03-31 18:23:05 +02:00
|
|
|
else:
|
2022-01-25 14:50:17 +01:00
|
|
|
print("Unknown command. Enter another command or try (H)elp.")
|
|
|
|
except EOFError:
|
2021-03-16 14:11:29 +01:00
|
|
|
print("Unknown command. Enter another command or try (H)elp.")
|
2022-01-25 14:50:17 +01:00
|
|
|
print("If you want to exit the assistant, please use the `exit` command.")
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("You sent a keyboard interrupt to the program.")
|
2022-02-24 13:45:12 +01:00
|
|
|
print("Would you like to exit?")
|
|
|
|
print("This will NOT save your log or end the game. yes/no")
|
2022-01-25 14:50:17 +01:00
|
|
|
confirmation = input()
|
|
|
|
confirmation = confirmation.lower()
|
|
|
|
if confirmation in ("y", "yes"):
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
print("Not exiting. Continuing normal operation.")
|
2021-03-16 14:11:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
game_loop()
|