class Meta: queryset = total_ratings(Rating.objects.all()) allowed_methods = ['get', 'post'] resource_name = 'rating' authentication = APIKeyAuthentication() fields = [ 'id', 'rating', 'rating_vp', 'rating_vt', 'rating_vz', 'dev', 'dev_vp', 'dev_vt', 'dev_vz', 'decay', ] filtering = { 'id': ALL, 'period': ALL_WITH_RELATIONS, 'player': ALL_WITH_RELATIONS, 'prev': ALL_WITH_RELATIONS, 'decay': ALL, 'domination': ALL, 'rating': ALL, 'rating_vp': ALL, 'rating_vt': ALL, 'rating_vz': ALL, 'dev': ALL, 'dev_vp': ALL, 'dev_vt': ALL, 'dev_vz': ALL, 'bf_rating': ALL, 'bf_rating_vp': ALL, 'bf_rating_vt': ALL, 'bf_rating_vz': ALL, 'bf_dev': ALL, 'bf_dev_vp': ALL, 'bf_dev_vt': ALL, 'bf_dev_vz': ALL, 'comp_rat': ALL, 'comp_rat_vp': ALL, 'comp_rat_vt': ALL, 'comp_rat_vz': ALL, 'position': ALL, 'position_vp': ALL, 'position_vt': ALL, 'position_vz': ALL, }
def race(request): race = get_param(request, 'race', 'all') if race not in 'PTZ': race = 'all' sub = ['All','Protoss','Terran','Zerg'][['all','P','T','Z'].index(race)] base = base_ctx('Records', sub, request) def sift(lst, num=5): ret, pls = [], set() for r in lst: if not r.player_id in pls: pls.add(r.player_id) ret.append(r) if len(ret) == num: return ret return ret high = ( filter_active(total_ratings(Rating.objects.all())) .filter(period__id__gt=16).select_related('player', 'period') ) if race != 'all': high = high.filter(player__race=race) base.update({ 'hightot': sift(high.order_by('-rating')[:200]), 'highp': sift(high.order_by('-tot_vp')[:200]), 'hight': sift(high.order_by('-tot_vt')[:200]), 'highz': sift(high.order_by('-tot_vz')[:200]), 'race': race if race != 'all' else '', }) return render_to_response('records.djhtml', base)
def race(request): race = get_param(request, 'race', 'all') if race not in 'PTZ': race = 'all' sub = ['All', 'Protoss', 'Terran', 'Zerg'][['all', 'P', 'T', 'Z'].index(race)] base = base_ctx('Records', sub, request) def sift(lst, num=5): ret, pls = [], set() for r in lst: if not r.player_id in pls: pls.add(r.player_id) ret.append(r) if len(ret) == num: return ret return ret high = (filter_active(total_ratings(Rating.objects.all())).filter( period__id__gt=16).select_related('player', 'period')) if race != 'all': high = high.filter(player__race=race) base.update({ 'hightot': sift(high.order_by('-rating')[:200]), 'highp': sift(high.order_by('-tot_vp')[:200]), 'hight': sift(high.order_by('-tot_vt')[:200]), 'highz': sift(high.order_by('-tot_vz')[:200]), 'race': race if race != 'all' else '', }) return render_to_response('records.djhtml', base)
def player(request, player_id): # {{{ Get player object and base context, generate messages and make changes if needed player = get_object_or_404(Player, id=player_id) base = base_ctx('Ranking', 'Summary', request, context=player) if request.method == 'POST' and base['adm']: form = PlayerModForm(request) base['messages'] += form.update_player(player) else: form = PlayerModForm(player=player) base['messages'] += generate_messages(player) # }}} # {{{ Various easy data matches = player.get_matchset() recent = matches.filter(date__gte=(date.today() - relativedelta(months=2))) base.update({ 'player': player, 'form': form, 'first': etn(lambda: matches.earliest('date')), 'last': etn(lambda: matches.latest('date')), 'totalmatches': matches.count(), 'offlinematches': matches.filter(offline=True).count(), 'aliases': player.alias_set.all(), 'earnings': ntz(player.earnings_set.aggregate(Sum('earnings'))['earnings__sum']), 'team': player.get_current_team(), 'total': count_winloss_player(matches, player), 'vp': count_matchup_player(matches, player, P), 'vt': count_matchup_player(matches, player, T), 'vz': count_matchup_player(matches, player, Z), 'totalf': count_winloss_player(recent, player), 'vpf': count_matchup_player(recent, player, P), 'vtf': count_matchup_player(recent, player, T), 'vzf': count_matchup_player(recent, player, Z), }) if player.country is not None: base['countryfull'] = transformations.cc_to_cn(player.country) # }}} # {{{ Recent matches matches = player.get_matchset(related=['rta','rtb','pla','plb','eventobj'])[0:10] if matches.exists(): base['matches'] = display_matches(matches, fix_left=player, ratings=True) # }}} # {{{ Team memberships team_memberships = list(player.groupmembership_set.filter(group__is_team=True).select_related('group')) team_memberships.sort(key=lambda t: t.id, reverse=True) team_memberships.sort(key=meandate, reverse=True) team_memberships.sort(key=lambda t: t.current, reverse=True) base['teammems'] = team_memberships # }}} # {{{ If the player has at least one rating if player.current_rating: ratings = total_ratings(player.rating_set.filter(period__computed=True)).select_related('period') base.update({ 'highs': ( ratings.latest('rating'), ratings.latest('tot_vp'), ratings.latest('tot_vt'), ratings.latest('tot_vz'), ), 'recentchange': player.get_latest_rating_update(), 'firstrating': ratings.earliest('period'), 'rating': player.current_rating, }) if player.current_rating.decay >= INACTIVE_THRESHOLD: base['messages'].append(Message(msg_inactive % player.tag, 'Inactive', type=Message.INFO)) base['charts'] = base['recentchange'].period_id > base['firstrating'].period_id else: base['messages'].append(Message('%s has no rating yet.' % player.tag, type=Message.INFO)) base['charts'] = False # }}} # {{{ If the player has enough games to make a chart if base['charts']: ratings = ( total_ratings(player.rating_set.filter(period_id__lte=base['recentchange'].period_id)) .select_related('period__end') .prefetch_related('prev__rta', 'prev__rtb') .order_by('period') ) # {{{ Add stories and other extra information earliest = base['firstrating'] latest = base['recentchange'] # Look through team changes teampoints = [] for mem in base['teammems']: if mem.start and earliest.period.end < mem.start < latest.period.end: teampoints.append({ 'date': mem.start, 'rating': interp_rating(mem.start, ratings), 'data': [{'date': mem.start, 'team': mem.group, 'jol': 'joins'}], }) if mem.end and earliest.period.end < mem.end < latest.period.end: teampoints.append({ 'date': mem.end, 'rating': interp_rating(mem.end, ratings), 'data': [{'date': mem.end, 'team': mem.group, 'jol': 'leaves'}], }) teampoints.sort(key=lambda p: p['date']) # Condense if team changes happened within 14 days cur = 0 while cur < len(teampoints) - 1: if (teampoints[cur+1]['date'] - teampoints[cur]['date']).days <= 14: teampoints[cur]['data'].append(teampoints[cur+1]['data'][0]) del teampoints[cur+1] else: cur += 1 # Sort first by date, then by joined/left for point in teampoints: point['data'].sort(key=lambda a: a['jol'], reverse=True) point['data'].sort(key=lambda a: a['date']) # Look through stories stories = player.story_set.all().select_related('event__fullname') for s in stories: if earliest.period.start < s.date < latest.period.start: s.rating = interp_rating(s.date, ratings) else: s.skip = True # }}} base.update({ 'ratings': add_counts(ratings), 'patches': PATCHES, 'stories': stories, 'teampoints': teampoints, }) else: base['messages'].append(Message(msg_nochart % player.tag, type=Message.INFO)) # }}} base.update({"title": player.tag}) return render_to_response('player.html', base)
def period(request, period_id=None): base = base_ctx('Ranking', 'Current', request) # {{{ Get period object if not period_id: period = base['curp'] else: period = get_object_or_404(Period, id=period_id, computed=True) if period.is_preview(): base['messages'].append(Message(msg_preview % cdate(period.end, _('F jS')), type=Message.INFO)) base['period'] = period if period.id != base['curp'].id: base['curpage'] = '' # }}} # {{{ Best and most specialised players qset = total_ratings(filter_active(Rating.objects.filter(period=period))).select_related('player') qsetp = qset.filter(player__race=P) qsett = qset.filter(player__race=T) qsetz = qset.filter(player__race=Z) base.update({ 'best': qset.latest('rating'), 'bestvp': qset.latest('tot_vp'), 'bestvt': qset.latest('tot_vt'), 'bestvz': qset.latest('tot_vz'), 'bestp': qsetp.latest('rating'), 'bestpvp': qsetp.latest('tot_vp'), 'bestpvt': qsetp.latest('tot_vt'), 'bestpvz': qsetp.latest('tot_vz'), 'bestt': qsett.latest('rating'), 'besttvp': qsett.latest('tot_vp'), 'besttvt': qsett.latest('tot_vt'), 'besttvz': qsett.latest('tot_vz'), 'bestz': qsetz.latest('rating'), 'bestzvp': qsetz.latest('tot_vp'), 'bestzvt': qsetz.latest('tot_vt'), 'bestzvz': qsetz.latest('tot_vz'), 'specvp': qset.extra(select={'d': 'rating_vp/dev_vp*(rating+1.5)'}).latest('d'), 'specvt': qset.extra(select={'d': 'rating_vt/dev_vt*(rating+1.5)'}).latest('d'), 'specvz': qset.extra(select={'d': 'rating_vz/dev_vz*(rating+1.5)'}).latest('d'), 'specpvp': qsetp.extra(select={'d': 'rating_vp/dev_vp*(rating+1.5)'}).latest('d'), 'specpvt': qsetp.extra(select={'d': 'rating_vt/dev_vt*(rating+1.5)'}).latest('d'), 'specpvz': qsetp.extra(select={'d': 'rating_vz/dev_vz*(rating+1.5)'}).latest('d'), 'spectvp': qsett.extra(select={'d': 'rating_vp/dev_vp*(rating+1.5)'}).latest('d'), 'spectvt': qsett.extra(select={'d': 'rating_vt/dev_vt*(rating+1.5)'}).latest('d'), 'spectvz': qsett.extra(select={'d': 'rating_vz/dev_vz*(rating+1.5)'}).latest('d'), 'speczvp': qsetz.extra(select={'d': 'rating_vp/dev_vp*(rating+1.5)'}).latest('d'), 'speczvt': qsetz.extra(select={'d': 'rating_vt/dev_vt*(rating+1.5)'}).latest('d'), 'speczvz': qsetz.extra(select={'d': 'rating_vz/dev_vz*(rating+1.5)'}).latest('d'), }) # }}} # {{{ Highest gainer and biggest losers # TODO: Fix these queries, highly dependent on the way django does things. gainers = filter_active(Rating.objects.filter(period=period))\ .filter(prev__isnull=False)\ .select_related('prev', 'player')\ .extra(select={'diff': 'rating.rating - T3.rating'})\ .order_by('-diff') losers = filter_active(Rating.objects.filter(period=period))\ .filter(prev__isnull=False)\ .select_related('prev', 'player')\ .extra(select={'diff': 'rating.rating - T3.rating'})\ .order_by('diff') base.update({ 'gainers': gainers[:5], 'losers': losers[:5] }) # }}} # {{{ Matchup statistics qset = period.match_set base['pvt_wins'], base['pvt_loss'] = count_matchup_games(qset, 'P', 'T') base['pvz_wins'], base['pvz_loss'] = count_matchup_games(qset, 'P', 'Z') base['tvz_wins'], base['tvz_loss'] = count_matchup_games(qset, 'T', 'Z') base.update({ 'pvp_games': count_mirror_games(qset, 'P'), 'tvt_games': count_mirror_games(qset, 'T'), 'zvz_games': count_mirror_games(qset, 'Z'), }) base['tot_mirror'] = base['pvp_games'] + base['tvt_games'] + base['zvz_games'] # }}} # {{{ Build country list all_players = Player.objects.filter(rating__period_id=period.id, rating__decay__lt=INACTIVE_THRESHOLD) base['countries'] = country_list(all_players) # }}} # {{{ Initial filtering of ratings entries = filter_active(period.rating_set).select_related('player') # Race filter race = get_param(request, 'race', 'ptzrs') q = Q() for r in race: q |= Q(player__race=r.upper()) entries = entries.filter(q) # Country filter nats = get_param(request, 'nats', 'all') if nats == 'foreigners': entries = entries.exclude(player__country='KR') elif nats != 'all': entries = entries.filter(player__country=nats) # Sorting sort = get_param(request, 'sort', '') if sort not in ['vp', 'vt', 'vz']: entries = entries.order_by('-rating', 'player__tag') else: entries = entries.extra(select={'d':'rating+rating_'+sort}).order_by('-d', 'player__tag') entries = entries.prefetch_related('prev') base.update({ 'race': race, 'nats': nats, 'sort': sort, }) # }}} # {{{ Pages etc. pagesize = SHOW_PER_LIST_PAGE page = int(get_param(request, 'page', 1)) nitems = entries.count() npages = nitems//pagesize + (1 if nitems % pagesize > 0 else 0) page = min(max(page, 1), npages) entries = entries[(page-1)*pagesize : page*pagesize] if page > 0 else [] base.update({ 'page': page, 'npages': npages, 'startcount': (page-1)*pagesize, 'entries': populate_teams(entries), 'nperiods': Period.objects.filter(computed=True).count(), }) # }}} base.update({ 'sortable': True, 'localcount': True, }) fmt_date = django_date_filter(period.end, "F jS, Y") # Translators: List (number): (date) base.update({"title": _("List {num}: {date}").format(num=period.id, date=fmt_date)}) return render_to_response('period.html', base)
def team(request, team_id): # {{{ Get team object and base context, generate messages and make changes if needed team = get_object_or_404(Group, id=team_id) base = base_ctx('Ranking', None, request) if request.method == 'POST' and base['adm']: form = TeamModForm(request) base['messages'] += form.update_team(team) else: form = TeamModForm(team=team) base.update({ 'team': team, 'form': form, }) base['messages'] += generate_messages(team) # }}} # {{{ Easy statistics players = team.groupmembership_set.filter(current=True, playing=True) player_ids = players.values('player') matches = Match.objects.filter(Q(pla__in=player_ids) | Q(plb__in=player_ids)) base.update({ 'nplayers': players.count(), 'nprotoss': players.filter(player__race=P).count(), 'nterran': players.filter(player__race=T).count(), 'nzerg': players.filter(player__race=Z).count(), 'earnings': ( Earnings.objects.filter(player__in=player_ids).aggregate(Sum('earnings'))['earnings__sum'] ), 'nmatches': matches.count(), 'noffline': matches.filter(offline=True).count(), }) # }}} # {{{ Player lists all_members = total_ratings( Rating.objects.all().order_by('-rating').filter( player__groupmembership__group=team, player__groupmembership__current=True, player__groupmembership__playing=True, period=base['curp'] ) ) base.update({ 'active': filter_active(all_members), 'inactive': filter_inactive(all_members), 'nonplaying': ( team.groupmembership_set.filter(current=True, playing=False) .select_related('player').order_by('player__tag') ), 'past': ( team.groupmembership_set.filter(current=False) .annotate(null_end=Count('end')) .select_related('player').order_by('-null_end', '-end', 'player__tag') ), }) # }}} base.update({"title": team.name}) return render_to_response('team.html', base)
def player(request, player_id): # {{{ Get player object and base context, generate messages and make changes if needed player = get_object_or_404(Player, id=player_id) base = base_ctx('Ranking', 'Summary', request, context=player) if request.method == 'POST' and 'modplayer' in request.POST and base['adm']: modform = PlayerModForm(request) base['messages'] += modform.update_player(player) else: modform = PlayerModForm(player=player) base['messages'] += generate_messages(player) # }}} # {{{ Various easy data matches = player.get_matchset() recent = matches.filter(date__gte=(date.today() - relativedelta(months=2))) base.update({ 'player': player, 'modform': modform, 'first': etn(lambda: matches.earliest('date')), 'last': etn(lambda: matches.latest('date')), 'totalmatches': matches.count(), 'offlinematches': matches.filter(offline=True).count(), 'aliases': player.alias_set.all(), 'earnings': ntz(player.earnings_set.aggregate(Sum('earnings'))['earnings__sum']), 'team': player.get_current_team(), 'total': count_winloss_player(matches, player), 'vp': count_matchup_player(matches, player, P), 'vt': count_matchup_player(matches, player, T), 'vz': count_matchup_player(matches, player, Z), 'totalf': count_winloss_player(recent, player), 'vpf': count_matchup_player(recent, player, P), 'vtf': count_matchup_player(recent, player, T), 'vzf': count_matchup_player(recent, player, Z), }) base['riv_nem_vic'] = zip_longest(player.rivals, player.nemesis, player.victim) if player.country is not None: base['countryfull'] = transformations.cc_to_cn(player.country) # }}} # {{{ Recent matches matches = player.get_matchset( related=['rta', 'rtb', 'pla', 'plb', 'eventobj'])[0:10] if matches.exists(): base['matches'] = display_matches(matches, fix_left=player, ratings=True) # }}} # {{{ Team memberships team_memberships = list( player.groupmembership_set.filter( group__is_team=True).select_related('group')) team_memberships.sort(key=lambda t: t.id, reverse=True) team_memberships.sort(key=meandate, reverse=True) team_memberships.sort(key=lambda t: t.current, reverse=True) base['teammems'] = team_memberships # }}} # {{{ If the player has at least one rating if player.current_rating: ratings = total_ratings(player.rating_set.filter( period__computed=True)).select_related('period') base.update({ 'highs': ( ratings.latest('rating'), ratings.latest('tot_vp'), ratings.latest('tot_vt'), ratings.latest('tot_vz'), ), 'recentchange': player.get_latest_rating_update(), 'firstrating': ratings.earliest('period'), 'rating': player.current_rating, }) if player.current_rating.decay >= INACTIVE_THRESHOLD: base['messages'].append( Message(msg_inactive % player.tag, 'Inactive', type=Message.INFO)) base['charts'] = base['recentchange'].period_id > base[ 'firstrating'].period_id else: base['messages'].append( Message(_('%s has no rating yet.') % player.tag, type=Message.INFO)) base['charts'] = False # }}} # {{{ If the player has enough games to make a chart if base['charts']: ratings = (total_ratings( player.rating_set.filter( period_id__lte=base['recentchange'].period_id)).select_related( 'period').prefetch_related('prev__rta', 'prev__rtb').order_by('period')) # {{{ Add stories and other extra information earliest = base['firstrating'] latest = base['recentchange'] # Look through team changes teampoints = [] for mem in base['teammems']: if mem.start and earliest.period.end < mem.start < latest.period.end: teampoints.append({ 'date': mem.start, 'rating': interp_rating(mem.start, ratings), 'data': [{ 'date': mem.start, 'team': mem.group, 'jol': _('joins') }], }) if mem.end and earliest.period.end < mem.end < latest.period.end: teampoints.append({ 'date': mem.end, 'rating': interp_rating(mem.end, ratings), 'data': [{ 'date': mem.end, 'team': mem.group, 'jol': _('leaves') }], }) teampoints.sort(key=lambda p: p['date']) # Condense if team changes happened within 14 days cur = 0 while cur < len(teampoints) - 1: if (teampoints[cur + 1]['date'] - teampoints[cur]['date']).days <= 14: teampoints[cur]['data'].append(teampoints[cur + 1]['data'][0]) del teampoints[cur + 1] else: cur += 1 # Sort first by date, then by joined/left for point in teampoints: point['data'].sort(key=lambda a: a['jol'], reverse=True) point['data'].sort(key=lambda a: a['date']) # Look through stories stories = player.story_set.all().select_related('event') for s in stories: if earliest.period.start < s.date < latest.period.start: s.rating = interp_rating(s.date, ratings) else: s.skip = True # }}} base.update({ 'ratings': add_counts(ratings), 'patches': PATCHES, 'stories': stories, 'teampoints': teampoints, }) else: base['messages'].append( Message(msg_nochart % player.tag, type=Message.INFO)) # }}} return render_to_response('player.djhtml', base)
def player(request, player_id): # {{{ Get player object and base context, generate messages and make changes if needed player = get_object_or_404(Player, id=player_id) base = base_ctx("Ranking", "Summary", request, context=player) if request.method == "POST" and "modplayer" in request.POST and base["adm"]: modform = PlayerModForm(request) base["messages"] += modform.update_player(player) else: modform = PlayerModForm(player=player) base["messages"] += generate_messages(player) # }}} # {{{ Various easy data matches = player.get_matchset() recent = matches.filter(date__gte=(date.today() - relativedelta(months=2))) base.update( { "player": player, "modform": modform, "first": etn(lambda: matches.earliest("date")), "last": etn(lambda: matches.latest("date")), "totalmatches": matches.count(), "offlinematches": matches.filter(offline=True).count(), "aliases": player.alias_set.all(), "earnings": ntz(player.earnings_set.aggregate(Sum("earnings"))["earnings__sum"]), "team": player.get_current_team(), "total": count_winloss_player(matches, player), "vp": count_matchup_player(matches, player, P), "vt": count_matchup_player(matches, player, T), "vz": count_matchup_player(matches, player, Z), "totalf": count_winloss_player(recent, player), "vpf": count_matchup_player(recent, player, P), "vtf": count_matchup_player(recent, player, T), "vzf": count_matchup_player(recent, player, Z), } ) riv = player.rivals or [] nem = player.nemesis or [] vic = player.victim or [] base["riv_nem_vic"] = zip_longest(riv, nem, vic) if player.country is not None: base["countryfull"] = transformations.cc_to_cn(player.country) # }}} # {{{ Recent matches matches = player.get_matchset(related=["rta", "rtb", "pla", "plb", "eventobj"])[0:10] if matches.exists(): base["matches"] = display_matches(matches, fix_left=player, ratings=True) # }}} # {{{ Team memberships team_memberships = list(player.groupmembership_set.filter(group__is_team=True).select_related("group")) team_memberships.sort(key=lambda t: t.id, reverse=True) team_memberships.sort(key=meandate, reverse=True) team_memberships.sort(key=lambda t: t.current, reverse=True) base["teammems"] = team_memberships # }}} # {{{ If the player has at least one rating if player.current_rating: ratings = total_ratings(player.rating_set.filter(period__computed=True)).select_related("period") base.update( { "highs": ( ratings.latest("rating"), ratings.latest("tot_vp"), ratings.latest("tot_vt"), ratings.latest("tot_vz"), ), "recentchange": player.get_latest_rating_update(), "firstrating": ratings.earliest("period"), "rating": player.current_rating, } ) if player.current_rating.decay >= INACTIVE_THRESHOLD: base["messages"].append(Message(msg_inactive % player.tag, "Inactive", type=Message.INFO)) base["charts"] = base["recentchange"].period_id > base["firstrating"].period_id else: base["messages"].append(Message(_("%s has no rating yet.") % player.tag, type=Message.INFO)) base["charts"] = False # }}} # {{{ If the player has enough games to make a chart if base["charts"]: ratings = ( total_ratings(player.rating_set.filter(period_id__lte=base["recentchange"].period_id)) .select_related("period__end") .prefetch_related("prev__rta", "prev__rtb") .order_by("period") ) # {{{ Add stories and other extra information earliest = base["firstrating"] latest = base["recentchange"] # Look through team changes teampoints = [] for mem in base["teammems"]: if mem.start and earliest.period.end < mem.start < latest.period.end: teampoints.append( { "date": mem.start, "rating": interp_rating(mem.start, ratings), "data": [{"date": mem.start, "team": mem.group, "jol": _("joins")}], } ) if mem.end and earliest.period.end < mem.end < latest.period.end: teampoints.append( { "date": mem.end, "rating": interp_rating(mem.end, ratings), "data": [{"date": mem.end, "team": mem.group, "jol": _("leaves")}], } ) teampoints.sort(key=lambda p: p["date"]) # Condense if team changes happened within 14 days cur = 0 while cur < len(teampoints) - 1: if (teampoints[cur + 1]["date"] - teampoints[cur]["date"]).days <= 14: teampoints[cur]["data"].append(teampoints[cur + 1]["data"][0]) del teampoints[cur + 1] else: cur += 1 # Sort first by date, then by joined/left for point in teampoints: point["data"].sort(key=lambda a: a["jol"], reverse=True) point["data"].sort(key=lambda a: a["date"]) # Look through stories stories = player.story_set.all().select_related("event__fullname") for s in stories: if earliest.period.start < s.date < latest.period.start: s.rating = interp_rating(s.date, ratings) else: s.skip = True # }}} base.update({"ratings": add_counts(ratings), "patches": PATCHES, "stories": stories, "teampoints": teampoints}) else: base["messages"].append(Message(msg_nochart % player.tag, type=Message.INFO)) # }}} return render_to_response("player.djhtml", base)
def team(request, team_id): # {{{ Get team object and base context, generate messages and make changes if needed team = get_object_or_404(Group, id=team_id) base = base_ctx('Teams', None, request) if request.method == 'POST' and base['adm']: form = TeamModForm(request) base['messages'] += form.update_team(team) else: form = TeamModForm(team=team) base.update({ 'team': team, 'form': form, }) base['messages'] += generate_messages(team) # }}} # {{{ Easy statistics players = team.groupmembership_set.filter(current=True, playing=True) player_ids = players.values('player') matches = Match.objects.filter( Q(pla__in=player_ids) | Q(plb__in=player_ids)) base.update({ 'nplayers': players.count(), 'nprotoss': players.filter(player__race=P).count(), 'nterran': players.filter(player__race=T).count(), 'nzerg': players.filter(player__race=Z).count(), 'earnings': (Earnings.objects.filter(player__in=player_ids).aggregate( Sum('earnings'))['earnings__sum']), 'nmatches': matches.count(), 'noffline': matches.filter(offline=True).count(), }) # }}} # {{{ Player lists all_members = total_ratings( Rating.objects.all().order_by('-rating').filter( player__groupmembership__group=team, player__groupmembership__current=True, player__groupmembership__playing=True, period=base['curp']).select_related('player').prefetch_related( 'prev')) base.update({ 'active': filter_active(all_members), 'inactive': filter_inactive(all_members), 'nonplaying': (team.groupmembership_set.filter( current=True, playing=False).select_related('player').order_by('player__tag')), 'past': (team.groupmembership_set.filter(current=False).annotate( null_end=Count('end')).select_related('player').order_by( '-null_end', '-end', 'player__tag')), }) # }}} return render_to_response('team.djhtml', base)
def period(request, period_id=None): base = base_ctx('Ranking', 'Current', request) # {{{ Get period object if not period_id: period = base['curp'] else: period = get_object_or_404(Period, id=period_id, computed=True) if period.is_preview(): base['messages'].append( Message(msg_preview % django_date_filter(period.end, 'F jS'), type=Message.INFO)) base['period'] = period if period.id != base['curp'].id: base['curpage'] = '' # }}} # {{{ Best and most specialised players qset = total_ratings(filter_active( Rating.objects.filter(period=period))).select_related('player') qsetp = qset.filter(player__race=P) qsett = qset.filter(player__race=T) qsetz = qset.filter(player__race=Z) base.update({ 'best': qset.latest('rating'), 'bestvp': qset.latest('tot_vp'), 'bestvt': qset.latest('tot_vt'), 'bestvz': qset.latest('tot_vz'), 'bestp': qsetp.latest('rating'), 'bestpvp': qsetp.latest('tot_vp'), 'bestpvt': qsetp.latest('tot_vt'), 'bestpvz': qsetp.latest('tot_vz'), 'bestt': qsett.latest('rating'), 'besttvp': qsett.latest('tot_vp'), 'besttvt': qsett.latest('tot_vt'), 'besttvz': qsett.latest('tot_vz'), 'bestz': qsetz.latest('rating'), 'bestzvp': qsetz.latest('tot_vp'), 'bestzvt': qsetz.latest('tot_vt'), 'bestzvz': qsetz.latest('tot_vz'), 'specvp': qset.extra(select={ 'd': 'rating_vp/dev_vp*(rating+1.5)' }).latest('d'), 'specvt': qset.extra(select={ 'd': 'rating_vt/dev_vt*(rating+1.5)' }).latest('d'), 'specvz': qset.extra(select={ 'd': 'rating_vz/dev_vz*(rating+1.5)' }).latest('d'), 'specpvp': qsetp.extra(select={ 'd': 'rating_vp/dev_vp*(rating+1.5)' }).latest('d'), 'specpvt': qsetp.extra(select={ 'd': 'rating_vt/dev_vt*(rating+1.5)' }).latest('d'), 'specpvz': qsetp.extra(select={ 'd': 'rating_vz/dev_vz*(rating+1.5)' }).latest('d'), 'spectvp': qsett.extra(select={ 'd': 'rating_vp/dev_vp*(rating+1.5)' }).latest('d'), 'spectvt': qsett.extra(select={ 'd': 'rating_vt/dev_vt*(rating+1.5)' }).latest('d'), 'spectvz': qsett.extra(select={ 'd': 'rating_vz/dev_vz*(rating+1.5)' }).latest('d'), 'speczvp': qsetz.extra(select={ 'd': 'rating_vp/dev_vp*(rating+1.5)' }).latest('d'), 'speczvt': qsetz.extra(select={ 'd': 'rating_vt/dev_vt*(rating+1.5)' }).latest('d'), 'speczvz': qsetz.extra(select={ 'd': 'rating_vz/dev_vz*(rating+1.5)' }).latest('d'), }) # }}} # {{{ Highest gainer and biggest losers # TODO: Fix these queries, highly dependent on the way django does things. gainers = filter_active(Rating.objects.filter(period=period))\ .filter(prev__isnull=False)\ .select_related('prev', 'player')\ .extra(select={'diff': 'rating.rating - T3.rating'})\ .order_by('-diff') losers = filter_active(Rating.objects.filter(period=period))\ .filter(prev__isnull=False)\ .select_related('prev', 'player')\ .extra(select={'diff': 'rating.rating - T3.rating'})\ .order_by('diff') base.update({'updown': zip(gainers[:5], losers[:5])}) # }}} # {{{ Matchup statistics qset = period.match_set base['pvt_wins'], base['pvt_loss'] = count_matchup_games(qset, 'P', 'T') base['pvz_wins'], base['pvz_loss'] = count_matchup_games(qset, 'P', 'Z') base['tvz_wins'], base['tvz_loss'] = count_matchup_games(qset, 'T', 'Z') base.update({ 'pvp_games': count_mirror_games(qset, 'P'), 'tvt_games': count_mirror_games(qset, 'T'), 'zvz_games': count_mirror_games(qset, 'Z'), }) base['tot_mirror'] = base['pvp_games'] + base['tvt_games'] + base[ 'zvz_games'] # }}} # {{{ Build country list all_players = Player.objects.filter(rating__period_id=period.id, rating__decay__lt=INACTIVE_THRESHOLD) base['countries'] = country_list(all_players) # }}} # {{{ Initial filtering of ratings entries = filter_active(period.rating_set).select_related('player') # Race filter race = get_param(request, 'race', 'ptzrs') q = Q() for r in race: q |= Q(player__race=r.upper()) entries = entries.filter(q) # Country filter nats = get_param(request, 'nats', 'all') if nats == 'foreigners': entries = entries.exclude(player__country='KR') elif nats != 'all': entries = entries.filter(player__country=nats) # Sorting sort = get_param(request, 'sort', '') if sort not in ['vp', 'vt', 'vz']: entries = entries.order_by('-rating', 'player__tag') else: entries = entries.extra(select={ 'd': 'rating+rating_' + sort }).order_by('-d', 'player__tag') entries = entries.prefetch_related('prev') base.update({ 'race': race, 'nats': nats, 'sort': sort, }) # }}} # {{{ Pages etc. pagesize = SHOW_PER_LIST_PAGE page = int(get_param(request, 'page', 1)) nitems = entries.count() npages = nitems // pagesize + (1 if nitems % pagesize > 0 else 0) page = min(max(page, 1), npages) entries = entries[(page - 1) * pagesize:page * pagesize] if page > 0 else [] pn_start, pn_end = page - 2, page + 2 if pn_start < 1: pn_end += 1 - pn_start pn_start = 1 if pn_end > npages: pn_start -= pn_end - npages pn_end = npages if pn_start < 1: pn_start = 1 base.update({ 'page': page, 'npages': npages, 'startcount': (page - 1) * pagesize, 'entries': populate_teams(entries), 'nperiods': Period.objects.filter(computed=True).count(), 'pn_range': range(pn_start, pn_end + 1), }) # }}} base.update({ 'sortable': True, 'localcount': True, }) fmt_date = django_date_filter(period.end, "F jS, Y") return render_to_response('period.djhtml', base)
def period(request, period_id): base = base_ctx('Ranking', 'Current', request) # {{{ Get period object period = get_object_or_404(Period, id=period_id, computed=True) if period.is_preview(): base['messages'].append(Message(msg_preview % period.end.strftime('%B %d'), type=Message.INFO)) base['period'] = period if period.id != base['curp'].id: base['curpage'] = '' # }}} # {{{ Best and most specialised players qset = total_ratings(filter_active(Rating.objects.filter(period=period))).select_related('player') qsetp = qset.filter(player__race=P) qsett = qset.filter(player__race=T) qsetz = qset.filter(player__race=Z) base.update({ 'best': qset.latest('rating'), 'bestvp': qset.latest('tot_vp'), 'bestvt': qset.latest('tot_vt'), 'bestvz': qset.latest('tot_vz'), 'bestp': qsetp.latest('rating'), 'bestpvp': qsetp.latest('tot_vp'), 'bestpvt': qsetp.latest('tot_vt'), 'bestpvz': qsetp.latest('tot_vz'), 'bestt': qsett.latest('rating'), 'besttvp': qsett.latest('tot_vp'), 'besttvt': qsett.latest('tot_vt'), 'besttvz': qsett.latest('tot_vz'), 'bestz': qsetz.latest('rating'), 'bestzvp': qsetz.latest('tot_vp'), 'bestzvt': qsetz.latest('tot_vt'), 'bestzvz': qsetz.latest('tot_vz'), 'specvp': qset.extra(select={'d': 'rating_vp/dev_vp*rating'}).latest('d'), 'specvt': qset.extra(select={'d': 'rating_vt/dev_vt*rating'}).latest('d'), 'specvz': qset.extra(select={'d': 'rating_vz/dev_vz*rating'}).latest('d'), 'specpvp': qsetp.extra(select={'d': 'rating_vp/dev_vp*rating'}).latest('d'), 'specpvt': qsetp.extra(select={'d': 'rating_vt/dev_vt*rating'}).latest('d'), 'specpvz': qsetp.extra(select={'d': 'rating_vz/dev_vz*rating'}).latest('d'), 'spectvp': qsett.extra(select={'d': 'rating_vp/dev_vp*rating'}).latest('d'), 'spectvt': qsett.extra(select={'d': 'rating_vt/dev_vt*rating'}).latest('d'), 'spectvz': qsett.extra(select={'d': 'rating_vz/dev_vz*rating'}).latest('d'), 'speczvp': qsetz.extra(select={'d': 'rating_vp/dev_vp*rating'}).latest('d'), 'speczvt': qsetz.extra(select={'d': 'rating_vt/dev_vt*rating'}).latest('d'), 'speczvz': qsetz.extra(select={'d': 'rating_vz/dev_vz*rating'}).latest('d'), }) # }}} # {{{ Matchup statistics qset = period.match_set base['pvt_wins'], base['pvt_loss'] = count_matchup_games(qset, 'P', 'T') base['pvz_wins'], base['pvz_loss'] = count_matchup_games(qset, 'P', 'Z') base['tvz_wins'], base['tvz_loss'] = count_matchup_games(qset, 'T', 'Z') base.update({ 'pvp_games': count_mirror_games(qset, 'P'), 'tvt_games': count_mirror_games(qset, 'T'), 'zvz_games': count_mirror_games(qset, 'Z'), }) base['tot_mirror'] = base['pvp_games'] + base['tvt_games'] + base['zvz_games'] # }}} # {{{ Build country list all_players = Player.objects.filter(rating__period_id=period.id, rating__decay__lt=INACTIVE_THRESHOLD) base['countries'] = country_list(all_players) # }}} # {{{ Initial filtering of ratings entries = filter_active(period.rating_set).select_related('player') # Race filter race = get_param(request, 'race', 'ptzrs') q = Q() for r in race: q |= Q(player__race=r.upper()) entries = entries.filter(q) # Country filter nats = get_param(request, 'nats', 'all') if nats == 'foreigners': entries = entries.exclude(player__country='KR') elif nats != 'all': entries = entries.filter(player__country=nats) # Sorting sort = get_param(request, 'sort', '') if sort not in ['vp', 'vt', 'vz']: entries = entries.order_by('-rating', 'player__tag') else: entries = entries.extra(select={'d':'rating+rating_'+sort}).order_by('-d', 'player__tag') base.update({ 'race': race, 'nats': nats, 'sort': sort, }) # }}} # {{{ Pages etc. pagesize = 40 page = int(get_param(request, 'page', 1)) nitems = entries.count() npages = nitems//pagesize + (1 if nitems % pagesize > 0 else 0) page = min(max(page, 1), npages) entries = entries[(page-1)*pagesize : page*pagesize] base.update({ 'page': page, 'npages': npages, 'startcount': (page-1)*pagesize, 'entries': populate_teams(entries), 'nperiods': Period.objects.filter(computed=True).count(), }) # }}} base.update({ 'sortable': True, 'localcount': True, }) fmt_date = django_date_filter(period.end, "F jS, Y") base.update({"title": "List {}: {}".format(period.id, fmt_date)}) return render_to_response('period.html', base)