示例#1
0
def get_achievement_level(request):
    """get all information about an achievement for a specific level""" 
    try:
        achievement_id = int(request.matchdict.get("achievement_id",None))
        level = int(request.matchdict.get("level",None))
    except:
        raise BadRequest("invalid input")
         
    def generate():
        achievement = Achievement.get_achievement(achievement_id)
         
        if not achievement:
            raise NotFound("achievement not found")
         
        level_output = Achievement.basic_output(achievement, [], True, level).get("levels").get(str(level), {"properties":{},"rewards":{}})
        if level_output.has_key("goals"):
            del level_output["goals"]
        if level_output.has_key("level"):
            del level_output["level"]
        return render("json",level_output)
     
    key = "/achievement/"+str(achievement_id)+"/level/"+str(level)
    request.response.content_type = 'application/json'
         
    return get_or_set(key,generate)
示例#2
0
def get_achievement_level(request):
    """get all information about an achievement for a specific level"""
    try:
        achievement_id = int(request.matchdict.get("achievement_id", None))
        level = int(request.matchdict.get("level", None))
    except:
        raise BadRequest("invalid input")

    def generate():
        achievement = Achievement.get_achievement(achievement_id)

        if not achievement:
            raise NotFound("achievement not found")

        level_output = Achievement.basic_output(achievement, [], True,
                                                level).get("levels").get(
                                                    str(level), {
                                                        "properties": {},
                                                        "rewards": {}
                                                    })
        if level_output.has_key("goals"):
            del level_output["goals"]
        if level_output.has_key("level"):
            del level_output["level"]
        return render("json", level_output)

    key = "/achievement/" + str(achievement_id) + "/level/" + str(level)
    request.response.content_type = 'application/json'

    return get_or_set(key, generate)
示例#3
0
def get_progress(request, return_object=False):
    """get all relevant data concerning the user's progress"""
    user_id = long(request.matchdict["user_id"])

    user = User.get_user(user_id)
    if not user:
        raise NotFound("user not found")

    def generate():
        achievements = Achievement.get_achievements_by_user_for_today(user)

        def ea(achievement):
            try:
                #print "evaluating "+`achievement["id"]`
                return Achievement.evaluate(user, achievement["id"])
            except FormularEvaluationException as e:
                return {
                    "error": "Cannot evaluate formular: " + e.message,
                    "id": achievement["id"]
                }
            except Exception as e:
                tb = traceback.format_exc()
                return {"error": tb, "id": achievement["id"]}

        check = lambda x: x != None and not x.has_key("error") and (x[
            "hidden"] == False or x["level"] > 0)

        evaluatelist = [ea(achievement) for achievement in achievements]

        ret = {
            "achievements": {x["id"]: x
                             for x in evaluatelist if check(x)},
            "achievement_errors": {
                x["id"]: x
                for x in evaluatelist if x != None and x.has_key("error")
            }
        }

        if return_object:
            return render("json", ret), ret
        else:
            return render("json", ret)

    key = "/progress/" + str(user_id)

    if not return_object:
        request.response.content_type = 'application/json'
        return get_or_set(key, generate)
    else:
        ret_str, ret = generate()
        set_value(key, ret_str)
        return ret
def get_progress(request, return_object=False):
    """get all relevant data concerning the user's progress"""
    user_id = long(request.matchdict["user_id"])
    
    user = User.get_user(user_id)
    if not user:
        raise NotFound("user not found")
    
        
    def generate():
        achievements = Achievement.get_achievements_by_user_for_today(user)
        
        def ea(achievement):
            try:
                #print "evaluating "+`achievement["id"]`
                return Achievement.evaluate(user, achievement["id"])
            except FormularEvaluationException as e:
                return { "error": "Cannot evaluate formular: " + e.message, "id" : achievement["id"] }
            except Exception as e:
                tb = traceback.format_exc()
                return { "error": tb, "id" : achievement["id"] }
            
        check = lambda x : x!=None and not x.has_key("error") and (x["hidden"]==False or x["level"]>0)
        
        evaluatelist = [ea(achievement) for achievement in achievements]
        
        ret = {
            "achievements" : {
                x["id"] : x for x in evaluatelist if check(x) 
            },
            "achievement_errors" : {
                x["id"] : x for x in evaluatelist if x!=None and x.has_key("error") 
            }
        }
        
        if return_object:
            return render("json",ret),ret
        else:
            return render("json",ret)
    
    key = "/progress/"+str(user_id)
    
    if not return_object:
        request.response.content_type = 'application/json'
        return get_or_set(key,generate)
    else:
        ret_str, ret = generate()
        set_value(key,ret_str)
        return ret
示例#5
0
def _get_progress(user,force_generation=False):
    def generate():
        achievements = Achievement.get_achievements_by_user_for_today(user)
        
        def ea(achievement):
            try:
                #print "evaluating "+`achievement["id"]`
                return Achievement.evaluate(user, achievement["id"])
            except FormularEvaluationException as e:
                return { "error": "Cannot evaluate formular: " + e.message, "id" : achievement["id"] }
            except Exception as e:
                tb = traceback.format_exc()
                return { "error": tb, "id" : achievement["id"] }
            
        check = lambda x : x!=None and not x.has_key("error") and (x["hidden"]==False or x["level"]>0)
        
        evaluatelist = [ea(achievement) for achievement in achievements]
        
        ret = {
            "achievements" : {
                x["id"] : x for x in evaluatelist if check(x) 
            },
            "achievement_errors" : {
                x["id"] : x for x in evaluatelist if x!=None and x.has_key("error") 
            }
        }
        
        return render("json",ret),ret
        
    key = "/progress/"+str(user.id)
    
    if not force_generation:
        #in this case, we do not return the decoded json object - the caller has to take of this if needed
        return get_or_set(key,lambda:generate()[0]), None
    else:
        ret_str, ret = generate()
        set_value(key,ret_str)
        return ret_str, ret