Exemple #1
0
def get_teams_by_summoner_id(summoner_id, region):
    """
    Get team data, given a summoner ID and region, and stores it in the DB.

    Each summoner can have 0 or more teams. This checks for matching teams in the DB
    and updates them if they are present, otherwise it creates a new team.
    Matches are made by comparing region and full_id.

    Team queries to the Riot API return the following data, per team:
    -timestamps for roster invites
    -timestamps for roster joins
    -summoner IDs for team roster
    -summoner ID for team owner
    -match summary for each game played
    -summary for overall performance in 5v5 and 3v3
    -team basic data (e.g. tag, name, etc)
    -timestamps for most recent game, team modified, team created
    """
    # Riot API returns HTTP 404 if summoner is not on any teams.
    try:
        teams_dto = riot_api.get_teams_for_summoner(summoner_id, region)
        got_teams = True
    except LoLException as e:
        got_teams = False
        if e == error_404:
            print('Summoner ID', summoner_id, 'is not on any teams (error_404).')
        else:
            got_teams = False
            print('ERROR getting teams:', e)

    if got_teams:
        # teams_dto is a list that will contain an entry for each team the summoner is on.

        for team in teams_dto:
            # Can we match this data to a Team in the DB?
            try:
                matching_team = Team.objects.filter(region=region).get(full_id=team['fullId'])
                # We found a match, so set the working Team to that.
                print('Found matching Team {} {}'.format(matching_team.region, matching_team.name))
                matched = True
                new_team = matching_team
            except ObjectDoesNotExist:
                # No match found, so create a new Team.
                matched = False
                print('No matching team found. Creating a new Team...')
                new_team = Team()

            # We get all the Team data from Riot API call,
            # so we can delete everything and just create new models below.
            if matched:
                new_team.roster.delete()
                new_team.delete()

            # Set everything in the Team model that isn't a related field.
            new_team.create_date = team['createDate']
            new_team.full_id = team['fullId']

            # If team hasn't played a game (this season?)
            if 'lastGameDate' in team:
                new_team.last_game_date = team['lastGameDate']
            new_team.last_joined_ranked_team_queue_date = team['lastJoinedRankedTeamQueueDate']
            new_team.modify_date = team['modifyDate']
            new_team.name = team['name']
            new_team.last_join_date = team['lastJoinDate']
            new_team.second_last_join_date = team['secondLastJoinDate']
            new_team.third_last_join_date = team['thirdLastJoinDate']
            new_team.status = team['status']
            new_team.tag = team['tag']
            new_team.region = region

            # This leaves: roster, teamStatDetails, matchHistory.
            roster_dto = team['roster']
            team_stat_details_dto = team['teamStatDetails']

            # If team hasn't played a game (extends past this season, so no games played ever!)
            if 'matchHistory' in team:
                match_history_dto = team['matchHistory']

            # Setup and save the Roster model.
            new_roster = Roster(owner_id=roster_dto['ownerId'])
            new_roster.save()

            # We can set the roster relation on the Team model now, and save it.
            new_team.roster = new_roster
            new_team.save()

            # Setup and save the TeamMemberInfo models for this Roster.
            for member in roster_dto['memberList']:
                new_member = TeamMemberInfo(invite_date=member['inviteDate'],
                                            join_date=member['joinDate'],
                                            player_id=member['playerId'],
                                            status=member['status'],
                                            roster=new_roster)
                new_member.save()

            # Setup and save the TeamStatDetail models for the Team.
            for stats in team_stat_details_dto:
                new_stats = TeamStatDetail(team_stat_type=stats['teamStatType'],
                                           average_games_played=stats['averageGamesPlayed'],
                                           wins=stats['wins'],
                                           losses=stats['losses'],
                                           team=new_team)
                new_stats.save()

            # Setup and save the MatchHistorySummary models for the Team, if it has a history.

            if match_history_dto:
                for match in match_history_dto:
                    new_match = MatchHistorySummary(assists=match['assists'],
                                                    date=match['date'],
                                                    deaths=match['deaths'],
                                                    game_id=match['gameId'],
                                                    game_mode=match['gameMode'],
                                                    invalid=match['invalid'],
                                                    kills=match['kills'],
                                                    map_id=match['mapId'],
                                                    opposing_team_kills=match['opposingTeamKills'],
                                                    opposing_team_name=match['opposingTeamName'],
                                                    win=match['win'],
                                                    team=new_team)
                    new_match.save()
Exemple #2
0
def get_teams_by_summoner_id(summoner_id, region):
    """
    Get team data, given a summoner ID and region, and stores it in the DB.

    Each summoner can have 0 or more teams. This checks for matching teams in the DB
    and updates them if they are present, otherwise it creates a new team.
    Matches are made by comparing region and full_id.

    Team queries to the Riot API return the following data, per team:
    -timestamps for roster invites
    -timestamps for roster joins
    -summoner IDs for team roster
    -summoner ID for team owner
    -match summary for each game played
    -summary for overall performance in 5v5 and 3v3
    -team basic data (e.g. tag, name, etc)
    -timestamps for most recent game, team modified, team created
    """
    # Riot API returns HTTP 404 if summoner is not on any teams.
    try:
        teams_dto = riot_api.get_teams_for_summoner(summoner_id, region)
        got_teams = True
    except LoLException as e:
        got_teams = False
        if e == error_404:
            print('Summoner ID', summoner_id,
                  'is not on any teams (error_404).')
        else:
            got_teams = False
            print('ERROR getting teams:', e)

    if got_teams:
        # teams_dto is a list that will contain an entry for each team the summoner is on.

        for team in teams_dto:
            # Can we match this data to a Team in the DB?
            try:
                matching_team = Team.objects.filter(region=region).get(
                    full_id=team['fullId'])
                # We found a match, so set the working Team to that.
                print('Found matching Team {} {}'.format(
                    matching_team.region, matching_team.name))
                matched = True
                new_team = matching_team
            except ObjectDoesNotExist:
                # No match found, so create a new Team.
                matched = False
                print('No matching team found. Creating a new Team...')
                new_team = Team()

            # We get all the Team data from Riot API call,
            # so we can delete everything and just create new models below.
            if matched:
                new_team.roster.delete()
                new_team.delete()

            # Set everything in the Team model that isn't a related field.
            new_team.create_date = team['createDate']
            new_team.full_id = team['fullId']

            # If team hasn't played a game (this season?)
            if 'lastGameDate' in team:
                new_team.last_game_date = team['lastGameDate']
            new_team.last_joined_ranked_team_queue_date = team[
                'lastJoinedRankedTeamQueueDate']
            new_team.modify_date = team['modifyDate']
            new_team.name = team['name']
            new_team.last_join_date = team['lastJoinDate']
            new_team.second_last_join_date = team['secondLastJoinDate']
            new_team.third_last_join_date = team['thirdLastJoinDate']
            new_team.status = team['status']
            new_team.tag = team['tag']
            new_team.region = region

            # This leaves: roster, teamStatDetails, matchHistory.
            roster_dto = team['roster']
            team_stat_details_dto = team['teamStatDetails']

            # If team hasn't played a game (extends past this season, so no games played ever!)
            if 'matchHistory' in team:
                match_history_dto = team['matchHistory']

            # Setup and save the Roster model.
            new_roster = Roster(owner_id=roster_dto['ownerId'])
            new_roster.save()

            # We can set the roster relation on the Team model now, and save it.
            new_team.roster = new_roster
            new_team.save()

            # Setup and save the TeamMemberInfo models for this Roster.
            for member in roster_dto['memberList']:
                new_member = TeamMemberInfo(invite_date=member['inviteDate'],
                                            join_date=member['joinDate'],
                                            player_id=member['playerId'],
                                            status=member['status'],
                                            roster=new_roster)
                new_member.save()

            # Setup and save the TeamStatDetail models for the Team.
            for stats in team_stat_details_dto:
                new_stats = TeamStatDetail(
                    team_stat_type=stats['teamStatType'],
                    average_games_played=stats['averageGamesPlayed'],
                    wins=stats['wins'],
                    losses=stats['losses'],
                    team=new_team)
                new_stats.save()

            # Setup and save the MatchHistorySummary models for the Team, if it has a history.

            if match_history_dto:
                for match in match_history_dto:
                    new_match = MatchHistorySummary(
                        assists=match['assists'],
                        date=match['date'],
                        deaths=match['deaths'],
                        game_id=match['gameId'],
                        game_mode=match['gameMode'],
                        invalid=match['invalid'],
                        kills=match['kills'],
                        map_id=match['mapId'],
                        opposing_team_kills=match['opposingTeamKills'],
                        opposing_team_name=match['opposingTeamName'],
                        win=match['win'],
                        team=new_team)
                    new_match.save()