diff options
author | emkael <emkael@tlen.pl> | 2019-05-23 01:04:04 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2019-05-23 01:04:04 +0200 |
commit | 3980223e145e2664fbe0e4b199a2172154dec76e (patch) | |
tree | 6a59214084d62188ac6f0f1da7f63f396a8151ca | |
parent | 5a6daf4d1498c3a35d99db37d1823c8128b95db7 (diff) |
Factoring out RZD parse/dump methods for single deal
-rw-r--r-- | dealconvert/formats/rzd.py | 54 |
1 files changed, 33 insertions, 21 deletions
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)) |