# Initialize the print state. self.state = None def globals_update(self, machine, values): '''Handle the globals_update event. Don't care about all the arguments, just check the last one.''' print_state = values[-1] if print_state == self.state: return print('State changed from {} to {}'.format(state_name[self.state], state_name[print_state])) self.state = print_state def __getattr__(self, attr): '''Anything else is requested. Return a function that can print the name and arguments when it is called.''' def call(*args, **kwargs): ## Uncomment the next line to see all the functions ## that are called. To handle any of them, define it ## like globals_update is defined above. #print('call', attr, 'args:', args, 'kwargs:', kwargs) pass return call # Open the connection to Franklin. p = websocketd.RPC('localhost:8000', monitor, tls = False) # Tell Franklin to inform us of updates. p.set_monitor(True) # Start the main loop (in the foreground), waiting for events. websocketd.fgloop()
def Game(): # Main function to start a game. {{{ global server, title_game, have_2d, have_3d, _num_players # Set up the game name. if not hasattr(__main__, 'name') or __main__.name is None: __main__.name = os.path.basename(sys.argv[0]).capitalize() # Initialize fhs module. if not fhs.initialized: fhs.init({}, packagename = __main__.name.lower(), game = True) # Set up other constants. if not hasattr(__main__, 'autokill'): __main__.autokill = True have_2d = fhs.read_data(os.path.join('html', '2d'), dir = True, opened = False) is not None have_3d = fhs.read_data(os.path.join('html', '3d'), dir = True, opened = False) is not None or not have_2d # Fill in min and max if not specified. assert hasattr(__main__, 'num_players') if isinstance(__main__.num_players, int): _num_players = (__main__.num_players, __main__.num_players) else: _num_players = __main__.num_players assert 1 <= _num_players[0] and (_num_players[1] is None or _num_players[0] <= _num_players[1]) # Build asset string for inserting in js. for subdir, use_3d in (('2d', False), ('3d', True)): targets = [] for base in ('img', 'jta', 'gani', 'audio', 'text'): for d in (os.path.join('html', base), os.path.join('html', subdir, base)): for p in fhs.read_data(d, dir = True, multiple = True, opened = False): targets.extend(f.encode('utf-8') for f in os.listdir(p) if not f.startswith('.') and not os.path.isdir(os.path.join(p, f))) if len(targets) > 0: loader_js[use_3d] = b'\n'.join(b"\tplease.load('" + f + b"');" for f in targets) else: # Nothing to load, but force the "finished loading" event to fire anyway. loader_js[use_3d] = b'\twindow.dispatchEvent(new CustomEvent("mgrl_media_ready"));' # Set up commands. cmds['leave'] = {None: leave} if hasattr(__main__, 'commands'): for c in __main__.commands: cmds[c] = {None: __main__.commands[c]} # Start up websockets server. config = fhs.module_get_config('webgame') httpdirs = [fhs.read_data(x, opened = False, multiple = True, dir = True) for x in ('html', os.path.join('html', '2d'), os.path.join('html', '3d'))] server = websocketd.RPChttpd(config['port'], Connection, tls = config['tls'], httpdirs = httpdirs[0] + httpdirs[1] + httpdirs[2]) server.handle_ext('png', 'image/png') server.handle_ext('jpg', 'image/jpeg') server.handle_ext('jpeg', 'image/jpeg') server.handle_ext('gif', 'image/gif') server.handle_ext('gani', 'text/plain') server.handle_ext('wav', 'audio/wav') server.handle_ext('ogg', 'audio/ogg') server.handle_ext('mp3', 'audio/mp3') server.handle_ext('jta', 'application/octet-stream') server.handle_ext('txt', 'text/plain') server.handle_ext('frag', 'text/plain') server.handle_ext('vert', 'text/plain') server.handle_ext('glsl', 'text/plain') # Set up title page. if hasattr(__main__, 'Title'): title_game = Instance(__main__.Title, '') else: title_game = Instance(Title, '') log('Game "%s" started' % __main__.name) # Main loop. websocketd.fgloop()
websocketd.call(None, Connection.add_port, '/dev/' + tty) except: # Try more generic approach. Don't use this by default, because it doesn't detect all ports on GNU/Linux. try: import serial.tools.list_ports for tty in serial.tools.list_ports.comports(): websocketd.call(None, Connection.add_port, tty[0]) except: traceback.print_exc() log('Not probing serial ports, because an error occurred: %s' % sys.exc_info()[1]) # }}} # Set default printer. {{{ if ' ' in config['printer']: default_printer = config['printer'].rsplit(' ', 1) else: default_printer = (config['printer'], None) # }}} httpd = Server(config['port'], Connection, disconnect_cb=Connection._disconnect, httpdirs=fhs.read_data('html', dir=True, multiple=True), address=config['address'], log=config['log'], tls=tls) log('Franklin server is running') websocketd.fgloop()