Esempio n. 1
0
 def cancel_looping_task(self):
     if self.looping_task is not None:
         logger.debug('Cancelling looping task')
         try:
             self.looping_task.cancel()
         except Exception as e:
             logger.warning("Could not cancel looping task (reason={})".format(e))
Esempio n. 2
0
    def on_disconnect(self, air_duct):
        logger.debug("{} is not connected to any air source".format(air_duct))

        if self.status != "playing":
            logger.debug("Game is not started yet: nothing to do")
            return

        if self.sequence_cursor != -1:  # Game is running
            air_duct.fan.off()
            if self.success_sequence[
                    self.sequence_cursor]["air_duct"] != air_duct.name:
                # It was not necessary to disconnect this air duct: considering as an error
                # TODO: reactivate this rule
                logger.debug(
                    "Should be considered an error but ignoring for now")
                # self.sequence_cursor = -1
                # self.bad_move_failure_animation()
        else:  # Game is not running
            if all([
                    ad.connected_source is None
                    for ad in self.air_ducts.values()
            ]):
                logger.debug("All air ducts are disconnected")
                self.restart_round()
            else:
                logger.debug("Some air ducts remain connected")
                self.display_connected_air_ducts_before_restart()
Esempio n. 3
0
    def onMessage(self, payload, isBinary):
        if isBinary:
            logger.warning(
                "Binary message received ({} bytes): ignoring".format(
                    len(payload)))
            return

        try:
            unicode_message = payload.decode('utf8')
        except UnicodeDecodeError:
            logger.exception("Cannot decode {}: ignoring".format(payload))
            return

        try:
            message = json.loads(unicode_message)
        except json.JSONDecodeError:
            logger.exception(
                "Cannot load {}: ignoring".format(unicode_message))
            return

        ok, warning = self.validate_message(message)
        if not ok:
            logger.info("Received {}".format(message))
            logger.warning(warning)
            logger.info("Ignoring")
        else:
            logger.debug("Received {}".format(message))
            self.log_message(message, to_server=False)
            self.factory.process_event(message[P.EVENT])
Esempio n. 4
0
 def difficulty(self, value):
     if value not in self.difficulties:
         logger.warning("Difficulty {} not in {}: skipping".format(
             value, ", ".join(self.difficulties)))
     else:
         logger.debug("Setting difficulty to {}".format(value))
         self._difficulty = value
Esempio n. 5
0
 def cancel_release_task(self):
     logger.debug('Cancelling release task')
     try:
         self.release_task.cancel()
     except Exception as e:
         logger.warning(
             "Could not cancel release task (reason={})".format(e))
Esempio n. 6
0
 def send_event(self, event):
     message = {
         P.MESSAGE_TYPE: P.MESSAGE_TYPE_EVENT,
         P.EVENT: event,
     }
     logger.debug("Sending message to the server: {}".format(message))
     self.send_json(message)
Esempio n. 7
0
    def send_json(self, dict_):
        unicode_json = json.dumps(dict_, ensure_ascii=False)
        bytes_ = unicode_json.encode("utf8")

        logger.debug("Sending {}".format(dict_))
        self.log_message(dict_, to_server=False)
        self.sendMessage(bytes_)
Esempio n. 8
0
    def status(self, value):
        if value not in self.STATUSES:
            logger.warning("Status {} not in {}: skipping".format(
                value, ", ".join(self.STATUSES)))
            return

        if value == self.status:
            logger.debug("Status is already {}: skipping".format(value))
            return

        logger.debug("Setting status to {}".format(value))
        self._status = value

        if self._status == "inactive":
            self.skip_skippable_animations()

            if self.unskippable_animation_task and self.unskippable_animation_task.active(
            ):
                self.unskippable_animation_task.cancel()
            self.on_inactive()

        elif self._status == "playing":
            self.skip_skippable_animations()

            if self.unskippable_animation_task and self.unskippable_animation_task.active(
            ):
                self.unskippable_animation_task.cancel()
            self.on_playing()

        elif self._status == "success":
            self.on_success()
Esempio n. 9
0
 def _on_chopstick_unplug(self):
     logger.debug("Executing letter {} chopstick unplug reaction".format(
         self.letters.index(self)))
     reaction = self.unplug_reactions.get(self.difficulty,
                                          self.reaction_do_nothing)
     reaction()
     self.check_success()
Esempio n. 10
0
    def _toggle(self, color, activate):
        if activate:
            logger.debug("Activating {}".format(self))
        else:
            logger.debug("Deactivating {}".format(self))

        self.on_toggle(color, activate)
Esempio n. 11
0
    def status(self, value):
        if value not in self.STATUSES:
            logger.warning("Status {} not in {}: skipping".format(
                value, ", ".join(self.STATUSES)))
            return

        if value == self.status:
            logger.debug("Status is already {}: skipping".format(value))
            return

        logger.debug("Setting status to {}".format(value))
        self._status = value

        for task_name, task in self.tasks.items():
            if task.active():
                task.cancel()
        self.tasks = {}

        if self._status == "chaos":
            self.on_chaos()
        elif self._status == "mute":
            self.on_mute()
        elif self._status == "playing":
            self.on_playing()
        elif self._status == "success":
            self.on_success()
Esempio n. 12
0
    def display_connected_air_ducts_before_restart(self):
        logger.debug("Displaying connected air ducts in red before restart")

        for ad_name, ad in self.air_ducts.items():
            if ad.connected_source is None:
                ad.set_color("black")
            else:
                ad.set_color("red")
Esempio n. 13
0
 def new_player(self):
     super(VLCLoopingChapterVideoPlayer, self).new_player()
     logger.debug('Setting time={} because current chapter is {}'.format(
         self.current_chapter['t_start'], self.current_chapter_id))
     # callLater because VLC seems to need some delay
     reactor.callLater(
         0.01, self.player.set_time,
         int(self.current_chapter['t_start'] * 1000))
Esempio n. 14
0
 def send_log(self, level, content):
     message = {
         P.MESSAGE_TYPE: P.MESSAGE_TYPE_LOG,
         P.LOG_LEVEL: level,
         P.LOG_CONTENT: content,
     }
     logger.debug("Sending message to the server: {}".format(message))
     self.send_json(message)
Esempio n. 15
0
 def reaction_toggle_blue_orange(self):
     if self.led_color == "blue":
         self.led_color = "orange"
     elif self.led_color == "orange":
         self.led_color = "blue"
     else:
         logger.debug("Letter {} color is {}: nothing to do".format(
             self.letters.index(self), self.led_color))
Esempio n. 16
0
 def new_player(self):
     super(VLCDynamicSlidesPlayer, self).new_player()
     logger.debug('Setting time={} because current slide is chapter id={}'.format(
         self.current_chapter['start'], self.slides[self.current_slide_index]))
     # callLater because VLC seems to need some delay
     reactor.callLater(
         0.01, self.player.set_time,
         int(self.current_chapter['start'] * 1000))
Esempio n. 17
0
 def send_i_am_node(self):
     message = {
         P.MESSAGE_TYPE: P.MESSAGE_TYPE_I_AM,
         P.I_AM_CLIENT_TYPE: P.CLIENT_NODE,
         P.I_AM_NAME: self.name,
         P.I_AM_CHANNEL: self.channel,
     }
     logger.debug("Declaring as a node")
     self.send_json(message)
Esempio n. 18
0
    def led_color(self, value):
        self._led_color = value
        logger.debug("Letter {}: {}".format(self.letters.index(self), value))

        # Get GRB values of given colors from settings
        red = self.colors.get(value, {}).get("r", 0)
        green = self.colors.get(value, {}).get("g", 0)
        blue = self.colors.get(value, {}).get("b", 0)
        Letter.led_strip[self.led_index] = (red, green, blue)
Esempio n. 19
0
    def cancel_tasks(self):
        logger.debug("Cancelling tasks")
        if self.led_task and self.led_task.active():
            logger.info("Cancelling led task")
            self.led_task.cancel()

        if self.stop_motor_task and self.stop_motor_task.active():
            logger.info("Cancelling stop motor task")
            self.stop_motor_task.cancel()
Esempio n. 20
0
    def on_chopstick_unplug(self):
        logger.debug("Letter {} chopstick unplugged".format(
            self.letters.index(self)))

        if self.delayed_reaction_task and self.delayed_reaction_task.active():
            logger.debug("Canceling previous reaction task")
            self.delayed_reaction_task.cancel()
        else:
            self.delayed_reaction_task = callLater(0.2,
                                                   self._on_chopstick_unplug)
Esempio n. 21
0
    def next_slide(self):
        self.current_slide_index = (self.current_slide_index + 1) % len(self.slides)

        time_before_slide = self.current_chapter['end'] - self.current_chapter['start']
        self.schedule_slide_task(time_before_slide)

        logger.debug('Setting player time to {} because current slide is chapter id={}'.format(
            self.current_chapter['start'], self.slides[self.current_slide_index]))
        self.player.set_time(int(self.current_chapter['start']) * 1000)
        self.service.notify_slide(self.current_slide_index)
Esempio n. 22
0
 def set_chapter(self, chapter_id):
     self.current_chapter_id = chapter_id
     self.cancel_looping_task()
     if self.player is not None:
         logger.debug('Setting time={} because current chapter is {}'.format(
             self.current_chapter['t_start'], self.current_chapter_id))
         self.player.set_time(int(self.current_chapter['t_start'] * 1000))
         if self.current_state == MediaPlayerMixin.STATE_PLAYING:
             time_before_loop = self.current_chapter['loop_b'] - self.current_chapter['t_start']
             self.schedule_looping_task(time_before_loop)
Esempio n. 23
0
    def new_success_sequence(self):
        logger.debug("Loading a new success sequence (try counter={})".format(
            self.try_counters[self.round]))

        round_sequences = self.difficulties[self.difficulty][self.round]
        sequence_index = (self.try_counters[self.round] -
                          1) % len(round_sequences)

        self.success_sequence = round_sequences[sequence_index]

        logger.info("The new success sequence is {}".format(
            self.success_sequence))
Esempio n. 24
0
 def set_slide(self, slide_index, chapter_id):
     self.current_slide_index = slide_index
     self.slides[self.current_slide_index] = chapter_id
     self.cancel_slide_task()
     if self.player is not None:
         logger.debug('Setting time={} because current slide is {}'.format(
             self.current_chapter['start'], self.slides[self.current_slide_index]))
         self.player.set_time(int(self.current_chapter['start'] * 1000))
         self.service.notify_slide(slide_index)
         if self.current_state == MediaPlayerMixin.STATE_PLAYING:
             time_before_slide = self.current_chapter['end'] - self.current_chapter['start']
             self.schedule_slide_task(time_before_slide)
Esempio n. 25
0
    def bad_move_failure_animation(self):
        logger.debug("Running bad move failure animation")

        self.skip_skippable_animations()

        for ad in self.air_ducts.values():
            ad.fan.off()

        def wait_and_display_connected_air_ducts_before_restart():
            self.animation_tasks["wait"] = callLater(
                0.3, self.display_connected_air_ducts_before_restart)

        self.animation_tasks["blink1"] = callLater(
            0,
            self.fluid_to_color,
            self.air_ducts["ad2"].led_index,
            "red",
            0.35,
            "blink1",
            self.fluid_to_color,
            self.air_ducts["ad2"].led_index,
            "black",
            0.35,
            "blink1",
        )
        self.animation_tasks["blink2"] = callLater(
            0.3,
            self.fluid_to_color,
            self.air_ducts["ad1"].led_index,
            "red",
            0.35,
            "blink2",
            self.fluid_to_color,
            self.air_ducts["ad1"].led_index,
            "black",
            0.35,
            "blink2",
        )
        self.animation_tasks["blink3"] = callLater(
            0.6,
            self.fluid_to_color,
            self.air_ducts["ad0"].led_index,
            "red",
            0.35,
            "blink3",
            self.fluid_to_color,
            self.air_ducts["ad0"].led_index,
            "black",
            0.35,
            "blink3",
            wait_and_display_connected_air_ducts_before_restart,
        )
Esempio n. 26
0
    def buildProtocol(self, addr):
        logger.debug('Connected')

        self.resetDelay()

        logger.debug("Building protocol")
        self.protocol = JustSockClientProtocol(
            self,
            self.name,
            self.channel
        )

        return self.protocol
Esempio n. 27
0
    def on_cell_activation(self):
        if self.is_activated:
            logger.debug(
                "Color {} is already activated: skipping".format(self))
            return

        # Hide oscillations
        if self.deactivation_task and self.deactivation_task.active():
            logger.debug("Canceling {} deactivation".format(self))
            self.deactivation_task.cancel()

        self.is_activated = True
        self.toggle()
Esempio n. 28
0
 def pause(self):
     if self.current_state == MediaPlayerMixin.STATE_NOT_STARTED:
         logger.debug('Player has not been started yet')
         logger.debug('Nothing to do')
     elif self.current_state == MediaPlayerMixin.STATE_PLAYING:
         logger.debug('Player is already playing')
         self._pause()
         self.current_state = MediaPlayerMixin.STATE_PAUSED
     elif self.current_state == MediaPlayerMixin.STATE_PAUSED:
         logger.debug('Player is paused and had already been started')
         logger.debug('Nothing to do')
     else:
         pass
Esempio n. 29
0
    def process_event(self, event):
        logger.debug("Processing event '{}'".format(event))
        if type(event) is not dict:
            logger.error("Unknown event: skipping")
            return

        category = self.get_category(event)
        method = self.get_method_from_category(category)

        self.check_arguments(event, method)
        self.pop_superfluous_fields(event, method)

        method(**event)
Esempio n. 30
0
 def step4():
     if self.round < 2:
         logger.debug("Going to next round".format(self.round))
         self.round += 1
         if all([
                 ad.connected_source is None
                 for ad in self.air_ducts.values()
         ]):
             self.restart_round()
         else:
             self.display_connected_air_ducts_before_restart()
     else:
         logger.debug("All rounds have been completed: victory!")
         self.success_animation(step5)