|
@@ -2,6 +2,8 @@ import sqlite3 as db
|
|
|
import sys
|
|
|
import uuid
|
|
|
|
|
|
+from game import money_amount
|
|
|
+
|
|
|
connection = None
|
|
|
cursor = None
|
|
|
|
|
@@ -30,21 +32,24 @@ def setup():
|
|
|
print('Database setup...')
|
|
|
|
|
|
replace = True
|
|
|
+
|
|
|
if replace:
|
|
|
- cursor.execute("DROP TABLE users")
|
|
|
- cursor.execute("DROP TABLE stocks")
|
|
|
- cursor.execute("DROP TABLE ownership")
|
|
|
- cursor.execute("DROP TABLE sessions")
|
|
|
+ print(' Dropping old tables...')
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS users")
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS ownables")
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS ownership")
|
|
|
+ cursor.execute("DROP TABLE IF EXISTS sessions")
|
|
|
|
|
|
+ print(' Creating tables...')
|
|
|
cursor.execute('''
|
|
|
CREATE TABLE IF NOT EXISTS users(
|
|
|
username VARCHAR(10) UNIQUE NOT NULL,
|
|
|
password VARCHAR(6) NOT NULL)
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
- CREATE TABLE IF NOT EXISTS stocks(
|
|
|
+ CREATE TABLE IF NOT EXISTS ownables(
|
|
|
name VARCHAR(10) UNIQUE NOT NULL,
|
|
|
- total_available INTEGER NOT NULL)
|
|
|
+ total_amount INTEGER NOT NULL)
|
|
|
''')
|
|
|
cursor.execute('''
|
|
|
CREATE TABLE IF NOT EXISTS ownership(
|
|
@@ -52,7 +57,7 @@ def setup():
|
|
|
stock_id INTEGER NOT NULL,
|
|
|
amount INTEGER NOT NULL DEFAULT 0,
|
|
|
FOREIGN KEY (user_id) REFERENCES users(rowid),
|
|
|
- FOREIGN KEY (stock_id) REFERENCES stocks(rowid),
|
|
|
+ FOREIGN KEY (stock_id) REFERENCES ownables(rowid),
|
|
|
UNIQUE (user_id, stock_id)
|
|
|
)
|
|
|
''')
|
|
@@ -64,10 +69,43 @@ def setup():
|
|
|
)
|
|
|
''')
|
|
|
|
|
|
+ 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):
|
|
|
connect()
|
|
|
|
|
|
+ # do not allow login as bank
|
|
|
+ if password == '':
|
|
|
+ return None
|
|
|
+
|
|
|
cursor.execute('''
|
|
|
SELECT rowid
|
|
|
FROM users
|
|
@@ -83,12 +121,16 @@ def login(username, password):
|
|
|
|
|
|
def register(username, password):
|
|
|
connect()
|
|
|
-
|
|
|
+ if username == '':
|
|
|
+ return False
|
|
|
+ if password == '':
|
|
|
+ return False
|
|
|
cursor.execute('''
|
|
|
INSERT INTO users
|
|
|
(username, password)
|
|
|
VALUES (? , ?)
|
|
|
''', (username, password))
|
|
|
+ return True
|
|
|
|
|
|
|
|
|
def new_session(user_id):
|
|
@@ -119,6 +161,8 @@ def drop_old_sessions():
|
|
|
|
|
|
|
|
|
def user_exists(username):
|
|
|
+ connect()
|
|
|
+
|
|
|
cursor.execute('''
|
|
|
SELECT rowid
|
|
|
FROM users
|
|
@@ -129,3 +173,9 @@ def user_exists(username):
|
|
|
return True
|
|
|
else:
|
|
|
return False
|
|
|
+
|
|
|
+
|
|
|
+def move_money(username):
|
|
|
+ connect()
|
|
|
+
|
|
|
+ cursor.execute()
|