· 8 years ago · Mar 01, 2018, 12:12 AM
1#Thanks to Freddukes for this!
2class SQLiteManager(object):
3 def __init__(self, pathFile):
4 if isinstance(pathFile, Path):
5 self.pathFile = pathFile
6 else:
7 self.pathFile = Path(pathFile)
8
9 self.connection = sqlite.connect(self.pathFile.joinpath('players.sqlite'))
10 self.cursor = self.connection.cursor()
11
12 self.connection.text_factory = str
13 self.execute("PRAGMA synchronous=NORMAL")
14 #self.execute("PRAGMA journal_mode=OFF")
15 self.execute("PRAGMA locking_mode=EXCLUSIVE")
16 self.execute("PRAGMA auto_vacuum=FULL")
17
18 self.execute("""\
19 CREATE TABLE IF NOT EXISTS Players (
20 UserID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
21 steamid VARCHAR(30) NOT NULL,
22 currace VARCHAR(30) NOT NULL,
23 name VARCHAR(30) NOT NULL,
24 totallevel INTEGER DEFAULT 0,
25 lastconnect INTEGER
26 )""")
27
28 self.execute("CREATE INDEX IF NOT EXISTS playersIndex ON Players(steamid)")
29
30 self.execute("""\
31 CREATE TABLE IF NOT EXISTS Races (
32 RaceID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
33 UserID INTEGER NOT NULL,
34 name VARCHAR(50) NOT NULL,
35 skills VARCHAR(50) NOT NULL,
36 level INTEGER DEFAULT 0,
37 xp INTEGER DEFAULT 0,
38 unused INTEGER DEFAULT 0
39 )""")
40
41 self.execute("CREATE INDEX IF NOT EXISTS racesIndex ON Races(UserID)")
42
43 def __len__(self):
44 self.execute("SELECT COUNT(*) FROM Players")
45 return int(self.cursor.fetchone()[0])
46
47 def __contains__(self, user):
48 if isinstance(user, (long, int)): #Tha Pwned
49 self.execute("SELECT steamid FROM Players WHERE UserID = ?", (user, ))
50 else:
51 self.execute("SELECT steamid FROM Players WHERE steamid = ?", (user, ))
52 return bool(self.cursor.fetchone())
53
54 #def __del__(self): #Tha Pwned (just remove the 3 lines here)
55 # self.save()
56 # self.close()
57
58 def execute(self, statement, args=None):
59 if args is None:
60 self.cursor.execute(statement)
61 else:
62 self.cursor.execute(statement, args)
63
64 def fetchone(self):
65 result = self.cursor.fetchone()
66 if hasattr(result, '__iter__'):
67 if len(result) == 1:
68 return result[0]
69 return result
70
71 def fetchall(self):
72 trueValues = []
73 for value in self.cursor.fetchall():
74 if isinstance(value, tuple):
75 if len(value) > 1:
76 trueValues.append(value)
77 else:
78 trueValues.append(value[0])
79 else:
80 trueValues.append(value)
81 return trueValues
82
83 def save(self):
84 self.connection.commit()
85
86 def close(self):
87 self.cursor.close()
88 self.connection.close()
89
90 def getUserIdFromSteamId(self, steamid):
91 self.execute("SELECT UserID FROM Players WHERE steamid = ?", (steamid, ))
92 value = self.cursor.fetchone()
93 if value is None:
94 return None
95
96 return value[0]
97
98 def addPlayer(self, steamid, name):
99 self.execute("INSERT INTO Players (steamid, currace, name, totallevel, lastconnect) VALUES (?,?,?,0,?)", (steamid, standardrace, self.removeWarnings(name), time.time())) #Tha Pwned
100 return self.cursor.lastrowid
101
102 def getRaceIdFromUserIdAndRace(self, userid, race):
103 if not isinstance(userid, (long, int)): #Tha Pwned
104 userid = self.getUserIdFromSteamId(userid)
105
106 self.execute("SELECT RaceID FROM Races WHERE UserID = ? AND name = ?", (userid, race))
107 value = self.cursor.fetchone()
108 if value is None:
109 return None
110
111 return value[0]
112
113 def addRaceIntoPlayer(self, userid, name):
114 if not isinstance(userid, (long, int)): #Tha Pwned
115 userid = self.getUserIdFromSteamId(userid)
116
117 self.execute("INSERT INTO Races (UserID, name, skills) VALUES (?,?,'')", (userid, name)) #Tha Pwned
118 return self.cursor.lastrowid
119
120 def updateRank(self):
121 self.execute("SELECT steamid FROM Players ORDER BY totallevel DESC")
122 results = self.cursor.fetchall()
123 self.ranks = []
124
125 for steamid in results:
126 self.ranks.append(steamid[0])
127
128 def getRank(self, steamid):
129 if steamid in self.ranks:
130 return self.ranks.index(steamid) + 1
131 return self.__len__()
132
133 def removeWarnings(self, value):
134 return str(value).replace("'", "").replace('"', '')
135#database = SQLiteManager(Path(es.getAddonPath('wcs')))
136database = SQLiteManager(Path(ini.path).joinpath('data'))
137
138
139tmp = {}
140#def getPlayer(userid):
141#def getPlayer(userid, steamid, name):
142def getPlayer(userid):
143 userid = int(userid)
144 if not userid in tmp:
145 tmp[userid] = PlayerObject(userid)
146 #tmp[userid] = PlayerObject(userid, steamid, name)
147 return tmp[userid]