summaryrefslogtreecommitdiff
path: root/dealconvert/formats/rzd.py
blob: 8ca1e97434340a4a68aa5a71d8a718cdbabcdccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import warnings

from . import DealFormat
from .. import dto

class RZDFormat(DealFormat):
    number_warning = '.rzd file format assumes consequent deal numbers from 1'

    @property
    def suffix(self):
        return '.rzd'

    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 - offset)%4][suit].append(self.cards[card])
                byte /= 4
        return deal.hands

    def parse_content(self, content):
        warnings.warn(self.number_warning)
        dealset = []
        header = None
        number = 1
        while True:
            data = content.read(13)
            if len(data) < 13:
                if len(data) != 0:
                    warnings.warn('.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)
            deal.hands = self.parse_deal(data)
            dealset.append(deal)
            number += 1
        return dealset

    def dump_deal(self, deal, offset=0):
        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:
                        raise RuntimeError('invalid card: %s' % (card))
                    values[idx*4+suit] = (i + offset)%4
        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):
        warnings.warn(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)
        for deal in dealset:
            out_file.write(self.dump_deal(deal))