def get_high_scores(self, request): """Return all scores ordered by points; an optional parameter (number_of_results) limits the number of results returned""" num = request.number_of_results if num: scores = Score.query().order(-Score.points).fetch(num) else: scores = Score.query().order(-Score.points).fetch() return ScoreForms(items=[score.to_form() for score in scores])
def get_user_rankings(self, request): """Returns a list of users ranked by win percentage""" users = User.query() if not users: raise endpoints.NotFoundException('No users found') user_ratings = [] for user in users: user_name = user.name scores = Score.query(Score.user == user.key) wins = scores.filter(Score.won == True).fetch() loses = scores.filter(Score.won == False).fetch() if not (wins or loses): continue win_percentage = (float(len(wins)) / (len(wins) + len(loses))) * 100 avg_moves = 0 for score in scores: avg_moves += score.moves avg_moves = avg_moves / (len(wins) + len(loses)) user_ratings.append([user_name, win_percentage, avg_moves]) user_ratings = sorted(user_ratings, key=lambda x: (-x[1], x[2])) messages = [] for rating in user_ratings: message = 'Name: ' + rating[0] + ', Win rating: ' message += '{0:.2f}%, Average Moves: '.format(rating[1]) message += str(rating[2]) messages.append(message) return StringMessages(items=[m for m in messages])
def get_high_scores(self, request): """Generate a list of high scores in descending order, like a leader-board!. Args: request: The GET_HIGH_SCORES_REQUEST object, which includes optional number_of_results, the default being 10. Returns: ScoreForms: Multiple ScoreForm container that contains all the scores' information, sorted by the minimum number of attempts used. In case of a tie, the game with higher number of maximum attempts is ranked higher. Raises: endpoints.NotFoundException: If no scores are found for any games. """ number_of_results = 10 if request.number_of_results is not None: number_of_results = int(request.number_of_results) # Fetch all the scores in descending order from the datastore. # In case of tie in attempts used by the user, the game with more attempts allowed wins. scores = Score.query(Score.won == True) \ .order(Score.attempts_used, -Score.attempts) \ .fetch(limit=number_of_results) if not scores: raise endpoints.NotFoundException( 'No scores found for any users!' ) return ScoreForms(items=[score.to_form() for score in scores])
def get_user_scores(request): user = User.query(User.name == request.user_name).get() if not user: raise endpoints.NotFoundException( 'A User with that name does not exist!') scores = Score.query(Score.user == user.key) return scores
def get_scores(self, request): """Return all scores. Args: request: None. Returns: ScoreForms: Multiple ScoreForm container. """ return ScoreForms(items=[score.to_form() for score in Score.query()])
def get_user_scores(self, request): '''Returns all of an individual User's scores''' user = User.query(User.name == request.user_name).get() if not user: raise endpoints.NotFoundException( 'A User with that name does not exist!') scores = Score.query(Score.user == user.key).order(-Score.harvest) return ScoreForms(items=[score.to_form() for score in scores])
def get_user_scores(self, request): """Returns all of an individual User's scores""" user = User.query(User.name == request.user_name).get() if not user: raise endpoints.NotFoundException( 'A User with that name does not exist!') scores = Score.query(Score.user == user.key) return ScoreForms(items=[score.to_form() for score in scores])
def get_high_scores(self, request): '''Returns a leaderboard, in score-descending order''' scores = Score.query().order(-Score.harvest) if request.number_of_results: max_results = int(request.number_of_results) # Omit negative values if max_results > 0: # Force an upper bound of 1000 results max_results = min(1000, max_results) scores = scores.fetch(max_results) return ScoreForms(items=[score.to_form() for score in scores])
def _cache_average_attempts(): """ Populates memcache with the average moves remaining of Games """ scores = Score.query(Score.game_status != 'started').fetch() if scores: scount = len(scores) total_attempts_remaining = sum([(6-score.move_count) for score in scores]) average = float(total_attempts_remaining)/scount memcache.set('MOVES_REMAINING', 'The average moves remaining is {:.2f}'. format(average)) else: memcache.set('MOVES_REMAINING', 'No games recorded')
def get(self): game_id = int(self.request.get('game_id')) load_game = self.request.get('load_game') jplayers = self.request.get('jplayers') logging.info("jplayers id is |%s|", jplayers) players = json.loads(jplayers) if jplayers != 'None' else [] scores = [] if load_game == "true": scores = Score.query(Score.game_id == game_id).fetch() else: for player in players: score = Score(player_name=player, game_id=game_id, score=int(self.request.get(player))) score.put() scores.append(score) final = True for score in scores: if score.score == None: final = False if final: game = get_game(int(game_id)) owings = calculate_owings(scores, game) payments = calculate_payments_from_owings(owings, game) else: owings = None payments = None context = { 'game_id': game_id, 'scores': scores, 'players': players, 'payments': payments } template = JINJA_ENVIRONMENT.get_template('templates/payments.html') self.response.write(template.render(context))
def get_user_scores(self, request): """Returns all of the User's scores. Args: request: The GET_USER_REQUEST objects, which contains a users name. Returns: ScoreForms: Multiple ScoreForm container. Raises: endpoints.NotFoundException: If no user found. """ user = User.query(User.name == request.user_name).get() # Raise an exception if a user with the user_name does not exist. if not user: raise endpoints.NotFoundException( 'A user with that name does not exist!') # Fetch all the scores for the user from the datastore. scores = Score.query(Score.user == user.key) return ScoreForms(items=[score.to_form() for score in scores])
def get_high_scores(self, request): """Generate a list of high scores in descending order, like a leader-board!. Args: request: The GET_HIGH_SCORES_REQUEST object, which includes optional number_of_results, the default being 10. Returns: ScoreForms: Multiple ScoreForm container that contains all the scores' information, sorted by the minimum number of attempts used. In case of a tie, the game with higher number of maximum attempts is ranked higher. Raises: endpoints.NotFoundException: If no scores are found for any games. """ number_of_results = 10 if request.number_of_results is not None: number_of_results = int(request.number_of_results) # Fetch all the scores in descending order from the datastore. # In case of tie in attempts used by the user, the game with more attempts allowed wins. scores = Score.query(Score.won == True) \ .order(Score.attempts_used, -Score.attempts) \ .fetch(limit=number_of_results) if not scores: raise endpoints.NotFoundException('No scores found for any users!') return ScoreForms(items=[score.to_form() for score in scores])
def get_scores(self, request): """Return all scores ordered by time_completed""" return ScoreForms(items=[ score.to_form() for score in Score.query().order(-Score.time_completed) ])
def get_high_scores(self, request): """Return a specified number of the leader board""" # Generates a high score list with descending order scores = Score.query().order(-Score.score)\ .fetch(request.number_of_results) return ScoreForms(items=[score.to_form() for score in scores])
def get_scores(self, request): """Return all scores""" return ScoreForms(items=[score.to_form() for score in Score.query()])
def get_top_scores(self, request): """Returns the top 10 scores""" scores = Score.query(Score.won == True) scores = scores.order(Score.moves).fetch(10) return ScoreForms(items=[score.to_form() for score in scores])