2021-08-20 06:43:13 +02:00
|
|
|
from typing import List, Tuple, Optional
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
from bemani.common import APIConstants, GameConstants, Profile, Parallel
|
2019-12-08 22:43:49 +01:00
|
|
|
from bemani.data.interfaces import APIProviderInterface
|
|
|
|
from bemani.data.api.base import BaseGlobalData
|
|
|
|
from bemani.data.mysql.user import UserData
|
|
|
|
from bemani.data.remoteuser import RemoteUser
|
|
|
|
from bemani.data.types import UserID
|
|
|
|
|
|
|
|
|
|
|
|
class GlobalUserData(BaseGlobalData):
|
|
|
|
def __init__(self, api: APIProviderInterface, user: UserData) -> None:
|
|
|
|
super().__init__(api)
|
|
|
|
self.user = user
|
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
def __format_ddr_profile(self, updates: Profile, profile: Profile) -> None:
|
2022-10-15 20:56:30 +02:00
|
|
|
area = profile.get_int("area", -1)
|
2019-12-08 22:43:49 +01:00
|
|
|
if area != -1:
|
2022-10-15 20:56:30 +02:00
|
|
|
updates["area"] = area
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
def __format_iidx_profile(self, updates: Profile, profile: Profile) -> None:
|
2022-10-15 20:56:30 +02:00
|
|
|
area = profile.get_int("area", -1)
|
2019-12-08 22:43:49 +01:00
|
|
|
if area != -1:
|
2022-10-15 20:56:30 +02:00
|
|
|
updates["pid"] = area
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
qpro = profile.get_dict("qpro")
|
|
|
|
updates["qpro"] = {}
|
2021-08-20 06:43:13 +02:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
head = qpro.get_int("head", -1)
|
2019-12-08 22:43:49 +01:00
|
|
|
if head != -1:
|
2022-10-15 20:56:30 +02:00
|
|
|
updates["qpro"]["head"] = head
|
|
|
|
hair = qpro.get_int("hair", -1)
|
2019-12-08 22:43:49 +01:00
|
|
|
if hair != -1:
|
2022-10-15 20:56:30 +02:00
|
|
|
updates["qpro"]["hair"] = hair
|
|
|
|
face = qpro.get_int("face", -1)
|
2019-12-08 22:43:49 +01:00
|
|
|
if face != -1:
|
2022-10-15 20:56:30 +02:00
|
|
|
updates["qpro"]["face"] = face
|
|
|
|
body = qpro.get_int("body", -1)
|
2019-12-08 22:43:49 +01:00
|
|
|
if body != -1:
|
2022-10-15 20:56:30 +02:00
|
|
|
updates["qpro"]["body"] = body
|
|
|
|
hand = qpro.get_int("hand", -1)
|
2019-12-08 22:43:49 +01:00
|
|
|
if hand != -1:
|
2022-10-15 20:56:30 +02:00
|
|
|
updates["qpro"]["hand"] = hand
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
def __format_jubeat_profile(self, updates: Profile, profile: Profile) -> None:
|
|
|
|
pass
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
def __format_museca_profile(self, updates: Profile, profile: Profile) -> None:
|
|
|
|
pass
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
def __format_popn_profile(self, updates: Profile, profile: Profile) -> None:
|
2022-10-15 20:56:30 +02:00
|
|
|
chara = profile.get_int("character", -1)
|
2019-12-08 22:43:49 +01:00
|
|
|
if chara != -1:
|
2022-10-15 20:56:30 +02:00
|
|
|
updates["chara"] = chara
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
def __format_reflec_profile(self, updates: Profile, profile: Profile) -> None:
|
2022-10-15 20:56:30 +02:00
|
|
|
icon = profile.get_int("icon", -1)
|
2019-12-08 22:43:49 +01:00
|
|
|
if icon != -1:
|
2022-10-15 20:56:30 +02:00
|
|
|
updates["config"] = {"icon_id": icon}
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
def __format_sdvx_profile(self, updates: Profile, profile: Profile) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __format_profile(self, profile: Profile) -> Profile:
|
|
|
|
new = Profile(
|
|
|
|
profile.game,
|
|
|
|
profile.version,
|
|
|
|
profile.refid,
|
|
|
|
profile.extid,
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"name": profile.get("name", ""),
|
2021-08-20 06:43:13 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if profile.game == GameConstants.DDR:
|
|
|
|
self.__format_ddr_profile(new, profile)
|
|
|
|
if profile.game == GameConstants.IIDX:
|
|
|
|
self.__format_iidx_profile(new, profile)
|
|
|
|
if profile.game == GameConstants.JUBEAT:
|
|
|
|
self.__format_jubeat_profile(new, profile)
|
|
|
|
if profile.game == GameConstants.MUSECA:
|
|
|
|
self.__format_museca_profile(new, profile)
|
|
|
|
if profile.game == GameConstants.POPN_MUSIC:
|
|
|
|
self.__format_popn_profile(new, profile)
|
|
|
|
if profile.game == GameConstants.REFLEC_BEAT:
|
|
|
|
self.__format_reflec_profile(new, profile)
|
|
|
|
if profile.game == GameConstants.SDVX:
|
|
|
|
self.__format_sdvx_profile(new, profile)
|
|
|
|
|
|
|
|
return new
|
|
|
|
|
2024-01-02 03:46:24 +01:00
|
|
|
def __profile_request(self, game: GameConstants, version: int, userid: UserID, exact: bool) -> Optional[Profile]:
|
2019-12-08 22:43:49 +01:00
|
|
|
# First, get or create the extid/refid for this virtual user
|
|
|
|
cardid = RemoteUser.userid_to_card(userid)
|
|
|
|
refid = self.user.get_refid(game, version, userid)
|
|
|
|
extid = self.user.get_extid(game, version, userid)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
profiles = Parallel.flatten(
|
|
|
|
Parallel.call(
|
|
|
|
[client.get_profiles for client in self.clients],
|
|
|
|
game,
|
|
|
|
version,
|
|
|
|
APIConstants.ID_TYPE_CARD,
|
|
|
|
[cardid],
|
|
|
|
)
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
for profile in profiles:
|
2022-10-15 20:56:30 +02:00
|
|
|
cards = [card.upper() for card in profile.get("cards", [])]
|
2019-12-08 22:43:49 +01:00
|
|
|
if cardid in cards:
|
2021-08-20 06:43:13 +02:00
|
|
|
# Don't take non-exact matches.
|
2022-10-15 20:56:30 +02:00
|
|
|
exact_match = profile.get("match", "partial") == "exact"
|
2019-12-08 22:43:49 +01:00
|
|
|
if exact and (not exact_match):
|
|
|
|
# This is a partial match, not for this game/version
|
|
|
|
continue
|
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
# Add in our defaults we always provide, convert it to a local format.
|
|
|
|
return self.__format_profile(
|
|
|
|
Profile(
|
|
|
|
game,
|
|
|
|
version,
|
|
|
|
refid,
|
|
|
|
extid,
|
|
|
|
profile,
|
|
|
|
)
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def from_cardid(self, cardid: str) -> Optional[UserID]:
|
|
|
|
userid = self.user.from_cardid(cardid)
|
|
|
|
if userid is None:
|
|
|
|
userid = RemoteUser.card_to_userid(cardid)
|
|
|
|
return userid
|
|
|
|
|
2024-01-02 03:46:24 +01:00
|
|
|
def from_refid(self, game: GameConstants, version: int, refid: str) -> Optional[UserID]:
|
2019-12-08 22:43:49 +01:00
|
|
|
return self.user.from_refid(game, version, refid)
|
|
|
|
|
2024-01-02 03:46:24 +01:00
|
|
|
def from_extid(self, game: GameConstants, version: int, extid: int) -> Optional[UserID]:
|
2019-12-08 22:43:49 +01:00
|
|
|
return self.user.from_extid(game, version, extid)
|
|
|
|
|
2024-01-02 03:46:24 +01:00
|
|
|
def get_profile(self, game: GameConstants, version: int, userid: UserID) -> Optional[Profile]:
|
2019-12-08 22:43:49 +01:00
|
|
|
if RemoteUser.is_remote(userid):
|
|
|
|
return self.__profile_request(game, version, userid, exact=True)
|
|
|
|
else:
|
|
|
|
return self.user.get_profile(game, version, userid)
|
|
|
|
|
2024-01-02 03:46:24 +01:00
|
|
|
def get_any_profile(self, game: GameConstants, version: int, userid: UserID) -> Optional[Profile]:
|
2019-12-08 22:43:49 +01:00
|
|
|
if RemoteUser.is_remote(userid):
|
|
|
|
return self.__profile_request(game, version, userid, exact=False)
|
|
|
|
else:
|
|
|
|
return self.user.get_any_profile(game, version, userid)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
def get_any_profiles(
|
|
|
|
self, game: GameConstants, version: int, userids: List[UserID]
|
|
|
|
) -> List[Tuple[UserID, Optional[Profile]]]:
|
2019-12-08 22:43:49 +01:00
|
|
|
if len(userids) == 0:
|
|
|
|
return []
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
remote_ids = [userid for userid in userids if RemoteUser.is_remote(userid)]
|
|
|
|
local_ids = [userid for userid in userids if not RemoteUser.is_remote(userid)]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if len(remote_ids) == 0:
|
|
|
|
# We only have local profiles here, just pass on to the underlying layer
|
|
|
|
return self.user.get_any_profiles(game, version, local_ids)
|
|
|
|
else:
|
|
|
|
# We have to fetch some local profiles and some remote profiles, and then
|
|
|
|
# merge them together
|
2024-01-02 03:46:24 +01:00
|
|
|
card_to_userid = {RemoteUser.userid_to_card(userid): userid for userid in remote_ids}
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
local_profiles, remote_profiles = Parallel.execute(
|
|
|
|
[
|
|
|
|
lambda: self.user.get_any_profiles(game, version, local_ids),
|
|
|
|
lambda: Parallel.flatten(
|
|
|
|
Parallel.call(
|
|
|
|
[client.get_profiles for client in self.clients],
|
|
|
|
game,
|
|
|
|
version,
|
|
|
|
APIConstants.ID_TYPE_CARD,
|
2024-01-02 03:46:24 +01:00
|
|
|
[RemoteUser.userid_to_card(userid) for userid in remote_ids],
|
2022-10-15 20:56:30 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
for profile in remote_profiles:
|
2022-10-15 20:56:30 +02:00
|
|
|
cards = [card.upper() for card in profile.get("cards", [])]
|
2019-12-08 22:43:49 +01:00
|
|
|
for card in cards:
|
|
|
|
# Map it back to the requested user
|
|
|
|
userid = card_to_userid.get(card)
|
|
|
|
if userid is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Sanitize the returned data
|
2022-10-15 20:56:30 +02:00
|
|
|
exact_match = profile.get("match", "partial") == "exact"
|
2019-12-08 22:43:49 +01:00
|
|
|
refid = self.user.get_refid(game, version, userid)
|
|
|
|
extid = self.user.get_extid(game, version, userid)
|
|
|
|
|
|
|
|
# Add in our defaults we always provide
|
|
|
|
local_profiles.append(
|
2021-08-20 06:43:13 +02:00
|
|
|
(
|
|
|
|
userid,
|
|
|
|
self.__format_profile(
|
|
|
|
Profile(
|
|
|
|
game,
|
|
|
|
version if exact_match else 0,
|
|
|
|
refid,
|
|
|
|
extid,
|
|
|
|
profile,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2019-12-08 22:43:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Mark that we saw this card/user
|
|
|
|
del card_to_userid[card]
|
|
|
|
|
|
|
|
# Finally, mark all missing remote profiles as None
|
|
|
|
for card in card_to_userid:
|
|
|
|
local_profiles.append((card_to_userid[card], None))
|
|
|
|
|
|
|
|
return local_profiles
|
|
|
|
|
2024-01-02 03:46:24 +01:00
|
|
|
def get_all_profiles(self, game: GameConstants, version: int) -> List[Tuple[UserID, Profile]]:
|
2019-12-08 22:43:49 +01:00
|
|
|
# Fetch local and remote profiles, and then merge by adding remote profiles to local
|
|
|
|
# profiles when we don't have a profile for that user ID yet.
|
2022-10-15 20:56:30 +02:00
|
|
|
local_cards, local_profiles, remote_profiles = Parallel.execute(
|
|
|
|
[
|
|
|
|
self.user.get_all_cards,
|
|
|
|
lambda: self.user.get_all_profiles(game, version),
|
|
|
|
lambda: Parallel.flatten(
|
|
|
|
Parallel.call(
|
|
|
|
[client.get_profiles for client in self.clients],
|
|
|
|
game,
|
|
|
|
version,
|
|
|
|
APIConstants.ID_TYPE_SERVER,
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
card_to_id = {cardid: userid for (cardid, userid) in local_cards}
|
|
|
|
id_to_profile = {userid: profile for (userid, profile) in local_profiles}
|
|
|
|
|
|
|
|
for profile in remote_profiles:
|
2022-10-15 20:56:30 +02:00
|
|
|
cardids = sorted([card.upper() for card in profile.get("cards", [])])
|
2019-12-08 22:43:49 +01:00
|
|
|
if len(cardids) == 0:
|
|
|
|
# We don't care about anonymous profiles
|
|
|
|
continue
|
|
|
|
|
|
|
|
local_cards = [cardid for cardid in cardids if cardid in card_to_id]
|
|
|
|
if len(local_cards) > 0:
|
|
|
|
# We have a local version of this profile!
|
|
|
|
continue
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
exact_match = profile.get("match", "partial") == "exact"
|
2019-12-08 22:43:49 +01:00
|
|
|
if not exact_match:
|
|
|
|
continue
|
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
# Create a fake user with this profile
|
2019-12-08 22:43:49 +01:00
|
|
|
userid = RemoteUser.card_to_userid(cardids[0])
|
|
|
|
refid = self.user.get_refid(game, version, userid)
|
|
|
|
extid = self.user.get_extid(game, version, userid)
|
|
|
|
|
|
|
|
# Add in our defaults we always provide
|
2021-08-20 06:43:13 +02:00
|
|
|
id_to_profile[userid] = self.__format_profile(
|
|
|
|
Profile(
|
|
|
|
game,
|
|
|
|
version,
|
|
|
|
refid,
|
|
|
|
extid,
|
|
|
|
profile,
|
|
|
|
),
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
return [(userid, id_to_profile[userid]) for userid in id_to_profile]
|