from collections import OrderedDict import sys import warnings import chardet from bcdd.BCalcWrapper import BCalcWrapper from bcdd.DDTable import DDTable from bcdd.Exceptions import FieldNotFoundException from bcdd.ParScore import ParScore from bcdd.PBNBoard import PBNBoard from . import BinaryFormat from .. import dto HTML_SUITS = OrderedDict([ ('s', u'\u2660'), ('h', u'\u2665'), ('d', u'\u2666'), ('c', u'\u2663') ]) _page_template = '''
%s
%s ''' class HTMLFormat(BinaryFormat): @property def suffix(self): return '.html' def __init__(self, *args, **kwargs): BinaryFormat.__init__(self, *args, **kwargs) self.deals_per_column = self.options.get('columns', 6) or 6 def parse_content(self, content): raise NotImplementedError def _get_html_hands(self, deal): suits = list(HTML_SUITS.keys()) return [ [ self._get_html_suit(suits[i], suit) for i, suit in enumerate(hand) ] for hand in deal.hands ] def _get_suit_symbol(self, suit): return '%s' % ( suit, HTML_SUITS[suit] ) def _get_html_suit(self, suit, cards): return ' %s %s' % ( self._get_suit_symbol(suit), ''.join(cards).replace('T', '10')) def _get_deal_header(self, board): try: conditions = '%s / %s' % ( 'NESW'[board.dealer], ('all' if board.vulnerable['EW'] else 'NS') if board.vulnerable['NS'] else ('EW' if board.vulnerable['EW'] else 'none') ) except: conditions = '' return '
' + \ '' + \ str(board.number) + \ '
' + \ conditions + \ '
' def _get_dd_data(self, board): data = { 'par': '', 'table': '' } pbn_board = PBNBoard(board.extra_fields) dd_table = DDTable(pbn_board) tricks_table = None try: tricks_table = dd_table.get_pbn_table() except FieldNotFoundException: try: tricks_table = dd_table.get_jfr_table() except FieldNotFoundException: pass if tricks_table is not None: data['table'] = '' data['table'] += '' for suit in 'shdc': data['table'] += '' data['table'] += '' player_order = [0, 2, 1, 3] # NESW -> NSEW denom_order = list(range(4, -1, -1)) # CDHSN -> NSHDC for player in player_order: data['table'] += '' data['table'] += '' for denom in denom_order: data['table'] += '' data['table'] += '' data['table'] += '
 nt' + self._get_suit_symbol(suit) + '
' + BCalcWrapper.PLAYERS[player] + '' data['table'] += str(tricks_table[player][denom] - 6) if tricks_table[player][denom] > 6 else ' ' data['table'] += '
' par_score = ParScore(pbn_board) par_contract = None try: par_contract = par_score.get_pbn_par_contract() except FieldNotFoundException: try: par_contract = par_score.get_jfr_par_contract() except FieldNotFoundException: pass if par_contract is not None: data['par'] = 'Minimax:
%d%s%s %s %+d
' % ( par_contract.level, 'NT' if par_contract.denomination == 'N' \ else self._get_suit_symbol(par_contract.denomination.lower()), 'x' if par_contract.doubled else '', par_contract.declarer, par_contract.score) return data def _get_table_directions(self): return '' + \ '' + \ '' + \ '' + \ '
 N 
W E
 S 
' def get_html_content(self, dealset): deal_rows = [] event_name = dealset[0].event event_name = event_name.decode(chardet.detect(event_name)['encoding']) while len(dealset) > 0: deal_rows.append(dealset[0:self.deals_per_column]) dealset = dealset[self.deals_per_column:] table_content = '' for row in deal_rows: table_content += '' for deal in row: table_content += '' table_content += '
' deal_cells = [ ['', '', '', ''], ['', '', '', ''], ['', '', '', ''], ] hands = self._get_html_hands(deal) dd_data = self._get_dd_data(deal) deal_cells[0][1] = '
'.join(hands[0]) deal_cells[0][3] = self._get_deal_header(deal) deal_cells[1][0] = '
'.join(hands[3]) deal_cells[1][1] = self._get_table_directions() deal_cells[1][2] = '
'.join(hands[1]) deal_cells[2][0] = dd_data['par'] deal_cells[2][1] = '
'.join(hands[2]) deal_cells[2][3] = dd_data['table'] deal_content = '' for deal_row in deal_cells: deal_content += '' for idx, deal_cell in enumerate(deal_row): deal_content += '' deal_content += '' deal_content += '
' % [30, 20, 20, 30][idx] deal_content += deal_cell deal_content += '
' table_content += deal_content table_content += '
' return _page_template % ( self.deals_per_column, event_name, table_content ) def output_content(self, out_file, dealset): out_file.write(self.get_html_content(dealset).encode('utf-8'))