Exemplo n.º 1
0
def _parse_pass(
    raw_event: Dict,
    team: Team,
    previous_event: Dict = None,
    next_event: Dict = None,
) -> Dict:
    if next_event is not None and next_event["type"] == DF_EVENT_TYPE_OFFSIDE:
        result = PassResult.OFFSIDE
    elif raw_event["type"] == DF_EVENT_TYPE_PASS_CORRECT:
        result = PassResult.COMPLETE
    elif next_event is not None and next_event["type"] in BALL_OUT_EVENTS:
        result = PassResult.OUT
    else:
        result = PassResult.INCOMPLETE

    receiver_coordinates = None
    # NOTE: in really rare cases coordinates may be missing
    coordinates = raw_event.get("coord", {}).get("2")
    if coordinates is not None:
        receiver_coordinates = _parse_coordinates(coordinates)

    # find receiver player, if possible
    receiver_player = None
    if "recvId" in raw_event:
        receiver_player = team.get_player_by_id(raw_event["recvId"])

    elif raw_event["type"] in (
            DF_EVENT_TYPE_THROW_IN,
            DF_EVENT_TYPE_CORNER_KICK,
    ):
        # there is no receiver data in this case, use next_event to deduce receiver
        if next_event is not None:
            if (next_event["team"] != raw_event["team"]
                    and next_event["type"] == DF_EVENT_TYPE_FOUL
                    and "recvId" in next_event):
                # foul from the opposite team
                receiver_player = team.get_player_by_id(next_event["recvId"])
                result = PassResult.COMPLETE
            elif (next_event["team"] == raw_event["team"]
                  and "plyrId" in next_event):
                # or same team event with plyrId
                receiver_player = team.get_player_by_id(next_event["plyrId"])
                result = PassResult.COMPLETE

    qualifiers = _get_event_qualifiers(raw_event, previous_event)

    return dict(
        result=result,
        receiver_coordinates=receiver_coordinates,
        receiver_player=receiver_player,
        receive_timestamp=parse_str_ts(raw_event),
        qualifiers=qualifiers,
    )
Exemplo n.º 2
0
def _parse_pass(pass_dict: Dict, team: Team, fidelity_version: int) -> Dict:
    if "outcome" in pass_dict:
        outcome_id = pass_dict["outcome"]["id"]
        if outcome_id == SB_PASS_OUTCOME_OUT:
            result = PassResult.OUT
        elif outcome_id == SB_PASS_OUTCOME_INCOMPLETE:
            result = PassResult.INCOMPLETE
        elif outcome_id == SB_PASS_OUTCOME_OFFSIDE:
            result = PassResult.OFFSIDE
        elif outcome_id == SB_PASS_OUTCOME_INJURY_CLEARANCE:
            result = PassResult.OUT
        elif outcome_id == SB_PASS_OUTCOME_UNKNOWN:
            result = None
        else:
            raise Exception(f"Unknown pass outcome: {outcome_id}")

        receiver_player = None
        receiver_coordinates = None
    else:
        result = PassResult.COMPLETE
        receiver_player = team.get_player_by_id(pass_dict["recipient"]["id"])
        receiver_coordinates = _parse_coordinates(
            pass_dict["end_location"], fidelity_version
        )

    return dict(
        result=result,
        receiver_coordinates=receiver_coordinates,
        receiver_player=receiver_player,
    )
Exemplo n.º 3
0
def _parse_pass(event: Dict, subtypes: List, team: Team) -> Dict:

    pass_type_id = event["type"]["id"]

    if pass_type_id == MS_PASS_OUTCOME_COMPLETE:
        result = PassResult.COMPLETE
        receiver_player = team.get_player_by_id(event["to"]["id"])
        receiver_coordinates = _parse_coordinates(event["end"])
        receive_timestamp = event["end"]["time"]
    else:
        if pass_type_id == MS_PASS_OUTCOME_OUT:
            result = PassResult.OUT
        elif pass_type_id == MS_PASS_OUTCOME_INCOMPLETE:
            if subtypes and MS_PASS_OUTCOME_OFFSIDE in subtypes:
                result = PassResult.OFFSIDE
            else:
                result = PassResult.INCOMPLETE
        else:
            raise Exception(f"Unknown pass outcome: {pass_type_id}")

        receiver_player = None
        receiver_coordinates = None
        receive_timestamp = None

    return dict(
        result=result,
        receiver_coordinates=receiver_coordinates,
        receiver_player=receiver_player,
        receive_timestamp=receive_timestamp,
    )
Exemplo n.º 4
0
def _parse_pass(event: Dict, previous_event: Dict, subtypes: List,
                team: Team) -> Dict:

    event_type_id = event["type"]["id"]

    if event_type_id == MS_PASS_OUTCOME_COMPLETE:
        result = PassResult.COMPLETE
        receiver_player = team.get_player_by_id(event["to"]["id"])
        receiver_coordinates = _parse_coordinates(event["end"])
        receive_timestamp = event["end"]["time"]
    else:
        if event_type_id == MS_PASS_OUTCOME_OUT:
            result = PassResult.OUT
        elif event_type_id == MS_PASS_OUTCOME_INCOMPLETE:
            if subtypes and MS_PASS_OUTCOME_OFFSIDE in subtypes:
                result = PassResult.OFFSIDE
            else:
                result = PassResult.INCOMPLETE
        else:
            raise DeserializationError(
                f"Unknown pass outcome: {event_type_id}")

        receiver_player = None
        receiver_coordinates = None
        receive_timestamp = None

    qualifiers = _get_event_qualifiers(event, previous_event, subtypes)

    return dict(
        result=result,
        receiver_coordinates=receiver_coordinates,
        receiver_player=receiver_player,
        receive_timestamp=receive_timestamp,
        qualifiers=qualifiers,
    )
Exemplo n.º 5
0
def _get_team_and_player(raw_event: Dict, home_team: Team,
                         away_team: Team) -> Tuple[Team, Player]:
    team = None
    player = None

    team_id = raw_event.get("team")
    if team_id is not None:
        team = home_team if str(team_id) == home_team.team_id else away_team

    if "plyrId" in raw_event:
        if team is not None:
            player = team.get_player_by_id(raw_event["plyrId"])
        else:
            # NOTE: sometime events are missing team
            player = home_team.get_player_by_id(raw_event["plyrId"])
            if player is not None:
                team = home_team
            else:
                player = away_team.get_player_by_id(raw_event["plyrId"])
                team = away_team
    return team, player
Exemplo n.º 6
0
def _parse_pass(event_chain: OrderedDict, team: Team) -> Dict:
    if event_chain["Play"]["Evaluation"] in (
            "successfullyCompleted",
            "successful",
    ):
        result = PassResult.COMPLETE
        if "Recipient" in event_chain["Play"]:
            receiver_player = team.get_player_by_id(
                event_chain["Play"]["Recipient"])
        else:
            # this attribute can be missing according to docs
            receiver_player = None
    else:
        result = PassResult.INCOMPLETE
        receiver_player = None

    return dict(
        result=result,
        receiver_player=receiver_player,
        qualifiers=_get_event_qualifiers(event_chain),
    )
Exemplo n.º 7
0
def _parse_pass(pass_dict: Dict, team: Team, fidelity_version: int) -> Dict:
    if "outcome" in pass_dict:
        outcome_id = pass_dict["outcome"]["id"]
        if outcome_id == SB_PASS_OUTCOME_OUT:
            result = PassResult.OUT
        elif outcome_id == SB_PASS_OUTCOME_INCOMPLETE:
            result = PassResult.INCOMPLETE
        elif outcome_id == SB_PASS_OUTCOME_OFFSIDE:
            result = PassResult.OFFSIDE
        elif outcome_id == SB_PASS_OUTCOME_INJURY_CLEARANCE:
            result = PassResult.OUT
        elif outcome_id == SB_PASS_OUTCOME_UNKNOWN:
            result = None
        else:
            raise DeserializationError(f"Unknown pass outcome: {outcome_id}")

        receiver_player = None
    else:
        result = PassResult.COMPLETE
        receiver_player = team.get_player_by_id(pass_dict["recipient"]["id"])

    receiver_coordinates = _parse_coordinates(
        pass_dict["end_location"],
        fidelity_version,
    )

    qualifiers = []
    pass_qualifiers = _get_pass_qualifiers(pass_dict)
    qualifiers.extend(pass_qualifiers)
    set_piece_qualifiers = _get_set_piece_qualifiers(pass_dict)
    qualifiers.extend(set_piece_qualifiers)
    body_part_qualifiers = _get_body_part_qualifiers(pass_dict)
    qualifiers.extend(body_part_qualifiers)

    return {
        "result": result,
        "receiver_coordinates": receiver_coordinates,
        "receiver_player": receiver_player,
        "qualifiers": qualifiers,
    }
Exemplo n.º 8
0
def _parse_substitution(raw_event: Dict, team: Team) -> Dict:
    player = team.get_player_by_id(raw_event["offId"])
    replacement_player = team.get_player_by_id(raw_event["inId"])

    return dict(player=player, replacement_player=replacement_player)
Exemplo n.º 9
0
def _parse_substitution(event_attributes: Dict, team: Team) -> Dict:
    return dict(
        player=team.get_player_by_id(event_attributes["PlayerOut"]),
        replacement_player=team.get_player_by_id(event_attributes["PlayerIn"]),
    )