2019-12-08 22:43:49 +01:00
|
|
|
from sqlalchemy import Table, Column, UniqueConstraint # type: ignore
|
|
|
|
from sqlalchemy.types import String, Integer, JSON # type: ignore
|
|
|
|
from sqlalchemy.dialects.mysql import BIGINT as BigInteger # type: ignore
|
|
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
from bemani.common import GameConstants, ValidatedDict, Time
|
2019-12-08 22:43:49 +01:00
|
|
|
from bemani.data.mysql.base import BaseData, metadata
|
|
|
|
from bemani.data.types import Achievement, Item, UserID
|
|
|
|
|
|
|
|
"""
|
|
|
|
Table for storing game settings that span multiple versions of the same
|
|
|
|
game, such as play statistics. This table intentionally doesn't have a
|
|
|
|
key on game version, just game string and userid.
|
|
|
|
"""
|
2021-05-31 20:07:03 +02:00
|
|
|
game_settings = Table(
|
2019-12-08 22:43:49 +01:00
|
|
|
'game_settings',
|
|
|
|
metadata,
|
|
|
|
Column('game', String(32), nullable=False),
|
|
|
|
Column('userid', BigInteger(unsigned=True), nullable=False),
|
|
|
|
Column('data', JSON, nullable=False),
|
|
|
|
UniqueConstraint('game', 'userid', name='game_userid'),
|
|
|
|
mysql_charset='utf8mb4',
|
|
|
|
)
|
|
|
|
|
|
|
|
"""
|
|
|
|
Table for storing shop items that are server-side verified.
|
|
|
|
"""
|
2021-05-31 20:07:03 +02:00
|
|
|
catalog = Table(
|
2019-12-08 22:43:49 +01:00
|
|
|
'catalog',
|
|
|
|
metadata,
|
|
|
|
Column('game', String(32), nullable=False),
|
|
|
|
Column('version', Integer, nullable=False),
|
|
|
|
Column('id', Integer, nullable=False),
|
|
|
|
Column('type', String(64), nullable=False),
|
|
|
|
Column('data', JSON, nullable=False),
|
|
|
|
UniqueConstraint('game', 'version', 'id', 'type', name='game_version_id_type'),
|
|
|
|
mysql_charset='utf8mb4',
|
|
|
|
)
|
|
|
|
|
|
|
|
"""
|
|
|
|
Table for storing series achievements that span multiple versions of the same
|
|
|
|
game, such as course scores. This table intentionally doesn't have a
|
|
|
|
key on game version, just game string and userid.
|
|
|
|
"""
|
2021-05-31 20:07:03 +02:00
|
|
|
series_achievement = Table(
|
2019-12-08 22:43:49 +01:00
|
|
|
'series_achievement',
|
|
|
|
metadata,
|
|
|
|
Column('game', String(32), nullable=False),
|
|
|
|
Column('userid', BigInteger(unsigned=True), nullable=False),
|
|
|
|
Column('id', Integer, nullable=False),
|
|
|
|
Column('type', String(64), nullable=False),
|
|
|
|
Column('data', JSON, nullable=False),
|
|
|
|
UniqueConstraint('game', 'userid', 'id', 'type', name='game_userid_id_type'),
|
|
|
|
mysql_charset='utf8mb4',
|
|
|
|
)
|
|
|
|
|
|
|
|
"""
|
|
|
|
Table for storing time-based game settings that aren't tied to a user
|
|
|
|
account, such as dailies, weeklies, etc.
|
|
|
|
"""
|
2021-05-31 20:07:03 +02:00
|
|
|
time_sensitive_settings = Table(
|
2019-12-08 22:43:49 +01:00
|
|
|
'time_sensitive_settings',
|
|
|
|
metadata,
|
|
|
|
Column('game', String(32), nullable=False),
|
|
|
|
Column('version', Integer, nullable=False),
|
|
|
|
Column('name', String(32), nullable=False),
|
|
|
|
Column('start_time', Integer, nullable=False, index=True),
|
|
|
|
Column('end_time', Integer, nullable=False, index=True),
|
|
|
|
Column('data', JSON, nullable=False),
|
|
|
|
UniqueConstraint('game', 'version', 'name', 'start_time', name='game_version_name_start_time'),
|
|
|
|
mysql_charset='utf8mb4',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class GameData(BaseData):
|
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def get_settings(self, game: GameConstants, userid: UserID) -> Optional[ValidatedDict]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a game and a user ID, look up game-wide settings as a dictionary.
|
|
|
|
|
|
|
|
It is expected that game classes call this function, and provide a consistent
|
|
|
|
game name from version to version, so game settings can be looked up across
|
|
|
|
all versions in a game series.
|
|
|
|
|
|
|
|
Parameters:
|
2021-08-19 21:21:22 +02:00
|
|
|
game - Enum value identifying a game series.
|
2019-12-08 22:43:49 +01:00
|
|
|
userid - Integer identifying a user, as possibly looked up by UserData.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A dictionary representing game settings stored by a game class, or None
|
|
|
|
if there are no settings for this game/user.
|
|
|
|
"""
|
|
|
|
sql = "SELECT data FROM game_settings WHERE game = :game AND userid = :userid"
|
2021-08-19 21:21:22 +02:00
|
|
|
cursor = self.execute(sql, {'game': game.value, 'userid': userid})
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if cursor.rowcount != 1:
|
|
|
|
# Settings doesn't exist
|
|
|
|
return None
|
|
|
|
|
|
|
|
result = cursor.fetchone()
|
|
|
|
return ValidatedDict(self.deserialize(result['data']))
|
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def put_settings(self, game: GameConstants, userid: UserID, settings: Dict[str, Any]) -> None:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a game and a user ID, save game-wide settings to the DB.
|
|
|
|
|
|
|
|
Parameters:
|
2021-08-19 21:21:22 +02:00
|
|
|
game - Enum value identifying a game series.
|
2019-12-08 22:43:49 +01:00
|
|
|
userid - Integer identifying a user.
|
|
|
|
settings - A dictionary of settings that a game wishes to retrieve later.
|
|
|
|
"""
|
|
|
|
# Add settings json to game settings
|
|
|
|
sql = (
|
|
|
|
"INSERT INTO game_settings (game, userid, data) " +
|
|
|
|
"VALUES (:game, :userid, :data) " +
|
|
|
|
"ON DUPLICATE KEY UPDATE data=VALUES(data)"
|
|
|
|
)
|
2021-08-19 21:21:22 +02:00
|
|
|
self.execute(sql, {'game': game.value, 'userid': userid, 'data': self.serialize(settings)})
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def get_achievement(self, game: GameConstants, userid: UserID, achievementid: int, achievementtype: str) -> Optional[ValidatedDict]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a game/userid and achievement id/type, find that achievement.
|
|
|
|
|
|
|
|
Note that there can be more than one achievement with the same ID and game/userid
|
|
|
|
as long as each one is a different type. Essentially, achievementtype namespaces achievements.
|
|
|
|
|
|
|
|
Parameters:
|
2021-08-19 21:21:22 +02:00
|
|
|
game - Enum value identifier of the game looking up the user.
|
2019-12-08 22:43:49 +01:00
|
|
|
userid - Integer user ID, as looked up by one of the above functions.
|
|
|
|
achievementid - Integer ID, as provided by a game.
|
|
|
|
achievementtype - The type of achievement.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A dictionary as stored by a game class previously, or None if not found.
|
|
|
|
"""
|
|
|
|
sql = (
|
|
|
|
"SELECT data FROM series_achievement "
|
|
|
|
"WHERE game = :game AND userid = :userid AND id = :id AND type = :type"
|
|
|
|
)
|
2021-08-19 21:21:22 +02:00
|
|
|
cursor = self.execute(sql, {'game': game.value, 'userid': userid, 'id': achievementid, 'type': achievementtype})
|
2019-12-08 22:43:49 +01:00
|
|
|
if cursor.rowcount != 1:
|
|
|
|
# score doesn't exist
|
|
|
|
return None
|
|
|
|
|
|
|
|
result = cursor.fetchone()
|
|
|
|
return ValidatedDict(self.deserialize(result['data']))
|
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def get_achievements(self, game: GameConstants, userid: UserID) -> List[Achievement]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a game/userid, find all achievements
|
|
|
|
|
|
|
|
Parameters:
|
2021-08-19 21:21:22 +02:00
|
|
|
game - Enum value identifier of the game looking up the user.
|
2019-12-08 22:43:49 +01:00
|
|
|
userid - Integer user ID, as looked up by one of the above functions.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of Achievement objects.
|
|
|
|
"""
|
|
|
|
sql = "SELECT id, type, data FROM series_achievement WHERE game = :game AND userid = :userid"
|
2021-08-19 21:21:22 +02:00
|
|
|
cursor = self.execute(sql, {'game': game.value, 'userid': userid})
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
achievements = []
|
|
|
|
for result in cursor.fetchall():
|
|
|
|
achievements.append(
|
|
|
|
Achievement(
|
|
|
|
result['id'],
|
|
|
|
result['type'],
|
|
|
|
None,
|
|
|
|
self.deserialize(result['data']),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return achievements
|
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def put_achievement(self, game: GameConstants, userid: UserID, achievementid: int, achievementtype: str, data: Dict[str, Any]) -> None:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a game/userid and achievement id/type, save an achievement.
|
|
|
|
|
|
|
|
Parameters:
|
2021-08-19 21:21:22 +02:00
|
|
|
game - Enum value identifier of the game looking up the user.
|
2019-12-08 22:43:49 +01:00
|
|
|
userid - Integer user ID, as looked up by one of the above functions.
|
|
|
|
achievementid - Integer ID, as provided by a game.
|
|
|
|
achievementtype - The type of achievement.
|
|
|
|
data - A dictionary of data that the game wishes to retrieve later.
|
|
|
|
"""
|
|
|
|
# Add achievement JSON to achievements
|
|
|
|
sql = (
|
|
|
|
"INSERT INTO series_achievement (game, userid, id, type, data) " +
|
|
|
|
"VALUES (:game, :userid, :id, :type, :data) " +
|
|
|
|
"ON DUPLICATE KEY UPDATE data=VALUES(data)"
|
|
|
|
)
|
2021-08-19 21:21:22 +02:00
|
|
|
self.execute(sql, {'game': game.value, 'userid': userid, 'id': achievementid, 'type': achievementtype, 'data': self.serialize(data)})
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def get_time_sensitive_settings(self, game: GameConstants, version: int, name: str) -> Optional[ValidatedDict]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a game/version/name, look up the current time-sensitive settings for this game.
|
|
|
|
|
|
|
|
Parameters:
|
2021-08-19 21:21:22 +02:00
|
|
|
game - Enum value identifier of the game we want settings for.
|
2019-12-08 22:43:49 +01:00
|
|
|
version - Integer identifying the game version we want settings for.
|
|
|
|
name - The name of the setting we are concerned with.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A ValidatedDict of stored settings if the current setting is found, or None otherwise.
|
|
|
|
If settings were found, they are guaranteed to include the attributes 'start_time' and
|
|
|
|
'end_time' which will both be seconds since the unix epoch (UTC).
|
|
|
|
"""
|
|
|
|
sql = (
|
|
|
|
"SELECT data, start_time, end_time FROM time_sensitive_settings WHERE "
|
|
|
|
"game = :game AND version = :version AND name = :name AND start_time <= :time AND end_time > :time"
|
|
|
|
)
|
2021-08-19 21:21:22 +02:00
|
|
|
cursor = self.execute(sql, {'game': game.value, 'version': version, 'name': name, 'time': Time.now()})
|
2019-12-08 22:43:49 +01:00
|
|
|
if cursor.rowcount != 1:
|
|
|
|
# setting doesn't exist
|
|
|
|
return None
|
|
|
|
|
|
|
|
result = cursor.fetchone()
|
|
|
|
retval = ValidatedDict(self.deserialize(result['data']))
|
|
|
|
retval['start_time'] = result['start_time']
|
|
|
|
retval['end_time'] = result['end_time']
|
|
|
|
return retval
|
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def get_all_time_sensitive_settings(self, game: GameConstants, version: int, name: str) -> List[ValidatedDict]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a game/version/name, look up all of the time-sensitive settings for this game.
|
|
|
|
|
|
|
|
Parameters:
|
2021-08-19 21:21:22 +02:00
|
|
|
game - Enum value identifier of the game we want settings for.
|
2019-12-08 22:43:49 +01:00
|
|
|
version - Integer identifying the game version we want settings for.
|
|
|
|
name - The name of the setting we are concerned with.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of ValidatedDict of stored settings if there were settings found, or [] otherwise.
|
|
|
|
If settings were found, they are guaranteed to include the attributes 'start_time' and
|
|
|
|
'end_time' which will both be seconds since the unix epoch (UTC).
|
|
|
|
"""
|
|
|
|
sql = (
|
|
|
|
"SELECT data, start_time, end_time FROM time_sensitive_settings WHERE "
|
|
|
|
"game = :game AND version = :version AND name = :name"
|
|
|
|
)
|
2021-08-19 21:21:22 +02:00
|
|
|
cursor = self.execute(sql, {'game': game.value, 'version': version, 'name': name})
|
2019-12-08 22:43:49 +01:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
# setting doesn't exist
|
|
|
|
return []
|
|
|
|
|
|
|
|
settings = []
|
|
|
|
for result in cursor.fetchall():
|
|
|
|
retval = ValidatedDict(self.deserialize(result['data']))
|
|
|
|
retval['start_time'] = result['start_time']
|
|
|
|
retval['end_time'] = result['end_time']
|
|
|
|
settings.append(retval)
|
|
|
|
return settings
|
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def put_time_sensitive_settings(self, game: GameConstants, version: int, name: str, settings: Dict[str, Any]) -> None:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a game/version/name and a settings dictionary that contains 'start_time' and 'end_time',
|
|
|
|
as seconds since the unix epoch (UTC), update the DB to store or update this time-sensitive
|
|
|
|
setting. Verifies that start time comes before end time, that there is at least one second in
|
|
|
|
the setting duration, and that this setting doesn't overlap any other setting already present.
|
|
|
|
|
|
|
|
Parameters:
|
2021-08-19 21:21:22 +02:00
|
|
|
game - Enum value identifier of the game we want settings for.
|
2019-12-08 22:43:49 +01:00
|
|
|
version - Integer identifying the game version we want settings for.
|
|
|
|
name - The name of the setting we are concerned with.
|
|
|
|
settings - A dictionary containing at least 'start_time' and 'end_time'.
|
|
|
|
"""
|
|
|
|
start_time = settings['start_time']
|
|
|
|
end_time = settings['end_time']
|
|
|
|
del settings['start_time']
|
|
|
|
del settings['end_time']
|
|
|
|
|
|
|
|
if start_time > end_time:
|
|
|
|
raise Exception('Start time is greater than end time!')
|
|
|
|
if start_time == end_time:
|
|
|
|
raise Exception('This setting spans zero seconds!')
|
|
|
|
|
|
|
|
# Verify that this isn't overlapping some event.
|
|
|
|
sql = """
|
|
|
|
SELECT start_time, end_time FROM time_sensitive_settings WHERE
|
|
|
|
game = :game AND version = :version AND name = :name AND
|
|
|
|
(
|
|
|
|
(start_time >= :start_time AND start_time < :end_time) OR
|
|
|
|
(end_time > :start_time AND end_time <= :end_time) OR
|
|
|
|
(start_time < :start_time AND end_time > :end_time)
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
cursor = self.execute(
|
|
|
|
sql,
|
|
|
|
{
|
2021-08-19 21:21:22 +02:00
|
|
|
'game': game.value,
|
2019-12-08 22:43:49 +01:00
|
|
|
'version': version,
|
|
|
|
'name': name,
|
|
|
|
'start_time': start_time,
|
|
|
|
'end_time': end_time,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
for result in cursor.fetchall():
|
|
|
|
if result['start_time'] == start_time and result['end_time'] == end_time:
|
|
|
|
# This is just this event being updated, that's fine.
|
|
|
|
continue
|
2020-01-07 22:29:07 +01:00
|
|
|
raise Exception(f'This event overlaps an existing one with start time {result["start_time"]} and end time {result["end_time"]}')
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Insert or update this setting
|
|
|
|
sql = (
|
|
|
|
"INSERT INTO time_sensitive_settings (game, version, name, start_time, end_time, data) "
|
|
|
|
"VALUES (:game, :version, :name, :start_time, :end_time, :data) "
|
|
|
|
"ON DUPLICATE KEY UPDATE data=VALUES(data)"
|
|
|
|
)
|
|
|
|
self.execute(
|
|
|
|
sql,
|
|
|
|
{
|
2021-08-19 21:21:22 +02:00
|
|
|
'game': game.value,
|
2019-12-08 22:43:49 +01:00
|
|
|
'version': version,
|
|
|
|
'name': name,
|
|
|
|
'start_time': start_time,
|
|
|
|
'end_time': end_time,
|
|
|
|
'data': self.serialize(settings),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def get_item(self, game: GameConstants, version: int, catid: int, cattype: str) -> Optional[ValidatedDict]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a game/userid and catalog id/type, find that catalog entry.
|
|
|
|
|
|
|
|
Note that there can be more than one catalog entry with the same ID and game/userid
|
|
|
|
as long as each one is a different type. Essentially, cattype namespaces catalog entry.
|
|
|
|
|
|
|
|
Parameters:
|
2021-08-19 21:21:22 +02:00
|
|
|
game - Enum value identifier of the game looking up this entry.
|
2019-12-08 22:43:49 +01:00
|
|
|
version - Integer identifier of the version looking up this entry.
|
|
|
|
catid - Integer ID, as provided by a game.
|
|
|
|
cattype - The type of catalog entry.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A dictionary as stored by a game class previously, or None if not found.
|
|
|
|
"""
|
|
|
|
sql = (
|
|
|
|
"SELECT data FROM catalog "
|
|
|
|
"WHERE game = :game AND version = :version AND id = :id AND type = :type"
|
|
|
|
)
|
2021-08-19 21:21:22 +02:00
|
|
|
cursor = self.execute(sql, {'game': game.value, 'version': version, 'id': catid, 'type': cattype})
|
2019-12-08 22:43:49 +01:00
|
|
|
if cursor.rowcount != 1:
|
|
|
|
# entry doesn't exist
|
|
|
|
return None
|
|
|
|
|
|
|
|
result = cursor.fetchone()
|
|
|
|
return ValidatedDict(self.deserialize(result['data']))
|
|
|
|
|
2021-08-19 21:21:22 +02:00
|
|
|
def get_items(self, game: GameConstants, version: int) -> List[Item]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a game/userid, find all items in the catalog.
|
|
|
|
|
|
|
|
Parameters:
|
2021-08-19 21:21:22 +02:00
|
|
|
game - Enum value identifier of the game looking up the catalog.
|
2019-12-08 22:43:49 +01:00
|
|
|
version - Integer identifier of the version looking up this catalog.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of Item objects.
|
|
|
|
"""
|
|
|
|
sql = "SELECT id, type, data FROM catalog WHERE game = :game AND version = :version"
|
2021-08-19 21:21:22 +02:00
|
|
|
cursor = self.execute(sql, {'game': game.value, 'version': version})
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
catalog = []
|
|
|
|
for result in cursor.fetchall():
|
|
|
|
catalog.append(
|
|
|
|
Item(
|
|
|
|
result['type'],
|
|
|
|
result['id'],
|
|
|
|
self.deserialize(result['data']),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return catalog
|