X-Git-Url: https://plomlompom.com/repos/feed.xml?a=blobdiff_plain;ds=sidebyside;f=plomrogue%2Fio.py;h=490706eefd9fe2ef203632dd6b255180839fd252;hb=f3e107445e4de3f4e79c3d369dd70f5bab8b7d0d;hp=68f580ba3fa689b78075a175ed4a2e880128235f;hpb=baab532ba5125527bdc7061864f7cc7db8d7abc3;p=plomrogue2 diff --git a/plomrogue/io.py b/plomrogue/io.py index 68f580b..490706e 100644 --- a/plomrogue/io.py +++ b/plomrogue/io.py @@ -13,23 +13,39 @@ class GameIO(): self.save_file = save_file self.servers = [] + def train_parser(self): + import string + self.parser.string_options = { + 'map_geometry': {'Hex', 'Square'}, + 'char': [c for c in + string.digits + string.ascii_letters + string.punctuation + + ' '], + 'direction': self.game.map_geometry.directions, + 'direction+here': ['HERE'] + self.game.map_geometry.directions, + 'thing_type': self.game.thing_types.keys() + } + def loop(self, q): """Handle commands coming through queue q, run game, send results back. - As basic flood protection, Only accepts one command per connection per - 1/100 of a second. + As basic flood protection, only accepts ten commands per connection per + 1/10 of a second. """ import time potential_flooders = {} while True: try: - command, connection_id = q.get(timeout=0.001) - if connection_id in potential_flooders: - if int(time.time() * 100) == potential_flooders[connection_id]: + connection_id, command = q.get(timeout=0.001) + now = int(time.time() * 10) + if connection_id in potential_flooders and \ + potential_flooders[connection_id][0] == now: + if potential_flooders[connection_id][1] > 10: continue - potential_flooders[connection_id] = int(time.time() * 100) - self.handle_input(connection_id, command) + potential_flooders[connection_id][1] += 1 + else: + potential_flooders[connection_id] = [now, 1] + self.handle_input(command, connection_id) except queue.Empty: self.game.run_tick() @@ -38,8 +54,20 @@ class GameIO(): The game loop works sequentially through game commands received via self.queue from connected servers' clients.""" + self.queue = queue.Queue() + + # optionally use this for main thread profiling: + # import cProfile + # class ProfiledThread(threading.Thread): + # def run(self): + # profiler = cProfile.Profile() + # profiler.runcall(threading.Thread.run, self) + # print('profiled thread finished') + # profiler.dump_stats('profile') + # c = ProfiledThread(target=self.loop, args=(self.queue,)) c = threading.Thread(target=self.loop, args=(self.queue,)) + c.start() def start_server(self, port, server_class, certfile=None, keyfile=None): @@ -80,9 +108,9 @@ class GameIO(): command(*args, connection_id=connection_id) elif god_mode: command(*args) - #if store and not hasattr(command, 'dont_save'): - # with open(self.game_file_name, 'a') as f: - # f.write(input_ + '\n') + # if store and not hasattr(command, 'dont_save'): + # with open(self.game_file_name, 'a') as f: + # f.write(input_ + '\n') except ArgError as e: answer(connection_id, 'ARGUMENT_ERROR ' + quote(str(e))) except PlayError as e: @@ -99,7 +127,7 @@ class GameIO(): """ if connection_id: for server in self.servers: - if connection_id in server.clients: + if connection_id in server.clients: client = server.clients[connection_id] client.put(msg) else: