1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2024-09-30 00:10:33 +02:00

Another update of the SQLite Extractor Script

- Added division of files
This commit is contained in:
LeMagnesium 2015-07-09 22:09:27 +02:00
parent 4d21dcf5b0
commit 01b997cfce

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
import sys, os, sqlite3 import sys, os, sqlite3
import encodings import encodings, calendar, time
from cStringIO import StringIO from cStringIO import StringIO
home = os.environ.get("HOME") home = os.environ.get("HOME")
@ -10,154 +10,157 @@ db = "%s/rollback/rollback.sqlite" % (home)
#[id=16000;actor=crabman;type=2;list=main,index=13,add=1, stacknode=default:glass,stackquantity=9,nodemeta=0;x=None, y=None,z=None,oldnode=,oldparam1=None,oldparam2=None,oldmeta=None:newnode=,newparam1=None,newparam2=None,newmeta=None] #[id=16000;actor=crabman;type=2;list=main,index=13,add=1, stacknode=default:glass,stackquantity=9,nodemeta=0;x=None, y=None,z=None,oldnode=,oldparam1=None,oldparam2=None,oldmeta=None:newnode=,newparam1=None,newparam2=None,newmeta=None]
class Convert_id(object): class Convert_id(object):
def __init__(self, base): def __init__(self, base):
self.__players = dict() self.__players = dict()
self.__nodes = dict() self.__nodes = dict()
try: try:
conn = sqlite3.connect(db) conn = sqlite3.connect(db)
cur = conn.cursor() cur = conn.cursor()
except Exception as err: except Exception as err:
print(err) print(err)
print("problème avec la base de données") print("problème avec la base de données")
sys.exit(1) sys.exit(1)
else: else:
try: try:
cur.execute("SELECT * from actor") cur.execute("SELECT * from actor")
except sqlite3.OperationalError as err: except sqlite3.OperationalError as err:
print(err) print(err)
else: else:
result = cur.fetchall() result = cur.fetchall()
for res in result: for res in result:
self.__players[res[0]] = res[1][len("player:"):] self.__players[res[0]] = res[1][len("player:"):]
try: try:
cur.execute("SELECT * from node") cur.execute("SELECT * from node")
except sqlite3.OperationalError as err: except sqlite3.OperationalError as err:
print(err) print(err)
else: else:
result = cur.fetchall() result = cur.fetchall()
for res in result: for res in result:
self.__nodes[res[0]] = res[1] self.__nodes[res[0]] = res[1]
finally: finally:
cur.close() cur.close()
conn.close() conn.close()
def get_player_name(self, player_id): def get_player_name(self, player_id):
if self.__players.has_key(player_id): if self.__players.has_key(player_id):
return self.__players[player_id] return self.__players[player_id]
else: else:
return("") return("")
def get_node_name(self, node_id):
if self.__nodes.has_key(node_id):
return self.__nodes[node_id]
else:
return("")
def get_node_name(self, node_id):
if self.__nodes.has_key(node_id):
return self.__nodes[node_id]
else:
return("")
######################################################################## ########################################################################
## Utilities ## Utilities
# #
def ston(a): def ston(a):
""" """
Returns a string equal to a or None Returns a string equal to a or None
""" """
if a: if a:
return str(a) return str(a)
else: else:
return "" return ""
def select_all_nodes(stamp):
try: def select_all_nodes(startstamp, endstamp):
conn = sqlite3.connect(db) try:
conn.text_factory = str conn = sqlite3.connect(db)
cur = conn.cursor() conn.text_factory = str
except sqlite3.OperationalError as err: cur = conn.cursor()
print(err) except sqlite3.OperationalError as err:
sys.exit(1) print(err)
try: sys.exit(1)
cur.execute("SELECT * FROM action WHERE timestamp >=:stamp", {"stamp":stamp}) try:
except sqlite3.OperationalError as err: cur.execute("SELECT * FROM action WHERE timestamp >=:startstamp AND timestamp < :endstamp", {"startstamp":startstamp, "endstamp":endstamp})
print(err) except sqlite3.OperationalError as err:
sys.exit(1) print(err)
else: sys.exit(1)
result = cur.fetchall() else:
finally: result = cur.fetchall()
cur.close() finally:
conn.close() cur.close()
return(result) conn.close()
return(result)
# Big-endian!!! # Big-endian!!!
def readU16(strm): def readU16(strm):
return (ord(strm.read(1)) << 16) + (ord(strm.read(1))) return (ord(strm.read(1)) << 16) + (ord(strm.read(1)))
def readU32(strm): def readU32(strm):
return (ord(strm.read(1)) << 24) + (ord(strm.read(1)) << 16) + (ord(strm.read(1)) << 8) + (ord(strm.read(1))) return (ord(strm.read(1)) << 24) + (ord(strm.read(1)) << 16) + (ord(strm.read(1)) << 8) + (ord(strm.read(1)))
def decode(chaine): def decode(chaine):
if not chaine : if not chaine :
return "[]" return "[]"
else: else:
strm = StringIO(chaine) strm = StringIO(chaine)
table = "[" table = "["
nEntries = readU32(strm) nEntries = readU32(strm)
for n in range(nEntries): for n in range(nEntries):
keyLen = readU16(strm) keyLen = readU16(strm)
key = strm.read(keyLen) key = strm.read(keyLen)
valLen = readU32(strm) valLen = readU32(strm)
val = strm.read(valLen) val = strm.read(valLen)
# Beware of potential quotes in meta, they must be escaped # Beware of potential quotes in meta, they must be escaped
# Fortunately their escape codes are the same in Python and Lua # Fortunately their escape codes are the same in Python and Lua
# (and pretty much every language influenced by C) # (and pretty much every language influenced by C)
# ------------ # ------------
# Attention aux potentiels quotes dedans les meta, il faut les escape # Attention aux potentiels quotes dedans les meta, il faut les escape
# Heureusement leur mode d'escape est quasi le même en Python que Lua # Heureusement leur mode d'escape est quasi le même en Python que Lua
table += '%s="%s"' % (key, val.encode('string-escape').replace('"', '\\"')) table += '%s="%s"' % (key, val.encode('string-escape').replace('"', '\\"'))
if n != nEntries-1: if n != nEntries-1:
table += ", " table += ", "
table += "]" table += "]"
return table return table
def to_table(node): def to_table(node):
# Init bracket and basic datas # Init bracket and basic datas
table = '[' table = '['
table += 'id=%d' % node[0] table += 'id=%d' % node[0]
table += ', actor="%s"' % Id.get_player_name(node[1]) table += ',actor="%s"' % Id.get_player_name(node[1])
table += ', type=%d' % node[3] table += ',type=%d' % node[3]
# Inventory Manipulation # Inventory Manipulation
table += ', list="%s"' % ston(node[4]) table += ';list="%s"' % ston(node[4])
table += ', index="%s"' % ston(node[5]) table += ',index="%s"' % ston(node[5])
table += ', add="%s"' % ston(node[6]) table += ',add="%s"' % ston(node[6])
table += ', stacknode="%s"' % Id.get_node_name(node[7]) table += ',stacknode="%s"' % Id.get_node_name(node[7])
table += ', stackquantity="%s"' % ston(node[8]) table += ',stackquantity="%s"' % ston(node[8])
table += ', nodemeta=%s' % ston(node[9]) table += ',nodemeta=%s' % node[9]
# Position # Position
table += ', x=%s, y=%s, z=%s' % (node[10], node[11], node[12]) table += ';x=%s, y=%s, z=%s' % (node[10], node[11], node[12])
# Old node # Old node
table += ', oldnode="%s"' % Id.get_node_name(node[13]) table += ';oldnode="%s"' % Id.get_node_name(node[13])
table += ', oldparam1="%s"' % node[14] table += ',oldparam1="%s"' % node[14]
table += ', oldparam2="%s"' % ston(node[15]) table += ',oldparam2="%s"' % ston(node[15])
table += ', oldmeta=%s' % decode(node[16]) table += ',oldmeta=%s' % decode(node[16])
# New node # New node
table += ', newnode="%s"' % Id.get_node_name(node[17]) table += ';newnode="%s"' % Id.get_node_name(node[17])
table += ', newparam1="%s"' % ston(node[18]) table += ',newparam1="%s"' % ston(node[18])
table += ', newparam2="%s"' % ston(node[19]) table += ',newparam2="%s"' % ston(node[19])
table += ', newmeta=%s' % decode(node[20]) table += ',newmeta=%s' % decode(node[20])
# Ending # Ending
table += ']\n' table += ']\n'
return (table) return (table)
def write_list(nodes): def write_list(nodes, i):
try: try:
name = "database-output.txt" name = "rollback/database-output.%s.txt" % i
f = open(name, "w") f = open(name, "w")
except Exception as err: except Exception as err:
print(err) print(err)
sys.exit(1) sys.exit(1)
for node in nodes: for node in nodes:
table = to_table(node) table = to_table(node)
f.write(table) f.write(table)
f.close() f.close()
######################################################################## ########################################################################
@ -165,12 +168,15 @@ def write_list(nodes):
# #
if __name__ == '__main__': if __name__ == '__main__':
Id = Convert_id(db) Id = Convert_id(db)
#select all nodes player as set i where time >= time #select all nodes player as set i where time >= time
all_nodes = select_all_nodes(0) #1418613979) timestamp = 1426978800
i = 0
# write in file while timestamp <= calendar.timegm(time.gmtime()):
# for x in range(0,len(all_nodes)): all_nodes = select_all_nodes(timestamp, timestamp+24*60*60)
# print(all_nodes[x]) if len(all_nodes) > 0:
write_list(all_nodes) write_list(all_nodes, i)
i += 1
print("%s to %s => %s entries" % (timestamp, timestamp+24*60*60, len(all_nodes)))
timestamp += 24*60*60