summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2019-05-23 00:53:01 +0200
committeremkael <emkael@tlen.pl>2019-05-23 00:53:01 +0200
commitbec85ffa0867b7a30e55294c230de890496e593e (patch)
tree37a77d5f335ab3d413b62451566f132846e84aa5
parentd661ad40b01fb219e391fce2935d1e55a4a5b3c9 (diff)
RZD format support
-rw-r--r--dealconvert/formats/rzd.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/dealconvert/formats/rzd.py b/dealconvert/formats/rzd.py
index e663500..55dade2 100644
--- a/dealconvert/formats/rzd.py
+++ b/dealconvert/formats/rzd.py
@@ -1,6 +1,60 @@
+import sys
+
from . import DealFormat
+from .. import dto
class RZDFormat(DealFormat):
+ number_warning = 'WARNING: .rzd file format assumes consequent deal numbers from 1'
+
@property
def suffix(self):
return '.rzd'
+
+ def parse_content(self, content):
+ dealset = []
+ header = None
+ number = 1
+ while True:
+ data = content.read(13)
+ if len(data) < 13:
+ if len(data) != 0:
+ print 'WARNING: .rzd data truncated: %s' % (data)
+ break
+ if header is None:
+ header = data
+ continue
+ deal = dto.Deal()
+ 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
+ dealset.append(deal)
+ number += 1
+ return dealset
+
+ def output_content(self, out_file, dealset):
+ 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))