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 predicate_mgr.eval_predicates(predicates, user)
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 predicate_mgr.eval_predicates(predicates, user)
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 = predicate_mgr.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
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 predicate_mgr.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 = predicate_mgr.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
def testAllPredicateStrings(self): """Tests all the predicate strings using the tester predicates.""" for pred in self.predicates: print pred predicate_mgr.eval_predicates(pred, self.user)
def is_level_unlock(user, level): """return True if the level is unlock.""" return level and predicate_mgr.eval_predicates(level.unlock_condition, user)
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 = predicate_mgr.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
def completed_quest(self, user): """Returns True if the user completed the quest.""" return predicate_mgr.eval_predicates(self.completion_conditions, user)
def can_add_quest(self, user): """Returns True if the user can add the quest.""" return predicate_mgr.eval_predicates(self.unlock_conditions, user)
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 = predicate_mgr.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