From 95c93a455fd0cbe68ee82e7145b2b60a83243811 Mon Sep 17 00:00:00 2001 From: Eric Froemling Date: Fri, 29 May 2020 16:34:20 -0700 Subject: [PATCH] Fixed a bug with get_source_player --- assets/src/ba_data/python/ba/_messages.py | 16 +++++++--------- assets/src/ba_data/python/bastd/actor/bomb.py | 14 +++----------- assets/src/ba_data/python/bastd/game/hockey.py | 8 ++++---- .../ba_data/python/bastd/game/targetpractice.py | 3 ++- docs/ba_module.md | 5 +---- 5 files changed, 17 insertions(+), 29 deletions(-) diff --git a/assets/src/ba_data/python/ba/_messages.py b/assets/src/ba_data/python/ba/_messages.py index e97a561a..dba1bfd6 100644 --- a/assets/src/ba_data/python/ba/_messages.py +++ b/assets/src/ba_data/python/ba/_messages.py @@ -301,7 +301,7 @@ class HitMessage: self.velocity_magnitude = velocity_magnitude self.radius = radius - # Invalid refs should never be passed to things. + # We should not be getting passed an invalid ref. assert source_player is None or source_player.exists() self._source_player = source_player self.kick_back = kick_back @@ -313,18 +313,16 @@ class HitMessage: def get_source_player( self, playertype: Type[PlayerType]) -> Optional[PlayerType]: - """Return the source-player if there is one and they still exist. - - The type of player for the current activity should be passed so that - the type-checker properly identifies the returned value as one. - """ + """Return the source-player if one exists and is the provided type.""" player: Any = self._source_player - assert isinstance(player, (playertype, type(None))) # We should not be delivering invalid refs. - # (technically if someone holds on to this message this can happen) + # (we could translate to None here but technically we are changing + # the message delivered which seems wrong) assert player is None or player.exists() - return player + + # Return the player *only* if they're the type given. + return player if isinstance(player, playertype) else None @dataclass diff --git a/assets/src/ba_data/python/bastd/actor/bomb.py b/assets/src/ba_data/python/bastd/actor/bomb.py index e33852a5..6cf5477a 100644 --- a/assets/src/ba_data/python/bastd/actor/bomb.py +++ b/assets/src/ba_data/python/bastd/actor/bomb.py @@ -852,18 +852,10 @@ class Bomb(ba.Actor): def get_source_player( self, playertype: Type[PlayerType]) -> Optional[PlayerType]: - """Return the source-player if there is one and they still exist. - - The type of player for the current activity should be passed so that - the type-checker properly identifies the returned value as one. - """ + """Return the source-player if one exists and is the provided type.""" player: Any = self._source_player - assert isinstance(player, (playertype, type(None))) - - # We should not be delivering invalid refs. - # (technically if someone holds on to this message this can happen) - assert player is None or player.exists() - return player + return (player if isinstance(player, playertype) and player.exists() + else None) def on_expire(self) -> None: super().on_expire() diff --git a/assets/src/ba_data/python/bastd/game/hockey.py b/assets/src/ba_data/python/bastd/game/hockey.py index 333a5d19..644f9da3 100644 --- a/assets/src/ba_data/python/bastd/game/hockey.py +++ b/assets/src/ba_data/python/bastd/game/hockey.py @@ -98,12 +98,12 @@ class Puck(ba.Actor): msg.force_direction[2]) # If this hit came from a player, log them as the last to touch us. - splayer = msg.get_source_player(Player) - if splayer is not None: + s_player = msg.get_source_player(Player) + if s_player is not None: activity = self._activity() if activity: - if splayer in activity.players: - self.last_players_to_touch[splayer.team.id] = splayer + if s_player in activity.players: + self.last_players_to_touch[s_player.team.id] = s_player else: super().handlemessage(msg) diff --git a/assets/src/ba_data/python/bastd/game/targetpractice.py b/assets/src/ba_data/python/bastd/game/targetpractice.py index 0c4c0252..7a94f331 100644 --- a/assets/src/ba_data/python/bastd/game/targetpractice.py +++ b/assets/src/ba_data/python/bastd/game/targetpractice.py @@ -173,7 +173,8 @@ class TargetPracticeGame(ba.TeamGameActivity[Player, Team]): # under us if we hit stuff (don't wanna get points for new targets). player = bomb.get_source_player(Player) if not player: - return # Could happen if they leave after throwing a bomb. + # It's possible the player left after throwing the bomb. + return bullseye = any( target.do_hit_at_position(pos, player) diff --git a/docs/ba_module.md b/docs/ba_module.md index 456f465d..ae318809 100644 --- a/docs/ba_module.md +++ b/docs/ba_module.md @@ -2584,10 +2584,7 @@ If the time-limit expires, end_game() will be called.

get_source_player()

get_source_player(self, playertype: Type[PlayerType]) -> Optional[PlayerType]

-

Return the source-player if there is one and they still exist.

- -

The type of player for the current activity should be passed so that -the type-checker properly identifies the returned value as one.

+

Return the source-player if one exists and is the provided type.