Esempio n. 1
0
def home(request):
    rating = Rating.objects.get(type=Rating.RR)
    today, rating_date = get_latest_rating_date(rating)
    rating_results = (RatingResult.objects.filter(rating=rating).filter(
        date=rating_date).prefetch_related("player").prefetch_related(
            "player__city").order_by("place"))[:16]

    upcoming_tournaments = (Tournament.public.filter(is_upcoming=True).filter(
        is_event=False).exclude(tournament_type=Tournament.FOREIGN_EMA).
                            prefetch_related("city").order_by("start_date"))

    events = (Tournament.public.filter(is_upcoming=True).filter(
        is_event=True).prefetch_related("city").order_by("start_date"))

    return render(
        request,
        "website/home.html",
        {
            "page": "home",
            "rating_results": rating_results,
            "rating": rating,
            "upcoming_tournaments": upcoming_tournaments,
            "events": events,
            "rating_date": rating_date,
            "today": today,
            "is_last": True,
        },
    )
Esempio n. 2
0
def player_rating_changes(request,
                          slug,
                          rating_slug,
                          year=None,
                          month=None,
                          day=None):
    player = get_object_or_404(Player, slug=slug)
    rating = get_object_or_404(Rating, slug=rating_slug)

    today, rating_date, is_last = parse_rating_date(year, month, day)
    if not rating_date:
        today, rating_date = get_latest_rating_date(rating)

    rating_result = get_object_or_404(RatingResult,
                                      rating=rating,
                                      player=player,
                                      date=rating_date)
    filtered_results = reversed(_get_rating_changes(rating, player, today))

    return render(
        request,
        "player/rating_changes.html",
        {
            "player": player,
            "rating": rating,
            "results": filtered_results,
            "rating_date": rating_date,
            "today": today,
            "rating_result": rating_result,
        },
    )
Esempio n. 3
0
def best_countries(request):
    rating = Rating.objects.get(type=Rating.EMA)
    today, rating_date = get_latest_rating_date(rating)
    ema_ratings = RatingResult.objects.filter(
        rating=rating,
        date=rating_date).prefetch_related("player", "rating",
                                           "player__country")
    countries = _get_countries_data(ema_ratings)
    return render(request, "ema/best_countries.html", {"countries": countries})
Esempio n. 4
0
def player_rating_details(request,
                          slug,
                          rating_slug,
                          year=None,
                          month=None,
                          day=None):
    player = get_object_or_404(Player, slug=slug)
    rating = get_object_or_404(Rating, slug=rating_slug)

    today, rating_date, is_last = parse_rating_date(year, month, day)
    if not rating_date:
        today, rating_date = get_latest_rating_date(rating)

    rating_result = get_object_or_404(RatingResult,
                                      rating=rating,
                                      player=player,
                                      date=rating_date)

    rating_deltas = (RatingDelta.objects.filter(
        player=player, rating=rating,
        date=rating_date).prefetch_related("player").prefetch_related(
            "tournament").order_by("-tournament__end_date"))

    top_tournaments_number = 3 if rating.is_online() else 4
    top_tournament_ids = tuple(d.tournament_id for d in sorted(
        rating_deltas,
        reverse=True,
        key=lambda d: float(d.base_rank) * float(d.coefficient_obj.age) *
        float(d.coefficient_value),
    ))[:top_tournaments_number]

    last_rating_place = RatingResult.objects.filter(
        rating=rating, date=rating_date).order_by("place").last().place
    filtered_results = _get_rating_changes(rating, player, today)

    return render(
        request,
        "player/rating_details.html",
        {
            "player": player,
            "rating": rating,
            "rating_deltas": rating_deltas,
            "rating_result": rating_result,
            "filtered_results": filtered_results,
            "last_rating_place": last_rating_place,
            "top_tournament_ids": top_tournament_ids,
            "rating_date": rating_date,
            "today": today,
            "is_last": is_last,
        },
    )
Esempio n. 5
0
def player_details(request, slug, year=None, month=None, day=None):
    player = get_object_or_404(Player, slug=slug)

    _, entered_date, _ = parse_rating_date(year, month, day)

    ratings = Rating.objects.all().order_by("order")
    rating_results = []
    for rating in ratings:
        if not entered_date:
            _, rating_date = get_latest_rating_date(rating)
        else:
            rating_date = entered_date

        result = RatingResult.objects.filter(date=rating_date,
                                             rating=rating,
                                             player=player).first()
        if result:
            rating_results.append(result)

    tournament_results = (TournamentResult.objects.filter(
        player=player).prefetch_related("tournament").order_by(
            "-tournament__end_date"))[:10]

    tenhou_data = TenhouNickname.objects.filter(player=player, is_main=True)
    ms_data = MSAccount.objects.filter(player=player).first()
    club_ratings = (ClubRating.objects.filter(player=player).prefetch_related(
        "club", "club__city").order_by("-games_count"))

    return render(
        request,
        "player/details.html",
        {
            "player": player,
            "rating_results": rating_results,
            "tournament_results": tournament_results,
            "tenhou_data": tenhou_data,
            "rating_date": entered_date,
            "ms_data": ms_data,
            "club_ratings": club_ratings,
        },
    )
Esempio n. 6
0
def rating_details(request,
                   slug,
                   year=None,
                   month=None,
                   day=None,
                   country_code=None):
    rating = get_object_or_404(Rating, slug=slug)

    today, rating_date, is_last = parse_rating_date(year, month, day)
    if not rating_date:
        today, rating_date = get_latest_rating_date(rating)

    is_ema = rating.type == Rating.EMA
    rating_results = (RatingResult.objects.filter(
        rating=rating).prefetch_related("player").prefetch_related(
            "player__city").prefetch_related("player__country").order_by(
                "place"))

    closest_date = RatingResult.objects.filter(
        rating=rating, date__lte=rating_date).order_by("date")
    if closest_date.exists():
        rating_date = closest_date.last().date
    else:
        raise Http404

    rating_results = rating_results.filter(date=rating_date)

    if rating.is_online():
        rating_results = rating_results.prefetch_related("player__tenhou")

    countries_data = {}
    if rating.type == Rating.EMA:
        countries_data = {}
        for rating_result in rating_results:
            country = rating_result.player.country
            if country.code not in countries_data:
                countries_data[country.code] = {
                    "players": 0,
                    "country": country
                }

            countries_data[country.code]["players"] += 1

        countries_data = sorted(countries_data.values(),
                                key=lambda x: x["players"],
                                reverse=True)

        if country_code:
            try:
                country = Country.objects.get(code=country_code)
                rating_results = rating_results.filter(player__country=country)
            except Country.DoesNotExist:
                pass

    render_as_json = request.GET.get("json")
    if render_as_json is not None:
        data = []
        for rating_result in rating_results:
            data.append({
                "id": rating_result.player.id,
                "place": rating_result.place,
                "scores": float(rating_result.score),
                "name": rating_result.player.full_name,
                "city": rating_result.player.city.name,
            })
        return JsonResponse(data, safe=False)

    return render(
        request,
        "rating/details.html",
        {
            "rating": rating,
            "rating_results": rating_results,
            "rating_date": rating_date,
            "is_last": is_last,
            "page": "rating",
            "countries_data": countries_data,
            "closest_date": closest_date,
            "country_code": country_code,
            "today": today,
            "is_ema": is_ema,
            "show_tournaments_numbers": True,
        },
    )
Esempio n. 7
0
def rating_tournaments(request, slug):
    rating = get_object_or_404(Rating, slug=slug)
    today, rating_date = get_latest_rating_date(rating)
    tournament_ids = (RatingDelta.objects.filter(date=rating_date).filter(
        rating=rating).filter(is_active=True).values_list("tournament_id",
                                                          flat=True))
    tournaments = (Tournament.public.filter(
        id__in=tournament_ids).prefetch_related("city").prefetch_related(
            "country").order_by("-end_date"))

    coefficients = (TournamentCoefficients.objects.filter(
        tournament_id__in=tournament_ids,
        rating=rating,
        date__lte=timezone.now().date()).order_by(
            "tournament_id", "-date").distinct("tournament_id"))

    if rating.type == Rating.EMA:
        coefficients_dict = {}
        top_tournament_ids = []
    else:
        stages_tournament_ids = HARDCODED_COEFFICIENTS.keys()

        coefficients_dict = {}
        for coefficient in coefficients:
            coefficients_dict[coefficient.tournament_id] = {
                "value": (float(coefficient.age) / 100.0) *
                float(coefficient.coefficient),
                "age":
                coefficient.age,
                "coefficient":
                coefficient.coefficient,
                "tournament_id":
                coefficient.tournament_id,
            }

            if coefficient.tournament_id in stages_tournament_ids:
                stage_coefficients = list(
                    set(HARDCODED_COEFFICIENTS[
                        coefficient.tournament_id].values()))
                for x in stage_coefficients:
                    value = (float(coefficient.age) / 100.0) * float(x)
                    coefficients_dict[coefficient.tournament_id] = {
                        "coefficient": x,
                        "age": coefficient.age,
                        "value": value,
                        "tournament_id": coefficient.tournament_id,
                    }

        top_tournaments_number = {
            Rating.RR: RatingRRCalculation.SECOND_PART_MIN_TOURNAMENTS,
            Rating.CRR: RatingCRRCalculation.SECOND_PART_MIN_TOURNAMENTS,
            Rating.ONLINE: RatingOnlineCalculation.SECOND_PART_MIN_TOURNAMENTS,
        }.get(rating.type)

        top_coefficients = sorted(coefficients_dict.values(),
                                  key=lambda t: t["value"],
                                  reverse=True)[:top_tournaments_number]

        top_tournament_ids = [c["tournament_id"] for c in top_coefficients]

    return render(
        request,
        "rating/rating_tournaments.html",
        {
            "rating": rating,
            "tournaments": tournaments,
            "page": "rating",
            "coefficients": coefficients_dict,
            "top_tournament_ids": top_tournament_ids,
        },
    )