Skip to content

Commit

Permalink
🎢 UX fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
asaf-kali committed Nov 8, 2024
1 parent 6c45828 commit 2b09ecd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
21 changes: 17 additions & 4 deletions app/bot/handlers/other/event_handler.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
25 changes: 15 additions & 10 deletions app/bot/handlers/parse/parse_map_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down

0 comments on commit 2b09ecd

Please sign in to comment.