Skip to content

Commit

Permalink
[Hockey] Fix situation code parsing
Browse files Browse the repository at this point in the history
- More adequately detect the situation on ice from the situation code.
- Replace usage of situation codes with human readable text on recap embeds.
- Remove [](None) from goal highlights that can't be found.
- Attempt to only post the period recap once instead of multiple times until the three stars are set.
- Fix a logging typo in schedules.
  • Loading branch information
TrustyJAID committed Dec 12, 2023
1 parent 44813da commit b0163a7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
45 changes: 35 additions & 10 deletions hockey/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,44 @@ def strength(self, home: bool) -> str:
----------
home: bool
Whether the situation represents the home team or not
1551 - Even Strength
0651 - Pulled away goalie
1560 - Pulled home goalie
1451 - Home power play
1541 - Away power play
1441 - 4v4
1331 - 3v3
1010 - Shootout Home shot
0101 - Shootout Away shot
"""
if self.home_skaters == self.away_skaters == 5:
return _("Even Strength")
if self.home_skaters == self.away_skaters == 4:
return _("4v4")
if self.home_skaters == self.away_skaters == 3:
return _("3v3")
situations = []
if self.home_skaters == 0 or self.away_skaters == 0:
# Special case return for shootout situations
return _("Shootout")
if home and self.home_goalie == 0 or not home and self.away_goalie == 0:
situations.append(_("Pulled Goalie"))
elif home and self.away_goalie == 0 or not home and self.home_goalie == 0:
situations.append(_("Empty Net"))

if (self.away_skaters + self.away_goalie) != (self.home_skaters + self.home_goalie):
if home:
if self.home_skaters < self.away_skaters:
situations.append(_("Shorthanded"))
else:
situations.append(_("Power Play"))
else:
if self.away_skaters < self.home_skaters:
situations.append(_("Shorthanded"))
else:
situations.append(_("Power Play"))

uneven = self.home_skaters < 5 or self.away_skaters < 5
if not uneven and (self.home_goalie != 0 and self.away_goalie != 0):
situations.append(_("Even Strength"))
situations.append(f"({self.away_skaters}v{self.home_skaters})")

if home is True and self.home_skaters > self.away_skaters:
return _("Power Play")
if home is False and self.home_skaters > self.away_skaters:
return _("Shorthanded")
return " ".join(s for s in situations)

def empty_net(self, home: bool) -> str:
"""
Expand Down Expand Up @@ -323,6 +347,7 @@ def to_goal(self, data: dict, content: Optional[dict] = None) -> Goal:
empty_net=self.situation.empty_net(home),
event=str(self.type_code),
link=self.get_highlight(content),
situation=self.situation,
)


Expand Down
37 changes: 24 additions & 13 deletions hockey/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,25 +496,35 @@ async def make_game_embed(

for goal in list_goals[ordinal]:
try:
emoji = f"<:{TEAMS[goal.team_name]['emoji']}>"
emoji = discord.PartialEmoji.from_str(TEAMS[goal.team_name]["emoji"])
except KeyError:
emoji = ""
left = ""
if goal.time_remaining:
left = _("\n{time} left in the {ord} period").format(
time=goal.time_remaining, ord=goal.period_ord
)
goal_msg += _(
"{emoji} [{team} {empty_net}{strength} Goal By {description} {left}]({link})\n\n"
).format(
emoji=emoji,
team=goal.team_name,
empty_net="EN " if goal.empty_net else "",
strength=goal.strength_code,
description=goal.description,
link=goal.link,
left=left,
)
if goal.link:
goal_msg += _(
"{emoji} [{team} {strength} Goal By {description} {left}]({link})\n\n"
).format(
emoji=emoji,
team=goal.team_name,
strength=goal.strength,
description=goal.description,
link=goal.link,
left=left,
)
else:
goal_msg += _(
"{emoji} {team} {strength} Goal By {description} {left}\n\n"
).format(
emoji=emoji,
team=goal.team_name,
strength=goal.strength,
description=goal.description,
left=left,
)

count = 0
continued = _("(Continued)")
Expand Down Expand Up @@ -774,7 +784,8 @@ async def check_game_state(self, bot: Red, count: int = 0) -> bool:
await self.check_team_goals(bot)
if end_third and old_game_state not in [GameState.final, GameState.official_final]:
log.debug("End of the third period %s @ %s", self.away_team, self.home_team)
await self.period_recap(bot, "3rd")
if old_game_state is not GameState.live_end_third:
await self.period_recap(bot, "3rd")
await self.save_game_state(bot, "END3rd")

if (
Expand Down
1 change: 1 addition & 0 deletions hockey/goal.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def __init__(self, **kwargs):
self.tasks: List[asyncio.Task] = []
self.home_shots: int = kwargs.get("home_shots", 0)
self.away_shots: int = kwargs.get("away_shots", 0)
self.situation = kwargs.get("situation")

def __repr__(self):
return "<Hockey Goal team={0.team_name} id={0.goal_id} >".format(self)
Expand Down
2 changes: 1 addition & 1 deletion hockey/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .helper import utc_to_local

_ = Translator("Hockey", __file__)
log = getLogger("red.trusty-cogs.hockey")
log = getLogger("red.trusty-cogs.Hockey")


class Schedule(menus.PageSource):
Expand Down

0 comments on commit b0163a7

Please sign in to comment.