def get_scores_for_user(user, stats=[]): valid_stats = [stat["name"] for stat in stats_settings.get_leaderboard_stats()] if not stats: # if no stats are provided just get all stats = valid_stats elif not set(stats).issubset(valid_stats): # you requested a stat that is not valid raise Exception("There is an invalid stat in request.") #TODOL client error try: high_score_table = boto3.resource('dynamodb').Table(CloudCanvas.get_setting("MainTable")) except ValueError as e: raise Exception("Error getting table reference") table_key = { "user" : user } table_response = high_score_table.get_item(Key=table_key) all_player_scores = table_response.get("Item", {}) if not all_player_scores: return [] score_list = [] for stat in stats: score = leaderboard_utils.generate_score_dict(all_player_scores, stat, user) if score: score_list.append(score) return score_list
def post(request): print("{} load test initialization entered.".format(c.GEM_NAME)) valid_stats = [] stats_response = stats_settings.get_leaderboard_stats() existing_stats = set([stat['name'] for stat in stats_response]) build_stats(existing_stats, valid_stats) print("{} load test initialization has finished.".format(c.GEM_NAME)) return {'stats': valid_stats}
def validate_request_data(score_data, cognito_id): # make sure we have all required fields missing_fields = [] for field in REQUIRED_FIELDS: if field not in score_data: missing_fields.append(field) if missing_fields: print( "Request is missing the following required fields in ScoreData ({})" .format(missing_fields.join(", "))) raise errors.ClientError("Request is missing required fields") if not stats_settings.is_stat_valid(score_data["stat"]): print("Client provided stat ({}) is not valid".format( score_data["stat"])) if len(stats_settings.get_leaderboard_stats()) == 0: raise errors.ClientError( "There are no stats on this leaderboard. Go to the Cloud Gem Portal to add a leaderboard" ) raise errors.ClientError("Invalid stat") if not leaderboard_utils.is_score_valid(score_data["value"], score_data["stat"]): print("Provided score ({}) is invalid for stat ({})".format( score_data["value"], score_data["stat"])) raise errors.ClientError("Invalid score") if cognito_id and not identity_validator.validate_user( score_data["user"], cognito_id): print("The user {} provided incorrect cognito ID {}".format( score_data["user"], cognito_id)) raise errors.ClientError( "This client is not authorized to submit scores for user {}". format(score_data["user"])) if cognito_id and not leaderboard_utils.is_user_valid(score_data["user"]): print("The user ({}) is invalid for stat ({})".format( score_data["user"], score_data["stat"])) raise errors.ClientError("Invalid user")
def delete(request, stat_name=None): if stat_name: stats_settings.remove_stat(stat_name) return {"stats": stats_settings.get_leaderboard_stats()}
def post(request, stat_def=None): if stat_def: stats_settings.add_stat(stat_def) return {"stats": stats_settings.get_leaderboard_stats()}
def get(request): return {"stats": stats_settings.get_leaderboard_stats()}