From 0e6fa8cb43d147ce6437c920eab1b76337c34ade Mon Sep 17 00:00:00 2001 From: emkael Date: Thu, 23 May 2019 01:12:47 +0200 Subject: CDS format based on RZD --- dealconvert/formats/cds.py | 27 +++++++++++++++++++++++++++ dealconvert/formats/rzd.py | 8 ++++---- 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'dealconvert/formats') diff --git a/dealconvert/formats/cds.py b/dealconvert/formats/cds.py index 637e18f..4c2a15a 100644 --- a/dealconvert/formats/cds.py +++ b/dealconvert/formats/cds.py @@ -1,6 +1,33 @@ from . import DealFormat +from .rzd import RZDFormat +from .. import dto class CDSFormat(DealFormat): @property def suffix(self): return '.cds' + + def __init__(self): + self.rzd_format = RZDFormat() + + def parse_content(self, content): + dealset = [] + while True: + data = content.read(14) + if len(data) < 14: + if len(data) != 0: + print 'WARNING: .cds data truncated: %s' % (data) + break + deal = dto.Deal() + deal.number = ord(data[0]) + deal.dealer = deal.get_dealer(deal.number) + deal.vulnerable = deal.get_vulnerability(deal.number) + deal.hands = self.rzd_format.parse_deal(data[1:], offset=1) + dealset.append(deal) + return dealset + + + def output_content(self, out_file, dealset): + for deal in dealset: + out_file.write(chr(deal.number)) + out_file.write(self.rzd_format.dump_deal(deal, offset=1)) diff --git a/dealconvert/formats/rzd.py b/dealconvert/formats/rzd.py index d400eae..d6cbc5e 100644 --- a/dealconvert/formats/rzd.py +++ b/dealconvert/formats/rzd.py @@ -10,12 +10,12 @@ class RZDFormat(DealFormat): def suffix(self): return '.rzd' - def parse_deal(self, data): + def parse_deal(self, data, offset=0): deal = dto.Deal() for card, byte in enumerate(data): byte = ord(byte) for suit in range(3, -1, -1): - deal.hands[byte%4][suit].append(self.cards[card]) + deal.hands[(byte%4 - offset)%4][suit].append(self.cards[card]) byte /= 4 return deal.hands @@ -42,7 +42,7 @@ class RZDFormat(DealFormat): number += 1 return dealset - def dump_deal(self, deal): + def dump_deal(self, deal, offset=0): value = '' values = [None] * 52 for i, hand in enumerate(deal.hands): @@ -53,7 +53,7 @@ class RZDFormat(DealFormat): except ValueError: print 'ERROR: invalid card: %s' % (card) sys.exit() - values[idx*4+suit] = i + values[idx*4+suit] = (i + offset)%4 for i in range(0, 13): byte = 0 for j in range(0, 4): -- cgit v1.2.3