diff --git a/bots/client_bot.py b/bots/client_bot.py index 1b85da6..e8c61f1 100644 --- a/bots/client_bot.py +++ b/bots/client_bot.py @@ -44,64 +44,15 @@ def _md_escape(text: str) -> str: return ''.join(result) -def _display_width(s: str) -> int: - w = 0 - for ch in s: - cp = ord(ch) - # Cyrillic, CJK, etc — typically 2-wide in monospace - if (0x0400 <= cp <= 0x04FF or 0x0500 <= cp <= 0x052F or - 0x2E80 <= cp <= 0x9FFF or 0xF900 <= cp <= 0xFAFF or - 0xFE30 <= cp <= 0xFE4F or cp == 0x2116): - w += 2 - else: - w += 1 - return w - - -def _pad_to_width(s: str, width: int) -> str: - cur = _display_width(s) - if cur >= width: - return s - return s + ' ' * (width - cur) - - -def _trim_to_width(s: str, width: int) -> str: - result = [] - cur = 0 - for ch in s: - cw = 2 if _display_width(ch) == 2 else 1 - if cur + cw > width: - break - result.append(ch) - cur += cw - return ''.join(result) - - -def _make_table_header(cols: list, widths: list) -> str: - parts = [_pad_to_width(c, w) for c, w in zip(cols, widths)] - line = ' │ '.join(parts) - sep = '─┼─'.join('─' * w for w in widths) - return line + '\n' + sep - - -def _make_table_row(cols: list, widths: list) -> str: - parts = [_pad_to_width(c, w) for c, w in zip(cols, widths)] - return ' │ '.join(parts) - - def format_pairings(pairings_data: dict, tournament_name: str) -> str: pairings = pairings_data['pairings'] rnd = pairings_data['round'] - max_name = 28 - cols = ['#', 'Белые', 'О', 'Чёрные', 'О'] - widths = [5, max_name, 5, max_name, 5] - - header_line = f'*{_md_escape(tournament_name)}*\n📋 *Тур {rnd}* — пары\n' - - table_lines = [] - table_lines.append('```') - table_lines.append(_make_table_header(cols, widths)) + lines = [ + f'*{_md_escape(tournament_name)}*', + f'📋 *Тур {rnd}* — пары', + '', + ] board = 1 for pairing in pairings: @@ -110,11 +61,7 @@ def format_pairings(pairings_data: dict, tournament_name: str) -> str: p1, p2, color = pairing if color == 'bye': - bye_name = _trim_to_width(p1.name, max_name) - table_lines.append( - _make_table_row( - [str(board), bye_name, str(p1.points), '— BYE —', ''], - widths)) + lines.append(f'{board}\\. {_md_escape(p1.name)} — BYE') board += 1 continue @@ -123,27 +70,20 @@ def format_pairings(pairings_data: dict, tournament_name: str) -> str: else: white, black = p2, p1 - w_name = _trim_to_width(white.name, max_name) - b_name = _trim_to_width(black.name, max_name) - w_pts = f'{white.points:.1f}' - b_pts = f'{black.points:.1f}' - - table_lines.append( - _make_table_row( - [str(board), w_name, w_pts, b_name, b_pts], - widths)) + lines.append( + f'{board}\\. {_md_escape(white.name)} ' + f'\\({white.points:.1f}\\) — ' + f'{_md_escape(black.name)} \\({black.points:.1f}\\)' + ) board += 1 - table_lines.append('```') - - footer = '' source = pairings_data.get('source', 'algorithm') if source == 'bbp_pairings_fide_2025': - footer = '\n_FIDE 2025 \(bbpPairings\)_' + lines.append('\n_FIDE 2025 \(bbpPairings\)_') elif source == 'swiss_algorithm_simplified': - footer = '\n_Упрощённый Swiss \(bbpPairings недоступен\)_' + lines.append('\n_Упрощённый Swiss \(bbpPairings недоступен\)_') - return header_line + '\n'.join(table_lines) + footer + return '\n'.join(lines) async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):