|
@@ -1,19 +1,44 @@
|
|
|
+import re
|
|
|
import sqlite3 as db
|
|
|
import sys
|
|
|
import uuid
|
|
|
|
|
|
from game import money_amount
|
|
|
+from util import debug
|
|
|
|
|
|
connection = None
|
|
|
cursor = None
|
|
|
+db_name = None
|
|
|
|
|
|
|
|
|
-def connect():
|
|
|
+def query_save_name():
|
|
|
+ global db_name
|
|
|
+ if debug:
|
|
|
+ db_name = 'test.db'
|
|
|
+ return
|
|
|
+ while True:
|
|
|
+ save_name = input('Name of the savegame: ')
|
|
|
+ if re.match(r"[A-Za-z0-9.-]{0,50}", save_name):
|
|
|
+ db_name = save_name + '.db'
|
|
|
+ return
|
|
|
+
|
|
|
+
|
|
|
+def connect(reconnect=False):
|
|
|
global connection
|
|
|
global cursor
|
|
|
+ global db_name
|
|
|
+ if reconnect:
|
|
|
+ # connection.commit()
|
|
|
+ # connection.close()
|
|
|
+ cursor = None
|
|
|
+ connection = None
|
|
|
+ db_name = None
|
|
|
+
|
|
|
if connection is None or cursor is None:
|
|
|
+ query_save_name()
|
|
|
+
|
|
|
try:
|
|
|
- connection = db.connect('boerse.db')
|
|
|
+ connection = db.connect(db_name)
|
|
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
@@ -31,7 +56,7 @@ def setup():
|
|
|
|
|
|
print('Database setup...')
|
|
|
|
|
|
- replace = True
|
|
|
+ replace = False
|
|
|
|
|
|
if replace:
|
|
|
print(' Dropping old tables...')
|
|
@@ -39,6 +64,7 @@ def setup():
|
|
|
cursor.execute("DROP TABLE IF EXISTS ownables")
|
|
|
cursor.execute("DROP TABLE IF EXISTS ownership")
|
|
|
cursor.execute("DROP TABLE IF EXISTS sessions")
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS transactions")
|
|
|
|
|
|
print(' Creating tables...')
|
|
|
cursor.execute('''
|
|
@@ -68,35 +94,43 @@ def setup():
|
|
|
FOREIGN KEY (user_id) REFERENCES users(rowid)
|
|
|
)
|
|
|
''')
|
|
|
-
|
|
|
- print(' Adding initial data...')
|
|
|
- cursor.execute('''
|
|
|
- INSERT INTO users
|
|
|
- (username, password)
|
|
|
- VALUES ("bank", "")
|
|
|
- ''')
|
|
|
- cursor.execute('''
|
|
|
- SELECT rowid
|
|
|
- FROM users
|
|
|
- WHERE username = "bank"
|
|
|
- ''')
|
|
|
- bank_id = cursor.fetchone()[0]
|
|
|
cursor.execute('''
|
|
|
- INSERT INTO ownables
|
|
|
- (name, total_amount)
|
|
|
- VALUES ("Kollar", ?)
|
|
|
- ''', (money_amount,))
|
|
|
- cursor.execute('''
|
|
|
- SELECT rowid
|
|
|
- FROM users
|
|
|
- WHERE username = "bank"
|
|
|
+ CREATE TABLE IF NOT EXISTS sessions(
|
|
|
+ user_id INTEGER NOT NULL,
|
|
|
+ session_id STRING NOT NULL,
|
|
|
+ FOREIGN KEY (user_id) REFERENCES users(rowid)
|
|
|
+ )
|
|
|
''')
|
|
|
- kollar_id = cursor.fetchone()[0]
|
|
|
- cursor.execute('''
|
|
|
- INSERT INTO ownership
|
|
|
- (user_id, stock_id, amount)
|
|
|
- VALUES (?, ?, ?)
|
|
|
- ''', (bank_id, kollar_id, money_amount))
|
|
|
+
|
|
|
+ if replace:
|
|
|
+ print(' Adding initial data...')
|
|
|
+ cursor.execute('''
|
|
|
+ INSERT INTO users
|
|
|
+ (username, password)
|
|
|
+ VALUES ("bank", "")
|
|
|
+ ''')
|
|
|
+ cursor.execute('''
|
|
|
+ SELECT rowid
|
|
|
+ FROM users
|
|
|
+ WHERE username = "bank"
|
|
|
+ ''')
|
|
|
+ bank_id = cursor.fetchone()[0]
|
|
|
+ cursor.execute('''
|
|
|
+ INSERT INTO ownables
|
|
|
+ (name, total_amount)
|
|
|
+ VALUES ("Kollar", ?)
|
|
|
+ ''', (money_amount,))
|
|
|
+ cursor.execute('''
|
|
|
+ SELECT rowid
|
|
|
+ FROM users
|
|
|
+ WHERE username = "bank"
|
|
|
+ ''')
|
|
|
+ kollar_id = cursor.fetchone()[0]
|
|
|
+ cursor.execute('''
|
|
|
+ INSERT INTO ownership
|
|
|
+ (user_id, stock_id, amount)
|
|
|
+ VALUES (?, ?, ?)
|
|
|
+ ''', (bank_id, kollar_id, money_amount))
|
|
|
|
|
|
|
|
|
def login(username, password):
|