Skip to content

Instantly share code, notes, and snippets.

@Hidendra
Created December 23, 2014 07:01
Show Gist options
  • Save Hidendra/492e1a97a3c1f6fe879d to your computer and use it in GitHub Desktop.
Save Hidendra/492e1a97a3c1f6fe879d to your computer and use it in GitHub Desktop.
Converts protection database from the feature/811-uuid-conversion branch back to master.
import MySQLdb
import json
conn = MySQLdb.connect(host = "127.0.0.1", user = "root", passwd = "", db = "lwc")
"""
Protections:
RENAME TABLE lwc_protections TO lwc_protections_old_uuid;
CREATE TABLE `lwc_protections` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`owner` varchar(255) DEFAULT NULL,
`type` int(11) DEFAULT NULL,
`x` int(11) DEFAULT NULL,
`y` int(11) DEFAULT NULL,
`z` int(11) DEFAULT NULL,
`data` text,
`blockId` int(11) DEFAULT NULL,
`world` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`date` varchar(255) DEFAULT NULL,
`last_accessed` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `protections_main` (`x`,`y`,`z`,`world`),
KEY `protections_utility` (`owner`),
KEY `protections_type` (`type`)
) ENGINE=MyISAM AUTO_INCREMENT=125385 DEFAULT CHARSET=latin1;
"""
cursor = conn.cursor()
### Load players
players = {} # intId => id (uuid || username)
cursor.execute("SELECT * FROM lwc_players")
for row in cursor.fetchall():
id = row[0]
uuid = row[1]
username = row[2]
players[id] = uuid or username
print "%d => %s" % (id, players[id])
### Convert protections
cursor.execute("SELECT COUNT(*) from lwc_protections_old_uuid")
total = cursor.fetchone()[0]
pos = 0
limit = 100
while True:
cursor.execute("SELECT * FROM lwc_protections_old_uuid LIMIT %s, %s", (pos, limit))
if cursor.rowcount == 0:
break
pos += cursor.rowcount
print 'Loaded %d/%d rows (%.2f%%)' % (pos, total, float(pos) / total * 100)
for row in cursor.fetchall():
id = row[0]
owner = row[1] # needs conversion
type = row[2]
x = row[3]
y = row[4]
z = row[5]
# flags = row[6] # some dbs have this, but can be discarded (junk)
data = row[6] # data.rights needs conversion
blockId = row[7]
world = row[8]
password = row[9]
date = row[10]
last_accessed = row[11]
# convert data
try:
data_json = json.loads(data)
if 'rights' in data_json:
for right in data_json['rights']:
if right['type'] == 1: # Player
right['name'] = players[int(right['name'])]
except TypeError:
data_json = "{}"
converted_owner = players[owner]
converted_data = json.dumps(data_json)
query = """
INSERT INTO lwc_protections
(id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed)
VALUES
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(query, [id, converted_owner, type, x, y, z, converted_data, blockId, world, password, date, last_accessed])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment