Browse Source

Fix in the data importers to use the separate userdata DB.

Iñigo Valentin 6 years ago
parent
commit
53af86a1f2
2 changed files with 34 additions and 24 deletions
  1. 22 19
      application/bin/log-profile.py
  2. 12 5
      application/bin/log-run.py

+ 22 - 19
application/bin/log-profile.py

@@ -55,9 +55,6 @@ def verifyKey(db, data, key):
     try:
         uid = data["wizard_info"]["wizard_id"]
         cursor = db.cursor()
-        #print('SELECT count(uid) AS c FROM player WHERE uid = ? AND api_key = ?;')
-        #print(uid)
-        #print(key)
         cursor.execute('SELECT count(uid) AS c FROM player WHERE uid = ? AND api_key = ?;', (uid, key))
         if cursor.fetchone()[0] == 1:
             status = True
@@ -77,10 +74,15 @@ It also disables referencial integrity.
 :raises IntegrityError: The queryes couldn't bre executed.
 :raises IOError: The sqlite file couldn't be created.
 """
-def openDatabase(name):
+def openDatabase():
+    kdb = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../sw.sqlite'
+    udb = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../../data/sw.sqlite'
     print('Configuring database...')
     try:
-        db = sqlite3.connect(name)
+        db = sqlite3.connect(kdb)
+        cursor = db.cursor()
+        cursor.execute("attach '" + udb + "' as data;")
+        cursor.close()
         return db
     except sqlite3.IntegrityError as e:
         print("Error executing statement: " + str(e))
@@ -101,21 +103,22 @@ It also disables referencial integrity.
 def clearData(db, data):
     print('Clearing previous data...')
     try:
-        uid = data["wizard_info"]["wizard_id"]
+        uid = str(data["wizard_info"]["wizard_id"])
+        print('UID: ' + str(uid))
         cursor = db.cursor()
         cursor.execute('PRAGMA foreign_keys = OFF;')
-        cursor.execute('DELETE FROM scenario WHERE uid = ?', (uid))
-        cursor.execute('DELETE FROM defense WHERE uid = ?', (uid))
-        cursor.execute('DELETE FROM unit WHERE uid = ?', (uid))
-        cursor.execute('DELETE FROM unit_skill WHERE uid = ?', (uid))
-        cursor.execute('DELETE FROM rune WHERE uid = ?', (uid))
-        cursor.execute('DELETE FROM building WHERE uid = ?', (uid))
-        cursor.execute('DELETE FROM decoration WHERE uid = ?', (uid))
-        cursor.execute('DELETE FROM inventory WHERE uid = ?', (uid))
-        #cursor.execute('DELETE FROM summon_special')
-        #cursor.execute('DELETE FROM guild')
-        cursor.execute('DELETE FROM guild_member WHERE uid = ?', (uid))
-        cursor.execute('DELETE FROM rune_craft WHERE uid = ?', (uid))
+        cursor.execute('DELETE FROM scenario WHERE uid = ?', [uid])
+        cursor.execute('DELETE FROM defense WHERE uid = ?', [uid])
+        cursor.execute('DELETE FROM unit_skill WHERE unit IN (SELECT id FROM unit WHERE uid = ?)', [uid])
+        cursor.execute('DELETE FROM unit WHERE uid = ?', [uid])
+        cursor.execute('DELETE FROM rune WHERE uid = ?', [uid])
+        cursor.execute('DELETE FROM building WHERE uid = ?', [uid])
+        cursor.execute('DELETE FROM decoration WHERE uid = ?', [uid])
+        cursor.execute('DELETE FROM inventory WHERE uid = ?', [uid])
+        cursor.execute('DELETE FROM summon_special') # For every player
+        cursor.execute('DELETE FROM guild') # TODO: Dont delete all guilds
+        cursor.execute('DELETE FROM guild_member') # TODO: Dont delete all guilds, fix table
+        cursor.execute('DELETE FROM rune_craft WHERE uid = ?', [uid])
         db.commit()
         cursor.close();
     except sqlite3.IntegrityError as e:
@@ -1061,7 +1064,7 @@ Begin script
 """
 data = readData()
 key = readKey()
-db = openDatabase(os.path.dirname(os.path.realpath(sys.argv[0])) + '/../sw.sqlite')
+db = openDatabase()
 if verifyKey(db, data, key) == False:
     print("Invalid API KEY...")
     sys.exit(-1)

+ 12 - 5
application/bin/log-run.py

@@ -68,16 +68,23 @@ Opens the database file and deletes from the user tables
 :raises IntegrityError: The queryes couldn't bre executed.
 :raises IOError: The sqlite file couldn't be created.
 """
-def openDatabase(name):
+def openDatabase():
+    kdb = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../sw.sqlite'
+    udb = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../../data/sw.sqlite'
     print('Configuring database...')
     try:
-        db = sqlite3.connect(name)
+        db = sqlite3.connect(kdb)
+        cursor = db.cursor()
+        cursor.execute("attach '" + udb + "' as data;"')
+        cursor.close()
         return db
+    except sqlite3.IntegrityError as e:
+        print("Error executing statement: " + str(e))
+        raise
     except IOError as e:
-        print("I/O Error connecting to database " + name + ": " + str(e))
+        print("I/O Error creating database " + name + ": " + str(e))
         raise
 
-
 """
 Inserts a row into the database.
 
@@ -179,7 +186,7 @@ Begin script
 """
 data = readData()
 key = readKey()
-db = openDatabase(os.path.dirname(os.path.realpath(sys.argv[0])) + '/../sw.sqlite')
+db = openDatabase()
 if verifyKey(db, data, key) == False:
     print("Invalid API KEY...")
     sys.exit(-1)