Final changes, introducing sample database dump and username-pass initialisation for mysql

This commit is contained in:
adithyagenie 2022-11-17 19:32:25 +05:30
parent c8264cf347
commit 80e00e0f41
6 changed files with 178 additions and 14 deletions

Binary file not shown.

79
dbdump.sql Normal file
View file

@ -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

View file

@ -1,8 +1,10 @@
import curses import curses
import os import os
import pickle
import random import random
import re import re
import string import string
import sys
from base64 import b64decode, b64encode from base64 import b64decode, b64encode
from time import sleep from time import sleep
@ -17,6 +19,15 @@ U = gamerid = None
quitting = False quitting = False
sql = con = None 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( def get(
@ -39,14 +50,19 @@ def post(
def databaseinit(): # Creates database if it doesn't exist def databaseinit(): # Creates database if it doesn't exist
try: 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 = tempsql.cursor()
tempcon.execute("CREATE DATABASE IF NOT EXISTS labyrinth") tempcon.execute("CREATE DATABASE IF NOT EXISTS labyrinth")
tempsql.commit() tempsql.commit()
global sql, con global sql, con
sql = mysql.connector.connect( 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() con = sql.cursor()
return True return True
@ -54,8 +70,8 @@ def databaseinit(): # Creates database if it doesn't exist
mysql.connector.errors.ProgrammingError, mysql.connector.errors.ProgrammingError,
mysql.connector.errors.DatabaseError, mysql.connector.errors.DatabaseError,
): ):
print("Invalid password/username.") print("Invalid MySQL password/username.")
return False sys.exit()
def tableinit(): def tableinit():
@ -780,7 +796,7 @@ def leaderboard(screen):
screen.addstr(sy, 50, str(i[2])) screen.addstr(sy, 50, str(i[2]))
screen.addstr(sy, 70, str(i[3])) screen.addstr(sy, 70, str(i[3]))
sy += 1 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: while True:
key = screen.getch() key = screen.getch()
if key == 27: if key == 27:

View file

@ -26,12 +26,9 @@ def about(screen):
screen.addstr( screen.addstr(
11, 11,
5, 5,
"This game makes use of the 'curses' module which runs on any operating system in the native terminal without" "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(12, 5, "use of any other external modules.")
screen.addstr( screen.addstr(
13, 13,
5, 5,

View file

@ -19,9 +19,9 @@ def save(screen, maze, coords):
names[i] = names[i].replace(".maze", "") names[i] = names[i].replace(".maze", "")
for i in range(len(names)): for i in range(len(names)):
for j in range(len(names) - i - 1): 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] 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: if not name:
name = "default" name = "default"
filename = f"maze_{str(num)}_{name}.maze" filename = f"maze_{str(num)}_{name}.maze"

View file

@ -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.")