Add sorting functionality to extra tables, homogenize code

This commit is contained in:
Ahrimdon 2024-03-03 04:50:38 -05:00
parent a8bb459638
commit b60e23c73c

View File

@ -433,18 +433,16 @@ def clients_table(existing_cur, new_cur, limit=None, sort_order='DESC'):
new_cur.execute("INSERT INTO Clients (Connections, Name, FirstConnection, Game, LastConnection, Level, Masked, TotalConnectionTime, IP) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", new_row) new_cur.execute("INSERT INTO Clients (Connections, Name, FirstConnection, Game, LastConnection, Level, Masked, TotalConnectionTime, IP) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", new_row)
def ip_address_table(existing_cur, new_cur, limit=None, sort_order='DESC'): def ip_address_table(existing_cur, new_cur, limit=None, sort_order='DESC'):
def fetch_client_info(src_cur): query = f"""
src_cur.execute(""" SELECT Name, SearchableIPAddress, DateAdded
SELECT Name, SearchableIPAddress, DateAdded FROM EFAlias FROM EFAlias
""") ORDER BY DateAdded {sort_order}
client_info = [] """
for row in src_cur.fetchall(): if limit:
name = row[0].replace('^7', '') query += f" LIMIT {limit}"
client_info.append((name, row[1], row[2]))
return client_info existing_cur.execute(query)
client_info = [(row[0].replace('^7', ''), row[1], row[2]) for row in existing_cur.fetchall()]
client_info = fetch_client_info(existing_cur)
new_cur.execute(""" new_cur.execute("""
CREATE TABLE IF NOT EXISTS "IPAddresses" ( CREATE TABLE IF NOT EXISTS "IPAddresses" (
@ -473,23 +471,22 @@ def inbox_messages_modified_table(existing_cur, new_cur, limit=None, sort_order=
) )
""") """)
existing_cur.execute("SELECT * FROM InboxMessages") query = f"""
SELECT InboxMessageId, CreatedDateTime, SourceClientId, DestinationClientId, ServerId, Message, IsDelivered
FROM InboxMessages
ORDER BY InboxMessageId {sort_order}
"""
if limit:
query += f" LIMIT {limit}"
existing_cur.execute(query)
inbox_messages = existing_cur.fetchall() inbox_messages = existing_cur.fetchall()
for msg in inbox_messages: for msg in inbox_messages:
msg_id, created, _, source_client_id, dest_client_id, server_id, message, is_delivered = msg msg_id, created, source_client_id, dest_client_id, server_id, message, is_delivered = msg
for client_id in (source_client_id, dest_client_id):
existing_cur.execute("SELECT CurrentAliasId FROM EFClients WHERE ClientId = ?", (client_id,))
alias_id = existing_cur.fetchone()[0]
existing_cur.execute("SELECT Name FROM EFAlias WHERE AliasId = ?", (alias_id,))
name = existing_cur.fetchone()[0].replace('^7', '')
if client_id == source_client_id:
origin = name
else:
target = name
origin = fetch_client_name(existing_cur, source_client_id)
target = fetch_client_name(existing_cur, dest_client_id)
read = "Yes" if is_delivered == 1 else "No" read = "Yes" if is_delivered == 1 else "No"
new_cur.execute(""" new_cur.execute("""
@ -497,6 +494,17 @@ def inbox_messages_modified_table(existing_cur, new_cur, limit=None, sort_order=
VALUES (?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?)
""", (msg_id, created, origin, target, server_id, message, read)) """, (msg_id, created, origin, target, server_id, message, read))
def fetch_client_name(cur, client_id):
cur.execute("SELECT CurrentAliasId FROM EFClients WHERE ClientId = ?", (client_id,))
alias_id = cur.fetchone()
if alias_id:
alias_id = alias_id[0]
cur.execute("SELECT Name FROM EFAlias WHERE AliasId = ?", (alias_id,))
name = cur.fetchone()
if name:
return name[0].replace('^7', '')
return 'Unknown'
def maps_table(existing_cur, new_cur, limit=None, sort_order='DESC'): def maps_table(existing_cur, new_cur, limit=None, sort_order='DESC'):
new_cur.execute(""" new_cur.execute("""
CREATE TABLE IF NOT EXISTS "Maps" ( CREATE TABLE IF NOT EXISTS "Maps" (