1
0
mirror of synced 2025-01-31 12:13:49 +01:00

Reformat all multi-line SQL to remove format calls and make more readable.

This commit is contained in:
Jennifer Taylor 2023-03-19 18:09:06 +00:00
parent dfd8f70970
commit bce2fa0624
7 changed files with 468 additions and 305 deletions

View File

@ -172,10 +172,10 @@ class BaseData:
expiration = Time.now() + expiration expiration = Time.now() + expiration
# Use that session # Use that session
sql = ( sql = """
"INSERT INTO session (id, session, type, expiration) " INSERT INTO session (id, session, type, expiration)
+ "VALUES (:id, :session, :optype, :expiration)" VALUES (:id, :session, :optype, :expiration)
) """
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {

View File

@ -115,11 +115,11 @@ class GameData(BaseData):
settings - A dictionary of settings that a game wishes to retrieve later. settings - A dictionary of settings that a game wishes to retrieve later.
""" """
# Add settings json to game settings # Add settings json to game settings
sql = ( sql = """
"INSERT INTO game_settings (game, userid, data) " INSERT INTO game_settings (game, userid, data)
+ "VALUES (:game, :userid, :data) " VALUES (:game, :userid, :data)
+ "ON DUPLICATE KEY UPDATE data=VALUES(data)" ON DUPLICATE KEY UPDATE data=VALUES(data)
) """
self.execute( self.execute(
sql, sql,
{"game": game.value, "userid": userid, "data": self.serialize(settings)}, {"game": game.value, "userid": userid, "data": self.serialize(settings)},
@ -147,10 +147,10 @@ class GameData(BaseData):
Returns: Returns:
A dictionary as stored by a game class previously, or None if not found. A dictionary as stored by a game class previously, or None if not found.
""" """
sql = ( sql = """
"SELECT data FROM series_achievement " SELECT data FROM series_achievement
"WHERE game = :game AND userid = :userid AND id = :id AND type = :type" WHERE game = :game AND userid = :userid AND id = :id AND type = :type
) """
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -212,11 +212,11 @@ class GameData(BaseData):
data - A dictionary of data that the game wishes to retrieve later. data - A dictionary of data that the game wishes to retrieve later.
""" """
# Add achievement JSON to achievements # Add achievement JSON to achievements
sql = ( sql = """
"INSERT INTO series_achievement (game, userid, id, type, data) " INSERT INTO series_achievement (game, userid, id, type, data)
+ "VALUES (:game, :userid, :id, :type, :data) " VALUES (:game, :userid, :id, :type, :data)
+ "ON DUPLICATE KEY UPDATE data=VALUES(data)" ON DUPLICATE KEY UPDATE data=VALUES(data)
) """
self.execute( self.execute(
sql, sql,
{ {
@ -244,10 +244,15 @@ class GameData(BaseData):
If settings were found, they are guaranteed to include the attributes 'start_time' and 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). 'end_time' which will both be seconds since the unix epoch (UTC).
""" """
sql = ( sql = """
"SELECT data, start_time, end_time FROM time_sensitive_settings WHERE " SELECT data, start_time, end_time FROM time_sensitive_settings
"game = :game AND version = :version AND name = :name AND start_time <= :time AND end_time > :time" WHERE
) game = :game AND
version = :version AND
name = :name AND
start_time <= :time AND
end_time > :time
"""
cursor = self.execute( cursor = self.execute(
sql, sql,
{"game": game.value, "version": version, "name": name, "time": Time.now()}, {"game": game.value, "version": version, "name": name, "time": Time.now()},
@ -278,10 +283,10 @@ class GameData(BaseData):
If settings were found, they are guaranteed to include the attributes 'start_time' and 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). 'end_time' which will both be seconds since the unix epoch (UTC).
""" """
sql = ( sql = """
"SELECT data, start_time, end_time FROM time_sensitive_settings WHERE " SELECT data, start_time, end_time FROM time_sensitive_settings
"game = :game AND version = :version AND name = :name" WHERE game = :game AND version = :version AND name = :name
) """
cursor = self.execute( cursor = self.execute(
sql, {"game": game.value, "version": version, "name": name} sql, {"game": game.value, "version": version, "name": name}
) )
@ -327,12 +332,12 @@ class GameData(BaseData):
# Verify that this isn't overlapping some event. # Verify that this isn't overlapping some event.
sql = """ sql = """
SELECT start_time, end_time FROM time_sensitive_settings WHERE SELECT start_time, end_time FROM time_sensitive_settings
game = :game AND version = :version AND name = :name AND WHERE game = :game AND version = :version AND name = :name AND
( (
(start_time >= :start_time AND start_time < :end_time) OR (start_time >= :start_time AND start_time < :end_time) OR
(end_time > :start_time AND end_time <= :end_time) OR (end_time > :start_time AND end_time <= :end_time) OR
(start_time < :start_time AND end_time > :end_time) (start_time < :start_time AND end_time > :end_time)
) )
""" """
cursor = self.execute( cursor = self.execute(
@ -354,11 +359,11 @@ class GameData(BaseData):
) )
# Insert or update this setting # Insert or update this setting
sql = ( sql = """
"INSERT INTO time_sensitive_settings (game, version, name, start_time, end_time, data) " INSERT INTO time_sensitive_settings (game, version, name, start_time, end_time, data)
"VALUES (:game, :version, :name, :start_time, :end_time, :data) " VALUES (:game, :version, :name, :start_time, :end_time, :data)
"ON DUPLICATE KEY UPDATE data=VALUES(data)" ON DUPLICATE KEY UPDATE data=VALUES(data)
) """
self.execute( self.execute(
sql, sql,
{ {
@ -389,10 +394,10 @@ class GameData(BaseData):
Returns: Returns:
A dictionary as stored by a game class previously, or None if not found. A dictionary as stored by a game class previously, or None if not found.
""" """
sql = ( sql = """
"SELECT data FROM catalog " SELECT data FROM catalog
"WHERE game = :game AND version = :version AND id = :id AND type = :type" WHERE game = :game AND version = :version AND id = :id AND type = :type
) """
cursor = self.execute( cursor = self.execute(
sql, {"game": game.value, "version": version, "id": catid, "type": cattype} sql, {"game": game.value, "version": version, "id": catid, "type": cattype}
) )

View File

@ -62,11 +62,14 @@ class LobbyData(BaseData):
always contain an 'id' field which is the play session ID, and a 'time' field always contain an 'id' field which is the play session ID, and a 'time' field
which represents the timestamp when the play session began. which represents the timestamp when the play session began.
""" """
sql = ( sql = """
"SELECT id, time, data FROM playsession " SELECT id, time, data FROM playsession
"WHERE game = :game AND version = :version AND userid = :userid " WHERE
"AND time > :time" game = :game AND
) version = :version AND
userid = :userid AND
time > :time
"""
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -101,11 +104,10 @@ class LobbyData(BaseData):
A list of Tuples, consisting of a UserID and the dictionary that would be A list of Tuples, consisting of a UserID and the dictionary that would be
returned for that user if get_play_session_info() was called for that user. returned for that user if get_play_session_info() was called for that user.
""" """
sql = ( sql = """
"SELECT id, time, userid, data FROM playsession " SELECT id, time, userid, data FROM playsession
"WHERE game = :game AND version = :version " WHERE game = :game AND version = :version AND time > :time
"AND time > :time" """
)
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -142,11 +144,11 @@ class LobbyData(BaseData):
del data["time"] del data["time"]
# Add json to player session # Add json to player session
sql = ( sql = """
"INSERT INTO playsession (game, version, userid, time, data) " INSERT INTO playsession (game, version, userid, time, data)
+ "VALUES (:game, :version, :userid, :time, :data) " VALUES (:game, :version, :userid, :time, :data)
+ "ON DUPLICATE KEY UPDATE time=VALUES(time), data=VALUES(data)" ON DUPLICATE KEY UPDATE time=VALUES(time), data=VALUES(data)
) """
self.execute( self.execute(
sql, sql,
{ {
@ -200,11 +202,14 @@ class LobbyData(BaseData):
always contain an 'id' field which is the lobby ID, and a 'time' field representing always contain an 'id' field which is the lobby ID, and a 'time' field representing
the timestamp the lobby was created. the timestamp the lobby was created.
""" """
sql = ( sql = """
"SELECT id, time, data FROM lobby " SELECT id, time, data FROM lobby
"WHERE game = :game AND version = :version AND userid = :userid " WHERE
"AND time > :time" game = :game AND
) version = :version AND
userid = :userid AND
time > :time
"""
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -238,10 +243,10 @@ class LobbyData(BaseData):
Returns: Returns:
A list of dictionaries representing lobby info stored by a game class. A list of dictionaries representing lobby info stored by a game class.
""" """
sql = ( sql = """
"SELECT userid, id, data FROM lobby " SELECT userid, id, data FROM lobby
"WHERE game = :game AND version = :version AND time > :time" WHERE game = :game AND version = :version AND time > :time
) """
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -278,11 +283,11 @@ class LobbyData(BaseData):
del data["time"] del data["time"]
# Add json to lobby # Add json to lobby
sql = ( sql = """
"INSERT INTO lobby (game, version, userid, time, data) " INSERT INTO lobby (game, version, userid, time, data)
+ "VALUES (:game, :version, :userid, :time, :data) " VALUES (:game, :version, :userid, :time, :data)
+ "ON DUPLICATE KEY UPDATE time=VALUES(time), data=VALUES(data)" ON DUPLICATE KEY UPDATE time=VALUES(time), data=VALUES(data)
) """
self.execute( self.execute(
sql, sql,
{ {

View File

@ -166,7 +166,10 @@ class MachineData(BaseData):
Returns: Returns:
A Machine object representing a machine, or None if not found. A Machine object representing a machine, or None if not found.
""" """
sql = "SELECT name, description, arcadeid, id, port, game, version, data FROM machine WHERE pcbid = :pcbid" sql = """
SELECT name, description, arcadeid, id, port, game, version, data
FROM machine WHERE pcbid = :pcbid
"""
cursor = self.execute(sql, {"pcbid": pcbid}) cursor = self.execute(sql, {"pcbid": pcbid})
if cursor.rowcount != 1: if cursor.rowcount != 1:
# Machine doesn't exist # Machine doesn't exist
@ -222,10 +225,18 @@ class MachineData(BaseData):
machine - A Machine object representing a machine. machine - A Machine object representing a machine.
""" """
# Update machine name based on game # Update machine name based on game
sql = ( sql = """
"UPDATE `machine` SET name = :name, description = :description, arcadeid = :arcadeid, " UPDATE `machine`
+ "port = :port, game = :game, version = :version, data = :data WHERE pcbid = :pcbid LIMIT 1" SET
) name = :name,
description = :description,
arcadeid = :arcadeid,
port = :port,
game = :game,
version = :version,
data = :data
WHERE pcbid = :pcbid LIMIT 1
"""
self.execute( self.execute(
sql, sql,
{ {
@ -280,10 +291,10 @@ class MachineData(BaseData):
# Add new machine # Add new machine
try: try:
sql = ( sql = """
"INSERT INTO `machine` (pcbid, name, description, port, arcadeid) " INSERT INTO `machine` (pcbid, name, description, port, arcadeid)
+ "VALUES (:pcbid, :name, :description, :port, :arcadeid)" VALUES (:pcbid, :name, :description, :port, :arcadeid)
) """
self.execute( self.execute(
sql, sql,
{ {
@ -327,10 +338,10 @@ class MachineData(BaseData):
Returns: Returns:
An Arcade object representing this arcade An Arcade object representing this arcade
""" """
sql = ( sql = """
"INSERT INTO arcade (name, description, pref, area, data, pin) " INSERT INTO arcade (name, description, pref, area, data, pin)
+ "VALUES (:name, :desc, :pref, :area, :data, '00000000')" VALUES (:name, :desc, :pref, :area, :data, '00000000')
) """
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -345,9 +356,10 @@ class MachineData(BaseData):
raise ArcadeCreationException("Failed to create arcade!") raise ArcadeCreationException("Failed to create arcade!")
arcadeid = cursor.lastrowid arcadeid = cursor.lastrowid
for owner in owners: for owner in owners:
sql = ( sql = """
"INSERT INTO arcade_owner (userid, arcadeid) VALUES(:userid, :arcadeid)" INSERT INTO arcade_owner (userid, arcadeid)
) VALUES (:userid, :arcadeid)
"""
self.execute(sql, {"userid": owner, "arcadeid": arcadeid}) self.execute(sql, {"userid": owner, "arcadeid": arcadeid})
new_arcade = self.get_arcade(arcadeid) new_arcade = self.get_arcade(arcadeid)
if new_arcade is None: if new_arcade is None:
@ -364,9 +376,10 @@ class MachineData(BaseData):
Returns: Returns:
An Arcade object if this arcade was found, or None otherwise. An Arcade object if this arcade was found, or None otherwise.
""" """
sql = ( sql = """
"SELECT name, description, pin, pref, area, data FROM arcade WHERE id = :id" SELECT name, description, pin, pref, area, data
) FROM arcade WHERE id = :id
"""
cursor = self.execute(sql, {"id": arcadeid}) cursor = self.execute(sql, {"id": arcadeid})
if cursor.rowcount != 1: if cursor.rowcount != 1:
# Arcade doesn't exist # Arcade doesn't exist
@ -396,11 +409,17 @@ class MachineData(BaseData):
arcade - An Arcade object that should be updated. arcade - An Arcade object that should be updated.
""" """
# Update machine name based on game # Update machine name based on game
sql = ( sql = """
"UPDATE `arcade` " UPDATE `arcade`
+ "SET name = :name, description = :desc, pin = :pin, pref = :pref, area = :area, data = :data " SET
+ "WHERE id = :arcadeid" name = :name,
) description = :desc,
pin = :pin,
pref = :pref,
area = :area,
data = :data
WHERE id = :arcadeid
"""
self.execute( self.execute(
sql, sql,
{ {
@ -416,9 +435,10 @@ class MachineData(BaseData):
sql = "DELETE FROM `arcade_owner` WHERE arcadeid = :arcadeid" sql = "DELETE FROM `arcade_owner` WHERE arcadeid = :arcadeid"
self.execute(sql, {"arcadeid": arcade.id}) self.execute(sql, {"arcadeid": arcade.id})
for owner in arcade.owners: for owner in arcade.owners:
sql = ( sql = """
"INSERT INTO arcade_owner (userid, arcadeid) VALUES(:userid, :arcadeid)" INSERT INTO arcade_owner (userid, arcadeid)
) VALUES (:userid, :arcadeid)
"""
self.execute(sql, {"userid": owner, "arcadeid": arcade.id}) self.execute(sql, {"userid": owner, "arcadeid": arcade.id})
def destroy_arcade(self, arcadeid: ArcadeID) -> None: def destroy_arcade(self, arcadeid: ArcadeID) -> None:
@ -515,11 +535,11 @@ class MachineData(BaseData):
setting - String identifying the particular setting we're interestsed in. setting - String identifying the particular setting we're interestsed in.
data - A dictionary that should be saved for this setting. data - A dictionary that should be saved for this setting.
""" """
sql = ( sql = """
"INSERT INTO arcade_settings (arcadeid, game, version, type, data) " INSERT INTO arcade_settings (arcadeid, game, version, type, data)
+ "VALUES (:id, :game, :version, :type, :data) " VALUES (:id, :game, :version, :type, :data)
"ON DUPLICATE KEY UPDATE data=VALUES(data)" ON DUPLICATE KEY UPDATE data=VALUES(data)
) """
self.execute( self.execute(
sql, sql,
{ {

View File

@ -151,19 +151,27 @@ class MusicData(BaseData):
# Add to user score # Add to user score
if new_record: if new_record:
# We want to update the timestamp/location to now if its a new record. # We want to update the timestamp/location to now if its a new record.
sql = ( sql = """
"INSERT INTO `score` (`userid`, `musicid`, `points`, `data`, `timestamp`, `update`, `lid`) " INSERT INTO `score` (`userid`, `musicid`, `points`, `data`, `timestamp`, `update`, `lid`)
+ "VALUES (:userid, :musicid, :points, :data, :timestamp, :update, :location) " VALUES (:userid, :musicid, :points, :data, :timestamp, :update, :location)
+ "ON DUPLICATE KEY UPDATE data = VALUES(data), points = VALUES(points), " ON DUPLICATE KEY UPDATE
+ "timestamp = VALUES(timestamp), `update` = VALUES(`update`), lid = VALUES(lid)" data = VALUES(data),
) points = VALUES(points),
`update` = VALUES(`update`),
timestamp = VALUES(timestamp),
lid = VALUES(lid)
"""
else: else:
# We only want to add the timestamp if it is new. # We don't want to add the timestamp of the record since it wasn't a new high score.
sql = ( # We also don't want to update thet location since this wasn't a new record.
"INSERT INTO `score` (`userid`, `musicid`, `points`, `data`, `timestamp`, `update`, `lid`) " sql = """
+ "VALUES (:userid, :musicid, :points, :data, :timestamp, :update, :location) " INSERT INTO `score` (`userid`, `musicid`, `points`, `data`, `timestamp`, `update`, `lid`)
+ "ON DUPLICATE KEY UPDATE data = VALUES(data), points = VALUES(points), `update` = VALUES(`update`)" VALUES (:userid, :musicid, :points, :data, :timestamp, :update, :location)
) ON DUPLICATE KEY UPDATE
data = VALUES(data),
points = VALUES(points),
`update` = VALUES(`update`)
"""
self.execute( self.execute(
sql, sql,
{ {
@ -213,10 +221,10 @@ class MusicData(BaseData):
ts = timestamp if timestamp is not None else Time.now() ts = timestamp if timestamp is not None else Time.now()
# Add to score history # Add to score history
sql = ( sql = """
"INSERT INTO `score_history` (userid, musicid, timestamp, lid, new_record, points, data) " INSERT INTO `score_history` (userid, musicid, timestamp, lid, new_record, points, data)
+ "VALUES (:userid, :musicid, :timestamp, :location, :new_record, :points, :data)" VALUES (:userid, :musicid, :timestamp, :location, :new_record, :points, :data)
) """
try: try:
self.execute( self.execute(
sql, sql,
@ -256,12 +264,30 @@ class MusicData(BaseData):
Returns: Returns:
The optional data stored by the game previously, or None if no score exists. The optional data stored by the game previously, or None if no score exists.
""" """
sql = ( sql = """
"SELECT music.songid AS songid, music.chart AS chart, score.id AS scorekey, score.timestamp AS timestamp, score.update AS `update`, score.lid AS lid, " SELECT
+ "(select COUNT(score_history.timestamp) FROM score_history WHERE score_history.musicid = music.id AND score_history.userid = :userid) AS plays, " music.songid AS songid,
+ "score.points AS points, score.data AS data FROM score, music WHERE score.userid = :userid AND score.musicid = music.id " music.chart AS chart,
+ "AND music.game = :game AND music.version = :version AND music.songid = :songid AND music.chart = :songchart" score.id AS scorekey,
) score.timestamp AS timestamp,
score.update AS `update`,
score.lid AS lid,
(
SELECT COUNT(score_history.timestamp)
FROM score_history
WHERE score_history.musicid = music.id AND score_history.userid = :userid
) AS plays,
score.points AS points,
score.data AS data
FROM score, music
WHERE
score.userid = :userid AND
score.musicid = music.id AND
music.game = :game AND
music.version = :version AND
music.songid = :songid AND
music.chart = :songchart
"""
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -303,13 +329,29 @@ class MusicData(BaseData):
Returns: Returns:
The optional data stored by the game previously, or None if no score exists. The optional data stored by the game previously, or None if no score exists.
""" """
sql = ( sql = """
"SELECT music.songid AS songid, music.chart AS chart, score.id AS scorekey, score.timestamp AS timestamp, score.update AS `update`, " SELECT
+ "score.userid AS userid, score.lid AS lid, " music.songid AS songid,
+ "(select COUNT(score_history.timestamp) FROM score_history WHERE score_history.musicid = music.id AND score_history.userid = score.userid) AS plays, " music.chart AS chart,
+ "score.points AS points, score.data AS data FROM score, music WHERE score.id = :scorekey AND score.musicid = music.id " score.id AS scorekey,
+ "AND music.game = :game AND music.version = :version" score.timestamp AS timestamp,
) score.update AS `update`,
score.userid AS userid,
score.lid AS lid,
(
SELECT COUNT(score_history.timestamp)
FROM score_history
WHERE score_history.musicid = music.id AND score_history.userid = score.userid
) AS plays,
score.points AS points,
score.data AS data
FROM score, music
WHERE
score.id = :scorekey AND
score.musicid = music.id AND
music.game = :game AND
music.version = :version
"""
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -357,12 +399,27 @@ class MusicData(BaseData):
Returns: Returns:
A list of Score objects representing all high scores for a game. A list of Score objects representing all high scores for a game.
""" """
sql = ( sql = """
"SELECT music.songid AS songid, music.chart AS chart, score.id AS scorekey, score.timestamp AS timestamp, score.update AS `update`, score.lid AS lid, " SELECT
+ "(select COUNT(score_history.timestamp) FROM score_history WHERE score_history.musicid = music.id AND score_history.userid = :userid) AS plays, " music.songid AS songid,
+ "score.points AS points, score.data AS data FROM score, music WHERE score.userid = :userid AND score.musicid = music.id " music.chart AS chart,
+ "AND music.game = :game AND music.version = :version" score.id AS scorekey,
) score.timestamp AS timestamp,
score.update AS `update`,
score.lid AS lid,
(
select COUNT(score_history.timestamp) FROM score_history
WHERE score_history.musicid = music.id AND score_history.userid = :userid
) AS plays,
score.points AS points,
score.data AS data
FROM score, music
WHERE
score.userid = :userid AND
score.musicid = music.id AND
music.game = :game AND
music.version = :version
"""
if since is not None: if since is not None:
sql = sql + " AND score.update >= :since" sql = sql + " AND score.update >= :since"
if until is not None: if until is not None:
@ -408,12 +465,18 @@ class MusicData(BaseData):
Returns: Returns:
A list of tuples, containing the songid and the number of plays across all charts for that song. A list of tuples, containing the songid and the number of plays across all charts for that song.
""" """
sql = ( sql = """
"SELECT music.songid AS songid, COUNT(score_history.timestamp) AS plays FROM score_history, music " SELECT
+ "WHERE score_history.userid = :userid AND score_history.musicid = music.id " music.songid AS songid,
+ "AND music.game = :game AND music.version = :version " COUNT(score_history.timestamp) AS plays
+ "GROUP BY songid ORDER BY plays DESC LIMIT :count" FROM score_history, music
) WHERE
score_history.userid = :userid AND
score_history.musicid = music.id AND
music.game = :game AND
music.version = :version
GROUP BY songid ORDER BY plays DESC LIMIT :count
"""
cursor = self.execute( cursor = self.execute(
sql, sql,
{"userid": userid, "game": game.value, "version": version, "count": count}, {"userid": userid, "game": game.value, "version": version, "count": count},
@ -436,12 +499,18 @@ class MusicData(BaseData):
Returns: Returns:
A list of tuples, containing the songid and the last played time for this song. A list of tuples, containing the songid and the last played time for this song.
""" """
sql = ( sql = """
"SELECT DISTINCT(music.songid) AS songid, score_history.timestamp AS timestamp FROM score_history, music " SELECT
+ "WHERE score_history.userid = :userid AND score_history.musicid = music.id " DISTINCT(music.songid) AS songid,
+ "AND music.game = :game AND music.version = :version " score_history.timestamp AS timestamp
+ "ORDER BY timestamp DESC LIMIT :count" FROM score_history, music
) WHERE
score_history.userid = :userid AND
score_history.musicid = music.id AND
music.game = :game AND
music.version = :version
ORDER BY timestamp DESC LIMIT :count
"""
cursor = self.execute( cursor = self.execute(
sql, sql,
{"userid": userid, "game": game.value, "version": version, "count": count}, {"userid": userid, "game": game.value, "version": version, "count": count},
@ -467,10 +536,16 @@ class MusicData(BaseData):
Returns: Returns:
A list of tuples, containing the songid and the number of plays across all charts for that song. A list of tuples, containing the songid and the number of plays across all charts for that song.
""" """
sql = ( sql = """
"SELECT music.songid AS songid, COUNT(score_history.timestamp) AS plays FROM score_history, music " SELECT
+ "WHERE score_history.musicid = music.id AND music.game = :game AND music.version = :version " music.songid AS songid,
) COUNT(score_history.timestamp) AS plays
FROM score_history, music
WHERE
score_history.musicid = music.id AND
music.game = :game AND
music.version = :version
"""
timestamp: Optional[int] = None timestamp: Optional[int] = None
if days is not None: if days is not None:
# Only select the last X days of hit chart # Only select the last X days of hit chart
@ -509,11 +584,19 @@ class MusicData(BaseData):
Returns: Returns:
A Song object representing the song details A Song object representing the song details
""" """
sql = ( sql = """
"SELECT music.name AS name, music.artist AS artist, music.genre AS genre, music.data AS data " SELECT
+ "FROM music WHERE music.game = :game AND music.version = :version AND " music.name AS name,
+ "music.songid = :songid AND music.chart = :songchart" music.artist AS artist,
) music.genre AS genre,
music.data AS data
FROM music
WHERE
music.game = :game AND
music.version = :version AND
music.songid = :songid AND
music.chart = :songchart
"""
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -553,10 +636,10 @@ class MusicData(BaseData):
Returns: Returns:
A list of Song objects detailing the song information for each song. A list of Song objects detailing the song information for each song.
""" """
sql = ( sql = """
"SELECT version, songid, chart, name, artist, genre, data FROM music " SELECT version, songid, chart, name, artist, genre, data
"WHERE music.game = :game" FROM music WHERE music.game = :game
) """
params: Dict[str, Any] = {"game": game.value} params: Dict[str, Any] = {"game": game.value}
if version is not None: if version is not None:
sql += " AND music.version = :version" sql += " AND music.version = :version"
@ -579,49 +662,6 @@ class MusicData(BaseData):
for result in cursor for result in cursor
] ]
def get_all_versions_of_song(
self,
game: GameConstants,
version: int,
songid: int,
songchart: int,
interested_versions: Optional[List[int]] = None,
) -> List[Song]:
"""
Given a game/version/songid/chart, look up all versions of that song across all game versions.
Parameters:
game - Enum value representing a game series.
version - Integer representing which version of the game.
songid - Integer representing the ID (from the game) for this song.
songchart - Integer representing the chart for this song.
Returns:
A list of Song objects representing all song versions.
"""
musicid = self.__get_musicid(game, version, songid, songchart)
sql = (
"SELECT version, songid, chart, name, artist, genre, data FROM music "
"WHERE music.id = :musicid"
)
if interested_versions is not None:
sql += f" AND music.version in ({','.join(str(int(v)) for v in interested_versions)})"
cursor = self.execute(sql, {"musicid": musicid})
return [
Song(
game,
result["version"],
result["songid"],
result["chart"],
result["name"],
result["artist"],
result["genre"],
self.deserialize(result["data"]),
)
for result in cursor
]
def get_all_scores( def get_all_scores(
self, self,
game: GameConstants, game: GameConstants,
@ -663,10 +703,20 @@ class MusicData(BaseData):
innerselect = innerselect + " AND chart = :songchart" innerselect = innerselect + " AND chart = :songchart"
# Finally, construct the full query # Finally, construct the full query
sql = ( sql = f"""
"SELECT ({}) AS songid, ({}) AS chart, id AS scorekey, points, timestamp, `update`, lid, data, userid, ({}) AS plays " SELECT
"FROM score WHERE musicid IN ({})" ({songidquery}) AS songid,
).format(songidquery, chartquery, playselect, innerselect) ({chartquery}) AS chart,
id AS scorekey,
points,
timestamp,
`update`,
lid,
data,
userid,
({playselect}) AS plays
FROM score WHERE musicid IN ({innerselect})
"""
# Now, limit the query # Now, limit the query
if userid is not None: if userid is not None:
@ -766,17 +816,30 @@ class MusicData(BaseData):
params["userlist"] = tuple(userlist) params["userlist"] = tuple(userlist)
else: else:
user_sql = f"SELECT userid FROM score WHERE score.musicid = played.musicid {location_sql} ORDER BY points DESC, timestamp DESC LIMIT 1" user_sql = f"SELECT userid FROM score WHERE score.musicid = played.musicid {location_sql} ORDER BY points DESC, timestamp DESC LIMIT 1"
records_sql = ( records_sql = f"""
f"SELECT ({user_sql}) AS userid, musicid FROM ({musicid_sql}) played" SELECT ({user_sql}) AS userid, musicid
) FROM ({musicid_sql}) played
"""
# Now, join it up against the score and music table to grab the info we need # Now, join it up against the score and music table to grab the info we need
sql = ( sql = f"""
"SELECT ({}) AS songid, ({}) AS chart, score.points AS points, score.userid AS userid, score.id AS scorekey, score.data AS data, " SELECT
+ "score.timestamp AS timestamp, score.update AS `update`, " ({songidquery}) AS songid,
+ "score.lid AS lid, (select COUNT(score_history.timestamp) FROM score_history WHERE score_history.musicid = score.musicid) AS plays " ({chartquery}) AS chart,
+ "FROM score, ({}) records WHERE records.userid = score.userid AND records.musicid = score.musicid" score.points AS points,
).format(songidquery, chartquery, records_sql) score.userid AS userid,
score.id AS scorekey,
score.data AS data,
score.timestamp AS timestamp,
score.update AS `update`,
score.lid AS lid,
(
SELECT COUNT(score_history.timestamp) FROM score_history
WHERE score_history.musicid = score.musicid
) AS plays
FROM score, ({records_sql}) records
WHERE records.userid = score.userid AND records.musicid = score.musicid
"""
cursor = self.execute(sql, params) cursor = self.execute(sql, params)
return [ return [
@ -811,11 +874,24 @@ class MusicData(BaseData):
Returns: Returns:
The optional data stored by the game previously, or None if no score exists. The optional data stored by the game previously, or None if no score exists.
""" """
sql = ( sql = """
"SELECT music.songid AS songid, music.chart AS chart, score_history.id AS scorekey, score_history.timestamp AS timestamp, score_history.userid AS userid, " SELECT
+ "score_history.lid AS lid, score_history.new_record AS new_record, score_history.points AS points, score_history.data AS data FROM score_history, music " music.songid AS songid,
+ "WHERE score_history.id = :scorekey AND score_history.musicid = music.id AND music.game = :game AND music.version = :version" music.chart AS chart,
) score_history.id AS scorekey,
score_history.timestamp AS timestamp,
score_history.userid AS userid,
score_history.lid AS lid,
score_history.new_record AS new_record,
score_history.points AS points,
score_history.data AS data
FROM score_history, music
WHERE
score_history.id = :scorekey AND
score_history.musicid = music.id AND
music.game = :game AND
music.version = :version
"""
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -882,10 +958,19 @@ class MusicData(BaseData):
innerselect = innerselect + " AND chart = :songchart" innerselect = innerselect + " AND chart = :songchart"
# Finally, construct the full query # Finally, construct the full query
sql = ( sql = f"""
"SELECT ({}) AS songid, ({}) AS chart, id AS scorekey, timestamp, points, new_record, lid, data, userid " SELECT
"FROM score_history WHERE musicid IN ({})" ({songidquery}) AS songid,
).format(songidquery, chartquery, innerselect) ({chartquery}) AS chart,
id AS scorekey,
timestamp,
points,
new_record,
lid,
data,
userid
FROM score_history WHERE musicid IN ({innerselect})
"""
# Now, limit the query # Now, limit the query
if userid is not None: if userid is not None:

View File

@ -170,11 +170,14 @@ class NetworkData(BaseData):
"Logic error, specify either 'daily' or 'weekly' for schedule type!" "Logic error, specify either 'daily' or 'weekly' for schedule type!"
) )
sql = ( sql = """
"SELECT year, day FROM scheduled_work " SELECT year, day FROM scheduled_work
"WHERE game = :game AND version = :version AND " WHERE
"name = :name AND schedule = :schedule" game = :game AND
) version = :version AND
name = :name AND
schedule = :schedule
"""
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -219,11 +222,11 @@ class NetworkData(BaseData):
if schedule == "daily": if schedule == "daily":
year, day = Time.days_into_year() year, day = Time.days_into_year()
sql = ( sql = """
"INSERT INTO scheduled_work (game, version, name, schedule, year, day) " INSERT INTO scheduled_work (game, version, name, schedule, year, day)
+ "VALUES (:game, :version, :name, :schedule, :year, :day) " VALUES (:game, :version, :name, :schedule, :year, :day)
+ "ON DUPLICATE KEY UPDATE year=VALUES(year), day=VALUES(day)" ON DUPLICATE KEY UPDATE year=VALUES(year), day=VALUES(day)
) """
self.execute( self.execute(
sql, sql,
{ {
@ -238,11 +241,11 @@ class NetworkData(BaseData):
if schedule == "weekly": if schedule == "weekly":
days = Time.week_in_days_since_epoch() days = Time.week_in_days_since_epoch()
sql = ( sql = """
"INSERT INTO scheduled_work (game, version, name, schedule, day) " INSERT INTO scheduled_work (game, version, name, schedule, day)
+ "VALUES (:game, :version, :name, :schedule, :day) " VALUES (:game, :version, :name, :schedule, :day)
+ "ON DUPLICATE KEY UPDATE day=VALUES(day)" ON DUPLICATE KEY UPDATE day=VALUES(day)
) """
self.execute( self.execute(
sql, sql,
{ {

View File

@ -509,12 +509,17 @@ class UserData(BaseData):
Returns: Returns:
A dictionary previously stored by a game class if found, or None otherwise. A dictionary previously stored by a game class if found, or None otherwise.
""" """
sql = ( sql = """
"SELECT refid.refid AS refid, extid.extid AS extid, profile.data AS data " SELECT refid.refid AS refid, extid.extid AS extid, profile.data AS data
+ "FROM refid, extid, profile " FROM refid, extid, profile
+ "WHERE refid.userid = :userid AND refid.game = :game AND refid.version = :version AND " WHERE
"extid.userid = refid.userid AND extid.game = refid.game AND profile.refid = refid.refid" refid.userid = :userid AND
) refid.game = :game AND
refid.version = :version AND
extid.userid = refid.userid AND
extid.game = refid.game AND
profile.refid = refid.refid
"""
cursor = self.execute( cursor = self.execute(
sql, {"userid": userid, "game": game.value, "version": version} sql, {"userid": userid, "game": game.value, "version": version}
) )
@ -650,12 +655,16 @@ class UserData(BaseData):
Returns: Returns:
A list of (UserID, dictionaries) previously stored by a game class for each profile. A list of (UserID, dictionaries) previously stored by a game class for each profile.
""" """
sql = ( sql = """
"SELECT refid.userid AS userid, refid.refid AS refid, extid.extid AS extid, profile.data AS data " SELECT refid.userid AS userid, refid.refid AS refid, extid.extid AS extid, profile.data AS data
"FROM refid, profile, extid " FROM refid, profile, extid
"WHERE refid.game = :game AND refid.version = :version " WHERE
"AND refid.refid = profile.refid AND extid.game = refid.game AND extid.userid = refid.userid" refid.game = :game AND
) refid.version = :version AND
refid.refid = profile.refid AND
extid.game = refid.game AND
extid.userid = refid.userid
"""
cursor = self.execute(sql, {"game": game.value, "version": version}) cursor = self.execute(sql, {"game": game.value, "version": version})
return [ return [
@ -683,10 +692,10 @@ class UserData(BaseData):
Returns: Returns:
A list of UserIDs for users that played this version of this game. A list of UserIDs for users that played this version of this game.
""" """
sql = ( sql = """
"SELECT refid.userid AS userid FROM refid " SELECT refid.userid AS userid FROM refid
"WHERE refid.game = :game AND refid.version = :version" WHERE refid.game = :game AND refid.version = :version
) """
cursor = self.execute(sql, {"game": game.value, "version": version}) cursor = self.execute(sql, {"game": game.value, "version": version})
return [UserID(result["userid"]) for result in cursor] return [UserID(result["userid"]) for result in cursor]
@ -708,11 +717,18 @@ class UserData(BaseData):
Returns: Returns:
A list of (UserID, Achievement) objects. A list of (UserID, Achievement) objects.
""" """
sql = ( sql = """
"SELECT achievement.id AS id, achievement.type AS type, achievement.data AS data, " SELECT
"refid.userid AS userid FROM achievement, refid WHERE refid.game = :game AND " achievement.id AS id,
"refid.version = :version AND refid.refid = achievement.refid" achievement.type AS type,
) achievement.data AS data,
refid.userid AS userid
FROM achievement, refid
WHERE
refid.game = :game AND
refid.version = :version AND
refid.refid = achievement.refid
"""
params: Dict[str, Any] = {"game": game.value, "version": version} params: Dict[str, Any] = {"game": game.value, "version": version}
if achievementtype is not None: if achievementtype is not None:
sql += " AND achievement.type = :type" sql += " AND achievement.type = :type"
@ -750,11 +766,11 @@ class UserData(BaseData):
refid = self.get_refid(game, version, userid) refid = self.get_refid(game, version, userid)
# Add profile json to game profile # Add profile json to game profile
sql = ( sql = """
"INSERT INTO profile (refid, data) " INSERT INTO profile (refid, data)
+ "VALUES (:refid, :json) " VALUES (:refid, :json)
+ "ON DUPLICATE KEY UPDATE data=VALUES(data)" ON DUPLICATE KEY UPDATE data=VALUES(data)
) """
self.execute(sql, {"refid": refid, "json": self.serialize(profile)}) self.execute(sql, {"refid": refid, "json": self.serialize(profile)})
# Update profile details just in case this was a new profile that was just saved. # Update profile details just in case this was a new profile that was just saved.
@ -866,11 +882,11 @@ class UserData(BaseData):
refid = self.get_refid(game, version, userid) refid = self.get_refid(game, version, userid)
# Add achievement JSON to achievements # Add achievement JSON to achievements
sql = ( sql = """
"INSERT INTO achievement (refid, id, type, data) " INSERT INTO achievement (refid, id, type, data)
+ "VALUES (:refid, :id, :type, :data) " VALUES (:refid, :id, :type, :data)
+ "ON DUPLICATE KEY UPDATE data=VALUES(data)" ON DUPLICATE KEY UPDATE data=VALUES(data)
) """
self.execute( self.execute(
sql, sql,
{ {
@ -902,9 +918,10 @@ class UserData(BaseData):
refid = self.get_refid(game, version, userid) refid = self.get_refid(game, version, userid)
# Nuke the achievement from the user # Nuke the achievement from the user
sql = ( sql = """
"DELETE FROM achievement WHERE refid = :refid AND id = :id AND type = :type" DELETE FROM achievement
) WHERE refid = :refid AND id = :id AND type = :type
"""
self.execute( self.execute(
sql, {"refid": refid, "id": achievementid, "type": achievementtype} sql, {"refid": refid, "id": achievementid, "type": achievementtype}
) )
@ -979,10 +996,10 @@ class UserData(BaseData):
refid = self.get_refid(game, version, userid) refid = self.get_refid(game, version, userid)
# Add achievement JSON to achievements # Add achievement JSON to achievements
sql = ( sql = """
"INSERT INTO time_based_achievement (refid, id, type, timestamp, data) " INSERT INTO time_based_achievement (refid, id, type, timestamp, data)
+ "VALUES (:refid, :id, :type, :ts, :data)" VALUES (:refid, :id, :type, :ts, :data)
) """
self.execute( self.execute(
sql, sql,
{ {
@ -1007,12 +1024,19 @@ class UserData(BaseData):
Returns: Returns:
A list of (UserID, Achievement) objects. A list of (UserID, Achievement) objects.
""" """
sql = ( sql = """
"SELECT time_based_achievement.id AS id, time_based_achievement.type AS type, " SELECT
"time_based_achievement.data AS data, time_based_achievement.timestamp AS timestamp, " time_based_achievement.id AS id,
"refid.userid AS userid FROM time_based_achievement, refid WHERE refid.game = :game AND " time_based_achievement.type AS type,
"refid.version = :version AND refid.refid = time_based_achievement.refid" time_based_achievement.data AS data,
) time_based_achievement.timestamp AS timestamp,
refid.userid AS userid
FROM time_based_achievement, refid
WHERE
refid.game = :game AND
refid.version = :version AND
refid.refid = time_based_achievement.refid
"""
cursor = self.execute(sql, {"game": game.value, "version": version}) cursor = self.execute(sql, {"game": game.value, "version": version})
return [ return [
@ -1052,7 +1076,16 @@ class UserData(BaseData):
Returns: Returns:
A dictionary as stored by a game class previously, or None if not found. A dictionary as stored by a game class previously, or None if not found.
""" """
sql = "SELECT data FROM link WHERE game = :game AND version = :version AND userid = :userid AND type = :type AND other_userid = :other_userid" sql = """
SELECT data
FROM link
WHERE
game = :game AND
version = :version AND
userid = :userid AND
type = :type AND
other_userid = :other_userid
"""
cursor = self.execute( cursor = self.execute(
sql, sql,
{ {
@ -1084,7 +1117,11 @@ class UserData(BaseData):
Returns: Returns:
A list of Link objects. A list of Link objects.
""" """
sql = "SELECT type, other_userid, data FROM link WHERE game = :game AND version = :version AND userid = :userid" sql = """
SELECT type, other_userid, data
FROM link
WHERE game = :game AND version = :version AND userid = :userid
"""
cursor = self.execute( cursor = self.execute(
sql, {"game": game.value, "version": version, "userid": userid} sql, {"game": game.value, "version": version, "userid": userid}
) )
@ -1120,11 +1157,11 @@ class UserData(BaseData):
data - A dictionary of data that the game wishes to retrieve later. data - A dictionary of data that the game wishes to retrieve later.
""" """
# Add link JSON to link # Add link JSON to link
sql = ( sql = """
"INSERT INTO link (game, version, userid, type, other_userid, data) " INSERT INTO link (game, version, userid, type, other_userid, data)
"VALUES (:game, :version, :userid, :type, :other_userid, :data) " VALUES (:game, :version, :userid, :type, :other_userid, :data)
"ON DUPLICATE KEY UPDATE data=VALUES(data)" ON DUPLICATE KEY UPDATE data=VALUES(data)
) """
self.execute( self.execute(
sql, sql,
{ {
@ -1155,7 +1192,15 @@ class UserData(BaseData):
linktype - The type of link. linktype - The type of link.
other_userid - Integer user ID of the account we're linked to. other_userid - Integer user ID of the account we're linked to.
""" """
sql = "DELETE FROM link WHERE game = :game AND version = :version AND userid = :userid AND type = :type AND other_userid = :other_userid" sql = """
DELETE FROM link
WHERE
game = :game AND
version = :version AND
userid = :userid AND
type = :type AND
other_userid = :other_userid
"""
self.execute( self.execute(
sql, sql,
{ {
@ -1200,10 +1245,10 @@ class UserData(BaseData):
Returns: Returns:
The new PASELI balance if successful, or None if there wasn't enough to apply the delta. The new PASELI balance if successful, or None if there wasn't enough to apply the delta.
""" """
sql = ( sql = """
"INSERT INTO balance (userid, arcadeid, balance) VALUES (:userid, :arcadeid, :delta) " INSERT INTO balance (userid, arcadeid, balance) VALUES (:userid, :arcadeid, :delta)
"ON DUPLICATE KEY UPDATE balance = balance + :delta" ON DUPLICATE KEY UPDATE balance = balance + :delta
) """
self.execute(sql, {"delta": delta, "userid": userid, "arcadeid": arcadeid}) self.execute(sql, {"delta": delta, "userid": userid, "arcadeid": arcadeid})
newbalance = self.get_balance(userid, arcadeid) newbalance = self.get_balance(userid, arcadeid)
if newbalance < 0: if newbalance < 0:
@ -1318,10 +1363,10 @@ class UserData(BaseData):
break break
# Use that extid # Use that extid
sql = ( sql = """
"INSERT INTO extid (game, extid, userid) " INSERT INTO extid (game, extid, userid)
+ "VALUES (:game, :extid, :userid)" VALUES (:game, :extid, :userid)
) """
try: try:
cursor = self.execute( cursor = self.execute(
sql, {"game": game.value, "extid": extid, "userid": userid} sql, {"game": game.value, "extid": extid, "userid": userid}
@ -1341,10 +1386,10 @@ class UserData(BaseData):
break break
# Use that refid # Use that refid
sql = ( sql = """
"INSERT INTO refid (game, version, refid, userid) " INSERT INTO refid (game, version, refid, userid)
+ "VALUES (:game, :version, :refid, :userid)" VALUES (:game, :version, :refid, :userid)
) """
try: try:
cursor = self.execute( cursor = self.execute(
sql, sql,