def __init__(self, **kwargs): #Grab some settings settings = kwargs.get('settings', {}) for setting in defaults.defstruct: val = kwargs.get(setting[1], settings.get(setting[1], setting[2])) setattr(self, setting[0], val) #Initialize plugin list #Plugins should never touch this self.timers = [] self.plugin_handlers = {flag: [] for name, flag in cflags.items()} self.plugin_dispatch = {ident: [] for ident in mcdata.structs} self.plugins = [plugin(self) for plugin in self.plugins] #Initialize socket and poll #Plugins should never touch these unless they know what they're doing self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setblocking(0) self.poll = select.poll() self.poll.register(self.sock, smask) #Initialize Event Loop/Network variables #Plugins should generally not touch these self.encrypted = False self.kill = False self.sess_err = False self.auth_err = False self.rbuff = bound_buffer.BoundBuffer() self.sbuff = b'' self.flags = 0 #Game State variables #Plugins should read these (but generally not write) self.world = smpmap.World() self.world_time = { 'world_age': 0, 'time_of_day': 0, } self.position = { 'x': 0, 'y': 0, 'z': 0, 'stance': 0, 'yaw': 0, 'pitch': 0, 'on_ground': False, } self.health = { 'health': 20, 'food': 20, 'food_saturation': 5, } self.playerlist = {} self.entitylist = {} self.spawn_position = { 'x': 0, 'y': 0, 'z': 0, }
def event_loop(self): #Set up signal handlers signal.signal(signal.SIGINT, self.signal_handler) signal.signal(signal.SIGTERM, self.signal_handler) #Fire off plugins that need to run after init for callback in self.plugin_handlers[cflags['START_EVENT']]: callback(flag) #starting webinterface socket websock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) websock.bind(("", webinterface_port)) websock.listen(1) read_list = [websock] while not (self.flags&cflags['KILL_EVENT'] and self.kill): self.getflags() if self.flags: for name, flag in cflags.items(): if self.flags&flag: #Default handlers if flag in fhandles: fhandles[flag](self) #Plugin handlers for callback in self.plugin_handlers[flag]: callback(flag) for index, timer in enumerate(self.timers): if timer.update(): timer.fire() if not timer.check(): del self.timers[index] if self.daemon: sys.stdout.flush() sys.stderr.flush() #waiting for new commands from webinterface. 0.01s timeout readable, writable, errored = select.select(read_list, [], [], 0.01) for s in readable: if s is s: komm, addr = s.accept() data = komm.recv(32) psicraft.dispatch_psicraft_command(data.decode(), self, komm) komm.close() if self.kill_flag: print("closing sockets & shutting down") s.close() break
def event_loop(self): #Set up signal handlers signal.signal(signal.SIGINT, self.signal_handler) signal.signal(signal.SIGTERM, self.signal_handler) #Fire off plugins that need to run after init for callback in self.plugin_handlers[cflags['START_EVENT']]: callback(flag) while not (self.flags&cflags['KILL_EVENT'] and self.kill): self.getflags() if self.flags: for name, flag in cflags.items(): if self.flags&flag: #Default handlers if flag in fhandles: fhandles[flag](self) #Plugin handlers for callback in self.plugin_handlers[flag]: callback(flag) for index, timer in enumerate(self.timers): if timer.update(): timer.fire() if not timer.check(): del self.timers[index] if self.daemon: sys.stdout.flush() sys.stderr.flush()