-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializer.py
More file actions
86 lines (72 loc) · 2.86 KB
/
Serializer.py
File metadata and controls
86 lines (72 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sqlite3, zlib
class Serializer:
# Constructor
def __init__(self, target):
self.name = "Worlds/" + target + '.db'
self.conn = sqlite3.connect(self.name)
c = self.conn.cursor()
try:
# Create Table
c.execute('''CREATE TABLE terrain(keys INTEGER NOT NULL PRIMARY KEY, binry TEXT)''')
self.conn.commit()
c.execute('''CREATE TABLE player(playername TEXT NOT NULL PRIMARY KEY, pickledplayer TEXT)''')
self.conn.commit()
except:
pass
# Save magic method
def __setitem__(self, key, chunkObj):
"""
Saves/Updates the string at a particular key location.
Requires the key as an int and chunkObj as UTF-8 string.
"""
c = self.conn.cursor()
try:
# Save string at new key location
c.execute('''INSERT INTO terrain VALUES (?,?)''', (key, zlib.compress(chunkObj)))
self.conn.commit()
except:
# Update string at existing key
c.execute('UPDATE terrain SET binry =? WHERE keys=?', (zlib.compress(chunkObj), key))
self.conn.commit()
# Load magic method
def __getitem__(self, key):
"""
Retrieves the string stored at a particular key location.
Requires the key as an int.
Returns the string at the key's location (if key is present) or None
"""
c = self.conn.cursor()
c.execute('''SELECT binry FROM terrain WHERE keys=?''', (key,))
res = c.fetchone()
self.conn.commit()
try: return zlib.decompress(res[0])
except: return res
def savePlayer(self, name, pickled):
"""
Saves/Updates the pickledplayer at a particular playername.
Requires the name as a string and pickled as UTF-8 string.
"""
c = self.conn.cursor()
try:
# Save pickledplayer at new playername
c.execute('''INSERT INTO player VALUES (?,?)''', (name, zlib.compress(pickled)))
self.conn.commit()
except:
# Update pickledplayer at existing playername
c.execute('UPDATE player SET pickledplayer =? WHERE playername=?', (zlib.compress(pickled), name))
self.conn.commit()
def loadPlayer(self, name):
"""
Retrieves the pickledplayer stored at a particular playername.
Requires the name as a string.
Returns the pickledplayer at the playername's location (if present) or None
"""
c = self.conn.cursor()
c.execute('''SELECT pickledplayer FROM player WHERE playername=?''', (name,))
res = c.fetchone()
self.conn.commit()
try: return zlib.decompress(res[0])
except: return res
# Close the connection
def stop(self):
self.conn.close()