예제 #1
0
def team(request, team_id, order="hit"):
    thisteam = Team.objects.get(teamID=team_id)
    team_info = TeamStat()
    team_info.name = thisteam.name
    team_info.teamID = thisteam.teamID

    game_all = Game.objects.filter(Q(away=team_id) | Q(home=team_id))
    game_list = []
    for game in game_all:
        g = GameStat()

        if (game.away.teamID != int(team_id)):
            opp = game.away
        else:
            opp = game.home

        game_result = game.get_result()
        if (len(game_result) == 0):
            result = '和'
            team_info.tie += 1
        elif (game_result[0].teamID == int(team_id)):
            result = '勝'
            team_info.win += 1
        else:
            result = '敗'
            team_info.lose += 1

        team_info.game_played += 1

        scores = game.get_scores()

        g.gameID = game.gameID
        g.date = game.date
        g.location = game.location
        g.opp = opp
        g.scores = str(sum(scores[0])) + ' : ' + str(sum(scores[1]))
        g.result = result
        game_list.append(g)

    # sort by date
    game_list = sorted(game_list, key=attrgetter('date'))
    team_info.stat()

    members = Member.objects.filter(team__teamID=team_id)

    members_bat = []
    members_pit = []
    team_bat = Hitter()
    team_pit = Pitcher()

    for player in members:
        #batting data
        bat = Batting.objects.filter(member__memberID=player.memberID,
                                     team=thisteam.teamID)

        if bat.exists():
            hitter = Hitter()
            for bat_detail in bat:
                hitter.name = player.name
                hitter.id = player.memberID
                hitter.games_played += 1
                hitter.pa += bat_detail.pa
                hitter.single += bat_detail.single
                hitter.double += bat_detail.double
                hitter.triple += bat_detail.triple
                hitter.hr += bat_detail.hr
                hitter.rbi += bat_detail.rbi
                hitter.r += bat_detail.run
                hitter.bb += bat_detail.bb
                hitter.so += bat_detail.so
                hitter.sf += bat_detail.sf
            hitter.stat()
            members_bat.append(hitter)
            team_bat.add(hitter)
            team_bat.stat()
        members_bat = sorted(members_bat, key=attrgetter(order), reverse=True)

        #pitching data
        pit = Pitching.objects.filter(member__memberID=player.memberID,
                                      team=thisteam.teamID)
        if pit.exists():
            pitcher = Pitcher()

            for pit_detail in pit:
                pitcher.name = player.name
                pitcher.games_played += 1
                pitcher.id = player.memberID
                pitcher.win += pit_detail.win
                pitcher.lose += pit_detail.lose
                pitcher.outs += pit_detail.outs
                pitcher.pa += pit_detail.pa
                pitcher.so += pit_detail.so
                pitcher.bb += pit_detail.bb
                pitcher.h += pit_detail.h
                pitcher.hr += pit_detail.hr
                pitcher.r += pit_detail.r
                pitcher.er += pit_detail.er
                pitcher.go += pit_detail.go
                pitcher.fo += pit_detail.fo

            pitcher.stat()
            members_pit.append(pitcher)
            team_pit.add(pitcher)
            team_pit.stat()

    context = {
        'thisteam': thisteam,
        'team_info': team_info,
        'game_list': game_list,
        'members_bat': members_bat,
        'members_pit': members_pit,
        'team_bat': team_bat,
        'team_pit': team_pit
    }

    return render(request, 'sbleague/team.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)