From 80e00e0f41e472e5c4fd330596f2668e2d98c2de Mon Sep 17 00:00:00 2001 From: adithyagenie Date: Thu, 17 Nov 2022 19:32:25 +0530 Subject: [PATCH] Final changes, introducing sample database dump and username-pass initialisation for mysql --- credentials.pickle | Bin 81 -> 157 bytes dbdump.sql | 79 +++++++++++++++++++++++++++++++ maze/modules/PlayerBase_func.py | 26 ++++++++-- maze/modules/about.py | 7 +-- maze/modules/maze_saveandload.py | 4 +- starter.py | 76 ++++++++++++++++++++++++++++- 6 files changed, 178 insertions(+), 14 deletions(-) create mode 100644 dbdump.sql diff --git a/credentials.pickle b/credentials.pickle index 795acdb350a395373b650f6b910ad3e8fda6d23d..f759867745281a4d225e736d832c831e4073c9df 100644 GIT binary patch delta 106 zcmWHX%c$JIGBub10&1sd^l&5>rKXfr7Nkz;VNK0V%*>gR!8B3K+8(IP1c$QR%HqPD aDLpKu#i>O=swh9d1V|Mm78k>KrFsCi%_EWk delta 29 gcmbQs7%0}jGS!U%0&1sd^suJpCT8YLnW$_H0A?}=x&QzG diff --git a/dbdump.sql b/dbdump.sql new file mode 100644 index 0000000..1324873 --- /dev/null +++ b/dbdump.sql @@ -0,0 +1,79 @@ +-- MySQL dump 10.13 Distrib 5.7.39, for Win64 (x86_64) +-- +-- Host: localhost Database: labyrinth +-- ------------------------------------------------------ +-- Server version 5.7.39-log + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `player_details` +-- + +DROP TABLE IF EXISTS `player_details`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `player_details` ( + `gamerid` char(4) NOT NULL, + `username` varchar(32) NOT NULL, + `email` varchar(32) NOT NULL, + `password` varchar(32) NOT NULL, + PRIMARY KEY (`gamerid`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `player_details` +-- + +LOCK TABLES `player_details` WRITE; +/*!40000 ALTER TABLE `player_details` DISABLE KEYS */; +INSERT INTO `player_details` VALUES ('1AB3','ABC_123','abc@gmail.com','Q0FCXzMyMQ=='),('2BC4','DEF_456','def@gmail.com','RkVEXzY1NA=='),('3CD5','GHI_789','mb8717@boysmgp.onmicrosoft.com','SUhHXzk4Nw=='),('4DE6','JKL_100','mb11859@boysmgp.onmicrosoft.com','TEtKXzAwMQ=='); +/*!40000 ALTER TABLE `player_details` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `scores` +-- + +DROP TABLE IF EXISTS `scores`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `scores` ( + `gamerid` char(4) NOT NULL, + `highscore` int(11) DEFAULT NULL, + `lastplayed` date DEFAULT NULL, + `timesplayed` int(11) DEFAULT NULL, + PRIMARY KEY (`gamerid`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `scores` +-- + +LOCK TABLES `scores` WRITE; +/*!40000 ALTER TABLE `scores` DISABLE KEYS */; +INSERT INTO `scores` VALUES ('1AB3',85012,'2022-11-02',3),('2BC4',90012,'2022-11-12',2),('4DE6',90120,'2022-10-24',5); +/*!40000 ALTER TABLE `scores` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2022-11-17 18:35:12 diff --git a/maze/modules/PlayerBase_func.py b/maze/modules/PlayerBase_func.py index bf1f6ca..1f3a72b 100644 --- a/maze/modules/PlayerBase_func.py +++ b/maze/modules/PlayerBase_func.py @@ -1,8 +1,10 @@ import curses import os +import pickle import random import re import string +import sys from base64 import b64decode, b64encode from time import sleep @@ -17,6 +19,15 @@ U = gamerid = None quitting = False sql = con = None +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"] + except EOFError: + pass def get( @@ -39,14 +50,19 @@ def post( def databaseinit(): # Creates database if it doesn't exist try: - tempsql = mysql.connector.connect(host="localhost", user="root", passwd="root") + tempsql = mysql.connector.connect( + host="localhost", user=MYSQL_USERNAME, passwd=MYSQL_PASSWORD + ) tempcon = tempsql.cursor() tempcon.execute("CREATE DATABASE IF NOT EXISTS labyrinth") tempsql.commit() global sql, con sql = mysql.connector.connect( - host="localhost", user="root", passwd="root", database="labyrinth" + host="localhost", + user=MYSQL_USERNAME, + passwd=MYSQL_PASSWORD, + database="labyrinth", ) con = sql.cursor() return True @@ -54,8 +70,8 @@ def databaseinit(): # Creates database if it doesn't exist mysql.connector.errors.ProgrammingError, mysql.connector.errors.DatabaseError, ): - print("Invalid password/username.") - return False + print("Invalid MySQL password/username.") + sys.exit() def tableinit(): @@ -780,7 +796,7 @@ def leaderboard(screen): screen.addstr(sy, 50, str(i[2])) screen.addstr(sy, 70, str(i[3])) sy += 1 - screen.addstr(y - 1, x - 35, "Press esc to return to main menu.") + screen.addstr(y - 2, x - 35, "Press esc to return to main menu.") while True: key = screen.getch() if key == 27: diff --git a/maze/modules/about.py b/maze/modules/about.py index f64c6a9..08b9e2d 100644 --- a/maze/modules/about.py +++ b/maze/modules/about.py @@ -26,12 +26,9 @@ def about(screen): screen.addstr( 11, 5, - "This game makes use of the 'curses' module which runs on any operating system in the native terminal without" - ) - screen.addstr( - 12, 5, - "use of any other external modules." + "This game makes use of the 'curses' module which runs on any operating system in the native terminal without", ) + screen.addstr(12, 5, "use of any other external modules.") screen.addstr( 13, 5, diff --git a/maze/modules/maze_saveandload.py b/maze/modules/maze_saveandload.py index b599767..6695171 100644 --- a/maze/modules/maze_saveandload.py +++ b/maze/modules/maze_saveandload.py @@ -19,9 +19,9 @@ def save(screen, maze, coords): names[i] = names[i].replace(".maze", "") for i in range(len(names)): for j in range(len(names) - i - 1): - if int(names[j].split('_')[1]) > int(names[j + 1].split('_')[1]): + if int(names[j].split("_")[1]) > int(names[j + 1].split("_")[1]): names[j], names[j + 1] = names[j + 1], names[j] - num = int(names[-1].split('_')[1]) + 1 + num = int(names[-1].split("_")[1]) + 1 if not name: name = "default" filename = f"maze_{str(num)}_{name}.maze" diff --git a/starter.py b/starter.py index 3ca4be9..364c7b9 100644 --- a/starter.py +++ b/starter.py @@ -1,3 +1,75 @@ -from maze.modules import bruh +import os +import pickle +import subprocess +import sys -bruh() +import mysql.connector + +from maze.modules import bruh +from maze.modules.PlayerBase_func import databaseinit + +with open("credentials.pickle", "rb") as f: + try: + while True: + d = pickle.load(f) + if d["credtype"] == "mysql": + user = d["user"] + password = d["pass"] + break + except EOFError: + user = password = None + + +def getcreds(): + if user and password: + return user, password + else: + print( + """MySQL username or password not initialised. Defaulting to 'root' and ''. +Run 'python starter.py initsql' to initialise credentials of your choice. """ + ) + with open("credentials.pickle", "rb+") as f: + try: + while True: + pos = f.tell() + d = pickle.load(f) + if d["credtype"] == "mysql": + d["user"] = "root" + d["pass"] = "" + f.seek(pos) + pickle.dump(d, f) + except EOFError: + pass + return None, None + + +if len(sys.argv) == 1: + getcreds() + bruh() + sys.exit() +else: + if sys.argv[1] == "dumpsample": + getcreds() + databaseinit() + subprocess.call( + f"mysql -u root --password=root -D labyrinth < {os.path.abspath('dbdump.sql')}", + shell=True, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + elif sys.argv[1] == "initsql": + user = input("Enter MySQL username: ") + password = input("Enter MySQL password: ") + with open("credentials.pickle", "rb+") as f: + try: + while True: + pos = f.tell() + d = pickle.load(f) + if d["credtype"] == "mysql": + d["user"] = user + d["pass"] = password + f.seek(pos) + pickle.dump(d, f) + except EOFError: + pass + print("Successfully set.")