class EventLoop: def __init__(self, window): self._event_dispatcher = EventDispatcher(self, window) self._running = False rotate_event = sdl2.SDL_Event() rotate_event.type = EventDispatcher.ROTATE_EVENT self._window = window self._rotate_event_pointer = ctypes.byref(rotate_event) def run(self): self._running = True while self._running: if self._window.can_rotate: sdl2.SDL_PushEvent(self._rotate_event_pointer) self._receive_events() def stop(self): self._running = False def _receive_events(self): event = sdl2.SDL_Event() event_pointer = ctypes.byref(event) while self._running and sdl2.SDL_PollEvent(event_pointer) != 0: self._event_dispatcher.dispatch(event)
class Game: def __init__(self): self.running = False self.event_dispatcher = EventDispatcher() self.graphics = gamegraphics.Graphics(self, self.event_dispatcher) self.map = gamemap.Map() self.player = player.Player(self, self.event_dispatcher) self.ai = ai.AI(self, self.event_dispatcher) def sigint_handler(sig, frame): self.event_dispatcher.add_event("quit", "Ctrl-C") signal.signal(signal.SIGINT, sigint_handler) self.event_dispatcher.register("quit", self.quit) def quit(self, cause=None): logger.info("Stopping game, cause: {}".format(cause)) self.running = False def get_move(self): move = input(">> ") self.event_dispatcher.add_event("movePlayer", move) def run(self): self.running = True self.graphics.setup_sprites() utility.repeat(self.event_dispatcher, "ai_update", 0.1, self.ai.update) while self.running: self.graphics.update() self.event_dispatcher.dispatch()
def tick(self): #print('--> tick()') if self.script_running and not self.cmd_running: self.next_cmd() self.raise_action('TICK') self.after(self.tick_interval, self.tick) dispatcher.dispatch()
async def run(self): ''' shared_market_data = { "taker_fee": exchange_variables["taker_fee"], -> done "CRO_holding_backup": exchange_variables["CRO_holding_backup"], -> done "balance_USDT": 0, -> done "balance_BTC": 0, -> done "balance_CRO": 0, -> done "price_BTC_sell_to_USDT": 0, -> done "price_CRO_buy_for_BTC": 0, -> done "fee_BTC_sell_in_USDT": 0, "price_CRO_buy_for_USDT": 0, -> done "last_CRO_price_in_USDT": 0, -> done "last_CRO_price_in_BTC": 0, -> done "fee_BTC_sell_in_CRO": 0, "price_BTC_buy_for_USDT": 0, -> done "fee_BTC_buy_in_BTC": 0, "fee_BTC_buy_in_CRO": 0, } ''' event_dispatcher = EventDispatcher() event_dispatcher.register_channel_handling_method("user.balance", self.handle_channel_event_user_balance) event_dispatcher.register_response_handling_method("public/get-instruments", self.handle_response_get_instruments) async with CryptoClient( client_type=CryptoClient.USER, debug=self.debug, logger=self.logger, api_key=self.crypto_com_client.crypto_com_api_key, api_secret=self.crypto_com_client.crypto_com_secret_key, channels=[ "user.balance" ] ) as client: periodic_call_get_instruments = PeriodicAsync(5, lambda: self.get_instruments(client)) await periodic_call_get_instruments.start() try: while True: event_or_response = None try: event_or_response = await client.next_event_or_response() event_dispatcher.dispatch(event_or_response) except Exception as e: if event_or_response: message = "Exception during handling user api event or response: {}".format(repr(e)) self.logger.exception(message) self.logger.error("Event or response that failed: {}".format(event_or_response)) else: message = "Exception during parsing user api event or response: {}".format(repr(e)) self.logger.exception(message) # TODO: Send pushover notification here with message continue finally: self.logger.info("Cleanup before closing worker...") await periodic_call_get_instruments.stop()
class EventLoop: def __init__(self, window): self._event_dispatcher = EventDispatcher(self, window) self._running = False def run(self): self._running = True while self._running: self._receive_events() def stop(self): self._running = False def _receive_events(self): event = sdl2.SDL_Event() event_pointer = ctypes.byref(event) while self._running and sdl2.SDL_PollEvent(event_pointer) != 0: self._event_dispatcher.dispatch(event)
async def run(self): event_dispatcher = EventDispatcher() event_dispatcher.register_channel_handling_method( "ticker.BTC_USDT", self.handle_channel_event_ticker_BTC_USDT) event_dispatcher.register_channel_handling_method( "ticker.CRO_USDT", self.handle_channel_event_ticker_CRO_USDT) event_dispatcher.register_channel_handling_method( "ticker.CRO_BTC", self.handle_channel_event_ticker_CRO_BTC) async with CryptoClient(client_type=CryptoClient.MARKET, debug=self.debug, logger=self.logger, channels=[ "ticker.BTC_USDT", "ticker.CRO_USDT", "ticker.CRO_BTC", ]) as client: try: while True: event_or_response = None try: event_or_response = await client.next_event_or_response( ) event_dispatcher.dispatch(event_or_response) except Exception as e: if event_or_response: message = "Exception during handling market data event or response: {}".format( repr(e)) self.logger.exception(message) self.logger.error( "Event or response that failed: {}".format( event_or_response)) else: message = "Exception during parsing market data event or response: {}".format( repr(e)) self.logger.exception(message) # TODO: Send pushover notification here with message continue finally: self.logger.info("Cleanup before closing worker...")