コード例 #1
0
def unlock_achievements(score, beatmap, user_data):
    """Return array of achievements the current play recived
	
	Arguments:
		score {Score} -- Score data recived from replay
		beatmap {Beatmap} -- Played beatmap
		user_data {dict} -- Info about the current player
	
	Returns:
		Array -- List of achievements for the current play
	"""
    achievements = []

    userID = userUtils.getID(score.playerName)
    user_cache = common.get_usercache(userID)

    # Get current gamemode and change value std to osu
    gamemode_index = score.gameMode

    # Check if user should run achivement recheck
    if user_cache["version"] < glob.ACHIEVEMENTS_VERSION:
        achievements += unlock_achievements_update(userID,
                                                   user_cache["version"])

    # Check if gameplay should get new achivement
    index = 1
    for handler in glob.achievementClasses.values():
        achievements += [
            x + index
            for x in handler.handle(gamemode_index, score, beatmap, user_data)
        ]
        index += handler.LENGTH

    # Add pending achievements that were added though redis or mysql
    achievements += [-x for x in user_cache["achievements"]
                     if x < 0]  # Negative achievements id's means its pending

    # Remove pending achievements from redis object since we added it to the post achievements
    user_cache["achievements"] = [
        x for x in user_cache["achievements"] if x > 0
    ]

    # Remove duplicated achievements (incase of unlock_achievements_update adding stuff)
    achievements = list(set(achievements))

    # Remove already achived achievements from list
    achievements = [
        x for x in achievements if x not in user_cache["achievements"]
    ]

    user_cache["achievements"] += achievements
    glob.redis.set("lets:user_achievement_cache:{}".format(userID),
                   json.dumps(user_cache), 1800)

    for achievement in achievements:
        userUtils.unlockAchievement(userID, achievement)

    return achievements
コード例 #2
0
def add_pending_achievement(userID, achievementID):
	user_cache = get_usercache(userID)
	if len([x for x in user_cache["achievements"] if x in [achievementID, -achievementID]]) > 0:
		print("Tried to add achievement:{} to user:{}, but failed due to duplicate entry.".format(achievementID, userID))
		return

	user_cache["achievements"].append(-achievementID)

	# Remove duplicates after merge
	user_cache["achievements"] = list(set(user_cache["achievements"]))

	glob.redis.set("lets:user_achievement_cache:{}".format(userID), json.dumps(user_cache), 1800)

	userUtils.unlockAchievement(userID, achievementID)
コード例 #3
0
ファイル: utils.py プロジェクト: Tiller431/lets-1
def unlock_achievements(score, beatmap, user_data):
    """Return array of achievements the current play recived
	
	Arguments:
		score {Score} -- Score data recived from replay
		beatmap {Beatmap} -- Played beatmap
		user_data {dict} -- Info about the current player
	
	Returns:
		Array -- List of achievements for the current play
	"""
    achievements = []

    userID = userUtils.getID(score.playerName)

    user_cache = glob.redis.get(
        "lets:user_achievement_cache:{}".format(userID))
    if user_cache is None:
        # Load from sql database
        user_cache = {}
        user_cache["version"] = userUtils.getAchievementsVersion(userID)
        user_cache["achievements"] = [
            x["achievement_id"] for x in glob.db.fetchAll(
                "SELECT achievement_id FROM users_achievements WHERE user_id=%s",
                [userID])
        ]
    else:
        user_cache = json.loads(user_cache.decode("utf-8"))

    # Get current gamemode and change value std to osu
    gamemode_index = score.gameMode

    # Check if user should run achivement recheck
    if user_cache["version"] < glob.ACHIEVEMENTS_VERSION:
        achievements += unlock_achievements_update(userID,
                                                   user_cache["version"])

    # Check if gameplay should get new achivement
    index = 1
    for handler in glob.achievementClasses.values():
        achievements += [
            x + index
            for x in handler.handle(gamemode_index, score, beatmap, user_data)
        ]
        index += handler.LENGTH

    # Remove duplicated achievements (incase of unlock_achievements_update adding stuff)
    achievements = list(set(achievements))

    # Remove already achived achievements from list
    achievements = [
        x for x in achievements if x not in user_cache["achievements"]
    ]

    user_cache["achievements"] += achievements
    glob.redis.set("lets:user_achievement_cache:{}".format(userID),
                   json.dumps(user_cache), 1800)

    for achievement in achievements:
        userUtils.unlockAchievement(userID, achievement)

    return achievements