refactor: extract dispatch dict and _handle_reaction in _handle_message() (closes #451)#459
refactor: extract dispatch dict and _handle_reaction in _handle_message() (closes #451)#459
Conversation
…ge() (closes #451) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request refactors the WebSocket message handling in custom_components/beatify/server/websocket.py by replacing a large conditional block with a dispatch dictionary and dedicated handler methods. This change improves code organization and reduces method complexity. The review feedback suggests utilizing the existing get_player_by_ws helper method in the admin handler to replace a manual iteration, enhancing code efficiency and consistency.
| sender = None | ||
| for player in list(game_state.players.values()): | ||
| if player.ws == ws: | ||
| sender = player | ||
| break |
There was a problem hiding this comment.
The manual iteration to find the player session by WebSocket can be replaced with the more efficient game_state.get_player_by_ws(ws) method. This improves readability and leverages the existing API already used in other handlers like _handle_reaction.
| sender = None | |
| for player in list(game_state.players.values()): | |
| if player.ws == ws: | |
| sender = player | |
| break | |
| sender = game_state.get_player_by_ws(ws) |
mholzi
left a comment
There was a problem hiding this comment.
Self-review: LGTM — clean refactor.
Dispatch dict: all 11 original if/elif branches correctly mapped; unknown types still log a warning.
Extracted methods: _handle_reaction(), _handle_join(), _handle_admin(), _handle_get_state() all preserve original logic exactly. Signatures are consistent with existing handler patterns (ws, data, game_state).
Signature normalization: _handle_leave and _handle_get_steal_targets correctly gained the unused data param for uniform dispatch — confirmed no other callers exist.
noqa cleanup: PLR0912/PLR0915 suppressions removed as expected.
No regressions, edge cases, or correctness issues found.
Fixes #451 — refactors the 252-line
_handle_message()into a dispatch dict pattern and extracts_handle_reaction(), removing PLR0912/PLR0915 noqa suppressions.