1

импорт персонажей, если игрок существует

This commit is contained in:
DmitriyMX 2023-03-21 14:21:29 +03:00
parent ef11ca3d82
commit 267151f546
No known key found for this signature in database
GPG Key ID: 83F1834955147B6F

120
import.py
View File

@ -6,6 +6,9 @@ from datetime import timedelta
from configparser import ConfigParser from configparser import ConfigParser
G_FLAG_FULL_IMPORT = False
class ParseConfig(Exception): class ParseConfig(Exception):
pass 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 = "[" _markings = "["
_f = True _f = True
for v in profile["markings"]: for v in profile["markings"]:
@ -83,7 +98,7 @@ def import14_profile(profile, preference_id, conn) -> None:
"%(flavor_text)s) " "%(flavor_text)s) "
"RETURNING profile_id", "RETURNING profile_id",
{ {
"slot": profile["slot"], "slot": slot,
"char_name": profile["name"], "char_name": profile["name"],
"age": profile["age"], "age": profile["age"],
"sex": profile["sex"], "sex": profile["sex"],
@ -116,54 +131,97 @@ def import14_profile(profile, preference_id, conn) -> None:
def import14_preference(player, conn) -> int: def import14_preference(player, conn) -> int:
with conn.cursor() as cur: with conn.cursor() as cur:
cur.execute("INSERT into preference (user_id, " if G_FLAG_FULL_IMPORT:
"selected_character_slot, admin_ooc_color) " cur.execute("INSERT into preference (user_id, "
"VALUES (%(user_id)s, %(slot)s, %(ooc_color)s) " "selected_character_slot, admin_ooc_color) "
"RETURNING preference_id", "VALUES (%(user_id)s, %(slot)s, %(ooc_color)s) "
{ "RETURNING preference_id",
"user_id": player["uuid"], {
"slot": player["preference"]["selected_slot"], "user_id": player["uuid"],
"ooc_color": player["preference"]["ooc_color"] "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] return cur.fetchone()[0]
def import14_playtime(tracker, timespent, uuid, conn) -> None: def import14_playtime(tracker, timespent, uuid, conn) -> None:
time_spent = timedelta(seconds=timespent)
with conn.cursor() as cur: with conn.cursor() as cur:
cur.execute("INSERT into play_time (player_id, tracker, time_spent) " cur.execute("select time_spent "
"VALUES (%(player_id)s, %(tracker)s, %(time_spent)s)", "from play_time "
"where player_id = %(uuid)s "
"and tracker like %(tracker)s",
{ {
"player_id": uuid, "uuid": uuid,
"tracker": tracker, "tracker": tracker
"time_spent": timedelta(seconds=timespent)
}) })
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: def import14(player) -> None:
global G_FLAG_FULL_IMPORT
params = read_config() params = read_config()
conn = psycopg2.connect(**params) conn = psycopg2.connect(**params)
with conn.cursor() as cur: with conn.cursor() as cur:
cur.execute("INSERT into player (user_id, first_seen_time, " cur.execute("select exists(select 1 from player "
"last_seen_user_name, last_seen_time, last_seen_address, " "where user_id = %(uuid)s)",
"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"], "uuid": 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"])
}) })
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) 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"]: 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(): for tracker, timespent in player["playtime"].items():
import14_playtime(tracker, timespent, player["uuid"], conn) import14_playtime(tracker, timespent, player["uuid"], conn)