Пример #1
0
def eval_page_unlock(user, page):
    """Returns True if the given page is unlocked based upon evaluation of its dependencies."""
    predicates = page.unlock_condition
    if not predicates:
        return False

    return utils.eval_predicates(predicates, user)
def eval_page_unlock(user, page):
    """Returns True if the given page is unlocked based upon evaluation of its dependencies."""
    predicates = page.unlock_condition
    if not predicates:
        return False

    return utils.eval_predicates(predicates, user)
Пример #3
0
def award_possible_badges(profile):
    """Award any possible badges to a user.
    """
    user_badges = []
    for awarded in profile.badgeaward_set.all().select_related():
        user_badges.append(awarded.badge)

    for badge in Badge.objects.all():
        if not badge in user_badges and utils.eval_predicates(badge.award_condition, profile.user):
            award_badge(profile=profile, badge=badge)
Пример #4
0
def eval_unlock(user, action):
    """Determine the unlock status of a task by dependency expression"""
    predicates = action.unlock_condition
    if not predicates:
        return False

    # after published is the default unlock rule for action
    # if not afterPublished(user, action.slug):
    #    return False

    return utils.eval_predicates(predicates, user)
Пример #5
0
def eval_unlock(user, action):
    """Determine the unlock status of a task by dependency expression"""
    predicates = action.unlock_condition
    if not predicates:
        return False

    # after published is the default unlock rule for action
    # if not afterPublished(user, action.slug):
    #    return False

    return utils.eval_predicates(predicates, user)
Пример #6
0
def get_level_actions(user):
    """Return the level list with the action info in categories"""
    levels = cache_mgr.get_cache("smartgrid-levels-%s" % user.username)

    if levels is None:
        completed_actions = get_completed_actions(user)
        levels = []
        for level in Level.objects.all():
            level.is_unlock = utils.eval_predicates(level.unlock_condition, user)
            if level.is_unlock:
                level.is_complete = True
                categories = []
                action_list = None
                category = None
                for action in level.action_set.all().select_related("category"):
                    if action.slug in completed_actions:
                        action.member = completed_actions[action.slug]
                        action.is_unlock = True
                        action.completed = True
                    else:
                        action.is_unlock = is_unlock(user, action)
                        action.completed = False

                    # if there is one action is not completed, set the level to in-completed
                    if not action.completed:
                        level.is_complete = False

                    # the action are ordered by level and category
                    if category != action.category:
                        if category:
                            # a new category
                            category.task_list = action_list
                            categories.append(category)

                        action_list = []
                        category = action.category

                    action_list.append(action)

                if category:
                    # last category
                    category.task_list = action_list
                    categories.append(category)

                level.cat_list = categories
            levels.append(level)

        # Cache the categories for 30 minutes (or until they are invalidated)
        cache_mgr.set_cache("smartgrid-levels-%s" % user, levels, 1800)

    return levels
Пример #7
0
def award_possible_badges(profile, trigger):
    """Award any possible badges to a user.
    """
    if not challenge_mgr.is_game_enabled("Badge Game Mechanics"):
        return

    user_badges = []
    for awarded in profile.badgeaward_set.all().select_related("badge"):
        user_badges.append(awarded.badge)

    for badge in Badge.objects.filter(award_trigger=trigger):
        if not badge in user_badges and utils.eval_predicates(badge.award_condition, profile.user):
            award_badge(profile=profile, badge=badge)
            print "Awarded %s badge to %s." % (badge, profile)
def get_levels(user):
    """Returns the list of annotated levels for the given user."""
    levels = []
    submitted_actions = get_submitted_actions(user)
    for level in Level.objects.all():
        level.is_unlock = utils.eval_predicates(level.unlock_condition, user)
        level.is_complete = True
        for row in Grid.objects.filter(level=level):
            action = row.action
            if action.slug not in submitted_actions:
                level.is_complete = False
                break
        levels.append(level)
    return levels
Пример #9
0
def get_level_actions(user):
    """Return the level list with the action info in categories"""
    levels = cache_mgr.get_cache('smartgrid-levels-%s' % user.username)

    if levels is None:
        completed_actions = get_completed_actions(user)
        levels = []
        for level in Level.objects.all():
            level.is_unlock = utils.eval_predicates(level.unlock_condition, user)
            if level.is_unlock:
                if level.unlock_condition != "True":
                    contents = "%s is unlocked." % level
                    obj, created = UserNotification.objects.\
                        get_or_create(recipient=user,
                                      contents=contents,
                                      level=UserNotification.LEVEL_CHOICES[2][0])
                    if created:  # only show the notification if it is new
                        obj.display_alert = True
                        obj.save()
                level.is_complete = True
                categories = []
                action_list = None
                category = None
                for action in level.action_set.all().select_related("category"):
                    if action.slug in completed_actions:
                        action.member = completed_actions[action.slug]
                        action.is_unlock = True
                        action.completed = True
                    else:
                        action.is_unlock = is_unlock(user, action)
                        action.completed = False

                    action.availablity = availablity(action)
                    # if there is one action is not completed, set the level to in-completed
                    if not action.completed:
                        level.is_complete = False

                    # the action are ordered by level and category
                    if category != action.category:
                        if category:
                            # a new category
                            category.task_list = action_list
                            categories.append(category)

                        action_list = []
                        category = action.category

                    action_list.append(action)

                if category:
                    # last category
                    category.task_list = action_list
                    categories.append(category)

                level.cat_list = categories
            levels.append(level)

        # Cache the categories for 30 minutes (or until they are invalidated)
        cache_mgr.set_cache('smartgrid-levels-%s' % user, levels, 1800)

    return levels
Пример #10
0
 def completed_quest(self, user):
     """Returns True if the user completed the quest."""
     return utils.eval_predicates(self.completion_conditions, user)
Пример #11
0
 def can_add_quest(self, user):
     """Returns True if the user can add the quest."""
     return utils.eval_predicates(self.unlock_conditions, user)
Пример #12
0
 def completed_quest(self, user):
     """Returns True if the user completed the quest."""
     return utils.eval_predicates(self.completion_conditions, user)
Пример #13
0
 def can_add_quest(self, user):
     """Returns True if the user can add the quest."""
     return utils.eval_predicates(self.unlock_conditions, user)
Пример #14
0
def is_level_unlock(user, level):
    """return True if the level is unlock."""
    return level and utils.eval_predicates(level.unlock_condition, user)
Пример #15
0
def get_level_actions(user):
    """Return the level list with the action info in categories"""
    levels = cache_mgr.get_cache('smartgrid-levels-%s' % user.username)

    if levels is None:
        completed_actions = get_completed_actions(user)
        levels = []
        for level in Level.objects.all():
            level.is_unlock = utils.eval_predicates(level.unlock_condition,
                                                    user)
            if level.is_unlock:
                if level.unlock_condition != "True":
                    contents = "%s is unlocked." % level
                    obj, created = UserNotification.objects.\
                        get_or_create(recipient=user,
                                      contents=contents,
                                      level=UserNotification.LEVEL_CHOICES[2][0])
                    if created:  # only show the notification if it is new
                        obj.display_alert = True
                        obj.save()
                level.is_complete = True
                categories = []
                action_list = None
                category = None
                for action in level.action_set.all().select_related(
                        "category"):
                    if action.slug in completed_actions:
                        action.member = completed_actions[action.slug]
                        action.is_unlock = True
                        action.completed = True
                    else:
                        action.is_unlock = is_unlock(user, action)
                        action.completed = False

                    action.availablity = availablity(action)
                    # if there is one action is not completed, set the level to in-completed
                    if not action.completed:
                        level.is_complete = False

                    # the action are ordered by level and category
                    if category != action.category:
                        if category:
                            # a new category
                            category.task_list = action_list
                            categories.append(category)

                        action_list = []
                        category = action.category

                    action_list.append(action)

                if category:
                    # last category
                    category.task_list = action_list
                    categories.append(category)

                level.cat_list = categories
            levels.append(level)

        # Cache the categories for 30 minutes (or until they are invalidated)
        cache_mgr.set_cache('smartgrid-levels-%s' % user, levels, 1800)

    return levels
Пример #16
0
def is_level_unlock(user, level):
    """return True if the level is unlock."""
    return level and utils.eval_predicates(level.unlock_condition, user)
Пример #17
0
def get_level_actions(user):  # pylint: disable=R0914,R0912,R0915
    """Returns the smart grid as defined in the Smart Grid Designer. The
    grid is a list of lists with the format [<Level>, [<ColumnGrid>*],
    [<Grid>*], [active columns], max_column, max_row]"""
    levels = cache_mgr.get_cache('smartgrid-levels-%s' % user.username)
    if levels is None:
        submitted_actions = get_submitted_actions(user)
        levels = []
        for level in Level.objects.all():
            level.is_unlock = utils.eval_predicates(level.unlock_condition, user)
            if level.is_unlock:  # only include unlocked levels
                if level.unlock_condition != "True":
                    contents = "%s is unlocked." % level
                    obj, created = UserNotification.objects.\
                        get_or_create(recipient=user,
                                      contents=contents,
                                      level=UserNotification.LEVEL_CHOICES[2][0])
                    if created:  # only show the notification if it is new
                        obj.display_alert = True
                        obj.save()
                level_ret = []
                level.is_complete = True
                level_ret.append(level)
                level_ret.append(ColumnGrid.objects.filter(level=level))
#                level_ret.append(Grid.objects.filter(level=level))

                max_column = len(ColumnGrid.objects.filter(level=level))
                max_row = 0
                just_actions = []
                # update each action
                for row in Grid.objects.filter(level=level):
                    action = Action.objects.get(slug=row.action.slug)
                    action.row = row.row
                    if row.row > max_row:
                        max_row = row.row
                    action.column = row.column
                    if row.column > max_column:
                        max_column = row.column
                    if action.slug in submitted_actions:
                        action.member = submitted_actions[action.slug]
                        action.is_unlock = True
                        action.completed = True
                    else:
                        action.is_unlock = is_unlock(user, action)
                        action.completed = False

                    action.availablity = availablity(action)
                    # if there is one action is not completed, set the level to in-completed
                    if not action.completed:
                        level.is_complete = False
                    just_actions.append(action)
                level_ret.append(just_actions)
                columns = []
                for cat in level_ret[1]:
                    if cat.column not in columns:
                        columns.append(cat.column)
                for act in level_ret[2]:
                    if act.column not in columns:
                        columns.append(act.column)
                level_ret.append(columns)
                level_ret.append(max_column)
                level_ret.append(max_row)
                levels.append(level_ret)
            else:
                level_ret = []
                level_ret.append(level)
                level_ret.append([])
                level_ret.append([])
                level_ret.append([])
                level_ret.append(0)
                level_ret.append(0)
                levels.append(level_ret)

        # Cache the levels for 30 minutes (or until they are invalidated)
        cache_mgr.set_cache('smartgrid-levels-%s' % user, levels, 1800)
    return levels  # pylint: enable=R0914,R0912,R0915