Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Importing modules
- import sqlite3
- # Constants
- DB_ROOT = "D:\\Sixth Form A2\\OCR Computing A2\\F454 - Computing Coursework\\game_data_db"
- # *** *** DATABASE *** *** #
- # Creates database/table
- def init_db():
- """
- Runs on startup of main
- program, creates table.
- """
- open_db()
- command = ("""
- CREATE TABLE IF NOT EXISTS players
- (username TEXT PRIMARY KEY,
- password TEXT,
- score INTEGER,
- kills INTEGER,
- time INTEGER)
- """)
- cur.execute(command)
- close_db()
- # Connects to database
- def open_db():
- """
- Makes the database and cursor
- global, connects to database.
- """
- global game_data_db
- game_data_db = sqlite3.connect(DB_ROOT)
- global cur
- cur = game_data_db.cursor()
- # Closes database
- def close_db():
- """
- Commits any changes to database and
- closes it.
- """
- game_data_db.commit()
- game_data_db.close()
- # Clears database
- def clear_db():
- """
- Clears all data from inside
- players table.
- """
- open_db()
- command = ("""
- DELETE FROM players
- """)
- cur.execute(command)
- close_db()
- # Displays all data in database
- def display_db():
- """
- Prints to the shell all
- data contained in database.
- """
- open_db()
- command = ("""
- SELECT * FROM players
- """)
- cur.execute(command)
- for row in cur:
- print(row)
- close_db()
- # Orders database by high score
- def order_db():
- """
- Orders the database rows,
- so first row has highest
- score.
- """
- open_db()
- command = ("""
- SELECT * FROM players
- ORDER BY score ASC
- """)
- cur.execute(command)
- close_db()
- # Adds new player to database
- def update_new_player(username, password):
- """
- Takes username and password as input,
- creates new record with blank score,
- then saves to database.
- """
- open_db()
- param = [(username, password, None, None, None)]
- command = ("""
- INSERT INTO players
- VALUES (?, ?, ?, ?, ?)
- """)
- cur.executemany(command, param)
- close_db()
- # Deletes player from database
- def delete_player(username):
- """
- Deletes the record for a player
- who's name matches the username
- input.
- """
- open_db()
- param = [username]
- command = ("""
- DELETE FROM players
- WHERE username = ?
- """)
- cur.execute(command, param)
- close_db()
- # Updates score attribute
- def update_player_score(username, score):
- """
- Takes username and a new score as
- input, checks to see if new score
- is larger than current score stored,
- if so updates database record to
- highest score.
- """
- open_db()
- result = retrieve_player_score(username)
- if result is None or result < score:
- param = [score, username]
- command = ("""
- UPDATE players
- SET score = ?
- WHERE username = ?
- """)
- cur.execute(command, param)
- close_db()
- # Retrieves the score for a player
- def retrieve_player_score(username):
- """
- Takes username as input.
- Retrieves score from record
- where usename matches input.
- """
- open_db()
- param = [username]
- command = ("""
- SELECT score FROM players
- WHERE username = ?
- """)
- result = cur.execute(command, param).fetchone()
- if result is not None:
- return result[0]
- close_db()
- # Updates kills attribute
- def update_player_kills(username, kills):
- """
- Takes username and a new kill count as
- input, checks to see if new count
- is larger than current kills stored,
- if so updates database record to
- highest kill count.
- """
- open_db()
- result = retrieve_player_kills(username)
- if result is None or result < kills:
- param = [kills, username]
- command = ("""
- UPDATE players
- SET kills = ?
- WHERE username = ?
- """)
- cur.execute(command, param)
- close_db()
- # Retrieves the highest kills for a player
- def retrieve_player_kills(username):
- """
- Takes username as input.
- Retrieves kills from record
- where usename matches input.
- """
- open_db()
- param = [username]
- command = ("""
- SELECT kills FROM players
- WHERE username = ?
- """)
- result = cur.execute(command, param).fetchone()
- if result is not None:
- return result[0]
- close_db()
- # Updates time attribute
- def update_player_time(username, time):
- """
- Takes username and a new time as
- input, checks to see if new time
- is larger than current time stored,
- if so updates database record to
- largest time.
- """
- open_db()
- result = retrieve_player_time(username)
- if result is None or result < time:
- param = [time, username]
- command = ("""
- UPDATE players
- SET time = ?
- WHERE username = ?
- """)
- cur.execute(command, param)
- close_db()
- # Retrieves the highest time for a player
- def retrieve_player_time(username):
- """
- Takes username as input.
- Retrieves time from record
- where usename matches input.
- """
- open_db()
- param = [username]
- command = ("""
- SELECT time FROM players
- WHERE username = ?
- """)
- result = cur.execute(command, param).fetchone()
- if result is not None:
- return result[0]
- close_db()
- # Retrieves the password for a player
- def retrieve_player_pass(username):
- """
- Takes username as input.
- Retrieves password from record
- where username matches input.
- """
- open_db()
- param = [username]
- command = ("""
- SELECT password FROM players
- WHERE username = ?
- """)
- result = cur.execute(command, param).fetchone()
- if result is not None:
- return result[0]
- close_db()
- # Retrieves a username from the database
- def retrieve_player_name(username):
- """
- Takes username as input.
- Retrieves username from record
- where usernames match.
- """
- open_db()
- param = [username]
- command = ("""
- SELECT username FROM players
- WHERE username = ?
- """)
- result = cur.execute(command, param).fetchone()
- if result is not None:
- return result[0]
- close_db()
- # *** *** VALIDATION *** *** #
- # Checks if name and password match up, logs in if so
- def validate_player_login(username, password):
- """
- Takes username and password.
- Checks database for a record where
- username and password match input,
- and returns True or False.
- """
- if username == retrieve_player_name(username):
- if password == retrieve_player_pass(username):
- return True
- else:
- return False
- else:
- return False
- # Checks if name and password for new user are valid
- def validate_player_details(username, password):
- """
- Takes username and password as input.
- Checks length and content for
- validity.
- """
- username = str(username)
- if len(username) is not 3:
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement