Esempio n. 1
0
def testSerializeCards():
    a = Bidding()
    a.nrOfPoints = 21
    i = 3
    for color in colors:
        a.nrOfCards[color] = i
        i = i + 1

    result = bid2Json(a)
    assert result == '{"__type__": "Bidding", "current": [], "whoStarts": "N", "nrOfPoints": 21, "nrOfCards": {"♣": 3, "♦": 4, "♥": 5, "♠": 6}}'
 def bidBuilder(dictionary: dict) -> Bidding:
     if "__type__" in dictionary and dictionary["__type__"] == "Bidding":
         result = Bidding()
         result.current = dictionary["current"]
         result.whoStarts = dictionary["whoStarts"]
         result.nrOfPoints = dictionary["nrOfPoints"]
         result.nrOfCards = dictionary["nrOfCards"]
         return result
     else:
         return dictionary
Esempio n. 3
0
def testEncodesEmptyBidding():
    a = Bidding()
    enc = BiddingEncoder()
    obj = enc.default(a)
    assert obj == {"__type__": "Bidding", "current": [], "whoStarts": 'N', "nrOfPoints": None, "nrOfCards": {}}
Esempio n. 4
0
def testSerializesBids():
    a = Bidding()
    a.current = ['pass', 'X', '1♣']
    result = bid2Json(a)
    assert result == '{"__type__": "Bidding", "current": ["pass", "X", "1♣"], "whoStarts": "N", "nrOfPoints": null, "nrOfCards": {}}'
Esempio n. 5
0
def testSerializesEmptyBidding():
    a = Bidding()
    result = bid2Json(a)
    assert result == '{"__type__": "Bidding", "current": [], "whoStarts": "N", "nrOfPoints": null, "nrOfCards": {}}'
Esempio n. 6
0
    def build(self):
        newBidBtn = buildButton('Nieuwe bieding',
                                lambda i: self.mediator.editBidding(Bidding()),
                                size_hint=(1.0, 0.1))

        encloseLyt = BoxLayout(size_hint=(1.0, 0.85))
        scrlView = ScrollView(size_hint=(1.0, None),
                              scroll_type=['content', 'bars'],
                              bar_width='10dp')
        encloseLyt.bind(size=scrlView.setter('size'))
        listLyt = GridLayout(cols=1, spacing=10, size_hint_y=None)
        listLyt.bind(minimum_height=listLyt.setter('height'))
        keys = self.mediator.getBiddingKeys()
        for (key, name,
             contract) in reversed(keys):  # ensure most recent is at the top

            def createCallbackSelect(key):
                def cb(instance):
                    self.mediator.loadBidding(key)

                return cb

            def createCallbackDelete(key):
                def cb(instance):
                    self.mediator.deleteBidding(key)

                return cb

            def createCallbackEdit(key, name):
                def cb(instance):
                    dad = instance.parent.parent  # GridLayout -> BoxLayout
                    dad.clear_widgets()

                    def setNameCallback(instance):
                        self.mediator.changeBiddingName(key, instance.text)

                    input = buildTextInput(setNameCallback,
                                           size_hint=(0.8, 1.0))
                    input.text = name
                    dad.add_widget(input)
                    dad.add_widget(
                        buildButton('Klaar',
                                    lambda i: setNameCallback(input),
                                    size_hint=(0.2, 1.0)))

                return cb

            itemLyt = BoxLayout(orientation='horizontal',
                                size_hint=(1.0, None))

            btns = GridLayout(rows=1,
                              spacing=[gap, 0],
                              padding=[gap, 0],
                              size_hint=(0.2, 1.0))
            btns.add_widget(
                buildIconButton(iconPencil, createCallbackEdit(key, name)))
            btns.add_widget(
                buildIconButton(iconTrashcan, createCallbackDelete(key)))

            itemNameLyt = AnchorLayout()
            itemNameLyt.add_widget(buildMultilineLabel(name))
            itemLyt.add_widget(
                buildButton(itemNameLyt,
                            createCallbackSelect(key),
                            size_hint=(0.7, 1.0)))
            itemLyt.add_widget(buildText(contract, size_hint=(0.1, 1.0)))
            itemLyt.add_widget(btns)

            newBidBtn.bind(height=itemLyt.setter('height'))

            listLyt.add_widget(itemLyt)

        scrlView.add_widget(listLyt)
        encloseLyt.add_widget(scrlView)
        self.rootLayout.add_widget(encloseLyt)
        self.rootLayout.add_widget(BoxLayout(size_hint=(1.0, 0.05)))
        self.rootLayout.add_widget(newBidBtn)
Esempio n. 7
0
def testIsDblAllowed():
    bidding = Bidding()

    bidding.current = [ ]
    assert not bidding.isAllowed('X')

    bidding.current = [ '1♣', '2♦' ]
    assert bidding.isAllowed('X')

    bidding.current = [ '1♣', '2♦', 'pass' ]
    assert not bidding.isAllowed('X')

    bidding.current = [ '1♣', '2♦', 'X' ]
    assert not bidding.isAllowed('X')

    bidding.current = [ '1♣', '2♦', 'pass', 'pass' ]
    assert bidding.isAllowed('X')

    bidding.current = ['1♣', 'X']
    assert not bidding.isAllowed('X')
Esempio n. 8
0
def testRdblnoDbl():
    bidding = Bidding()

    bidding.current = ['1♣']
    assert not bidding.isAllowed('XX')
Esempio n. 9
0
def testNoDblOnPartner():
    bidding = Bidding()

    bidding.current = ['X', 'X', 'pass']

    assert not bidding.isAllowed('X')
Esempio n. 10
0
def testIsRdblAllowed():
    bidding = Bidding()

    bidding.current = ["1♣", 'X']
    assert bidding.isAllowed('XX')
Esempio n. 11
0
def testDoubleRdbl():
    bidding = Bidding()

    bidding.current = ["1♣", 'XX']
    assert not bidding.isAllowed('XX')
Esempio n. 12
0
def testIsAllowed():
    bidding = Bidding()
    assert bidding.isAllowed('pass')
def testLeaderN():
    bidding = Bidding()
    bidding.current = ['1♥', 'pass', '1SA', 'pass', 'pass', 'pass']
    bidding.whoStarts = north

    assert bidding.contract() == ('1SA', south)
def testLeaderW():
    bidding = Bidding()
    bidding.current = ['1♥', 'pass', '1SA', 'pass', '2♦', 'pass', 'pass', '2♥', 'pass', 'pass', '3♣', 'pass', 'pass', 'pass']
    bidding.whoStarts = east

    assert bidding.contract() == ('3♣', west)
Esempio n. 15
0
 def editBidding(self, bidding: Bidding):
     self.setBidding(bidding)
     if (bidding.needsSpecification()):
         self.showSpecification()
     else:
         self.showBidding()
Esempio n. 16
0
class Mediator():
    bidding: Bidding = Bidding()
    advice: str = None

    __currentBiddingKey: str = None
    __storage = JsonStore('allbids.json')
    __patternExplanation = re.compile('\n[\t ]+')
    __switchTo: Callable[[str], None]

    def __init__(self, switchTo):
        self.__switchTo = switchTo

    def getBiddingKeys(self):
        keys = self.__storage.keys()
        return [(key, self.__storage.get(key)["name"],
                 self.__storage.get(key)["contract"]) for key in keys]

    def setBidding(self, bidding: Bidding):
        self.bidding = bidding

    def showAdvice(self):
        cards = self.bidding.nrOfCards
        print("('autogenerated', %s, %d, %d, %d, %d, %d, '???', '???')" %
              (str(self.bidding.current), self.bidding.nrOfPoints,
               cards[spades], cards[hearts], cards[diamonds], cards[clubs]))
        (bid, explainTag) = bids(list(self.bidding.current),
                                 self.bidding.nrOfPoints, cards[spades],
                                 cards[hearts], cards[diamonds], cards[clubs])
        explanation = uitleg_nl(explainTag).replace("\t", "")
        cleaned = re.sub(self.__patternExplanation, '\n\n', explanation)
        self.advice = (bid, cleaned)

        self.__switchTo(adviceScreen)

    def closeAdvice(self):
        self.showBidding()

    def editBidding(self, bidding: Bidding):
        self.setBidding(bidding)
        if (bidding.needsSpecification()):
            self.showSpecification()
        else:
            self.showBidding()

    def closeBidding(self):
        if self.__currentBiddingKey != None:
            key = uuid4().hex
        else:
            key = self.__currentBiddingKey
        name = "Bidding at " + datetime.now().strftime("%c")
        value = bid2Json(self.bidding)
        (contract, leader) = self.bidding.contract()
        fileContract = "AP" if contract == "AP" else contract + " " + leader
        self.__storage.put(key, name=name, bid=value, contract=fileContract)
        self.__switchTo(fileChooserScreen)

    def loadBidding(self, key):
        bidding = json2Bid(self.__storage.get(key)['bid'])
        self.__currentBiddingKey = key
        self.editBidding(bidding)

    def deleteBidding(self, key):
        self.__storage.delete(key)
        self.showBiddingChooser()

    def changeBiddingName(self, key, value):
        self.__storage.put(key,
                           bid=self.__storage.get(key)['bid'],
                           contract=self.__storage.get(key)['contract'],
                           name=value)
        self.showBiddingChooser()

    def closeSpecification(self):
        if (not self.bidding.needsSpecification()):
            self.showBidding()

    def showSpecification(self):
        self.__switchTo(specificationScreen)

    def showBidding(self):
        self.__switchTo(biddingScreen)

    def showBiddingChooser(self):
        self.__currentBiddingKey = None
        self.__switchTo(fileChooserScreen)

    def showCredits(self):
        self.__switchTo(creditsScreen)