From 2b09ecdcb33caafb8cf11b1b833a15ad61c4cccb Mon Sep 17 00:00:00 2001 From: Asaf Kali Date: Fri, 8 Nov 2024 23:20:24 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A2=20UX=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/bot/handlers/other/event_handler.py | 21 +++++++++++++---- app/bot/handlers/parse/parse_map_handler.py | 25 ++++++++++++--------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/app/bot/handlers/other/event_handler.py b/app/bot/handlers/other/event_handler.py index 0d15807..e363192 100644 --- a/app/bot/handlers/other/event_handler.py +++ b/app/bot/handlers/other/event_handler.py @@ -1,5 +1,6 @@ +from collections import defaultdict from random import random -from typing import TYPE_CHECKING, Any, Callable, Optional, Type +from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Type import sentry_sdk from beautifultable import BeautifulTable @@ -21,7 +22,7 @@ ) from codenames.game.board import Board from codenames.game.card import Card -from codenames.game.color import TeamColor +from codenames.game.color import CardColor, TeamColor from codenames.game.move import PASS_GUESS, Hint from codenames.game.player import PlayerRole from codenames.game.state import GameState @@ -194,7 +195,7 @@ def fast_forward(self, state: GameState): if state.is_game_over: self.send_game_summary(state=state) log.update_context(game_id=None) - self.update_session(game_id=None) + self.reset_session() from bot.handlers.other.help import HelpMessageHandler self.trigger(HelpMessageHandler) @@ -357,11 +358,23 @@ def parsed_board(self) -> Board: def send_parsing_state(self): parsed_board = self.parsed_board() keyboard = build_board_keyboard(table=parsed_board.as_table, is_game_over=True) - message = "OK! Here's the board.\nClick on any card to fix it. When you are done, click /done." + color_stats = _get_color_stats(board=parsed_board) + color_stats_str = " ".join(rf"\[{count} {color.emoji}]" for color, count in color_stats.items()) + message = f"""OK! Here's the board. +Color stats: {color_stats_str} +Click on any card to fix it. When you are done, click /done.""" text = self.send_markdown(text=message, reply_markup=keyboard) self.update_session(last_keyboard_message_id=text.message_id) +def _get_color_stats(board: Board) -> Dict[CardColor, int]: + stats: Dict[CardColor, int] = defaultdict(int) + for card in board.cards: + stats[card.color] += 1 + stats = dict(sorted(stats.items(), key=lambda item: item[1], reverse=True)) + return stats + + def _should_skip_turn(current_player_role: PlayerRole, config: GameConfig) -> bool: if current_player_role != PlayerRole.GUESSER: return False diff --git a/app/bot/handlers/parse/parse_map_handler.py b/app/bot/handlers/parse/parse_map_handler.py index a02e59a..5c478b8 100644 --- a/app/bot/handlers/parse/parse_map_handler.py +++ b/app/bot/handlers/parse/parse_map_handler.py @@ -2,30 +2,35 @@ from bot.config import get_config from bot.handlers.other.event_handler import EventHandler from bot.handlers.parse.photos import _get_base64_photo -from bot.models import BotState, ParsingState +from bot.models import BotState from codenames.game.color import CardColor - # Map -> Board + + class ParseMapHandler(EventHandler): def handle(self): photo_base64 = _get_base64_photo(photos=self.update.message.photo) card_colors = _parse_map_colors(photo_base64) - self._send_as_emoji_table(card_colors) - parsing_state = ParsingState(language="heb", card_colors=card_colors) - self.update_session(parsing_state=parsing_state) + table = self._as_emoji_table(card_colors) + self.update_parsing_state(card_colors=card_colors) # Board parsing - self.send_text("🧩 Please send me a picture of the board:") + message = f"""I got: +{table} +You will have a chance to fix any mistakes later. + +🧩 Please send me a picture of the board: +""" + self.send_text(message) return BotState.PARSE_BOARD - def _send_as_emoji_table(self, card_colors: list[CardColor]): - result = "I got: \n\n" + def _as_emoji_table(self, card_colors: list[CardColor]) -> str: + result = "" for i in range(0, len(card_colors), 5): row = card_colors[i : i + 5] row_emojis = " ".join(card.emoji for card in row) result += f"{row_emojis}\n" - result += "\nYou will have a chance to fix any mistakes later." - self.send_text(result) + return result def _parse_map_colors(photo_base64: str) -> list[CardColor]: