Fixed a bug with get_source_player

This commit is contained in:
Eric Froemling 2020-05-29 16:34:20 -07:00
parent 6d861f4088
commit 95c93a455f
5 changed files with 17 additions and 29 deletions

View File

@ -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

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -2584,10 +2584,7 @@ If the time-limit expires, end_game() will be called.</p>
<dt><h4><a name="method_ba_HitMessage__get_source_player">get_source_player()</a></dt></h4><dd>
<p><span>get_source_player(self, playertype: Type[PlayerType]) -&gt; Optional[PlayerType]</span></p>
<p>Return the source-player if there is one and they still exist.</p>
<p>The type of player for the current activity should be passed so that
the type-checker properly identifies the returned value as one.</p>
<p>Return the source-player if one exists and is the provided type.</p>
</dd>
</dl>