2019-12-08 22:43:49 +01:00
|
|
|
# vim: set fileencoding=utf-8
|
2022-11-13 02:20:51 +01:00
|
|
|
import random
|
2022-11-14 00:12:13 +01:00
|
|
|
import struct
|
2022-11-13 02:20:51 +01:00
|
|
|
from typing import Dict, Iterable, List, Optional, Set, Tuple
|
2021-09-07 19:56:15 +02:00
|
|
|
from typing_extensions import Final
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
from bemani.backend.base import Base
|
|
|
|
from bemani.backend.core import CoreHandler, CardManagerHandler, PASELIHandler
|
2022-08-18 00:29:56 +02:00
|
|
|
from bemani.common import DBConstants, GameConstants, ValidatedDict, Model, Profile
|
|
|
|
from bemani.data import Data, Score, UserID, Config
|
2019-12-08 22:43:49 +01:00
|
|
|
from bemani.protocol import Node
|
|
|
|
|
|
|
|
|
|
|
|
class JubeatBase(CoreHandler, CardManagerHandler, PASELIHandler, Base):
|
|
|
|
"""
|
|
|
|
Base game class for all Jubeat versions. Handles common functionality for getting
|
|
|
|
profiles based on refid, creating new profiles, looking up and saving scores.
|
|
|
|
"""
|
|
|
|
|
2021-09-07 19:56:15 +02:00
|
|
|
game: GameConstants = GameConstants.JUBEAT
|
|
|
|
|
|
|
|
GAME_FLAG_BIT_PLAYED: Final[int] = 0x1
|
|
|
|
GAME_FLAG_BIT_CLEARED: Final[int] = 0x2
|
|
|
|
GAME_FLAG_BIT_FULL_COMBO: Final[int] = 0x4
|
|
|
|
GAME_FLAG_BIT_EXCELLENT: Final[int] = 0x8
|
|
|
|
GAME_FLAG_BIT_NEARLY_FULL_COMBO: Final[int] = 0x10
|
|
|
|
GAME_FLAG_BIT_NEARLY_EXCELLENT: Final[int] = 0x20
|
|
|
|
GAME_FLAG_BIT_NO_GRAY: Final[int] = 0x40
|
|
|
|
GAME_FLAG_BIT_NO_YELLOW: Final[int] = 0x80
|
|
|
|
|
|
|
|
PLAY_MEDAL_FAILED: Final[int] = DBConstants.JUBEAT_PLAY_MEDAL_FAILED
|
|
|
|
PLAY_MEDAL_CLEARED: Final[int] = DBConstants.JUBEAT_PLAY_MEDAL_CLEARED
|
2024-01-02 03:46:24 +01:00
|
|
|
PLAY_MEDAL_NEARLY_FULL_COMBO: Final[int] = DBConstants.JUBEAT_PLAY_MEDAL_NEARLY_FULL_COMBO
|
2021-09-07 19:56:15 +02:00
|
|
|
PLAY_MEDAL_FULL_COMBO: Final[int] = DBConstants.JUBEAT_PLAY_MEDAL_FULL_COMBO
|
2024-01-02 03:46:24 +01:00
|
|
|
PLAY_MEDAL_NEARLY_EXCELLENT: Final[int] = DBConstants.JUBEAT_PLAY_MEDAL_NEARLY_EXCELLENT
|
2021-09-07 19:56:15 +02:00
|
|
|
PLAY_MEDAL_EXCELLENT: Final[int] = DBConstants.JUBEAT_PLAY_MEDAL_EXCELLENT
|
|
|
|
|
|
|
|
CHART_TYPE_BASIC: Final[int] = 0
|
|
|
|
CHART_TYPE_ADVANCED: Final[int] = 1
|
|
|
|
CHART_TYPE_EXTREME: Final[int] = 2
|
2022-08-17 03:58:49 +02:00
|
|
|
CHART_TYPE_HARD_BASIC: Final[int] = 3
|
|
|
|
CHART_TYPE_HARD_ADVANCED: Final[int] = 4
|
|
|
|
CHART_TYPE_HARD_EXTREME: Final[int] = 5
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-08-18 00:29:56 +02:00
|
|
|
def __init__(self, data: Data, config: Config, model: Model) -> None:
|
|
|
|
super().__init__(data, config, model)
|
2022-10-15 20:56:30 +02:00
|
|
|
if model.rev == "X" or model.rev == "Y":
|
2022-08-18 00:29:56 +02:00
|
|
|
self.omnimix = True
|
|
|
|
else:
|
|
|
|
self.omnimix = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def music_version(self) -> int:
|
|
|
|
if self.omnimix:
|
|
|
|
return DBConstants.OMNIMIX_VERSION_BUMP + self.version
|
|
|
|
return self.version
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
def previous_version(self) -> Optional["JubeatBase"]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Returns the previous version of the game, based on this game. Should
|
|
|
|
be overridden.
|
|
|
|
"""
|
|
|
|
return None
|
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
def put_profile(self, userid: UserID, profile: Profile) -> None:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Save a new profile for this user given a game/version. Overrides but calls
|
|
|
|
the same functionality in Base, to ensure we don't save calculated values.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
userid - The user ID we are saving the profile for.
|
|
|
|
profile - A dictionary that should be looked up later using get_profile.
|
|
|
|
"""
|
2022-10-15 20:56:30 +02:00
|
|
|
if "has_old_version" in profile:
|
|
|
|
del profile["has_old_version"]
|
2019-12-08 22:43:49 +01:00
|
|
|
super().put_profile(userid, profile)
|
|
|
|
|
2021-08-20 06:43:13 +02:00
|
|
|
def format_profile(self, userid: UserID, profile: Profile) -> Node:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Base handler for a profile. Given a userid and a profile dictionary,
|
|
|
|
return a Node representing a profile. Should be overridden.
|
|
|
|
"""
|
2022-10-15 20:56:30 +02:00
|
|
|
return Node.void("gametop")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2024-01-02 03:46:24 +01:00
|
|
|
def format_scores(self, userid: UserID, profile: Profile, scores: List[Score]) -> Node:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Base handler for a score list. Given a userid, profile and a score list,
|
|
|
|
return a Node representing a score list. Should be overridden.
|
|
|
|
"""
|
2022-10-15 20:56:30 +02:00
|
|
|
return Node.void("gametop")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2024-01-02 03:46:24 +01:00
|
|
|
def unformat_profile(self, userid: UserID, request: Node, oldprofile: Profile) -> Profile:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Base handler for profile parsing. Given a request and an old profile,
|
|
|
|
return a new profile that's been updated with the contents of the request.
|
|
|
|
Should be overridden.
|
|
|
|
"""
|
|
|
|
return oldprofile
|
|
|
|
|
|
|
|
def get_profile_by_refid(self, refid: Optional[str]) -> Optional[Node]:
|
|
|
|
"""
|
|
|
|
Given a RefID, return a formatted profile node. Basically every game
|
|
|
|
needs a profile lookup, even if it handles where that happens in
|
|
|
|
a different request. This is provided for code deduplication.
|
|
|
|
"""
|
|
|
|
if refid is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# First try to load the actual profile
|
|
|
|
userid = self.data.remote.user.from_refid(self.game, self.version, refid)
|
|
|
|
profile = self.get_profile(userid)
|
|
|
|
if profile is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Now try to find out if the profile is new or old
|
|
|
|
oldversion = self.previous_version()
|
|
|
|
oldprofile = oldversion.get_profile(userid)
|
2022-10-15 20:56:30 +02:00
|
|
|
profile["has_old_version"] = oldprofile is not None
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Now, return it
|
|
|
|
return self.format_profile(userid, profile)
|
|
|
|
|
|
|
|
def new_profile_by_refid(self, refid: Optional[str], name: Optional[str]) -> Node:
|
|
|
|
"""
|
|
|
|
Given a RefID and an optional name, create a profile and then return
|
|
|
|
a formatted profile node. Similar rationale to get_profile_by_refid.
|
|
|
|
"""
|
|
|
|
if refid is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if name is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
name = "なし"
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# First, create and save the default profile
|
|
|
|
userid = self.data.remote.user.from_refid(self.game, self.version, refid)
|
2021-08-20 06:43:13 +02:00
|
|
|
profile = Profile(
|
|
|
|
self.game,
|
|
|
|
self.version,
|
|
|
|
refid,
|
|
|
|
0,
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"name": name,
|
2021-08-20 06:43:13 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
self.put_profile(userid, profile)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Now, reload and format the profile, looking up the has old version flag
|
|
|
|
oldversion = self.previous_version()
|
|
|
|
oldprofile = oldversion.get_profile(userid)
|
2022-10-15 20:56:30 +02:00
|
|
|
profile["has_old_version"] = oldprofile is not None
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
return self.format_profile(userid, profile)
|
|
|
|
|
2024-01-02 03:46:24 +01:00
|
|
|
def get_scores_by_extid(self, extid: Optional[int], partition: int, total_partitions: int) -> Optional[Node]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given an ExtID, return a formatted score node. Similar rationale to
|
2023-08-19 21:04:12 +02:00
|
|
|
get_profile_by_refid. Note that this takes into account the game's
|
|
|
|
desire to partition scores into separate fetches to ensure that we
|
|
|
|
don't make any one request too long. We handle the logic for that here.
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
if extid is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
userid = self.data.remote.user.from_extid(self.game, self.version, extid)
|
|
|
|
profile = self.get_profile(userid)
|
|
|
|
if profile is None:
|
|
|
|
return None
|
2023-08-19 21:04:12 +02:00
|
|
|
|
2023-08-19 22:08:35 +02:00
|
|
|
cache_key = f"get_scores_by_extid-{extid}"
|
2024-09-18 05:01:51 +02:00
|
|
|
scores: Optional[List[Score]]
|
2023-08-19 22:08:35 +02:00
|
|
|
|
|
|
|
if partition == 1:
|
|
|
|
# We fetch all scores on the first partition and then divy up
|
|
|
|
# the scores across total_partitions fetches. If it is small
|
|
|
|
# enough, we don't bother.
|
2024-01-02 03:46:24 +01:00
|
|
|
scores = self.data.remote.music.get_scores(self.game, self.music_version, userid)
|
2023-08-19 22:08:35 +02:00
|
|
|
else:
|
|
|
|
# We will want to fetch the remaining scores that were in our
|
2024-09-18 04:50:09 +02:00
|
|
|
# cache. If the cache is empty, due to some error, or because
|
|
|
|
# we cached nothing below, then we will end up returning an
|
|
|
|
# empty list. This shouldn't happen, but guard against crashing
|
|
|
|
# if this returns None anyway.
|
|
|
|
scores = self.cache.get(cache_key) or []
|
2023-08-19 22:08:35 +02:00
|
|
|
|
|
|
|
if len(scores) < 50:
|
|
|
|
# We simply return the whole amount for this, and cache nothing.
|
|
|
|
rest = []
|
|
|
|
else:
|
|
|
|
groups = (total_partitions - partition) + 1
|
|
|
|
pivot = len(scores) // groups
|
|
|
|
|
|
|
|
rest = scores[pivot:]
|
|
|
|
scores = scores[:pivot]
|
|
|
|
|
|
|
|
# Cache the rest of the scores for next iteration, unless we're on the
|
|
|
|
# last iteration.
|
|
|
|
if partition == total_partitions:
|
|
|
|
if rest:
|
2024-01-02 03:46:24 +01:00
|
|
|
raise Exception("Logic error, should not have gotten additional scores to cache on last iteration!")
|
2023-08-19 22:08:35 +02:00
|
|
|
self.cache.delete(cache_key)
|
|
|
|
else:
|
|
|
|
self.cache.set(cache_key, rest, timeout=60)
|
|
|
|
|
|
|
|
# Format the chunk of scores we have to send back to the client.
|
2019-12-08 22:43:49 +01:00
|
|
|
return self.format_scores(userid, profile, scores)
|
|
|
|
|
|
|
|
def update_score(
|
|
|
|
self,
|
|
|
|
userid: UserID,
|
|
|
|
timestamp: int,
|
|
|
|
songid: int,
|
|
|
|
chart: int,
|
|
|
|
points: int,
|
|
|
|
medal: int,
|
|
|
|
combo: int,
|
2022-10-15 20:56:30 +02:00
|
|
|
ghost: Optional[List[int]] = None,
|
|
|
|
stats: Optional[Dict[str, int]] = None,
|
|
|
|
music_rate: Optional[int] = None,
|
2019-12-08 22:43:49 +01:00
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Given various pieces of a score, update the user's high score and score
|
|
|
|
history in a controlled manner, so all games in Jubeat series can expect
|
|
|
|
the same attributes in a score.
|
|
|
|
"""
|
|
|
|
# Range check medals
|
|
|
|
if medal not in [
|
|
|
|
self.PLAY_MEDAL_FAILED,
|
|
|
|
self.PLAY_MEDAL_CLEARED,
|
|
|
|
self.PLAY_MEDAL_NEARLY_FULL_COMBO,
|
|
|
|
self.PLAY_MEDAL_FULL_COMBO,
|
|
|
|
self.PLAY_MEDAL_NEARLY_EXCELLENT,
|
|
|
|
self.PLAY_MEDAL_EXCELLENT,
|
|
|
|
]:
|
2020-01-07 22:29:07 +01:00
|
|
|
raise Exception(f"Invalid medal value {medal}")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
oldscore = self.data.local.music.get_score(
|
|
|
|
self.game,
|
2022-08-18 00:29:56 +02:00
|
|
|
self.music_version,
|
2019-12-08 22:43:49 +01:00
|
|
|
userid,
|
|
|
|
songid,
|
|
|
|
chart,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Score history is verbatum, instead of highest score
|
|
|
|
history = ValidatedDict({})
|
|
|
|
oldpoints = points
|
|
|
|
|
|
|
|
if oldscore is None:
|
|
|
|
# If it is a new score, create a new dictionary to add to
|
|
|
|
scoredata = ValidatedDict({})
|
|
|
|
raised = True
|
|
|
|
highscore = True
|
|
|
|
else:
|
|
|
|
# Set the score to any new record achieved
|
|
|
|
raised = points > oldscore.points
|
|
|
|
highscore = points >= oldscore.points
|
|
|
|
points = max(oldscore.points, points)
|
|
|
|
scoredata = oldscore.data
|
|
|
|
|
|
|
|
# Replace medal with highest value
|
2022-10-15 20:56:30 +02:00
|
|
|
scoredata.replace_int("medal", max(scoredata.get_int("medal"), medal))
|
|
|
|
history.replace_int("medal", medal)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Increment counters based on medal
|
|
|
|
if medal == self.PLAY_MEDAL_CLEARED:
|
2022-10-15 20:56:30 +02:00
|
|
|
scoredata.increment_int("clear_count")
|
2019-12-08 22:43:49 +01:00
|
|
|
if medal == self.PLAY_MEDAL_FULL_COMBO:
|
2022-10-15 20:56:30 +02:00
|
|
|
scoredata.increment_int("full_combo_count")
|
2019-12-08 22:43:49 +01:00
|
|
|
if medal == self.PLAY_MEDAL_EXCELLENT:
|
2022-10-15 20:56:30 +02:00
|
|
|
scoredata.increment_int("excellent_count")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# If we have a combo, replace it
|
2022-10-15 20:56:30 +02:00
|
|
|
scoredata.replace_int("combo", max(scoredata.get_int("combo"), combo))
|
|
|
|
history.replace_int("combo", combo)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if stats is not None:
|
|
|
|
if raised:
|
|
|
|
# We have stats, and there's a new high score, update the stats
|
2022-10-15 20:56:30 +02:00
|
|
|
scoredata.replace_dict("stats", stats)
|
|
|
|
history.replace_dict("stats", stats)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if ghost is not None:
|
|
|
|
# Update the ghost regardless, but don't bother with it in history
|
2022-10-15 20:56:30 +02:00
|
|
|
scoredata.replace_int_array("ghost", len(ghost), ghost)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-08-17 03:58:49 +02:00
|
|
|
if music_rate is not None:
|
|
|
|
if oldscore is not None:
|
2022-10-15 20:56:30 +02:00
|
|
|
if music_rate > oldscore.data.get_int("music_rate"):
|
|
|
|
scoredata.replace_int("music_rate", music_rate)
|
2022-08-17 03:58:49 +02:00
|
|
|
else:
|
2022-10-15 20:56:30 +02:00
|
|
|
scoredata.replace_int("music_rate", music_rate)
|
|
|
|
history.replace_int("music_rate", music_rate)
|
2022-08-17 03:58:49 +02:00
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
# Look up where this score was earned
|
|
|
|
lid = self.get_machine_id()
|
|
|
|
|
|
|
|
# Write the new score back
|
|
|
|
self.data.local.music.put_score(
|
|
|
|
self.game,
|
2022-08-18 00:29:56 +02:00
|
|
|
self.music_version,
|
2019-12-08 22:43:49 +01:00
|
|
|
userid,
|
|
|
|
songid,
|
|
|
|
chart,
|
|
|
|
lid,
|
|
|
|
points,
|
|
|
|
scoredata,
|
|
|
|
highscore,
|
|
|
|
timestamp=timestamp,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Save the history of this score too
|
|
|
|
self.data.local.music.put_attempt(
|
|
|
|
self.game,
|
2022-08-18 00:29:56 +02:00
|
|
|
self.music_version,
|
2019-12-08 22:43:49 +01:00
|
|
|
userid,
|
|
|
|
songid,
|
|
|
|
chart,
|
|
|
|
lid,
|
|
|
|
oldpoints,
|
|
|
|
history,
|
|
|
|
raised,
|
|
|
|
timestamp=timestamp,
|
|
|
|
)
|
2022-11-13 02:20:51 +01:00
|
|
|
|
|
|
|
def default_select_jbox(self) -> Set[int]:
|
|
|
|
gameitems = self.data.local.game.get_items(self.game, self.version)
|
|
|
|
default_main: Set[int] = set()
|
|
|
|
|
|
|
|
for gameitem in gameitems:
|
|
|
|
if gameitem.type == "emblem":
|
2024-01-02 03:46:24 +01:00
|
|
|
if gameitem.data.get_int("layer") == 2 and gameitem.data.get_int("rarity") == 1:
|
2022-11-13 02:20:51 +01:00
|
|
|
default_main.add(gameitem.id)
|
|
|
|
|
|
|
|
return default_main
|
|
|
|
|
|
|
|
def random_select_jbox(self, owned_emblems: Set[int]) -> Tuple[int, int]:
|
|
|
|
gameitems = self.data.local.game.get_items(self.game, self.version)
|
|
|
|
normalemblems: Set[int] = set()
|
|
|
|
premiumemblems: Set[int] = set()
|
|
|
|
for gameitem in gameitems:
|
|
|
|
if gameitem.type == "emblem":
|
|
|
|
if gameitem.id in owned_emblems:
|
|
|
|
# We don't want to give out random emblems that are already owned.
|
|
|
|
continue
|
|
|
|
|
|
|
|
if gameitem.data.get_int("rarity") in {1, 2, 3}:
|
|
|
|
normalemblems.add(gameitem.id)
|
|
|
|
if gameitem.data.get_int("rarity") in {4, 5}:
|
|
|
|
premiumemblems.add(gameitem.id)
|
|
|
|
|
2022-11-13 03:55:57 +01:00
|
|
|
# If they've earned all the premium emblems, give them normal emblems instead.
|
|
|
|
if normalemblems and not premiumemblems:
|
|
|
|
premiumemblems = normalemblems
|
|
|
|
|
|
|
|
# Now, try to default to the default emblem, in the case that the person
|
|
|
|
# has earned every single part (unlikely).
|
|
|
|
if not normalemblems:
|
|
|
|
normalemblems = self.default_select_jbox()
|
|
|
|
if not premiumemblems:
|
|
|
|
premiumemblems = self.default_select_jbox()
|
|
|
|
|
|
|
|
# Default to some hand-picked emblems in case the catalog is not available.
|
2022-11-13 02:20:51 +01:00
|
|
|
normalindex = 2
|
|
|
|
premiumindex = 1
|
|
|
|
if normalemblems:
|
2024-09-01 22:42:20 +02:00
|
|
|
normalindex = random.sample(list(normalemblems), 1)[0]
|
2022-11-13 02:20:51 +01:00
|
|
|
if premiumemblems:
|
2024-09-01 22:42:20 +02:00
|
|
|
premiumindex = random.sample(list(premiumemblems), 1)[0]
|
2022-11-13 02:20:51 +01:00
|
|
|
|
|
|
|
return normalindex, premiumindex
|
|
|
|
|
|
|
|
def calculate_owned_items(self, item_list: List[int]) -> Set[int]:
|
|
|
|
owned_items: Set[int] = set()
|
|
|
|
|
|
|
|
for index in range(len(item_list) * 32):
|
|
|
|
offset = 1 << (index % 32)
|
|
|
|
bucket = index // 32
|
|
|
|
|
|
|
|
if (item_list[bucket] & offset) != 0:
|
|
|
|
owned_items.add(index)
|
|
|
|
|
|
|
|
return owned_items
|
|
|
|
|
|
|
|
def create_owned_items(self, items: Iterable[int], size: int) -> List[int]:
|
|
|
|
items_list = [0] * size
|
|
|
|
|
|
|
|
for index in items:
|
|
|
|
offset = 1 << (index % 32)
|
|
|
|
bucket = index // 32
|
|
|
|
|
|
|
|
items_list[bucket] |= offset
|
|
|
|
|
2022-11-14 00:12:13 +01:00
|
|
|
return [struct.unpack("i", struct.pack("I", item))[0] for item in items_list]
|