From 267151f54666260facde5d42828eae6d4045b559 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Tue, 21 Mar 2023 14:21:29 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D0=BC=D0=BF=D0=BE=D1=80=D1=82=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D1=81=D0=BE=D0=BD=D0=B0=D0=B6=D0=B5=D0=B9,=20?= =?UTF-8?q?=D0=B5=D1=81=D0=BB=D0=B8=20=D0=B8=D0=B3=D1=80=D0=BE=D0=BA=20?= =?UTF-8?q?=D1=81=D1=83=D1=89=D0=B5=D1=81=D1=82=D0=B2=D1=83=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- import.py | 120 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 89 insertions(+), 31 deletions(-) diff --git a/import.py b/import.py index 383c2bd..d754386 100644 --- a/import.py +++ b/import.py @@ -6,6 +6,9 @@ from datetime import timedelta from configparser import ConfigParser +G_FLAG_FULL_IMPORT = False + + class ParseConfig(Exception): pass @@ -58,7 +61,19 @@ def import14_job(job_name, priority, profile_id, conn) -> None: }) -def import14_profile(profile, preference_id, conn) -> None: +def last_slot_profile(preference_id, conn) -> int: + with conn.cursor() as cur: + cur.execute("select slot from profile " + "where preference_id = %(preference_id)s " + "order by slot desc " + "limit 1", + { + "preference_id": preference_id + }) + return cur.fetchone()[0] + + +def import14_profile(slot, profile, preference_id, conn) -> None: _markings = "[" _f = True for v in profile["markings"]: @@ -83,7 +98,7 @@ def import14_profile(profile, preference_id, conn) -> None: "%(flavor_text)s) " "RETURNING profile_id", { - "slot": profile["slot"], + "slot": slot, "char_name": profile["name"], "age": profile["age"], "sex": profile["sex"], @@ -116,54 +131,97 @@ def import14_profile(profile, preference_id, conn) -> None: def import14_preference(player, conn) -> int: with conn.cursor() as cur: - cur.execute("INSERT into preference (user_id, " - "selected_character_slot, admin_ooc_color) " - "VALUES (%(user_id)s, %(slot)s, %(ooc_color)s) " - "RETURNING preference_id", - { - "user_id": player["uuid"], - "slot": player["preference"]["selected_slot"], - "ooc_color": player["preference"]["ooc_color"] - }) + if G_FLAG_FULL_IMPORT: + cur.execute("INSERT into preference (user_id, " + "selected_character_slot, admin_ooc_color) " + "VALUES (%(user_id)s, %(slot)s, %(ooc_color)s) " + "RETURNING preference_id", + { + "user_id": player["uuid"], + "slot": player["preference"]["selected_slot"], + "ooc_color": player["preference"]["ooc_color"] + }) + else: + cur.execute("select preference_id from preference " + "where user_id = %(uuid)s", + { + "uuid": player["uuid"], + }) return cur.fetchone()[0] def import14_playtime(tracker, timespent, uuid, conn) -> None: + time_spent = timedelta(seconds=timespent) + with conn.cursor() as cur: - cur.execute("INSERT into play_time (player_id, tracker, time_spent) " - "VALUES (%(player_id)s, %(tracker)s, %(time_spent)s)", + cur.execute("select time_spent " + "from play_time " + "where player_id = %(uuid)s " + "and tracker like %(tracker)s", { - "player_id": uuid, - "tracker": tracker, - "time_spent": timedelta(seconds=timespent) + "uuid": uuid, + "tracker": tracker }) + row = cur.fetchone() + if row is None: + cur.execute("INSERT into play_time (player_id, tracker, time_spent) " + "VALUES (%(player_id)s, %(tracker)s, %(time_spent)s)", + { + "player_id": uuid, + "tracker": tracker, + "time_spent": time_spent + }) + elif row[0] < time_spent: + cur.execute("UPDATE play_time " + "set time_spent = %(time_spent)s " + "where player_id = %(uuid)s " + "and tracker like %(tracker)s", + { + "uuid": uuid, + "tracker": tracker, + "time_spent": time_spent + }) def import14(player) -> None: + global G_FLAG_FULL_IMPORT params = read_config() conn = psycopg2.connect(**params) with conn.cursor() as cur: - cur.execute("INSERT into player (user_id, first_seen_time, " - "last_seen_user_name, last_seen_time, last_seen_address, " - "last_seen_hwid) " - "VALUES (%(user_id)s, %(first_seen_time)s, " - "%(last_seen_user_name)s, %(last_seen_time)s, " - "%(last_seen_address)s, %(last_seen_hwid)s)", + cur.execute("select exists(select 1 from player " + "where user_id = %(uuid)s)", { - "user_id": player["uuid"], - "first_seen_time": datetime.fromtimestamp( - player["first_seen_time"]), - "last_seen_user_name": player["name"], - "last_seen_time": datetime.fromtimestamp( - player["last_seen_time"]), - "last_seen_address": player["ip"], - "last_seen_hwid": bytearray.fromhex(player["hwid"]) + "uuid": player["uuid"] }) + exists = cur.fetchone()[0] + if not exists: + G_FLAG_FULL_IMPORT = True + cur.execute("INSERT into player (user_id, first_seen_time, " + "last_seen_user_name, last_seen_time, last_seen_address, " + "last_seen_hwid) " + "VALUES (%(user_id)s, %(first_seen_time)s, " + "%(last_seen_user_name)s, %(last_seen_time)s, " + "%(last_seen_address)s, %(last_seen_hwid)s)", + { + "user_id": player["uuid"], + "first_seen_time": datetime.fromtimestamp( + player["first_seen_time"]), + "last_seen_user_name": player["name"], + "last_seen_time": datetime.fromtimestamp( + player["last_seen_time"]), + "last_seen_address": player["ip"], + "last_seen_hwid": bytearray.fromhex(player["hwid"]) + }) + preference_id = import14_preference(player, conn) + slot = 0 + if not G_FLAG_FULL_IMPORT: + slot = last_slot_profile(preference_id, conn) + 1 for profile in player["profiles"]: - import14_profile(profile, preference_id, conn) + import14_profile(slot, profile, preference_id, conn) + slot += 1 for tracker, timespent in player["playtime"].items(): import14_playtime(tracker, timespent, player["uuid"], conn)