Remove time-based sea monster. Better error-handling

This commit is contained in:
Chris Proctor 2024-11-05 13:41:44 -05:00
parent 814b7f7643
commit 8acd9dad2b
2 changed files with 58 additions and 68 deletions

View File

@ -6,51 +6,34 @@
import os
from pathlib import Path
from urllib.request import urlretrieve
from threading import Timer
from datetime import datetime
import sys
sys.path.append('../../..')
from fancy_printing import print_fancy
CHEST_TIMER_FILE = ".timer"
SECRET = "318"
APPROACH_CHEST = [
"You approach the chest and see that the lock is surprisingly free of rust. In fact, the code dials turn smoothly. Try entering a code."
]
CHEST_OPENS = [
"The chest snaps open, releasing several huge air bubbles. You look into the chest and see..."
]
TREASURE = [
"something shining brightly from within, so bright you can't quite make out what it is."
]
MONSTER1 = [
"*rumble* *rumble* *rumble*",
"Around the reef, nothing seems out of place."
]
MONSTER2 = [
"*rumble* *rumble* *rumble*",
"A SEA MONSTER APPEARS FROM THE REEF!",
"The chest snaps open, releasing several huge air bubbles. You look into the chest and see something shining brightly from within, so bright you can't quite make out what it is."
]
INSTRUCTIONS = [
"The sea monster must have seen you open the chest! There's no time to squander. You grab the treasure from the chest without taking a closer look! Quick, make a bag and store your treasure inside, using the following commands:",
"You grab the treasure from the chest...it's so shiny! Make a bag and store your treasure inside, using the following commands:",
"mkdir bag",
"mv treasure.jpg bag",
"Then get back to the surface ASAP! Don't forget to take your treasure bag with you. Remember, `..` means 'the parent directory,' so these commands will move the bag to the parent directory and then move yourself:",
"Then get back to the surface. Don't forget to take your treasure bag with you. Remember, `..` means 'the parent directory,' so these commands will move the bag to the parent directory and then move yourself:",
"mv bag ../bag",
"cd .."
]
WRONG_CODE = [
"nothing happens. Maybe next time."
]
print_fancy(APPROACH_CHEST)
guess = input(" > ")
if guess.strip() == SECRET:
Path(CHEST_TIMER_FILE).write_text(datetime.now().isoformat())
print_fancy(CHEST_OPENS)
print_fancy(INSTRUCTIONS)
os.system('cp ./../../../.assets/fork.jpg treasure.jpg')
Timer(1.0, print_fancy, [TREASURE]).start()
Timer(5.0, print_fancy, [MONSTER1]).start()
Timer(9.0, print_fancy, [MONSTER2]).start()
Timer(13.0, print_fancy, [INSTRUCTIONS]).start()
else:
print_fancy(["nothing happens. Maybe next time."])
print_fancy(WRONG_CODE)

View File

@ -7,70 +7,77 @@
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?"
"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."
]
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?"
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 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.",
"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."
]
def reset():
if Path('bag').exists():
rmtree('bag')
Path(CHEST_TIMER_FILE).unlink()
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 chest_was_opened():
return Path(CHEST_TIMER_FILE).exists()
def find_file_in_path(path, file):
results = list(Path(path).rglob(file))
if results:
return results[0]
def bag_is_present():
def bag_is_here():
return Path("bag").exists()
def treasure_is_present():
return Path("bag/treasure.jpg").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 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
def treasure_is_here_outside_of_bag():
return Path("treasure.jpg").exists()
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:
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)