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

View File

@ -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,6 +131,7 @@ def import14_profile(profile, preference_id, conn) -> None:
def import14_preference(player, conn) -> int:
with conn.cursor() as cur:
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) "
@ -125,25 +141,63 @@ def import14_preference(player, conn) -> int:
"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("select time_spent "
"from play_time "
"where player_id = %(uuid)s "
"and tracker like %(tracker)s",
{
"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": timedelta(seconds=timespent)
"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("select exists(select 1 from player "
"where user_id = %(uuid)s)",
{
"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) "
@ -162,8 +216,12 @@ def import14(player) -> None:
})
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)