Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions custom_components/beatify/game/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,30 @@ def create_game(
"song_count": len(songs),
}

def _build_song_dict(self, include_reveal: bool = False) -> dict:
"""Build the song info dict from current_song.

Args:
include_reveal: If True, include year and fun_fact fields
shown only during REVEAL phase.
"""
song = self.current_song
result = {
"artist": song.get("artist", "Unknown"),
"title": song.get("title", "Unknown"),
"album_art": song.get(
"album_art", "/beatify/static/img/no-artwork.svg"
),
}
if include_reveal:
result["year"] = song.get("year")
result["fun_fact"] = song.get("fun_fact", "")
result["fun_fact_de"] = song.get("fun_fact_de", "")
result["fun_fact_es"] = song.get("fun_fact_es", "")
result["fun_fact_fr"] = song.get("fun_fact_fr", "")
result["fun_fact_nl"] = song.get("fun_fact_nl", "")
return result
Comment on lines +410 to +432
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The centralized helper method _build_song_dict currently relies on the internal state self.current_song. It is more robust and modular to pass the song data as an argument. Additionally, the return type should be more specific (dict[str, Any]) to match the rest of the file's type hinting style, and the repetitive fun_fact field assignments can be simplified with a loop. Since the method doesn't depend on other instance state, it can be marked as a @staticmethod.

    @staticmethod
    def _build_song_dict(song: dict[str, Any], include_reveal: bool = False) -> dict[str, Any]:
        """Build the song info dict from song data.

        Args:
            song: The song dictionary to process.
            include_reveal: If True, include year and fun_fact fields
                            shown only during REVEAL phase.
        """
        result = {
            "artist": song.get("artist", "Unknown"),
            "title": song.get("title", "Unknown"),
            "album_art": song.get(
                "album_art", "/beatify/static/img/no-artwork.svg"
            ),
        }
        if include_reveal:
            result["year"] = song.get("year")
            for field in ["fun_fact", "fun_fact_de", "fun_fact_es", "fun_fact_fr", "fun_fact_nl"]:
                result[field] = song.get(field, "")
        return result


def _state_playing(self) -> dict[str, Any]:
"""Return PLAYING phase-specific state fragment."""
fragment: dict[str, Any] = {
Expand All @@ -430,13 +454,7 @@ def _state_playing(self) -> dict[str, Any]:
}
# Song info WITHOUT year during PLAYING (hidden until reveal)
if self.current_song:
fragment["song"] = {
"artist": self.current_song.get("artist", "Unknown"),
"title": self.current_song.get("title", "Unknown"),
"album_art": self.current_song.get(
"album_art", "/beatify/static/img/no-artwork.svg"
),
}
fragment["song"] = self._build_song_dict()
# Story 20.1: Artist challenge (hide answer during PLAYING)
ac = self._challenge_manager.get_artist_challenge_dict(include_answer=False)
if ac is not None:
Expand All @@ -461,19 +479,7 @@ def _state_reveal(self) -> dict[str, Any]:
}
# Filtered song info during REVEAL — exclude URIs, alt_artists, internal fields
if self.current_song:
fragment["song"] = {
"artist": self.current_song.get("artist", "Unknown"),
"title": self.current_song.get("title", "Unknown"),
"year": self.current_song.get("year"),
"album_art": self.current_song.get(
"album_art", "/beatify/static/img/no-artwork.svg"
),
"fun_fact": self.current_song.get("fun_fact", ""),
"fun_fact_de": self.current_song.get("fun_fact_de", ""),
"fun_fact_es": self.current_song.get("fun_fact_es", ""),
"fun_fact_fr": self.current_song.get("fun_fact_fr", ""),
"fun_fact_nl": self.current_song.get("fun_fact_nl", ""),
}
fragment["song"] = self._build_song_dict(include_reveal=True)
# Round analytics (Story 13.3 AC4)
if self.round_analytics:
fragment["round_analytics"] = self.round_analytics.to_dict()
Expand Down
Loading