From 3980223e145e2664fbe0e4b199a2172154dec76e Mon Sep 17 00:00:00 2001 From: emkael Date: Thu, 23 May 2019 01:04:04 +0200 Subject: Factoring out RZD parse/dump methods for single deal --- dealconvert/formats/rzd.py | 54 ++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 21 deletions(-) (limited to 'dealconvert/formats/rzd.py') diff --git a/dealconvert/formats/rzd.py b/dealconvert/formats/rzd.py index 55dade2..d400eae 100644 --- a/dealconvert/formats/rzd.py +++ b/dealconvert/formats/rzd.py @@ -10,7 +10,17 @@ class RZDFormat(DealFormat): def suffix(self): return '.rzd' + def parse_deal(self, data): + 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]) + byte /= 4 + return deal.hands + def parse_content(self, content): + print self.number_warning dealset = [] header = None number = 1 @@ -27,34 +37,36 @@ class RZDFormat(DealFormat): deal.number = number deal.dealer = deal.get_dealer(number) deal.vulnerable = deal.get_vulnerability(number) - 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]) - byte /= 4 + deal.hands = self.parse_deal(data) dealset.append(deal) number += 1 return dealset + def dump_deal(self, deal): + value = '' + values = [None] * 52 + for i, hand in enumerate(deal.hands): + for suit, cards in enumerate(hand): + for card in cards: + try: + idx = self.cards.index(card) + except ValueError: + print 'ERROR: invalid card: %s' % (card) + sys.exit() + values[idx*4+suit] = i + for i in range(0, 13): + byte = 0 + for j in range(0, 4): + byte *= 4 + byte += values[4*i+j] + value += chr(byte) + return value + def output_content(self, out_file, dealset): + print self.number_warning board_count = len(dealset) out_file.write(chr(board_count%256)) out_file.write(chr(board_count/256)) out_file.write(' '*11) - values = [None] * 52 for deal in dealset: - for i, hand in enumerate(deal.hands): - for suit, cards in enumerate(hand): - for card in cards: - try: - idx = self.cards.index(card) - except ValueError: - print 'ERROR: invalid card: %s' % (card) - sys.exit() - values[idx*4+suit] = i - for i in range(0, 13): - byte = 0 - for j in range(0, 4): - byte *= 4 - byte += values[4*i+j] - out_file.write(chr(byte)) + out_file.write(self.dump_deal(deal)) -- cgit v1.2.3