home · contact · privacy
Refactor parser code.
[plomrogue2] / plomrogue / io.py
index 8cba18d7a483664047b3601890fec3df2c4490a9..490706eefd9fe2ef203632dd6b255180839fd252 100644 (file)
@@ -13,11 +13,23 @@ 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 (currently commented out).
+        As basic flood protection, only accepts ten commands per connection per
+        1/10 of a second.
 
         """
         import time
@@ -25,13 +37,14 @@ class GameIO():
         while True:
             try:
                 connection_id, command = q.get(timeout=0.001)
-
-                # FIXME: this would catch the init command flood
-                #if connection_id in potential_flooders:
-                #    if int(time.time() * 100) == potential_flooders[connection_id]:
-                #        continue
-                #potential_flooders[connection_id] = int(time.time() * 100)
-
+                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][1] += 1
+                else:
+                    potential_flooders[connection_id] = [now, 1]
                 self.handle_input(command, connection_id)
             except queue.Empty:
                 self.game.run_tick()