예제 #1
0
def index(request) : 

	team_list={}
	
	games = Game.objects.filter(gameID__gte = 4000)
	
	# --- team ranking
	teams = Team.objects.filter(current__gte = 1)

	team_list=[]
	team_map = {}
	for team in teams:
		t = TeamStat()
		t.name 		= team.name
		t.teamID 	= team.teamID
		t.current 	= team.current
		team_map[team.teamID] = t

	for game in games:
		awayID = game.away.teamID
		homeID = game.home.teamID
		
		team_map[awayID].game_played += 1
		team_map[homeID].game_played += 1

		result = game.get_result()
		if( not result ): # tie
			team_map[awayID].tie += 1
			team_map[homeID].tie += 1
		else:
			team_map[result[0].teamID].win  += 1
			team_map[result[1].teamID].lose += 1

	for team in team_map.values():
		team.stat()
		team_list.append(team)
		# if(team.current == 1):
		# 	league_list[0].team_list.append(team)
		# else: # current == 2
		# 	league_list[1].team_list.append(team)

	
	team_list = sorted(team_list, key=attrgetter('percent'), reverse=True)

	top = team_list[0]
	for team in team_list:
		team.GB = ( (top.win - team.win) + (team.lose -  top.lose) ) / 2.0

	team_list = sorted(team_list , key=attrgetter('percent'), reverse=True)
	team_list = sorted(team_list , key=attrgetter('GB'))
	team_list[0].GB = '-'
	


	players = Member.objects.all()
	# --- batting ranking
	thr = 1
	batting_list = calculate_batting_rank(players)
	batting_list = filter(lambda list : not list.name.startswith("OB"),batting_list)

	batting_list = sorted(batting_list, cmp=lambda x,y:cmp(int(y.pa),int(x.pa)))
	batting_list = sorted(batting_list, cmp=lambda x,y:cmp(float(y.avg),float(x.avg)))
	avg_list = batting_list[0:5]

	batting_list = sorted(batting_list, cmp=lambda x,y:cmp(float(y.avg),float(x.avg)))
	batting_list = sorted(batting_list, cmp=lambda x,y:cmp(int(y.hit),int(x.hit)))
	hit_list = batting_list[0:5]

	batting_list = sorted(batting_list, cmp=lambda x,y:cmp(int(x.pa),int(y.pa)))
	batting_list = sorted(batting_list, cmp=lambda x,y:cmp(int(y.hr),int(x.hr)))
	hr_list = batting_list[0:5]

	batting_list = sorted(batting_list, cmp=lambda x,y:cmp(int(y.rbi),int(x.rbi)))
	rbi_list = batting_list[0:5]

	# --- pitching ranking
	thr = 1
	pitching_list = calculate_pitching_rank(players,thr)
	pitching_list = filter(lambda list : not list.name.startswith("OB"),pitching_list)

	pitching_list = sorted(pitching_list, cmp=lambda x,y : cmp(int(y.outs),float(x.outs)))
	pitching_list = sorted(pitching_list, cmp=lambda x,y : cmp(float(x.era),float(y.era)))
	era_list = pitching_list[0:5]

	pitching_list = sorted(pitching_list, cmp=lambda x,y : cmp(float(x.era),float(y.era)))
	pitching_list = sorted(pitching_list, cmp=lambda x,y : cmp(int(y.win),float(x.win)))
	win_list = pitching_list[0:5]

	pitching_list = sorted(pitching_list, cmp=lambda x,y : cmp(int(x.outs),float(y.outs)))
	pitching_list = sorted(pitching_list, cmp=lambda x,y : cmp(int(y.so),float(x.so)))
	so_list = pitching_list[0:5]

	pitching_list = sorted(pitching_list, cmp=lambda x,y : cmp(float(x.era),float(y.era)))
	pitching_list = sorted(pitching_list, cmp=lambda x,y : cmp(float(x.whip),float(y.whip)))
	whip_list = pitching_list[0:5]
	

	context = {'team_list' : team_list, 'avg_list': avg_list, 'hit_list': hit_list, 'hr_list': hr_list, 'rbi_list': rbi_list, 'era_list': era_list, 'win_list': win_list, 'so_list': so_list, 'whip_list': whip_list}
	
	return render (request, 'sbleague/index.html', context)
예제 #2
0
def index(request):

    league_name = ['League A', 'League B']
    league_list = []
    for i in range(2):
        league = League()
        league.name = league_name[i]
        league_list.append(league)

    games = Game.objects.all()

    # --- team ranking
    teams = Team.objects.filter(current__gte=1)

    team_map = {}
    for team in teams:
        t = TeamStat()
        t.name = team.name
        t.teamID = team.teamID
        t.current = team.current
        team_map[team.teamID] = t

    for game in games:
        awayID = game.away.teamID
        homeID = game.home.teamID

        team_map[awayID].game_played += 1
        team_map[homeID].game_played += 1

        result = game.get_result()
        if (not result):  # tie
            team_map[awayID].tie += 1
            team_map[homeID].tie += 1
        else:
            team_map[result[0].teamID].win += 1
            team_map[result[1].teamID].lose += 1

    for team in team_map.values():
        team.stat()
        if (team.current == 1):
            league_list[0].team_list.append(team)
        else:  # current == 2
            league_list[1].team_list.append(team)

    for league in league_list:
        team_list = sorted(league.team_list,
                           key=attrgetter('percent'),
                           reverse=True)

        top = team_list[0]
        for team in team_list:
            team.GB = ((top.win - team.win) + (team.lose - top.lose)) / 2.0

        team_list = sorted(team_list, key=attrgetter('percent'), reverse=True)
        team_list = sorted(team_list, key=attrgetter('GB'))
        team_list[0].GB = '-'
        league.team_list = team_list

    players = Member.objects.all()
    # --- batting ranking
    thr = 1
    batting_list = calculate_batting_rank(players, thr)
    batting_list = filter(lambda list: not list.name.startswith("OB"),
                          batting_list)

    batting_list = sorted(batting_list,
                          cmp=lambda x, y: cmp(int(y.pa), int(x.pa)))
    batting_list = sorted(batting_list,
                          cmp=lambda x, y: cmp(float(y.avg), float(x.avg)))
    avg_list = batting_list[0:5]

    batting_list = sorted(batting_list,
                          cmp=lambda x, y: cmp(float(y.avg), float(x.avg)))
    batting_list = sorted(batting_list,
                          cmp=lambda x, y: cmp(int(y.hit), int(x.hit)))
    hit_list = batting_list[0:5]

    batting_list = sorted(batting_list,
                          cmp=lambda x, y: cmp(int(x.pa), int(y.pa)))
    batting_list = sorted(batting_list,
                          cmp=lambda x, y: cmp(int(y.hr), int(x.hr)))
    hr_list = batting_list[0:5]

    batting_list = sorted(batting_list,
                          cmp=lambda x, y: cmp(int(y.rbi), int(x.rbi)))
    rbi_list = batting_list[0:5]

    # --- pitching ranking
    thr = 1
    pitching_list = calculate_pitching_rank(players, thr)
    pitching_list = filter(lambda list: not list.name.startswith("OB"),
                           pitching_list)

    pitching_list = sorted(pitching_list,
                           cmp=lambda x, y: cmp(int(y.outs), float(x.outs)))
    pitching_list = sorted(pitching_list,
                           cmp=lambda x, y: cmp(float(x.era), float(y.era)))
    era_list = pitching_list[0:5]

    pitching_list = sorted(pitching_list,
                           cmp=lambda x, y: cmp(float(x.era), float(y.era)))
    pitching_list = sorted(pitching_list,
                           cmp=lambda x, y: cmp(int(y.win), float(x.win)))
    win_list = pitching_list[0:5]

    pitching_list = sorted(pitching_list,
                           cmp=lambda x, y: cmp(int(x.outs), float(y.outs)))
    pitching_list = sorted(pitching_list,
                           cmp=lambda x, y: cmp(int(y.so), float(x.so)))
    so_list = pitching_list[0:5]

    pitching_list = sorted(pitching_list,
                           cmp=lambda x, y: cmp(float(x.era), float(y.era)))
    pitching_list = sorted(pitching_list,
                           cmp=lambda x, y: cmp(float(x.whip), float(y.whip)))
    whip_list = pitching_list[0:5]

    context = {
        'league_list': league_list,
        'avg_list': avg_list,
        'hit_list': hit_list,
        'hr_list': hr_list,
        'rbi_list': rbi_list,
        'era_list': era_list,
        'win_list': win_list,
        'so_list': so_list,
        'whip_list': whip_list
    }

    return render(request, 'sbleague/index.html', context)