generated from mwc/lab_terminal
this lab, especially becuase I had to try it a few times before getting under the 60 second timer. When I was having trouble getting mwc update to work, I ended up opening the code using code return_to_ship.py to see what the problem might have been. I did some googling with what the path fucntion does and started to think that maybe my log file was not being created. I added print(Path(LOG_FILE).absolute()) to the code and then redid the adventure and then looked at the hidden files in Finder and saw that the log file was created. I removed that code line and re-saved return_to_ship.py. I'm still not sure if the saving of the file or not having a log file was the issue before, but I think everything should be all set now. I think the terminal might be useful if you want to create a number of files or add a program/funciton that may create files for you. This would be easier than doing everything manually with the Finder.
78 lines
3.1 KiB
Python
78 lines
3.1 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 datetime import timedelta
|
|
from fancy_printing import print_fancy
|
|
|
|
CHEST_TIMER_FILE = "adventure/seafloor/coral_reef/.timer"
|
|
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..."
|
|
]
|
|
FORGOT_BAG = [
|
|
"You forgot your treasure bag! Hurry back to get it before the sea monster hides it away forever!"
|
|
]
|
|
EMPTY_BAG = [
|
|
"You brought the bag back to the surface, but the treasure is not inside. What a disappointment after all that work. Will you attempt the dive again?"
|
|
]
|
|
LOST_TREASURE = [
|
|
"You are swimming as fast as you can towards the boat but you can feel the water begin to pull you back as the sea monster opens its giant mouth. You kick with all your might, sure that you are about to breath your last breath.",
|
|
"Suddenly... The treasure bag slips out of your hand!",
|
|
"It swirls down through the water and into the mouth of the sea monster. The beast's mouth snaps closed and it jets away into the depth of the ocean, taking with it the treasure. You are safe... but will you attempt the dive again?"
|
|
]
|
|
VICTORY = [
|
|
"You are swimming as fast as you can towards the boat but you can feel the water begin to pull you back as the sea monster opens its giant mouth. You kick with all your might, sure that you are about to breathe your last breath. "
|
|
"Suddenly... A hand appears! You've made it to the boat! The crew pulls you into the boat, just in time to avoid the sea monster's vicious maw. You're safe at last! 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."
|
|
]
|
|
|
|
def reset():
|
|
if Path('bag').exists():
|
|
rmtree('bag')
|
|
Path('adventure/seafloor/coral_reef/.timer').unlink()
|
|
|
|
def win():
|
|
print_fancy(VICTORY)
|
|
win_time = datetime.now().isoformat()
|
|
Path(LOG_FILE).write_text(f"finished the lab at {win_time}\n")
|
|
|
|
|
|
def chest_was_opened():
|
|
return Path(CHEST_TIMER_FILE).exists()
|
|
|
|
def bag_is_present():
|
|
return Path("bag").exists()
|
|
|
|
def treasure_is_present():
|
|
return Path("bag/treasure.jpg").exists()
|
|
|
|
def time_since_chest_was_opened():
|
|
if chest_was_opened():
|
|
treasure_opened_time = datetime.fromisoformat(Path(CHEST_TIMER_FILE).read_text())
|
|
elapsed_time = datetime.now() - treasure_opened_time
|
|
return elapsed_time.seconds
|
|
else:
|
|
return 0
|
|
|
|
if chest_was_opened():
|
|
if bag_is_present():
|
|
if treasure_is_present():
|
|
if time_since_chest_was_opened() < 60:
|
|
win()
|
|
else:
|
|
reset()
|
|
print_fancy(LOST_TREASURE)
|
|
else:
|
|
reset()
|
|
print_fancy(EMPTY_BAG)
|
|
else:
|
|
print_fancy(FORGOT_BAG)
|
|
else:
|
|
print_fancy(BEGINNING)
|