def get_achievements(self, game: str, userid: UserID) -> List[Achievement]: """ Given a game/userid, find all achievements Parameters: game - String identifier of the game looking up the user. 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" cursor = self.execute(sql, {'game': game, 'userid': userid}) achievements = [] for result in cursor.fetchall(): achievements.append( Achievement( result['id'], result['type'], None, self.deserialize(result['data']), )) return achievements
def get_all_time_based_achievements(self, game: str, version: int) -> List[Tuple[UserID, Achievement]]: """ Given a game/version, find all time-based achievements for all players. Parameters: game - String identifier of the game looking up the user. version - Integer version of the game looking up the user. Returns: A list of (UserID, Achievement) objects. """ sql = ( "SELECT time_based_achievement.id AS id, time_based_achievement.type AS type, " "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, 'version': version}) achievements = [] for result in cursor.fetchall(): achievements.append( ( UserID(result['userid']), Achievement( result['id'], result['type'], result['timestamp'], self.deserialize(result['data']), ), ) ) return achievements
def get_time_based_achievements( self, game: str, version: int, userid: UserID, achievementtype: Optional[str] = None, since: Optional[int] = None, until: Optional[int] = None, ) -> List[Achievement]: """ Given a game/version/userid, find all time-based achievements Parameters: game - String identifier of the game looking up the user. version - Integer version of the game looking up the user. userid - Integer user ID, as looked up by one of the above functions. achievementtype - Optional string specifying to constrain to a type of achievement. since - Return achievements since this time (inclusive). until - Return achievements until this time (exclusive). Returns: A list of Achievement objects. """ refid = self.get_refid(game, version, userid) sql = "SELECT id, type, timestamp, data FROM time_based_achievement WHERE refid = :refid" if achievementtype is not None: sql += " AND type = :type" if since is not None: sql += " AND timestamp >= :since" if until is not None: sql += " AND timestamp < :until" cursor = self.execute( sql, { 'refid': refid, 'type': achievementtype, 'since': since, 'until': until }) achievements = [] for result in cursor.fetchall(): achievements.append( Achievement( result['id'], result['type'], result['timestamp'], self.deserialize(result['data']), )) return achievements
def test_get_league_absentees(self) -> None: data = Mock() data.local = Mock() data.local.user = Mock() # Test that we do the right thing with empty input self.assertEqual( JubeatProp._get_league_absentees( None, 999, [], ), [], ) # Test that a user who never played doesn't get flagged absentee data.local.user.get_achievements = Mock(return_value=[]) self.assertEqual( JubeatProp._get_league_absentees( data, 999, [1337], ), [], ) data.local.user.get_achievements.assert_called_once_with( JubeatProp.game, JubeatProp.version, 1337, ) data.local.user.get_achievements.reset_mock() # Test that a user who only skipped last week doesn't get flagged absentee data.local.user.get_achievements = Mock(return_value=[ Achievement(997, 'league', None, {}), ]) self.assertEqual( JubeatProp._get_league_absentees( data, 999, [1337], ), [], ) data.local.user.get_achievements.assert_called_once_with( JubeatProp.game, JubeatProp.version, 1337, ) data.local.user.get_achievements.reset_mock() # Test that a user who skipped last two week gets flagged absentee data.local.user.get_achievements = Mock(return_value=[ Achievement(996, 'league', None, {}), ]) self.assertEqual( JubeatProp._get_league_absentees( data, 999, [1337], ), [1337], ) data.local.user.get_achievements.assert_called_once_with( JubeatProp.game, JubeatProp.version, 1337, ) data.local.user.get_achievements.reset_mock() # Test that a user who skipped last three week doesn't get flagged # (they got flagged last week) data.local.user.get_achievements = Mock(return_value=[ Achievement(995, 'league', None, {}), ]) self.assertEqual( JubeatProp._get_league_absentees( data, 999, [1337], ), [], ) data.local.user.get_achievements.assert_called_once_with( JubeatProp.game, JubeatProp.version, 1337, ) data.local.user.get_achievements.reset_mock() # Test that a user who skipped last four week gets flagged absentee data.local.user.get_achievements = Mock(return_value=[ Achievement(994, 'league', None, {}), ]) self.assertEqual( JubeatProp._get_league_absentees( data, 999, [1337], ), [1337], ) data.local.user.get_achievements.assert_called_once_with( JubeatProp.game, JubeatProp.version, 1337, ) data.local.user.get_achievements.reset_mock()