diff options
author | emkael <emkael@tlen.pl> | 2019-07-18 02:49:53 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2019-07-18 02:50:55 +0200 |
commit | 196c5b1ea6a7370ed6fb3f3e4b405a9d0dcee551 (patch) | |
tree | 18e6f2cd7fb688ec1c28d8cb3221902da942e451 | |
parent | 06b85057c1dd79097a874143dfdf56d5e0d1ea98 (diff) |
Double-dummy analysis for PBN files
-rw-r--r-- | deal-converter.py | 4 | ||||
-rw-r--r-- | dealconvert/__init__.py | 4 | ||||
-rw-r--r-- | dealconvert/formats/__init__.py | 4 | ||||
-rw-r--r-- | dealconvert/formats/cds.py | 2 | ||||
-rw-r--r-- | dealconvert/formats/dup.py | 2 | ||||
-rw-r--r-- | dealconvert/formats/pbn.py | 31 | ||||
-rw-r--r-- | http/api/api.py | 7 | ||||
-rw-r--r-- | 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 <code>LIN</code>. Jeśli chcesz obsługi jakiegoś innego formatu, pisz jak wyżej. </dd> <dt>Czemu w pliku PBN nie ma analizy w widne?</dt> - <dd>Nie ma, pracuję nad tym. Możesz ją sobie łatwo (i szybko!) wykonać przy pomocy <a href="https://github.com/emkael/bcdd/">BCDD</a>.</dd> + <dd> + <s>Nie ma, pracuję nad tym. Możesz ją sobie łatwo (i szybko!) wykonać przy pomocy <a href="https://github.com/emkael/bcdd/">BCDD</a>.</s><br /> + Jak to "nie ma"? Może masz popsuty plik. + </dd> <dt>Czemu dostaję mnóstwo ostrzeżeń <kbd>.xxx file format assumes consequent deal numbers from 1</kbd>?</dt> <dd>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.</dd> <dt>Czemu mój PBN nie chce się otworzyć w BigDealu?</dt> |