コード例 #1
0
 def __init__(self):
     Fsm.__init__(self, 'idle')
     self.append_state(name='idle', on_enter=self.showtime)
     self.append_state(name='playing',
                       on_enter=self.radio_on,
                       on_exit=self.radio_off)
     self.append_transition(src='idle',
                            event='inc',
                            condition=None,
                            action=self.bright_inc,
                            dst=None),
     self.append_transition(src='idle',
                            event='on',
                            condition=None,
                            action=None,
                            dst='playing'),
     self.append_transition(src='idle',
                            event='alarm',
                            condition=self.switchstate,
                            action=None,
                            dst='playing'),
     self.append_transition(src='playing',
                            event='inc',
                            condition=None,
                            action=self.vol_up,
                            dst=None),
     self.append_transition(src='playing',
                            event='off',
                            condition=None,
                            action=None,
                            dst='idle'),
     self.vol = 0
     self.brightness = 0
     self.switch = True
コード例 #2
0
ファイル: scheduler.py プロジェクト: dfedyk/rift-python
 def run(self):
     while True:
         # Process timers in two places because FSM event processing might cause timers to be
         # created, and timer expire processing might cause FSM events to be queued.
         timeout = TIMER_SCHEDULER.trigger_all_expired_timers()
         rx_ready, tx_ready, _ = select.select(self._rx_fds, self._tx_fds, [], timeout)
         for rx_fd in rx_ready:
             handler = self._handlers_by_rx_fd[rx_fd]
             handler.ready_to_read()
         for tx_fd in tx_ready:
             handler = self._handlers_by_tx_fd[tx_fd]
             handler.ready_to_write()
         TIMER_SCHEDULER.trigger_all_expired_timers()
         Fsm.process_queued_events()
コード例 #3
0
ファイル: core.py プロジェクト: Skimik/bookbot2
def handler(message, callback_id=None, callback_data=None):
    print(message)
    print(callback_id)
    print(callback_data)
    user_checker(message)
    if stop_ddos(message) and pass_cheсker(message):
        Fsm().executor(message, callback_id, callback_data)
コード例 #4
0
ファイル: core.py プロジェクト: Skimik/bookbot2
def user_checker(message):
    print('------------1==============')
    if Database(table_name='user_db').select_db(
            'telegram_id', 'where telegram_id = {}'.format(
                message.chat.id))['available'] == 0:
        print('------------2==============')
        Fsm().register(message)
        keyboard_test = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True)
コード例 #5
0
ファイル: test_fsm.py プロジェクト: pieterlauwers/fsm
def test_fsm_state_exit():
    a = 0

    def stop():
        nonlocal a
        a = a - 1

    state_machine = Fsm('playing')
    state_machine.append_state(name='playing', on_exit=stop)
    state_machine.append_transition(src='playing', event='off', dst='idle')
    state_machine.event('off')
    assert state_machine.state == 'idle'
    assert a == -1
コード例 #6
0
ファイル: test_fsm.py プロジェクト: pieterlauwers/fsm
def test_fsm_state_enter():
    a = 0

    def start():
        nonlocal a
        a = a + 1

    state_machine = Fsm('idle')
    state_machine.append_state(name='playing', on_enter=start)
    state_machine.append_transition(src='idle', event='on', dst='playing')
    state_machine.event('on')
    assert state_machine.state == 'playing'
    assert a == 1
コード例 #7
0
ファイル: scheduler.py プロジェクト: jadedeane/rift-python
 def run(self):
     while True:
         # This needs to be a loop because processing an event can create a timer and processing
         # an expired timer can create an event.
         while Fsm.events_pending(
         ) or TIMER_SCHEDULER.expired_timers_pending():
             # Process all queued events
             start_time = time.monotonic()
             Fsm.process_queued_events()
             duration = time.monotonic() - start_time
             self.max_pending_events_proc_time = max(
                 self.max_pending_events_proc_time, duration)
             # Process all expired timers
             start_time = time.monotonic()
             timeout = TIMER_SCHEDULER.trigger_all_expired_timers()
             duration = time.monotonic() - start_time
             self.max_expired_timers_proc_time = max(
                 self.max_expired_timers_proc_time, duration)
         # Wait for ready to read or expired timer
         start_time = time.monotonic()
         rx_ready, _, _ = select.select(self._rx_fds, [], [], timeout)
         duration = time.monotonic() - start_time
         self.max_select_proc_time = max(self.max_select_proc_time,
                                         duration)
         # Check for timer slips
         if timeout is not None:
             slip_time = duration - timeout
             if slip_time > 0.01:
                 self.slip_count_10ms += 1
             if slip_time > 0.1:
                 self.slip_count_100ms += 1
             if slip_time > 1.0:
                 self.slip_count_1000ms += 1
         # Process all handlers that are ready to read
         for rx_fd in rx_ready:
             start_time = time.monotonic()
             handler = self._handlers_by_rx_fd[rx_fd]
             handler.ready_to_read()
             duration = time.monotonic() - start_time
             self.max_ready_to_read_proc_time = max(
                 self.max_ready_to_read_proc_time, duration)
コード例 #8
0
ファイル: powerfsm.py プロジェクト: wrboyce/ad-powerfsm
    def initialize(self):
        entity_id = f'sensor.{self.args["id"]}'
        power_sensor = self.args["power_sensor"]
        power_sensor_attr = None
        if ":" in power_sensor:
            power_sensor, power_sensor_attr = power_sensor.split(":")
        idle_power = self.args["idle_power"]
        idle_timeout = self.args.get("idle_timeout", 0)
        idle_state = self.args.get("idle_state", "Idle")
        work_state = self.args.get("work_state", "Working")
        attn_state = self.args.get("attn_state", "Needs Attention")
        requires_attn = self.args.get("requires_attn", False)

        is_idle = Condition(
            id="is_idle",
            entity=power_sensor,
            attribute=power_sensor_attr,
            operator=LE,
            operand=idle_power,
            stability_time=idle_timeout,
        )
        is_working = Condition(
            id="is_working",
            entity=power_sensor,
            attribute=power_sensor_attr,
            operator=GT,
            operand=idle_power,
        )

        states = [
            mk_state(
                mode=States.IDLE,
                name=idle_state,
                next_state=States.WORK,
                condition=is_working,
            ),
            mk_state(
                mode=States.WORK,
                name=work_state,
                next_state=requires_attn and States.ATTN or States.IDLE,
                condition=is_idle,
            ),
        ]
        if requires_attn:
            states.append(
                mk_state(
                    mode=States.ATTN,
                    name=attn_state,
                    next_state=States.WORK,
                    condition=is_working,
                ))

        Fsm(self, id=self.args["id"], entity=entity_id, states=states)
コード例 #9
0
    def __init__(self, start_px, start_py, *groups):
        Sprite.__init__(self, *groups)
        self.px = start_px
        self.py = start_py
        self.yVel = 0
        self.states = {
            "stand_still": self.stand_still,
            "move": self.move,
            "jump": self.jump,
            "get_down": self.get_down,
            "attack": self.attack,
            "fall": self.fall
        }

        self.fsm = Fsm(active_state="fall", states=self.states)
        self.cannot_move_to = None
        self.images = {}
        self._base_image_path = "src/sprites/"
        self._load_state_images()
        self.image = self.images[self.fsm.get_state()][0]
        self.convert_image()
        self.rect = self.image.get_rect()
コード例 #10
0
ファイル: test_fsm.py プロジェクト: pieterlauwers/fsm
def test_fsm_transition_condition_true():
    def test():
        return True

    state_machine = Fsm('idle')
    state_machine.append_transition(src='idle',
                                    event='on',
                                    condition=test,
                                    dst='playing')
    state_machine.event('on')
    assert state_machine.state == 'playing'
コード例 #11
0
ファイル: test_fsm.py プロジェクト: pieterlauwers/fsm
def test_fsm_transition_action_only():
    a = 0

    def inc():
        nonlocal a
        a = a + 1

    state_machine = Fsm('idle')
    state_machine.append_transition(
        src='idle',
        event='on',
        action=inc,
    )
    state_machine.event('on')
    assert state_machine.state == 'idle'
    assert a == 1
コード例 #12
0
ファイル: test_fsm.py プロジェクト: pieterlauwers/fsm
def test_fsm_transition_action_condition_false():
    a = 0

    def inc():
        nonlocal a
        a = a + 1

    def test():
        return False

    state_machine = Fsm('idle')
    state_machine.append_transition(src='idle',
                                    event='on',
                                    condition=test,
                                    action=inc,
                                    dst='playing')
    state_machine.event('on')
    assert state_machine.state == 'idle'
    assert a == 0
コード例 #13
0
ファイル: test_fsm.py プロジェクト: pieterlauwers/fsm
def test_fsm_initialize_default():
    state_machine = Fsm()
    assert state_machine.state is None
コード例 #14
0
ファイル: test_fsm.py プロジェクト: pieterlauwers/fsm
def test_fsm_unknown_transition():
    state_machine = Fsm('idle')
    state_machine.event('on')
    assert state_machine.state == 'idle'
コード例 #15
0
ファイル: test_fsm.py プロジェクト: pieterlauwers/fsm
def test_fsm_basic_transition():
    state_machine = Fsm('idle')
    state_machine.append_transition(src='idle', event='on', dst='playing')
    state_machine.event('on')
    assert state_machine.state == 'playing'
コード例 #16
0
class Characters(Sprite):

    """"
    Base class for all characters of the game
    """

    def __init__(self, start_px, start_py, *groups):
        Sprite.__init__(self, *groups)
        self.px = start_px
        self.py = start_py
        self.yVel = 0
        self.states = {
            "stand_still": self.stand_still,
            "move": self.move,
            "jump": self.jump,
            "get_down": self.get_down,
            "attack": self.attack,
            "fall": self.fall
        }

        self.fsm = Fsm(active_state="fall", states=self.states)
        self.cannot_move_to = None
        self.images = {}
        self._base_image_path = "src/sprites/"
        self._load_state_images()
        self.image = self.images[self.fsm.get_state()][0]
        self.convert_image()
        self.rect = self.image.get_rect()

    def _load_state_images(self):
        base_image_path = self._base_image_path
        images = config.load_game_config_file("src/characters.yaml")
        hero_images = images["hero_state_images"]
        for state in self.states:
            self.images[state] = [Characters._load_scale_2x(
                base_image_path + image) for image in hero_images[state]]

    @classmethod
    def _load_scale_2x(cls, image):
        return pygame.transform.scale2x(pygame.image.load(image))

    def _load_image_in_actual_side(self, index):
        state = self.fsm.get_state()
        if self.fsm.side == "left":
            self.image = pygame.transform.flip(
                self.images[state][index], True, False)
        else:
            self.image = self.images[state][index]

    def stand_still(self):
        self._load_image_in_actual_side(0)
        self.convert_image()

    def jump(self):
    		# self.yVel -= 1.2
    		self.py += 1.2
    		print self.py
    		self.rect.move_ip(0, int(self.py))
    		self.convert_image()
    		# if self.yVel < -10:
    		# 	self.fsm.set_state("fall")

    def fall(self):
    	return
    	self.py += self.yVel
    	self.yVel += 1.2
    	self.rect.move_ip(0, self.py)

    def get_down(self):
        raise NotImplementedError

    def attack(self):
        attack_count = self.fsm.attack_count
        self._load_image_in_actual_side(attack_count)
        if attack_count == 4:
            self.fsm.attack_count = 0
            self.fsm.set_state("stand_still")
        else:
            self.fsm.attack_count += 1
        self.convert_image()

    def move(self):
        side_moves = self.fsm.moves_count
        self._load_image_in_actual_side(side_moves)
        if self.fsm.side == 'left':
            self.rect.move_ip(-10, 0)
            self.px -= 10
        else:
            self.rect.move_ip(10, 0)
            self.px += 10
        self.convert_image()
        time.sleep(0.075)

    def convert_image(self):
        '''
        Convert the character image and set colorkey to magenta(i.e pynk)
        '''
        self.image.set_alpha(None, RLEACCEL)
        self.image.convert()
        self.image.set_colorkey(MAGENTA, RLEACCEL)
コード例 #17
0
ファイル: test_fsm.py プロジェクト: pieterlauwers/fsm
def test_fsm_initialize_initial_state():
    state_machine = Fsm('idle')
    assert state_machine.state == 'idle'
コード例 #18
0
ファイル: turnstile.py プロジェクト: stuharley8/CS3040-Labs
# BEGIN ...
from fsm import Fsm
# END ...
turnstile = \
  Fsm() \
    .add_state("locked", is_final = True) \
      .add_transition("ticket", "collect", "unlocked") \
      .add_transition("pass", "alarm", "exception") \
    .add_state("unlocked") \
      .add_transition("ticket", "eject", "unlocked") \
      .add_transition("pass", None, "locked") \
    .add_state("exception") \
      .add_transition("ticket", "eject", "exception") \
      .add_transition("pass", None, "exception") \
      .add_transition("mute", None, "exception") \
      .add_transition_range('1', '5', 'punch', 'go') \
      .add_transition("release", None, "locked")
コード例 #19
0
ファイル: real.py プロジェクト: stuharley8/CS3040-Labs
# Author: Stuart Harley

from fsm import Fsm

real = \
    Fsm() \
        .add_state("Start") \
            .add_transition_range("0", "9", None, "Before ./e") \
            .add_transition(".", None, "Decimal point") \
        .add_state("Before ./e", is_final=True) \
            .add_transition_range("0", "9", None, "Before ./e") \
            .add_transition(".", None, "Decimal point") \
            .add_transition("e", None, "E") \
        .add_state("Decimal point") \
            .add_transition_range("0", "9", None, "After .") \
        .add_state("After .", is_final=True) \
            .add_transition_range("0", "9", None, "After .") \
            .add_transition("e", None, "E") \
        .add_state("E") \
            .add_transition_range("0", "9", None, "After e") \
        .add_state("After e", is_final=True) \
            .add_transition_range("0", "9", None, "After e")
コード例 #20
0
ファイル: test_fsm.py プロジェクト: pieterlauwers/fsm
def test_fsm_set_state():
    state_machine = Fsm()
    state_machine.state = 'idle'
    assert state_machine.state == 'idle'