def _update_ratings(): calc = RatingCalculator() START_RATING = cur_config().rating_start players = {} match_ids = list(set(PlayerResult.objects.filter(rating=None).values_list('match_id', flat=True))) for match in Match.objects.filter(id__in=match_ids).order_by('date', 'id'): player_positions = match.get_positions() rating_results = [] for p in player_positions: # Fetch the current rating value #if not players.get(p['id'], False): rated_results = PlayerResult.objects.filter(player=p['id']).exclude(rating=None).order_by('-match__date', '-match__id') if not rated_results.exists(): rating = START_RATING else: rating = rated_results[0].rating #else: # rating = players[p['id']] rating_results.append(RatingResult(p['id'], rating, p['position'])) # Calculate new ratings new_player_ratings = calc.new_ratings(rating_results) # Update for p in new_player_ratings: players[p.dbid] = p.rating PlayerResult.objects.filter(player=p.dbid).filter(match=match).update(rating=p.rating)
def _update_ratings(): calc = RatingCalculator() START_RATING = cur_config().rating_start players = {} match_ids = list( set( PlayerResult.objects.filter(rating=None).values_list('match_id', flat=True))) for match in Match.objects.filter(id__in=match_ids).order_by('date', 'id'): player_positions = match.get_positions() rating_results = [] for p in player_positions: # Fetch the current rating value rated_results = PlayerResult.objects.filter( player=p['id']).exclude(rating=None).order_by( '-match__date', '-match__id') if not rated_results.exists(): rating = START_RATING else: rating = rated_results[0].rating rating_results.append(RatingResult(p['id'], rating, p['position'])) # Calculate new ratings new_player_ratings = calc.new_ratings(rating_results) # Update for p in new_player_ratings: players[p.dbid] = p.rating PlayerResult.objects.filter(player=p.dbid).filter( match=match).update(rating=p.rating)
def rating_description(request): conf = cur_config() data = { 'K_VALUE': int(conf.rating_k), 'START_RATING': int(conf.rating_start) } return render(request, 'rating-description.html', data)
def players(request): places_strings = request.GET.getlist('places[]', None) # Validate that all places exists place_ids = [] for place in places_strings: place_ids.append(get_object_or_404(Place, name=place).id) if not place_ids: place_ids = Place.objects.values_list('id', flat=True) place_ids = sorted(place_ids) # Create stats _update_ratings() match_winners_cache = {} players = [] for player in Player.objects.all(): player_results = PlayerResult.objects.filter(player=player) player_result_ids = player_results.values_list('match_id', flat=True) matches = Match.objects.filter(id__in=player_result_ids, place_id__in=place_ids) # Played - Win Ratio won = 0 for match in matches: winners = match_winners_cache.get(match.id, False) if not winners: # Not in cache winners = match.get_winners() match_winners_cache[match.id] = winners if player in winners: won += 1 played_count = matches.count() if played_count == 0: win_percent = 0 else: win_percent = int(round(won * 100.00 / played_count)) # Get last rating if player_results.exists(): rating = int(player_results.order_by('-match__date', '-match__id')[0].rating) else: rating = "-" players.append({'id': player.id, 'name': player.name, 'played': played_count, 'won': won, 'win_perc': win_percent, 'rating': rating}) places = [] for place in Place.objects.all(): p = {'name': place.name} p['selected'] = "selected" if (place.id in place_ids) else "" places.append(p) data = {'players': players, 'places': places, 'config': {'active_treshold': cur_config().active_player_match_treshold}} return render(request, 'players.html', data)
def rating(request): _update_ratings() if PlayerResult.objects.count() == 0: return render_to_response('rating.html', {}, context_instance=RequestContext(request)) max_rating = PlayerResult.objects.aggregate(Max('rating'))['rating__max'] max_obj = PlayerResult.objects.select_related('player').filter( rating=max_rating).order_by('match__date', 'match__id')[0] min_rating = PlayerResult.objects.aggregate(Min('rating'))['rating__min'] min_obj = PlayerResult.objects.select_related('player').filter( rating=min_rating).order_by('match__date', 'match__id')[0] # Only list active players active_players = [] players = Player.objects.all() active_player_match_treshold = cur_config().active_player_match_treshold for p in players: if PlayerResult.objects.filter( player_id=p.id).count() >= active_player_match_treshold: active_players.append(p) # Create data context and return response data = { 'max': { 'pid': max_obj.player_id, 'pname': max_obj.player.name, 'mid': max_obj.match_id, 'rating': max_obj.rating }, 'min': { 'pid': min_obj.player_id, 'pname': min_obj.player.name, 'mid': min_obj.match_id, 'rating': min_obj.rating }, 'players': [p.get_ratings() for p in active_players], 'player_names': [p.name for p in active_players], } return render(request, 'rating.html', data)
def rating(request): _update_ratings() if PlayerResult.objects.count() == 0: return render_to_response('rating.html', {}, context_instance=RequestContext(request)) max_rating = PlayerResult.objects.aggregate(Max('rating'))['rating__max'] max_obj = PlayerResult.objects.select_related('player').filter(rating=max_rating).order_by('match__date', 'match__id')[0] min_rating = PlayerResult.objects.aggregate(Min('rating'))['rating__min'] min_obj = PlayerResult.objects.select_related('player').filter(rating=min_rating).order_by('match__date', 'match__id')[0] # Only list active players active_players = [] players = Player.objects.all() active_player_match_treshold = cur_config().active_player_match_treshold for p in players: if PlayerResult.objects.filter(player_id=p.id).count() >= active_player_match_treshold: active_players.append(p) # Create data context and return response data = {'max': {'pid': max_obj.player_id, 'pname': max_obj.player.name, 'mid': max_obj.match_id, 'rating': max_obj.rating}, 'min': {'pid': min_obj.player_id, 'pname': min_obj.player.name, 'mid': min_obj.match_id, 'rating': min_obj.rating}, 'players': [p.get_ratings() for p in active_players], 'player_names': [p.name for p in active_players], } return render(request, 'rating.html', data)
def rating_description(request): conf = cur_config() data = {'K_VALUE': int(conf.rating_k), 'START_RATING': int(conf.rating_start)} return render(request, 'rating-description.html', data)
def rating_description(request): conf = cur_config() data = {'K_VALUE': int(conf.rating_k), 'START_RATING': int(conf.rating_start)} return render_to_response('rating-description.html', data, context_instance=RequestContext(request))
def __init__(self): self.K = cur_config().rating_k
def players(request): places_strings = request.GET.getlist('places[]', None) # Validate that all places exists place_ids = [] for place in places_strings: place_ids.append(get_object_or_404(Place, name=place).id) if not place_ids: place_ids = Place.objects.values_list('id', flat=True) place_ids = sorted(place_ids) # Create stats _update_ratings() match_winners_cache = {} players = [] for player in Player.objects.all(): player_results = PlayerResult.objects.filter(player=player) player_result_ids = player_results.values_list('match_id', flat=True) matches = Match.objects.filter(id__in=player_result_ids, place_id__in=place_ids) # Played - Win Ratio won = 0 for match in matches: winners = match_winners_cache.get(match.id, False) if not winners: # Not in cache winners = match.get_winners() match_winners_cache[match.id] = winners if player in winners: won += 1 played_count = matches.count() if played_count == 0: win_percent = 0 else: win_percent = int(round(won * 100.00 / played_count)) # Get last rating if player_results.exists(): rating = int( player_results.order_by('-match__date', '-match__id')[0].rating) else: rating = "-" players.append({ 'id': player.id, 'name': player.name, 'played': played_count, 'won': won, 'win_perc': win_percent, 'rating': rating }) places = [] for place in Place.objects.all(): p = {'name': place.name} p['selected'] = "selected" if (place.id in place_ids) else "" places.append(p) data = { 'players': players, 'places': places, 'config': { 'active_treshold': cur_config().active_player_match_treshold } } return render(request, 'players.html', data)