Files

234 lines
7.2 KiB
Python

# Imports
import random, time, logging, replit, os
# Logging format
fmt = "%(levelname)s @ ln %(lineno)d in `%(filename)s`: %(message)s"
logging.basicConfig(format=fmt, level=logging.WARN)
# Set account to nil to avoid errors
account = None
# Check if user would like to log in
accountCheck = input(
"Would you like to login?\n(This is for data stores)\n(Y/[N])\nTo get your data, type \"request\"\nYou can type DELETE (in all caps) to delete all save data.\n> "
)
# Login handler
if str.lower(accountCheck) == "y":
signedIn = True
print("Loading profile...")
# Check if logged in
if os.environ["REPL_OWNER"] != "five-nine":
try:
user = os.environ["REPL_OWNER"]
print(f"Logged in as {user}")
# Create profile
if user not in replit.db.keys():
replit.db[user] = {"wins": 0, "losses": 0}
print("Loaded profile!")
# Load user data
if user in replit.db.keys():
account = replit.db.get(user)
try:
print(
f"You have {account['wins']} wins and {account['losses']} losses. Rating: {round((account['wins'] / (account['wins'] + account['losses'])), 2) * 100}%"
)
except ZeroDivisionError:
print("No previous games found.")
except KeyError:
logging.error("Unable to get account information. Are you logged in?")
signedIn = False
else:
signedIn = False
print(
"It looks like you're not signed in. Please sign in to use data stores"
)
elif str.lower(accountCheck) == "request":
signedIn = True
print("Loading data...")
if os.environ["REPL_OWNER"] != "five-nine":
try:
user = os.environ["REPL_OWNER"]
print(f"Logged in as {user}")
if user not in replit.db.keys():
print("You don't have any data to request?!?!")
if user in replit.db.keys():
account = replit.db.get(user)
print(
f"\n-=+=-\nYour data looks like this:\n{replit.db.get(user)}\n-=+=-")
except KeyError:
logging.error(
"Unable to get acount information -- Not logged in somehow.")
signedIn = False
else:
signedIn = False
print(
"It looks like you're not signed in. Please sign in to view user data")
elif accountCheck == "DEBUG":
print(
"Debug commands:\nDEBUG -- WINSTAT\nDEBUG -- LOSSSTAT\n\nCase-sensitive!"
)
logging.fatal("Debug exit")
exit()
elif accountCheck == "DEBUG -- WINSTAT":
print("This is debug mode -- expect errors.")
if os.environ["REPL_OWNER"] != "five-nine":
try:
user = os.environ["REPL_OWNER"]
print(f"Logged in as {user}")
if user not in replit.db.keys():
print("You don't have any save data to edit!")
if user in replit.db.keys():
account = replit.db.get(user)
while True:
try:
editWinStat = int(input("Enter the stat to set win count to\n> "))
account["wins"] = editWinStat
print("Saved.")
print(
f"\n-=+=-\nYour data now looks like this:\n{replit.db.get(user)}\n-=+=-"
)
break
except ValueError:
print("Must be a number!")
except KeyError:
logging.error("Unable to get account information -- Not logged in.")
elif accountCheck == "DEBUG -- LOSSSTAT":
print("This is debug mode -- expect errors.")
if os.environ["REPL_OWNER"] != "five-nine":
try:
user = os.environ["REPL_OWNER"]
print(f"Logged in as {user}")
if user not in replit.db.keys():
print("You don't have any save data to edit!")
if user in replit.db.keys():
account = replit.db.get(user)
while True:
try:
editWinStat = int(input("Enter the stat to set loss count to\n> "))
account["losses"] = editWinStat
print("Saved.")
print(
f"\n-=+=-\nYour data now looks like this:\n{replit.db.get(user)}\n-=+=-"
)
break
except ValueError:
print("Must be a number!")
except KeyError:
logging.error("Unable to get account information -- Not logged in.")
elif accountCheck == "DELETE":
# Load profile
print("Loading profile...")
# Check if logged in
if os.environ["REPL_OWNER"] != "five-nine":
try:
user = os.environ["REPL_OWNER"]
print(f"Logged in as {user}")
if user not in replit.db.keys():
# Show if user tries to delete save data but isn't in the database
print("You don't have any save data to erase???")
else:
# Delete save data
del replit.db[user]
print("Save data deleted.\nLogged out.")
signedIn = False
except KeyError:
print("Unable to get account information. Are you logged in?")
signedIn = False
else:
signedIn = False
print("You aren't signed in so the data store couldn't be erased.")
else:
# If user doesn't sign in.
signedIn = False
print("Continuing without logging in...")
logging.basicConfig(format=fmt, level=logging.DEBUG)
# MAIN SEGMENT
print("\nThis is a game where you guess a number. It's so original!")
def main():
try:
playTo = int(
input(
"[SETUP] Please enter the amount of guesses you can have (I'd recommend 20)\n> "
))
except ValueError:
logging.error("Value must be an integer!\n\n")
main()
except KeyboardInterrupt:
logging.debug(f"Exiting via KeyboardInterrupt.")
exit()
except:
logging.fatal("An unhandled exception occured.")
exit()
print(
f'\nGuess a number between 1 and 1000, you will be told if your guess was too low or too high.\nYou have {playTo} guesses.'
)
finalVal = random.randrange(1, 1000)
guessLeft = playTo
for i in range(playTo):
try:
userNum = int(input("\n> "))
except ValueError:
logging.error(
"Value must be an integer!\nWasting a turn by setting the value to -1...\n\n"
)
userNum = -1
except KeyboardInterrupt:
logging.debug(
f"Exiting via KeyboardInterrupt, the number was {finalVal}")
time.sleep(5)
exit()
except:
logging.fatal("An unhandled exception occured.")
exit()
if userNum == finalVal:
print(f'-=+ YOU WIN! +=-\nWith {guessLeft} guesses remaining.')
if signedIn == True:
account["wins"] += 1
print(
f"You now have {account['wins']} win(s) with a rating of {round((account['wins'] / (account['wins'] + account['losses'])), 2) * 100}%"
)
else:
print("Did not save data as you're not signed in")
time.sleep(5)
logging.debug("Exiting...")
exit()
elif userNum < finalVal:
print("Too low")
guessLeft = guessLeft - 1
print(f'{guessLeft} guesses remaining...')
else:
print("Too high")
guessLeft = guessLeft - 1
print(f'{guessLeft} guesses remaining...')
print("\n-=+ GAME OVER +=-")
print(f'The number was {finalVal}. Better luck next time.')
if signedIn == True:
account["losses"] += 1
print(
f"You now have {account['losses']} losse(s) with a rating of {round((account['wins'] / (account['wins'] + account['losses'])), 2) * 100}%"
)
else:
print("Did not save data as you're not signed in")
time.sleep(5)
logging.debug("Exiting...")
exit()
if __name__ == "__main__":
main()