def get_user_rankings(self, request): """Get the rankings of each player""" items = [] users = User.query() for user in users: wins = 0 guesses = 0 #Use .fetch() to turn query into an iterable list you can take the len() of scores = Score.query(Score.user == user.key).fetch() if scores: for score in scores: guesses += score.guesses if score.won: wins += 1 winning_percentage = 100 * wins / float(len(scores)) items.append(user.to_form(wins, guesses, winning_percentage)) #Lambda defines nameless inline function #items.sort(key=lambda u: u.winning_percentage, reverse = True) #http://stackoverflow.com/questions/12749398/using-a-comparator-function-to-sort\ def _compare_user_games(a, b): """Sort user rankings by winning_percentage, then # wins, then # guesses""" if a.winning_percentage != b.winning_percentage: return int(a.winning_percentage - b.winning_percentage) elif a.wins != b.wins: return int(a.wins - b.wins) else: return b.guesses - a.guesses items = sorted(items, cmp=_compare_user_games, reverse=True) return UserForms(items=items)
def get_user_rankings(self, request): """Returns the user rankings as determined by each user's winning percent. Ties in winning_percent are broken by secondarily sorting by num of user wins. """ users = User.query().order(-User.winning_percent, -User.wins) if not users: raise endpoints.NotFoundException('There are no users!') return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): """Return list of users ranked in descending order of total score""" rankings = [] users = User.query().fetch() for user in users: user_score = Score.query( Score.user == user.key).order(-Score.points).fetch() total_score = sum([score.points for score in user_score]) rankings.append((user, total_score)) rankings.sort(key=lambda tup: tup[1], reverse=True) return UserForms( items=[result[0].to_form(result[1]) for result in rankings])
def get_user_rankings(self, request): """Get user rankings""" users = User.query() for user in users: games = Score.query(Score.user == user.key) win = Score.query().filter(Score.result == 'Win').fetch() draw = Score.query().filter(Score.result == 'Draw').fetch() lose = Score.query().filter(Score.result == "Lose").fetch() # The final score is base on both performance and participation. final_score = 5*len(win) + 3*len(win)+ 1*len(lose) user.rankingscore = final_score user.put() return UserForms(items=[user.to_form() for user in User.query().order(-User.rankingscore)])
def get_user_rankings(self, request): """Returns rankings of all Users""" users = User.query() if not users: raise endpoints.NotFoundException('No User exists!') for user in users: scores = Score.query(Score.user == user.key).fetch() p = 0 if scores: count = len(scores) for score in scores: p += score.score p = p / count user.performance = p user.put() users = User.query().order(-User.performance) return UserForms(users=[user.to_form() for user in users])
def get_user_rankings(self, request): """Returns user rankings""" users = User.query() for user in users: total_rankings = 0 ranking = 0 games = Game.query(ancestor=user.key) for count, game in enumerate(games): # calculate ranking correct_guesses = (len(game.correct_guesses) - game.correct_guesses.count('_')) total_guesses = correct_guesses + len(game.incorrect_guesses) score = Score.query(ancestor=game.key).get() if score.points > 0 and total_guesses > 0: ranking = score.points / total_guesses + score.points total_rankings += ranking if total_rankings > 0: user.ranking = total_rankings / count user.put() users = User.query().order(-User.ranking) return UserForms(users=[user.to_form() for user in users])
def get_users(self, request): """Return all users, sorted by username.""" return UserForms(users=[ user.to_form() for user in User.query().order(User.username) ])
def get_user_rankings(self, request): """Return all user wins and win percentages""" users = User.query(User.total_games > 0).fetch() users = sorted(users, key=lambda x: x.win_percentage, reverse=True) return UserForms(items=[user.to_form() for user in users])
def get_high_scores(self, request): """Return all Users ranked by their win""" users = User.query(User.total_played > 0).fetch() users = sorted(users, key=lambda x: x.wins, reverse=True) users = users[0:request.rank_depth] return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): '''Returns all players ranked by performance''' users = User.query() users = sorted(users, key=lambda x: x.wins, reverse=True) return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): '''Rank users by scores''' qry1 = User.query(User.score >= 0) users = qry1.order(-User.score) return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): """Return all scores""" return UserForms(items=[user.to_form() for user in User.query().order(-User.rate)])
def get_user_rankings(self, request): """Return user rankings according to user's performance in descending order.""" users = User.query().order(-User.performance).order(User.wins) return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): """Return all Users ranked by points""" users = User.query().fetch() users = sorted(users, key=lambda x: x.score, reverse=True) return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): """Return all scores""" users = User.query().order(-User.points) return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): users = User.query().order(-User.performance) return UserForms(items=[user.to_form() for user in users])
def get_high_scores(self, request): """Generate a list of high scores of won games in descending order""" users = User.query().fetch(limit=request.number_of_results) users = sorted(users, key=lambda x: x.wins, reverse=True) return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): """Returns all users sorted by ranking""" users = User.query().order(-User.total_score) return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): """ Order users by who has the most wins """ users = User.query(User.wins > 0).order(User.wins) return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): """Return all wins, losses, and games total played.""" # Players with the most wins will be ranked first. users = User.query().order(-User.game_wins).fetch() return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): """Return all Users ranked by their win percentage""" users = User.query(User.total_played > 0).fetch() users = sorted(users, key=lambda x: x.win_percentage, reverse=True) return UserForms(items=[user.to_form() for user in users])
def get_user_rankings(self, request): """Returns users in order by wins""" users = User.query().order(-User.wins) return UserForms(items=[user.to_form() for user in users])