generated from mwc/lab_terminal
84 lines
3.0 KiB
Python
84 lines
3.0 KiB
Python
# return_to_ship.py
|
|
# -----------------
|
|
# By MWC Contributors
|
|
#
|
|
# This is a Python program. You should run it by typing `python return_to_ship.py`.
|
|
|
|
from shutil import rmtree
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
from fancy_printing import print_fancy
|
|
|
|
LOG_FILE = ".log"
|
|
BEGINNING = [
|
|
"Your adventure has only just begun. You are not yet ready to return to the ship. More secrets await you in the ocean's depths. Use `ls` to look around, and use `cd adventure` to start the adventure..."
|
|
]
|
|
EMPTY_BAG = [
|
|
"You brought the bag back to the surface, but the treasure is not inside. You'll have to go back and open the chest again."
|
|
]
|
|
RUSTY_TREASURE = [
|
|
"As you reach the surface, you look at the treasure in your hands. To your dismay, it has corroded into bits because you didn't keep it in its bag. You'll have to get another treasure from the chest...next time make sure you store it inside a bag."
|
|
]
|
|
VICTORY = [
|
|
"You've made it back to the boat! The crew pulls you in. Now you can finally show off the treasure you risked your life for... Use `open bag/treasure.jpg` to take a peek.",
|
|
"Congratulations! You have completed the Terminal Adventure."
|
|
]
|
|
COMMANDS = [
|
|
"As a reminder, here are some useful commands:",
|
|
"To move a file or a directory from old to new: mv old new",
|
|
"To create a directory named bag: mkdir bag"
|
|
]
|
|
|
|
def win():
|
|
print_fancy(VICTORY)
|
|
win_time = datetime.now().isoformat()
|
|
Path(LOG_FILE).write_text(f"finished the lab at {win_time}\n")
|
|
|
|
def find_file_in_path(path, file):
|
|
results = list(Path(path).rglob(file))
|
|
if results:
|
|
return results[0]
|
|
|
|
def bag_is_here():
|
|
return Path("bag").exists()
|
|
|
|
def treasure_is_here_in_bag():
|
|
bag_path = Path("bag")
|
|
treasure_in_bag_path = find_file_in_path(bag_path, "treasure.jpg")
|
|
return bag_path.exists() and treasure_in_bag_path
|
|
|
|
def treasure_is_here_outside_of_bag():
|
|
return Path("treasure.jpg").exists()
|
|
|
|
def find_treasure():
|
|
return find_file_in_path("adventure", "treasure.jpg")
|
|
|
|
def find_bag():
|
|
return find_file_in_path("adventure", "bag")
|
|
|
|
if treasure_is_here_in_bag():
|
|
win()
|
|
elif bag_is_here():
|
|
rmtree("bag")
|
|
print_fancy(EMPTY_BAG)
|
|
elif treasure_is_here_outside_of_bag():
|
|
Path("treasure.jpg").unlink()
|
|
print_fancy(RUSTY_TREASURE)
|
|
print_fancy(COMMANDS)
|
|
elif not find_treasure():
|
|
print_fancy(BEGINNING)
|
|
elif find_bag():
|
|
bag_path = find_bag()
|
|
treasure_path = find_treasure()
|
|
if treasure_path.is_relative_to(bag_path):
|
|
message = f"You left your treasure bag at {bag_path}! Bring it back to the surface."
|
|
else:
|
|
message = f"You left your bag at {bag_path} and your treasure at {treasure_path}! Put the treasure in the bag, and then bring the bag to the surface."
|
|
print_fancy([message])
|
|
print_fancy(COMMANDS)
|
|
else:
|
|
treasure_path = find_treasure()
|
|
message = f"You left your treasure at {treasure_path}! Create a bag, put the treasure inside, and bring the back back to the surface."
|
|
print_fancy([message])
|
|
print_fancy(COMMANDS)
|