From 196c5b1ea6a7370ed6fb3f3e4b405a9d0dcee551 Mon Sep 17 00:00:00 2001 From: emkael Date: Thu, 18 Jul 2019 02:49:53 +0200 Subject: Double-dummy analysis for PBN files --- deal-converter.py | 4 +++- dealconvert/__init__.py | 4 ++-- dealconvert/formats/__init__.py | 4 ++++ dealconvert/formats/cds.py | 2 +- dealconvert/formats/dup.py | 2 +- dealconvert/formats/pbn.py | 31 +++++++++++++++++++++++-------- http/api/api.py | 7 +++++-- http/index.html | 5 ++++- 8 files changed, 43 insertions(+), 16 deletions(-) diff --git a/deal-converter.py b/deal-converter.py index e56c33a..3491b47 100644 --- a/deal-converter.py +++ b/deal-converter.py @@ -1,5 +1,7 @@ from __future__ import print_function -import argparse, sys, warnings +import argparse, os, sys, warnings + +sys.path.append(os.path.realpath(os.path.join('.', 'pybcdd'))) from dealconvert import DealConverter diff --git a/dealconvert/__init__.py b/dealconvert/__init__.py index 56d28da..e25db66 100644 --- a/dealconvert/__init__.py +++ b/dealconvert/__init__.py @@ -12,12 +12,12 @@ class DealConverter(object): for output in output_files: self.detect_format(output).output(output, deal_set) - def detect_format(self, filename): + def detect_format(self, filename, interactive=True): for deal_format in globals()['formats'].__all__: if deal_format not in self.formats: self.formats[deal_format] = getattr( globals()[deal_format], - deal_format.upper() + 'Format')() + deal_format.upper() + 'Format')(interactive) if self.formats[deal_format].match_file(filename): return self.formats[deal_format] raise RuntimeError('Unrecognized file extension: %s' % filename) diff --git a/dealconvert/formats/__init__.py b/dealconvert/formats/__init__.py index 9c37cf9..cdd8703 100644 --- a/dealconvert/formats/__init__.py +++ b/dealconvert/formats/__init__.py @@ -3,6 +3,10 @@ import glob class DealFormat(object): cards = 'AKQJT98765432' + + def __init__(self, interactive=True): + self.interactive = interactive + def parse(self, input_file): with open(input_file, 'rb') as content: return self.parse_content(content) diff --git a/dealconvert/formats/cds.py b/dealconvert/formats/cds.py index f624612..c7eeb5a 100644 --- a/dealconvert/formats/cds.py +++ b/dealconvert/formats/cds.py @@ -9,7 +9,7 @@ class CDSFormat(DealFormat): def suffix(self): return '.cds' - def __init__(self): + def __init__(self, interactive=True): self.rzd_format = RZDFormat() def parse_content(self, content): diff --git a/dealconvert/formats/dup.py b/dealconvert/formats/dup.py index 1a2e1ba..19dca0f 100644 --- a/dealconvert/formats/dup.py +++ b/dealconvert/formats/dup.py @@ -10,7 +10,7 @@ class DUPFormat(DealFormat): def suffix(self): return '.dup' - def __init__(self): + def __init__(self, interactive=True): self.bri = BRIFormat() self.dge = DGEFormat() diff --git a/dealconvert/formats/pbn.py b/dealconvert/formats/pbn.py index ead8fdd..bc52462 100644 --- a/dealconvert/formats/pbn.py +++ b/dealconvert/formats/pbn.py @@ -1,4 +1,8 @@ -import re +import re, sys, warnings + +from bcdd.PBNBoard import PBNBoard +from bcdd.DDTable import DDTable +from bcdd.ParScore import ParScore from . import DealFormat from .. import dto @@ -115,21 +119,32 @@ class PBNFormat(DealFormat): def output_content(self, out_file, dealset): for board in dealset: - out_file.write('[Event "%s"]\r\n' % (board.event)) - out_file.write('[Board "%d"]\r\n' % (board.number)) - out_file.write('[Dealer "%s"]\r\n' % ( + lines = [] + lines.append('[Event "%s"]' % (board.event)) + lines.append('[Board "%d"]' % (board.number)) + lines.append('[Dealer "%s"]' % ( 'NESW'[board.dealer] )) - out_file.write('[Vulnerable "%s"]\r\n' % ( + lines.append('[Vulnerable "%s"]' % ( ('All' if board.vulnerable['EW'] else 'NS') if board.vulnerable['NS'] else ('EW' if board.vulnerable['EW'] else 'None') )) - out_file.write('[Deal "N:%s"]\r\n' % ( + lines.append('[Deal "N:%s"]' % ( ' '.join([ '.'.join([''.join(suit) for suit in hand]) for hand in board.hands ]))) for field in board.extra_fields: - out_file.write(field + '\r\n') - out_file.write('\r\n') + lines.append(field) + try: + dd_board = PBNBoard(lines) + dd_table = DDTable(dd_board).get_dd_table(self.interactive) + dd_contract = ParScore(dd_board).get_par_contract(dd_table) + dd_board.save_dd_table(dd_table) + dd_board.save_par_contract(dd_contract) + lines = [field.raw_field for field in dd_board.fields] + except Exception as e: + warnings.warn('unable to determine double-dummy data: %s' % e) + for line in lines + ['']: + out_file.write(line + '\r\n') diff --git a/http/api/api.py b/http/api/api.py index be2635f..6a9a785 100644 --- a/http/api/api.py +++ b/http/api/api.py @@ -6,7 +6,10 @@ from StringIO import StringIO from mod_python import apache, Session OLDPATH = copy.copy(sys.path) -sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) +sys.path.append( + os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) +sys.path.append( + os.path.abspath(os.path.join(os.path.dirname(__file__), '../../pybcdd'))) from dealconvert import DealConverter sys.path = OLDPATH @@ -95,7 +98,7 @@ def handle_upload(response, request): try: output_name = '.'.join(params['name'].split('.')[:-1] + [output_type]) output_return['name'] = output_name - output = converter.detect_format(output_name) + output = converter.detect_format(output_name, False) output_id, output_path = _get_file_id() token = _get_rand_string(16) output_buffer = StringIO() diff --git a/http/index.html b/http/index.html index 9f40cf1..743fc25 100644 --- a/http/index.html +++ b/http/index.html @@ -36,7 +36,10 @@ W przyszłości rozważane jest wsparcie dla formatu LIN. Jeśli chcesz obsługi jakiegoś innego formatu, pisz jak wyżej.
Czemu w pliku PBN nie ma analizy w widne?
-
Nie ma, pracuję nad tym. Możesz ją sobie łatwo (i szybko!) wykonać przy pomocy BCDD.
+
+ Nie ma, pracuję nad tym. Możesz ją sobie łatwo (i szybko!) wykonać przy pomocy BCDD.
+ Jak to "nie ma"? Może masz popsuty plik. +
Czemu dostaję mnóstwo ostrzeżeń .xxx file format assumes consequent deal numbers from 1?
Niektóre formaty nie przechowują numerów rozdań. W takich sytuacjach, zarówno przy imporcie, jak i eksporcie, konwerter zakłada, że rozdania mają kolejne numery, od 1. Może to doprowadzić do zmiany numeracji rozdań, jeśli oryginalny plik nie zaczynał się od rozdania nr 1 albo numeracja zawierała dziury.
Czemu mój PBN nie chce się otworzyć w BigDealu?
-- cgit v1.2.3