Verified Commit bacb1018 authored by Jakob Moser's avatar Jakob Moser
Browse files

Add debug statements to Director

parent 5c519643
Loading
Loading
Loading
Loading
+24 −2
Original line number Diff line number Diff line
@@ -40,13 +40,16 @@ class Director:
        self.nothing_requested()

    def card_placed(self, card_id: int) -> None:
        logging.debug("Card placed")
        card = Card.get_only(card_id)

        if not card:
            loging.debug("Card is not in database, presenting failure screen")
            self._last_card_presentation_failed = True
            self.play.PaymentFailure(self).present()
            return

        logging.debug("Card is in database")
        self._last_placed_card = card
        self._last_card_presentation_failed = False
        self._last_card_placed_at = monotonic()
@@ -59,7 +62,10 @@ class Director:
            case self.play.PaymentSuccess:
                self._do_payment(card)

        logging.debug("Card handling done")

    def card_removed(self) -> None:
        logging.debug("Card removed")
        self._last_card_removed_at = monotonic()
        self._ensure_minimum_presentation_time()
        if self._next_scene_on_card_placed is self.play.Balance and self._last_card_presentation_failed:
@@ -71,6 +77,7 @@ class Director:
            self.nothing_requested()

    def screen_touched(self) -> None:
        logging.debug("Screen touched")
        # TODO Extend this to take x: int, y: int parameters
        # TODO Maybe also add a screen_touch_released method etc.
        if self._next_scene_on_card_placed is not self.play.Balance:
@@ -80,7 +87,6 @@ class Director:
        """
        Ensure that the presentation of a "card placed" scene lasts for at least a certain time.
        """

        if self._last_card_removed_at is None or self._last_card_placed_at is None:
            assert False, "Card removal and placement timestamps should be set"
            return
@@ -91,22 +97,33 @@ class Director:
        )
        # This is how long we want to present the scene
        minimum_presentation_time = 3
        # This is how long we still have to present the scene
        sleep_time = max(0, minimum_presentation_time - elapsed_presentation_time)

        sleep(max(0, minimum_presentation_time - elapsed_presentation_time))
        logging.debug(f"Ensuring minimum presentation time. Elapsed: {elapsed}s, Minimum: {minimum_presentation_time}s => Sleep time: {sleep_time}s")
        sleep(sleep_time)
        logging.debug("Ensured minium presentation time.")

    def nothing_requested(self) -> None:
        logging.debug("Nothing requested, presenting Idle scene")
        self._next_scene_on_card_placed = self.play.PaymentSuccess
        self.play.Idle(self, price_cents=DRINK).present()
        logging.debug("Presented Idle scene")

    def reversal_requested(self) -> None:
        logging.debug("Reversal requested, presenting WaitForCard scene")
        self._next_scene_on_card_placed = self.play.Reversal
        self.play.WaitForCard(self).present()
        logging.debug("Presented WaitForCard scene")

    def balance_requested(self) -> None:
        logging.debug("Balance requested, presenting WaitForCard scene")
        self._next_scene_on_card_placed = self.play.Balance
        self.play.WaitForCard(self).present()
        logging.debug("Presented WaitForCard scene")

    def _do_payment(self, card: Card) -> None:
        logging.debug("Performing payment, presenting PaymentSuccess scene")
        # We just pretend that this is certainly atomic and there is no way that we will ever have a race condition here
        old_balance_cents = card.owner.balance_cents
        card.owner.pay(DRINK)
@@ -121,8 +138,10 @@ class Director:
            new_balance_cents=new_balance_cents,
            person_name=card.owner.name,
        ).present()
        logging.debug("Presented PaymentSuccess scene")

    def _do_reversal(self, card: Card) -> None:
        logging.debug("Performing reversal, presenting PaymentSuccess scene")
        # We just pretend that this is certainly atomic and there is no way that we will ever have a race condition here
        old_balance_cents = card.owner.balance_cents
        card.owner.pay(-DRINK)
@@ -137,10 +156,13 @@ class Director:
            new_balance_cents=new_balance_cents,
            person_name=card.owner.name,
        ).present()
        logging.debug("Presented Reversal scene")

    def _do_balance(self, card: Card) -> None:
        logging.debug("Presenting Balance scene")
        self.play.Balance(
            self,
            balance_cents=card.owner.balance_cents,
            person_name=card.owner.name,
        ).present()
        logging.debug("Presented Balance scene")