1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2024-11-09 12:00:17 +01:00
server-nalc/other_things/scripts/SQLextract.py

156 lines
3.1 KiB
Python
Raw Normal View History

2015-07-09 15:32:54 +02:00
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys, os, sqlite3
home = os.environ.get("HOME")
db = "%s/rollback/rollback.sqlite" % (home)
2015-07-09 16:50:24 +02:00
#############
## Node IDs
#
2015-07-09 15:32:54 +02:00
2015-07-09 16:50:24 +02:00
def get_node_ids():
2015-07-09 15:32:54 +02:00
try:
conn = sqlite3.connect(db)
cur = conn.cursor()
except sqlite3.OperationalError as err:
print(err)
sys.exit(1)
try:
2015-07-09 16:50:24 +02:00
cur.execute("SELECT * FROM node")
2015-07-09 15:32:54 +02:00
except sqlite3.OperationalError as err:
print(err)
sys.exit(1)
else:
result = cur.fetchall()
finally:
cur.close()
conn.close()
return(result)
2015-07-09 16:50:24 +02:00
node_ids = get_node_ids()
2015-07-09 15:32:54 +02:00
2015-07-09 16:50:24 +02:00
def get_node(nodeid):
if not nodeid:
return None
else:
return node_ids[nodeid-1][1]
2015-07-09 15:32:54 +02:00
2015-07-09 16:50:24 +02:00
###############
## Player IDs
#
2015-07-09 15:32:54 +02:00
2015-07-09 16:50:24 +02:00
def get_player_ids():
2015-07-09 15:32:54 +02:00
try:
conn = sqlite3.connect(db)
cur = conn.cursor()
except sqlite3.OperationalError as err:
print(err)
sys.exit(1)
try:
2015-07-09 16:50:24 +02:00
cur.execute("SELECT * FROM actor")
2015-07-09 15:32:54 +02:00
except sqlite3.OperationalError as err:
print(err)
sys.exit(1)
else:
result = cur.fetchall()
finally:
cur.close()
conn.close()
2015-07-09 16:50:24 +02:00
return result
2015-07-09 15:32:54 +02:00
2015-07-09 16:50:24 +02:00
player_ids = get_player_ids()
2015-07-09 15:32:54 +02:00
2015-07-09 16:50:24 +02:00
def get_player(id):
if id:
return player_ids[id-1][1][len("player:"):]
else:
2015-07-09 15:32:54 +02:00
return None
2015-07-09 16:50:24 +02:00
########################################################################
## Utilities
#
2015-07-09 15:32:54 +02:00
2015-07-09 16:50:24 +02:00
def ston(a):
"""
Returns a string equal to a or None
"""
if a:
return str(a)
2015-07-09 15:32:54 +02:00
else:
2015-07-09 16:50:24 +02:00
return a
2015-07-09 15:32:54 +02:00
2015-07-09 16:50:24 +02:00
def select_all_nodes(stamp):
2015-07-09 15:32:54 +02:00
try:
conn = sqlite3.connect(db)
2015-07-09 16:50:24 +02:00
conn.text_factory = str
2015-07-09 15:32:54 +02:00
cur = conn.cursor()
2015-07-09 16:50:24 +02:00
except sqlite3.OperationalError as err:
print(err)
sys.exit(1)
2015-07-09 15:32:54 +02:00
try:
2015-07-09 16:50:24 +02:00
cur.execute("SELECT * FROM action WHERE timestamp >=:stamp", {"stamp":stamp})
2015-07-09 15:32:54 +02:00
except sqlite3.OperationalError as err:
print(err)
2015-07-09 16:50:24 +02:00
sys.exit(1)
2015-07-09 15:32:54 +02:00
else:
2015-07-09 16:50:24 +02:00
result = cur.fetchall()
2015-07-09 15:32:54 +02:00
finally:
cur.close()
conn.close()
2015-07-09 16:50:24 +02:00
return(result)
2015-07-09 15:32:54 +02:00
def write_list(nodes):
try:
name = "database-output.txt"
f = open(name, "w")
except Exception as err:
print(err)
sys.exit(1)
for node in nodes:
2015-07-09 16:50:24 +02:00
# Init bracket and basic datas
2015-07-09 15:32:54 +02:00
f.write("[")
f.write("id=%d" % node[0])
f.write(";actor=%s" % get_player(node[1]))
f.write(";type=%d" % node[3])
2015-07-09 16:50:24 +02:00
# Inventory Manipulation
2015-07-09 15:32:54 +02:00
f.write(";list=%s" % ston(node[4]))
f.write(",index=%s" % ston(node[5]))
f.write(",add=%s" % ston(node[6]))
f.write(",stacknode=%s" % get_node(node[7]))
f.write(",stackquantity=%s" % ston(node[8]))
f.write(",nodemeta=%s" % ston(node[9]))
2015-07-09 16:50:24 +02:00
# Position
2015-07-09 15:32:54 +02:00
f.write(";x=%s,y=%s,z=%s" % (node[10], node[11], node[12]))
2015-07-09 16:50:24 +02:00
# Old node
2015-07-09 15:32:54 +02:00
f.write(";oldnode=%s" % get_node(node[13]))
f.write(",oldparam1=%s" % node[14])
f.write(",oldparam2=%s" % ston(node[15]))
f.write(",oldmeta=%s" % ston(node[16]))
2015-07-09 16:50:24 +02:00
# New node
2015-07-09 15:32:54 +02:00
f.write(":newnode=%s" % get_node(node[17]))
f.write(",newparam1=%s" % ston(node[18]))
f.write(",newparam2=%s" % ston(node[19]))
f.write(",newmeta=%s" % ston(node[20]))
2015-07-09 16:50:24 +02:00
# Ending
2015-07-09 15:32:54 +02:00
f.write("]\n")
f.close()
2015-07-09 16:50:24 +02:00
########################################################################
## Main
#
2015-07-09 15:32:54 +02:00
if __name__ == '__main__':
#select all nodes player as set i where time >= time
all_nodes = select_all_nodes(0) #1418613979)
# write in file
# for x in range(0,len(all_nodes)):
# print(all_nodes[x])
write_list(all_nodes)