2022-11-02 20:02:13 +00:00
|
|
|
import curses
|
2022-11-04 11:25:22 +00:00
|
|
|
import os
|
2022-11-17 14:02:25 +00:00
|
|
|
import pickle
|
2022-11-02 20:02:13 +00:00
|
|
|
import random
|
2022-11-04 11:25:22 +00:00
|
|
|
import re
|
2022-11-02 20:02:13 +00:00
|
|
|
import string
|
2022-11-17 14:02:25 +00:00
|
|
|
import sys
|
2022-11-02 20:02:13 +00:00
|
|
|
from base64 import b64decode, b64encode
|
2022-11-04 11:25:22 +00:00
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
import mysql.connector
|
|
|
|
|
2022-11-24 07:46:04 +00:00
|
|
|
import maze.menu
|
2022-11-04 11:25:22 +00:00
|
|
|
|
|
|
|
from .password_forget import sender
|
2022-11-02 20:02:13 +00:00
|
|
|
|
|
|
|
loggedin = False
|
|
|
|
U = gamerid = None
|
|
|
|
quitting = False
|
|
|
|
|
|
|
|
sql = con = None
|
2022-11-17 14:02:25 +00:00
|
|
|
with open("credentials.pickle", "rb") as f:
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
d = pickle.load(f)
|
|
|
|
if d["credtype"] == "mysql":
|
|
|
|
MYSQL_USERNAME = d["user"]
|
|
|
|
MYSQL_PASSWORD = d["pass"]
|
2022-11-23 18:03:16 +00:00
|
|
|
except:
|
2022-11-17 14:02:25 +00:00
|
|
|
pass
|
2022-11-02 20:02:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get(
|
|
|
|
query,
|
|
|
|
): # Simplifed function to fetch records from mysql instead of typing fetchall over nd over
|
|
|
|
con.execute(query)
|
|
|
|
r = con.fetchall()
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def post(
|
|
|
|
query,
|
|
|
|
): # Simplifed function to commit queries to mysql instead of typing this again and again
|
|
|
|
con.execute(query)
|
|
|
|
try:
|
|
|
|
sql.commit()
|
|
|
|
except:
|
|
|
|
print("ERROR OCCURED COMMITTING CHANGES")
|
|
|
|
|
|
|
|
|
|
|
|
def databaseinit(): # Creates database if it doesn't exist
|
|
|
|
try:
|
2022-11-17 14:02:25 +00:00
|
|
|
tempsql = mysql.connector.connect(
|
|
|
|
host="localhost", user=MYSQL_USERNAME, passwd=MYSQL_PASSWORD
|
|
|
|
)
|
2022-11-02 20:02:13 +00:00
|
|
|
tempcon = tempsql.cursor()
|
|
|
|
tempcon.execute("CREATE DATABASE IF NOT EXISTS labyrinth")
|
|
|
|
tempsql.commit()
|
2022-11-26 18:09:00 +00:00
|
|
|
tempcon.close()
|
2022-11-02 20:02:13 +00:00
|
|
|
global sql, con
|
|
|
|
sql = mysql.connector.connect(
|
2022-11-17 14:02:25 +00:00
|
|
|
host="localhost",
|
|
|
|
user=MYSQL_USERNAME,
|
|
|
|
passwd=MYSQL_PASSWORD,
|
|
|
|
database="labyrinth",
|
2022-11-02 20:02:13 +00:00
|
|
|
)
|
|
|
|
con = sql.cursor()
|
|
|
|
return True
|
|
|
|
except (
|
|
|
|
mysql.connector.errors.ProgrammingError,
|
|
|
|
mysql.connector.errors.DatabaseError,
|
|
|
|
):
|
2022-11-17 14:02:25 +00:00
|
|
|
print("Invalid MySQL password/username.")
|
|
|
|
sys.exit()
|
2022-11-02 20:02:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def tableinit():
|
|
|
|
try:
|
|
|
|
post(
|
|
|
|
"CREATE TABLE IF NOT EXISTS player_details\
|
|
|
|
(\
|
|
|
|
gamerid CHAR(4) PRIMARY KEY,\
|
|
|
|
username VARCHAR(32) NOT NULL,\
|
|
|
|
email VARCHAR(32) NOT NULL,\
|
|
|
|
password VARCHAR(32) NOT NULL\
|
|
|
|
) "
|
|
|
|
)
|
2022-11-26 18:09:00 +00:00
|
|
|
for i in ["maze_scores", "pong_scores", "snake_scores", "wordle_scores"]:
|
|
|
|
post(
|
|
|
|
f"CREATE TABLE IF NOT EXISTS {i}\
|
|
|
|
(\
|
|
|
|
gamerid CHAR(4) PRIMARY KEY,\
|
|
|
|
highscore INT,\
|
|
|
|
lastscore INT,\
|
|
|
|
lastplayed DATE,\
|
|
|
|
timesplayed INT\
|
|
|
|
) "
|
|
|
|
)
|
2022-11-02 20:02:13 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
print("ERROR: Creating Table(s)")
|
|
|
|
|
|
|
|
|
|
|
|
def screenhandler(screen): # MAIN MENU
|
|
|
|
h, w = screen.getmaxyx()
|
|
|
|
global loggedin, U, gamerid
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
screen.addstr(1, w // 2 - 8, "ACCOUNT SETTINGS")
|
|
|
|
if loggedin:
|
|
|
|
screen.addstr(2, w // 2 - 8, f"Logged in as: {U}")
|
|
|
|
screen.addstr(h // 2 - 3, w // 2 - 4, "1. Login")
|
|
|
|
screen.addstr(h // 2 - 2, w // 2 - 8, "2. Create Account")
|
|
|
|
screen.addstr(h // 2 - 1, w // 2 - 12, "3. Modify account details")
|
|
|
|
screen.addstr(h // 2 - 0, w // 2 - 12, "4. View account details")
|
|
|
|
screen.addstr(h // 2 + 1, w // 2 - 8, "5. Delete Account")
|
2022-11-04 11:21:32 +00:00
|
|
|
if not loggedin:
|
|
|
|
screen.addstr(h // 2 + 2, w // 2 - 9, "6. Forgot Password?")
|
|
|
|
screen.addstr(h // 2 + 3, w // 2 - 3, "esc. Quit")
|
|
|
|
else:
|
|
|
|
screen.addstr(h // 2 + 2, w // 2 - 3, "6. Logout")
|
|
|
|
screen.addstr(h // 2 + 3, w // 2 - 3, "esc. Quit")
|
2022-11-04 19:22:19 +00:00
|
|
|
screen.border()
|
2022-11-02 20:02:13 +00:00
|
|
|
screen.refresh()
|
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == ord("1"):
|
|
|
|
login(screen)
|
|
|
|
elif key == ord("2"):
|
|
|
|
new_add(screen)
|
|
|
|
elif key == ord("3"):
|
2022-11-04 19:22:19 +00:00
|
|
|
modify_account(screen)
|
2022-11-02 20:02:13 +00:00
|
|
|
elif key == ord("4"):
|
|
|
|
view_account(screen)
|
|
|
|
elif key == ord("5"):
|
2022-11-04 19:22:19 +00:00
|
|
|
delete(screen)
|
2022-11-02 20:02:13 +00:00
|
|
|
elif key == ord("6"):
|
2022-11-04 11:21:32 +00:00
|
|
|
if not loggedin:
|
|
|
|
forgotpassword(screen)
|
|
|
|
elif loggedin:
|
|
|
|
logout(screen)
|
2022-11-02 20:02:13 +00:00
|
|
|
elif key == 27:
|
2022-11-24 07:46:04 +00:00
|
|
|
maze.menu.menu(screen)
|
2022-11-02 20:02:13 +00:00
|
|
|
break
|
|
|
|
screen.refresh()
|
|
|
|
|
|
|
|
|
|
|
|
def screenwipe(screen, sx, sy): # Failed password and stuff reset from screen
|
|
|
|
y, x = screen.getmaxyx()
|
|
|
|
for i in range(sx, x):
|
|
|
|
screen.addstr(sy, i, " ")
|
|
|
|
for i in range(sy + 1, y - 1):
|
|
|
|
for j in range(0, x - 1):
|
|
|
|
if screen.instr(i, j, 1) != " ":
|
|
|
|
screen.addstr(i, j, " ")
|
2022-11-04 19:22:19 +00:00
|
|
|
screen.border()
|
2022-11-02 20:02:13 +00:00
|
|
|
screen.refresh()
|
|
|
|
|
|
|
|
|
2022-11-04 19:22:19 +00:00
|
|
|
def input(
|
2022-11-02 20:02:13 +00:00
|
|
|
y, x, screen, ispassword=False
|
|
|
|
): # Function to get type-able inputs, with delete, esc and other keys
|
|
|
|
inputted = ""
|
|
|
|
orig_y, orig_x = y, x
|
2022-11-24 07:46:04 +00:00
|
|
|
screen.nodelay(False)
|
2022-11-02 20:02:13 +00:00
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == 10:
|
|
|
|
break
|
|
|
|
elif key == 8:
|
|
|
|
if x > orig_x:
|
|
|
|
x -= 1
|
|
|
|
screen.addstr(y, x, " ")
|
|
|
|
inputted = inputted[: len(inputted) - 1]
|
|
|
|
elif key == 27:
|
|
|
|
global quitting
|
|
|
|
quitting = True
|
|
|
|
break
|
2022-11-24 07:46:04 +00:00
|
|
|
elif key != -1:
|
2022-11-02 20:02:13 +00:00
|
|
|
inputted += chr(key)
|
|
|
|
if ispassword:
|
|
|
|
screen.addstr(y, x, "*")
|
|
|
|
else:
|
|
|
|
screen.addstr(y, x, chr(key))
|
|
|
|
x += 1
|
|
|
|
|
|
|
|
return inputted
|
|
|
|
|
|
|
|
|
|
|
|
def list_getter(field): # Feed in the field name you want, get all records of it
|
|
|
|
index = {"username": 1, "email": 2, "password": 3}
|
|
|
|
return_list = []
|
|
|
|
res = get("SELECT * FROM player_details")
|
|
|
|
for i in res:
|
|
|
|
return_list.append(i[index[field]])
|
|
|
|
return return_list
|
|
|
|
|
|
|
|
|
2022-11-24 07:46:04 +00:00
|
|
|
def login(screen, calledby=None): # Function to log in
|
2022-11-02 20:02:13 +00:00
|
|
|
global quitting, U, gamerid, loggedin
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
2022-11-04 19:22:19 +00:00
|
|
|
screen.border()
|
2022-11-02 20:02:13 +00:00
|
|
|
y, x = screen.getmaxyx()
|
|
|
|
usernamelist = list_getter("username")
|
|
|
|
screen.addstr(1, x // 2 - 3, "LOGIN")
|
|
|
|
screen.addstr(y // 2 - 2, x // 2 - 7, "Username: ")
|
|
|
|
while True:
|
2022-11-04 19:22:19 +00:00
|
|
|
inputU = input(y // 2 - 2, x // 2 + 3, screen)
|
2022-11-02 20:02:13 +00:00
|
|
|
if quitting:
|
|
|
|
screen.addstr("Going back to account settings...")
|
|
|
|
sleep(3)
|
|
|
|
quitting = False
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
2022-11-04 19:22:19 +00:00
|
|
|
screenhandler(screen)
|
2022-11-04 11:21:32 +00:00
|
|
|
return
|
2022-11-02 20:02:13 +00:00
|
|
|
if inputU not in usernamelist:
|
|
|
|
screen.addstr(
|
2022-11-04 19:22:19 +00:00
|
|
|
y // 2, 5, "Username does not exist. Do you want to create one? (y/n)"
|
2022-11-02 20:02:13 +00:00
|
|
|
)
|
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == ord("y"):
|
2022-11-24 07:46:04 +00:00
|
|
|
if calledby is not None:
|
2022-11-02 20:02:13 +00:00
|
|
|
new_add(screen, calledby=calledby)
|
|
|
|
else:
|
|
|
|
new_add(screen)
|
|
|
|
return
|
|
|
|
elif key == ord("n"):
|
|
|
|
screenwipe(screen, x // 2 + 3, y // 2 - 2)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
res = get(
|
|
|
|
f"SELECT password,\
|
|
|
|
gamerid\
|
|
|
|
FROM player_details\
|
|
|
|
WHERE username = '{inputU}' "
|
|
|
|
)
|
|
|
|
actualpass = (b64decode(res[0][0].encode("ascii"))).decode("ascii")
|
|
|
|
screen.addstr(y // 2, x // 2 - 7, "Password: ")
|
|
|
|
while True:
|
2022-11-04 19:22:19 +00:00
|
|
|
inputP = input(y // 2, x // 2 + 3, screen, ispassword=True)
|
2022-11-02 20:02:13 +00:00
|
|
|
if quitting:
|
2022-11-04 19:22:19 +00:00
|
|
|
screen.addstr(y // 2 + 3, 5, "Going back to account settings...")
|
2022-11-02 20:02:13 +00:00
|
|
|
sleep(3)
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
screenhandler(screen)
|
|
|
|
if inputP == actualpass:
|
|
|
|
loggedin = True
|
|
|
|
gamerid = res[0][1]
|
|
|
|
U = inputU
|
|
|
|
screen.addstr(y // 2 + 2, x // 2 - 4, "Login Successful!")
|
2022-11-24 07:46:04 +00:00
|
|
|
if calledby is not None:
|
2022-11-02 20:02:13 +00:00
|
|
|
screen.addstr(y // 2 + 3, x // 2 - 4, "Updating score...")
|
|
|
|
screen.refresh()
|
|
|
|
sleep(3)
|
2022-11-26 18:09:00 +00:00
|
|
|
Update_score(calledby[0], calledby[1])
|
2022-11-02 20:02:13 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
screen.addstr(y // 2 + 3, x // 2 - 4, "Returning to menu screen...")
|
|
|
|
screen.refresh()
|
|
|
|
sleep(3)
|
|
|
|
screenhandler(screen)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
screen.addstr(y // 2 + 2, x // 2 - 4, "Wrong password. Try again.")
|
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == 10:
|
|
|
|
screenwipe(screen, sx=x // 2 + 3, sy=y // 2)
|
|
|
|
screen.refresh()
|
|
|
|
break
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
def user(
|
|
|
|
screen, sy, sx, optionaltxt="Enter username: "
|
|
|
|
): # Function to get new username for account creation
|
|
|
|
if quitting:
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
return
|
|
|
|
screen.refresh()
|
|
|
|
userlist = list_getter("username")
|
|
|
|
screen.addstr(sy, sx, optionaltxt)
|
|
|
|
while True:
|
2022-11-04 19:22:19 +00:00
|
|
|
Name = input(sy, sx + len(optionaltxt), screen)
|
2022-11-02 20:02:13 +00:00
|
|
|
if quitting:
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
return
|
|
|
|
if Name in userlist:
|
|
|
|
screen.addstr(
|
2022-11-04 19:22:19 +00:00
|
|
|
sy + 1, 5, "Username already exists, please choose a different one"
|
2022-11-02 20:02:13 +00:00
|
|
|
)
|
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == 10:
|
|
|
|
screenwipe(screen, sx=sx + len(optionaltxt), sy=sy)
|
|
|
|
screen.refresh()
|
|
|
|
break
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
return Name
|
|
|
|
|
|
|
|
|
|
|
|
def password(screen, sy, sx, optionaltxt="Enter Password: "):
|
|
|
|
if quitting:
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
return
|
|
|
|
sl, su, p = 0, 0, 0
|
|
|
|
screen.refresh()
|
|
|
|
while True:
|
|
|
|
screen.addstr(sy, sx, optionaltxt)
|
2022-11-04 19:22:19 +00:00
|
|
|
Password = input(sy, sx + len(optionaltxt), screen, ispassword=True)
|
2022-11-02 20:02:13 +00:00
|
|
|
if quitting:
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
return
|
|
|
|
for i in Password:
|
|
|
|
if i.islower():
|
|
|
|
sl += 1
|
|
|
|
elif i.isupper():
|
|
|
|
su += 1
|
|
|
|
elif i.isdigit():
|
|
|
|
p += 1
|
|
|
|
if sl >= 1 and su >= 1 and p >= 1 and len(Password) <= 10:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
screen.addstr(sy + 1, sx + 2, "Invalid Password")
|
|
|
|
screen.addstr(sy + 2, 5, "Password must contain uppercase, lowercase and")
|
|
|
|
screen.addstr(sy + 3, 5, "digits and should be lesser than 10 characters")
|
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == 10:
|
|
|
|
screenwipe(screen, sx=sx + len(optionaltxt), sy=sy)
|
|
|
|
screen.refresh()
|
|
|
|
break
|
|
|
|
continue
|
|
|
|
|
|
|
|
encoded_pass = (b64encode(Password.encode("ascii"))).decode("ascii")
|
|
|
|
return encoded_pass
|
|
|
|
|
|
|
|
|
2022-11-04 19:22:19 +00:00
|
|
|
def email(screen, sy, sx, optionaltxt="Enter Email: "): # Function to accept email id
|
2022-11-02 20:02:13 +00:00
|
|
|
if quitting:
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
return
|
|
|
|
ev = re.compile(r"[a-z0-9]+@[a-z]+\.[a-z]{2,3}")
|
|
|
|
emailist = list_getter("email")
|
|
|
|
y, x = screen.getmaxyx()
|
|
|
|
screen.refresh()
|
|
|
|
while True:
|
|
|
|
screen.addstr(sy, sx, optionaltxt)
|
2022-11-04 19:22:19 +00:00
|
|
|
email = input(sy, sx + len(optionaltxt), screen)
|
2022-11-02 20:02:13 +00:00
|
|
|
if quitting:
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
return
|
|
|
|
if email in emailist:
|
|
|
|
screen.addstr(
|
|
|
|
sy + 1, sx, "Given email already exists. Enter a different email."
|
|
|
|
)
|
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == 10:
|
|
|
|
screenwipe(screen, sx=sx + len(optionaltxt), sy=sy)
|
|
|
|
screen.refresh()
|
|
|
|
break
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if ev.match(email):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
screen.addstr(y // 2 + 1, x // 2 - 5, "Invalid Email")
|
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == 10:
|
|
|
|
screenwipe(screen, sx=x // 2 + 3, sy=y // 2)
|
|
|
|
screen.refresh()
|
|
|
|
break
|
|
|
|
continue
|
|
|
|
return email
|
|
|
|
|
|
|
|
|
2022-11-24 07:46:04 +00:00
|
|
|
def new_add(screen, calledby=None):
|
2022-11-02 20:02:13 +00:00
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
2022-11-04 19:22:19 +00:00
|
|
|
screen.border()
|
2022-11-02 20:02:13 +00:00
|
|
|
y, x = screen.getmaxyx()
|
|
|
|
global quitting
|
|
|
|
screen.addstr(1, x // 2 - 8, "ACCOUNT CREATION")
|
|
|
|
add_name = user(
|
|
|
|
screen, y // 2 - 4, x // 2 - 10
|
|
|
|
) # calling fn user for username, password and email
|
|
|
|
add_password = password(screen, y // 2 - 2, x // 2 - 10)
|
2022-11-04 19:22:19 +00:00
|
|
|
add_email = email(screen, y // 2, x // 2 - 10)
|
2022-11-02 20:02:13 +00:00
|
|
|
add_gamerid = "".join(
|
|
|
|
random.choices(string.ascii_uppercase + string.digits, k=4)
|
|
|
|
) # Generates random 4 character alphanumeric
|
|
|
|
|
|
|
|
if add_name == None or add_password == None or add_email == None:
|
|
|
|
screen.refresh()
|
|
|
|
screen.addstr(
|
2022-11-04 19:22:19 +00:00
|
|
|
y // 2 + 2, 5, "Cancelling account creation. Returning to account menu..."
|
2022-11-02 20:02:13 +00:00
|
|
|
)
|
|
|
|
sleep(3)
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
quitting = False
|
|
|
|
screenhandler(screen)
|
|
|
|
else:
|
|
|
|
post(
|
|
|
|
f"INSERT INTO player_details\
|
|
|
|
(gamerid,\
|
|
|
|
username,\
|
|
|
|
email,\
|
|
|
|
password)\
|
|
|
|
VALUES ('{add_gamerid}',\
|
|
|
|
'{add_name}',\
|
|
|
|
'{add_email}',\
|
|
|
|
'{add_password}')"
|
|
|
|
)
|
|
|
|
screen.refresh()
|
2022-11-24 07:46:04 +00:00
|
|
|
if calledby is not None:
|
2022-11-02 20:02:13 +00:00
|
|
|
screen.addstr(
|
2022-11-04 19:22:19 +00:00
|
|
|
y // 2 + 2, 5, "Account has been created. Returning to login..."
|
2022-11-02 20:02:13 +00:00
|
|
|
)
|
|
|
|
screen.refresh()
|
|
|
|
sleep(3)
|
|
|
|
login(screen, calledby=calledby)
|
|
|
|
else:
|
|
|
|
screen.addstr(
|
2022-11-04 19:22:19 +00:00
|
|
|
y // 2 + 2, 5, "Account has been created. Returning to account menu..."
|
2022-11-02 20:02:13 +00:00
|
|
|
)
|
|
|
|
screen.refresh()
|
|
|
|
sleep(3)
|
|
|
|
screenhandler(screen)
|
|
|
|
|
|
|
|
|
2022-11-04 19:22:19 +00:00
|
|
|
def modify_account(screen):
|
2022-11-02 20:02:13 +00:00
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
2022-11-04 19:22:19 +00:00
|
|
|
screen.border()
|
2022-11-02 20:02:13 +00:00
|
|
|
y, x = screen.getmaxyx()
|
2022-11-04 19:22:19 +00:00
|
|
|
global loggedin, quitting, U
|
2022-11-02 20:02:13 +00:00
|
|
|
screen.addstr(1, x // 2 - 8, "MODIFY ACCOUNT SETTINGS")
|
|
|
|
if loggedin == False:
|
|
|
|
screen.addstr(
|
|
|
|
y // 2,
|
2022-11-04 19:22:19 +00:00
|
|
|
5,
|
2022-11-02 20:02:13 +00:00
|
|
|
"Please log in to you account... Redirecting you to the login menu",
|
|
|
|
)
|
|
|
|
screen.refresh()
|
|
|
|
sleep(3)
|
|
|
|
login(screen)
|
|
|
|
return
|
|
|
|
screen.addstr(3, x // 2 - 8, "What do you want to modify?")
|
|
|
|
screen.addstr(y // 2 - 3, x // 2 - 4, "1. Username")
|
|
|
|
screen.addstr(y // 2 - 1, x // 2 - 4, "2. Email")
|
|
|
|
screen.addstr(y // 2 + 1, x // 2 - 4, "esc. Quit")
|
|
|
|
|
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == 27:
|
|
|
|
break
|
|
|
|
|
|
|
|
elif key == ord("1"):
|
|
|
|
screenwipe(screen, 0, 2)
|
|
|
|
screen.refresh()
|
|
|
|
newuser = user(
|
|
|
|
screen,
|
|
|
|
y // 2,
|
|
|
|
x // 2 - 10,
|
|
|
|
optionaltxt="Enter new username: ",
|
|
|
|
)
|
|
|
|
post(
|
|
|
|
f"UPDATE player_details\
|
|
|
|
SET username = '{newuser}'\
|
|
|
|
WHERE gamerid = '{gamerid}' "
|
|
|
|
)
|
2022-11-04 19:22:19 +00:00
|
|
|
U = newuser
|
|
|
|
break
|
2022-11-02 20:02:13 +00:00
|
|
|
|
|
|
|
elif key == ord("2"):
|
|
|
|
screenwipe(screen, 0, 2)
|
|
|
|
screen.refresh()
|
2022-11-04 19:22:19 +00:00
|
|
|
newemail = email(
|
2022-11-02 20:02:13 +00:00
|
|
|
screen, y // 2, x // 2 - 10, optionaltxt="Enter new email: "
|
|
|
|
)
|
|
|
|
post(
|
|
|
|
f"UPDATE player_details\
|
|
|
|
SET email = '{newemail}'\
|
|
|
|
WHERE gamerid = '{gamerid}' "
|
|
|
|
)
|
2022-11-04 19:22:19 +00:00
|
|
|
break
|
2022-11-02 20:02:13 +00:00
|
|
|
|
|
|
|
screenwipe(screen, 0, 2)
|
|
|
|
screen.refresh()
|
|
|
|
screen.addstr(
|
|
|
|
y // 2, 5, "Details successfully updated. Returning to account menu..."
|
|
|
|
)
|
|
|
|
screen.refresh()
|
|
|
|
sleep(3)
|
|
|
|
screenhandler(screen)
|
2022-11-04 19:22:19 +00:00
|
|
|
return
|
2022-11-02 20:02:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def view_account(screen):
|
2022-11-26 18:09:00 +00:00
|
|
|
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
|
|
|
|
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
|
2022-11-02 20:02:13 +00:00
|
|
|
global loggedin, U, gamerid
|
|
|
|
y, x = screen.getmaxyx()
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
2022-11-04 19:22:19 +00:00
|
|
|
screen.border()
|
2022-11-26 18:09:00 +00:00
|
|
|
screen.addstr(1, x // 2 - 9, "VIEW ACCOUNT DETAILS")
|
2022-11-02 20:02:13 +00:00
|
|
|
if not loggedin:
|
|
|
|
screen.addstr(
|
|
|
|
y // 2,
|
2022-11-04 19:22:19 +00:00
|
|
|
5,
|
2022-11-02 20:02:13 +00:00
|
|
|
"Please log in to you account... Redirecting you to the login menu",
|
|
|
|
)
|
|
|
|
screen.refresh()
|
|
|
|
sleep(3)
|
|
|
|
login(screen)
|
|
|
|
return
|
|
|
|
player_details = get(
|
|
|
|
f"SELECT *\
|
2022-11-26 18:09:00 +00:00
|
|
|
FROM player_details\
|
|
|
|
WHERE gamerid = '{gamerid}' "
|
2022-11-02 20:02:13 +00:00
|
|
|
)
|
2022-11-26 18:09:00 +00:00
|
|
|
screen.addstr(3, x // 2 - 7, "Gamer ID: " + player_details[0][0])
|
|
|
|
screen.addstr(5, x // 2 - 8, "Username: " + player_details[0][1])
|
|
|
|
screen.addstr(7, x// 2 - 9, "Email: " + player_details[0][2])
|
|
|
|
j = 0
|
|
|
|
for i, tablename in enumerate(["maze_scores", "pong_scores", "snake_scores", "wordle_scores"]):
|
|
|
|
score_details = get(f"SELECT * FROM {tablename} WHERE gamerid = '{gamerid}'")
|
|
|
|
if not score_details:
|
|
|
|
score_details.append(("Bruh",) + ("Not yet available.",) * 3)
|
|
|
|
screen.addstr(8 + 3 * i + j + 1, x // 2 - 15, f"{tablename.split('_')[0].capitalize()}: ", curses.color_pair(3))
|
|
|
|
screen.addstr(8 + 3 * i + 1 + j, x // 2 - 7, "High Score: " + str(score_details[0][1]), curses.color_pair(2))
|
|
|
|
screen.addstr(9 + 3 * i + 1 + j, x // 2 - 7, "Last Played: " + str(score_details[0][2]))
|
|
|
|
screen.addstr(10 + 3 * i + 1 + j, x // 2 - 7, "Times Played: " + str(score_details[0][3]))
|
|
|
|
j += 1
|
2022-11-02 20:02:13 +00:00
|
|
|
|
2022-11-04 19:22:19 +00:00
|
|
|
screen.addstr(y - 1, 5, "Press esc to return to main menu.")
|
2022-11-02 20:02:13 +00:00
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == 27:
|
|
|
|
break
|
|
|
|
screen.refresh()
|
|
|
|
screenhandler(screen)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2022-11-04 19:22:19 +00:00
|
|
|
def delete(screen):
|
2022-11-02 20:02:13 +00:00
|
|
|
y, x = screen.getmaxyx()
|
|
|
|
global loggedin, gamerid, U
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
2022-11-04 19:22:19 +00:00
|
|
|
screen.border()
|
2022-11-02 20:02:13 +00:00
|
|
|
if loggedin == False:
|
|
|
|
screen.addstr(
|
|
|
|
y // 2,
|
2022-11-04 19:22:19 +00:00
|
|
|
5,
|
2022-11-02 20:02:13 +00:00
|
|
|
"Please log in to you account... Redirecting you to the login menu",
|
|
|
|
)
|
|
|
|
screen.refresh()
|
|
|
|
sleep(3)
|
|
|
|
login(screen)
|
|
|
|
return
|
2022-11-04 19:22:19 +00:00
|
|
|
screen.addstr(y // 2 - 3, 10, "Do you really want to delete your account?")
|
|
|
|
screen.addstr(y // 2 - 2, 10, "Press y to delete or n to return to account menu.")
|
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == ord("y"):
|
|
|
|
break
|
|
|
|
elif key == ord("n"):
|
|
|
|
screen.addstr(y // 2, 5, "Returning to the account menu")
|
|
|
|
sleep(3)
|
|
|
|
screenhandler(screen)
|
|
|
|
return
|
2022-11-02 20:02:13 +00:00
|
|
|
post(
|
|
|
|
f"DELETE FROM player_details\
|
|
|
|
WHERE gamerid = '{gamerid}'"
|
|
|
|
)
|
2022-11-04 19:22:19 +00:00
|
|
|
curses.ungetch(" ")
|
|
|
|
screen.addstr(y // 2, 10, "Account has been deleted. Returning to account menu...")
|
|
|
|
screen.refresh()
|
2022-11-02 20:02:13 +00:00
|
|
|
loggedin = False
|
|
|
|
gamerid = U = None
|
2022-11-04 19:22:19 +00:00
|
|
|
sleep(3)
|
|
|
|
screenhandler(screen)
|
|
|
|
return
|
2022-11-02 20:02:13 +00:00
|
|
|
|
|
|
|
|
2022-11-26 18:09:00 +00:00
|
|
|
def Update_score(score, game):
|
|
|
|
tablename = game + '_scores'
|
2022-11-02 20:02:13 +00:00
|
|
|
global U, gamerid, loggedin
|
|
|
|
if not loggedin:
|
|
|
|
return "guest"
|
2022-11-26 18:09:00 +00:00
|
|
|
res = get(f"SELECT * FROM {tablename} WHERE gamerid = '{gamerid}'")
|
2022-11-02 20:02:13 +00:00
|
|
|
if not res:
|
2022-11-26 18:09:00 +00:00
|
|
|
post(f"INSERT INTO {tablename} (gamerid, highscore, lastscore, timesplayed) VALUES ('{gamerid}',0, 0, 0)")
|
|
|
|
res = get(f"SELECT * FROM {tablename} WHERE gamerid = '{gamerid}'")
|
2022-11-02 20:02:13 +00:00
|
|
|
# implement to ask whether to update
|
2022-11-26 18:09:00 +00:00
|
|
|
if res[0][1] < score:
|
|
|
|
post(
|
|
|
|
f"UPDATE {tablename}\
|
|
|
|
SET highscore = '{score}',\
|
|
|
|
lastscore = {score}\
|
|
|
|
WHERE gamerid = '{gamerid}'"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
post(
|
|
|
|
f"UPDATE {tablename}\
|
|
|
|
SET lastscore = '{score}'\
|
|
|
|
WHERE gamerid = '{gamerid}'"
|
|
|
|
)
|
|
|
|
Update_lp_tp(tablename)
|
2022-11-02 20:02:13 +00:00
|
|
|
|
|
|
|
|
2022-11-26 18:09:00 +00:00
|
|
|
def Update_lp_tp(tablename):
|
2022-11-02 20:02:13 +00:00
|
|
|
global U, gamerid, loggedin
|
|
|
|
if not loggedin:
|
|
|
|
return "guest"
|
|
|
|
post(
|
2022-11-26 18:09:00 +00:00
|
|
|
f"UPDATE {tablename}\
|
2022-11-02 20:02:13 +00:00
|
|
|
SET lastplayed = Now(),\
|
|
|
|
timesplayed = timesplayed + 1\
|
|
|
|
WHERE gamerid = '{gamerid}'"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def forgotpassword(screen):
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
y, x = screen.getmaxyx()
|
|
|
|
screen.addstr(1, x // 2 - 7, "FORGOT PASSWORD")
|
|
|
|
screen.refresh()
|
|
|
|
global quitting
|
|
|
|
usernamelist = list_getter("username")
|
|
|
|
tries = 0
|
|
|
|
screen.addstr(y // 2 - 2, x // 2 - 7, "Username: ")
|
|
|
|
while True:
|
2022-11-04 19:22:19 +00:00
|
|
|
input_u = input(y // 2 - 2, x // 2 + 3, screen)
|
2022-11-02 20:02:13 +00:00
|
|
|
if quitting:
|
|
|
|
screen.addstr("Going back to account settings...")
|
|
|
|
sleep(3)
|
|
|
|
quitting = False
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
screenhandler(screen)
|
|
|
|
return
|
|
|
|
|
|
|
|
if input_u not in usernamelist:
|
|
|
|
screen.addstr(
|
2022-11-02 20:04:30 +00:00
|
|
|
y // 2 - 1,
|
2022-11-04 19:22:19 +00:00
|
|
|
5,
|
2022-11-02 20:04:30 +00:00
|
|
|
"Username does not exist. Press Enter/Space to continue...",
|
2022-11-02 20:02:13 +00:00
|
|
|
)
|
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == ord(" ") or key == 10:
|
2022-11-02 20:04:30 +00:00
|
|
|
screenwipe(screen, sy=y // 2 - 2, sx=x // 2 + 3)
|
2022-11-02 20:02:13 +00:00
|
|
|
screen.refresh()
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2022-11-02 20:04:30 +00:00
|
|
|
res = get(
|
|
|
|
f"SELECT email FROM player_details\
|
|
|
|
WHERE username = '{input_u}'"
|
|
|
|
)
|
2022-11-02 20:02:13 +00:00
|
|
|
email = res[0][0]
|
|
|
|
otp = sender(input_u, email)
|
2022-11-04 19:22:19 +00:00
|
|
|
screen.addstr(y // 2 + 1, 5, "Enter OTP recieved in registered mail address:")
|
2022-11-02 20:02:13 +00:00
|
|
|
while True:
|
|
|
|
screen.refresh()
|
2022-11-04 19:22:19 +00:00
|
|
|
enter_otp = input(y // 2 + 1, 47, screen)
|
2022-11-02 20:02:13 +00:00
|
|
|
if quitting:
|
|
|
|
screen.addstr("Going back to account settings...")
|
|
|
|
sleep(3)
|
|
|
|
quitting = False
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
screenhandler(screen)
|
|
|
|
return
|
|
|
|
if str(otp) == enter_otp:
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
screen.addstr(1, x // 2 - 7, "FORGOT PASSWORD")
|
|
|
|
while True:
|
2022-11-02 20:04:30 +00:00
|
|
|
enter_pass = password(
|
|
|
|
screen, y // 2 - 2, x // 2 - 7, optionaltxt="Enter new password: "
|
|
|
|
)
|
|
|
|
confirm_pass = password(
|
|
|
|
screen, y // 2 + 1, x // 2 - 7, optionaltxt="Confirm password: "
|
|
|
|
)
|
2022-11-02 20:02:13 +00:00
|
|
|
if enter_pass == confirm_pass:
|
|
|
|
break
|
|
|
|
else:
|
2022-11-02 20:04:30 +00:00
|
|
|
screen.addstr(
|
|
|
|
y // 2 + 3,
|
2022-11-04 19:22:19 +00:00
|
|
|
5,
|
2022-11-02 20:04:30 +00:00
|
|
|
"Passwords do not match. Press Enter to try again.",
|
|
|
|
)
|
2022-11-02 20:02:13 +00:00
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == 10:
|
2022-11-02 20:04:30 +00:00
|
|
|
screenwipe(screen, sy=y // 2 - 2, sx=x // 2 - 7)
|
2022-11-02 20:02:13 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
if tries < 10:
|
2022-11-02 20:04:30 +00:00
|
|
|
screen.addstr(
|
|
|
|
y // 2 + 3,
|
2022-11-04 19:22:19 +00:00
|
|
|
5,
|
2022-11-02 20:04:30 +00:00
|
|
|
"Entered OTP is wrong. Press esc to exit or Enter to try again.",
|
|
|
|
)
|
2022-11-02 20:02:13 +00:00
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == 10:
|
|
|
|
screenwipe(screen, sy=y // 2 + 1, sx=47)
|
|
|
|
tries += 1
|
|
|
|
screen.refresh()
|
|
|
|
break
|
|
|
|
elif key == 27:
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
screenhandler(screen)
|
|
|
|
return
|
|
|
|
continue
|
|
|
|
|
|
|
|
else:
|
2022-11-02 20:04:30 +00:00
|
|
|
screen.addstr(
|
|
|
|
y // 2 + 3,
|
2022-11-04 19:22:19 +00:00
|
|
|
5,
|
2022-11-02 20:04:30 +00:00
|
|
|
"Entered OTP is wrong. Maximum tries exceeded. Returning to account menu...",
|
|
|
|
)
|
2022-11-02 20:02:13 +00:00
|
|
|
sleep(5)
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
screenhandler(screen)
|
|
|
|
return
|
|
|
|
break
|
|
|
|
|
2022-11-02 20:04:30 +00:00
|
|
|
post(
|
|
|
|
f"UPDATE player_details\
|
2022-11-02 20:02:13 +00:00
|
|
|
SET password = '{enter_pass}'\
|
2022-11-02 20:04:30 +00:00
|
|
|
WHERE username = '{input_u}'"
|
|
|
|
)
|
2022-11-02 20:02:13 +00:00
|
|
|
screen.addstr(y // 2 + 3, x // 2 - 10, "Password has been changed successfully.")
|
|
|
|
screen.addstr(y // 2 + 4, x // 2 - 8, "Returning to account menu...")
|
|
|
|
sleep(3)
|
|
|
|
screenhandler(screen)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2022-11-04 11:21:32 +00:00
|
|
|
def logout(screen):
|
|
|
|
y, x = screen.getmaxyx()
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
2022-11-27 17:59:24 +00:00
|
|
|
scree.border()
|
2022-11-04 11:21:32 +00:00
|
|
|
screen.addstr(1, x // 2 - 2, "LOGOUT")
|
|
|
|
screen.addstr(y // 2, 5, "Logging out of your account...")
|
|
|
|
global loggedin, U, gamerid
|
|
|
|
loggedin = False
|
|
|
|
U = gamerid = None
|
|
|
|
screen.refresh()
|
|
|
|
sleep(5)
|
|
|
|
screen.clear()
|
|
|
|
screen.refresh()
|
|
|
|
screenhandler(screen)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2022-11-02 20:02:13 +00:00
|
|
|
def leaderboard(screen):
|
|
|
|
y, x = screen.getmaxyx()
|
2022-11-27 17:59:24 +00:00
|
|
|
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
|
|
|
|
curses.init_pair(6, curses.COLOR_CYAN, curses.COLOR_BLACK)
|
|
|
|
screen.keypad(True)
|
|
|
|
leaderboardquit = True
|
|
|
|
tables = {0:"maze_scores", 1:"pong_scores", 2:"snake_scores", 3:"wordle_scores"}
|
|
|
|
current_page = 0
|
|
|
|
while leaderboardquit:
|
|
|
|
screen.clear()
|
|
|
|
screen.border()
|
|
|
|
screen.refresh()
|
|
|
|
screen.addstr(1, x // 2 - 5, "LEADERBOARD", curses.A_BOLD)
|
|
|
|
screen.addstr(3, x // 2 - 2, f"{tables[current_page].split('_')[0].upper()}", curses.color_pair(6) | curses.A_BOLD)
|
|
|
|
res = get(
|
|
|
|
f"SELECT p.gamerid,\
|
|
|
|
p.username,\
|
|
|
|
s.highscore \
|
|
|
|
FROM player_details p,\
|
|
|
|
{tables[current_page]} s\
|
|
|
|
WHERE p.gamerid = s.gamerid "
|
|
|
|
)
|
2022-11-02 20:02:13 +00:00
|
|
|
|
2022-11-27 17:59:24 +00:00
|
|
|
for i in range(len(res) - 1): # Sorting
|
|
|
|
for j in range(len(res) - 1 - i):
|
|
|
|
if res[j][2] < res[j + 1][2]:
|
|
|
|
res[j], res[j + 1] = res[j + 1], res[j]
|
|
|
|
|
|
|
|
screen.addstr(5, 13, "Gamer ID", curses.color_pair(3))
|
|
|
|
screen.addstr(5, 30, "Username", curses.color_pair(3))
|
|
|
|
screen.addstr(5, 50, "High Score", curses.color_pair(3))
|
|
|
|
sy = 7
|
|
|
|
for i in res:
|
|
|
|
screen.addstr(sy, 13, str(i[0]))
|
|
|
|
screen.addstr(sy, 30, str(i[1]))
|
|
|
|
screen.addstr(sy, 50, str(i[2]), curses.color_pair(2) | curses.A_BOLD)
|
|
|
|
sy += 1
|
|
|
|
screen.addstr(y - 2, 2, "Use arrow keys for different pages.", curses.A_DIM)
|
|
|
|
screen.addstr(y - 2, x - 36, "Press [esc] to return to main menu.", curses.A_DIM)
|
|
|
|
while True:
|
|
|
|
key = screen.getch()
|
|
|
|
if key == 27:
|
|
|
|
leaderboardquit = False
|
|
|
|
break
|
|
|
|
elif key == curses.KEY_LEFT and current_page > 0:
|
|
|
|
current_page -= 1
|
|
|
|
break
|
|
|
|
elif key == curses.KEY_RIGHT and current_page < 3:
|
|
|
|
current_page += 1
|
|
|
|
break
|
2022-11-02 20:02:13 +00:00
|
|
|
screen.refresh()
|
2022-11-24 07:46:04 +00:00
|
|
|
maze.menu.menu(screen)
|
2022-11-02 20:02:13 +00:00
|
|
|
return
|