bridgehandscanner / hand_record.py
vincentlui's picture
hand record
6ee449a
raw
history blame
No virus
3.6 kB
from fpdf import FPDF
import tempfile
import os
import bridgebots
from pbn_util import merge_pbn, parse_pbn
DEALER_LIST = ['N', 'E', 'S', 'W']
VULNERABILITY_LIST = ["None","NS","EW","All","NS","EW","All","None","EW","All","None","NS","All","None","NS","EW"]
SUITS = [bridgebots.Suit.SPADES, bridgebots.Suit.HEARTS, bridgebots.Suit.DIAMONDS, bridgebots.Suit.CLUBS]
SUIT_SYMBOLS = ['♠','♥','♦','♣']
def create_hand_record_pdf(pbn_paths):
filepath_merged_pbn = merge_pbn(pbn_paths)
results = parse_pbn(filepath_merged_pbn)
fd,fn = tempfile.mkstemp(".pdf")
pdf = FPDF()
pdf.add_page()
pdf.add_font('times2', style='', fname='times.ttf')
pdf.set_font("times2", "", 8)
pdf.c_margin = 0.1
table_config = {
'borders_layout':'NONE',
'col_widths':3,
'line_height':pdf.font_size + 0.5,
'align': 'L',
'text_align': 'L',
'first_row_as_headings': False,
}
start_x,start_y = 10,10
table_size = 45, 48
table_margin = 2
for i, result in enumerate(results):
deal = result[0]
board_no = int(result[1][0]['Board'])
page_i = i % 20
if (i % 20 == 0) and (i != 0):
pdf.add_page()
row_idx = page_i // 4
col_idx = page_i % 4
x = start_x + (table_size[0] + 2 * table_margin + 1) * col_idx
y = start_y + (table_size[1] + 2 * table_margin + 1) * row_idx
top_left = x - table_margin, y - table_margin
top_right = x + table_size[0] + table_margin, y - table_margin
bottom_left = x - table_margin, y + table_size[1] + table_margin
bottom_right = x + table_size[0] + table_margin, y + table_size[1] + table_margin
pdf.set_xy(x,y)
pdf.line(*top_left, *bottom_left)
pdf.line(*top_left, * top_right)
pdf.line(*top_right, *bottom_right)
pdf.line(*bottom_left, *bottom_right)
dealer = DEALER_LIST[(board_no-1) % 4]
vul = VULNERABILITY_LIST[(board_no-1) % 16]
with pdf.table(**table_config) as table:
row = table.row()
row.cell(f'{board_no}\n{dealer}/{vul}', colspan=6, rowspan=4, align='C', v_align='C')
for i, (suit, values) in enumerate(zip(SUIT_SYMBOLS, get_values(deal, 'N'))):
if i!=0:
row = table.row()
print_suit_values(pdf,row,suit,values)
row = table.row()
# row = table.row()
for i, (suit, values1, values2) in enumerate(zip(SUIT_SYMBOLS, get_values(deal, 'W'), get_values(deal, 'E'))):
row = table.row()
print_suit_values(pdf, row, suit, values1)
row.cell('',colspan=5)
print_suit_values(pdf, row, suit, values2)
row = table.row()
row = table.row()
row.cell('', colspan=6, rowspan=4)
for i, (suit, values) in enumerate(zip(SUIT_SYMBOLS, get_values(deal, 'S'))):
if i!=0:
row = table.row()
print_suit_values(pdf, row, suit, values)
pdf.output(fn)
return fn
def get_values(result, direction):
d = bridgebots.Direction.from_str(direction)
hand = result.hands[d]
return [''.join([value.abbreviation()
for value in hand.suits[suit]])
for suit in SUITS]
def print_suit_values(pdf, row, suit, values):
if (suit=='♥') or (suit=='♦'): # Hearts or Diamonds
pdf.set_text_color(255,0,0)
row.cell(suit, colspan=1)
pdf.set_text_color(0,0,0)
row.cell(values, colspan=4)