Peter Ward avatar Peter Ward committed fbf403c

Add ZMQ engine and viewer.

Comments (0)

Files changed (2)

snakegame/engine.py

     def __init__(
         self,
         rows, columns, n_apples,
-        wrap=True, results=False,
+        wrap=True,
         random=None,
         *args, **kwargs
     ):
 
         self.wrap = wrap
         self.bots = {}
-        self.results = None
-        if results:
-            self.results = open('results.csv', 'a+')
 
         self.new_game(rows, columns, n_apples)
 
         bot = self.bots[letter]
         del self.bots[letter]
 
-        if not self.results:
-            return
-
-        try:
-            name = bot[0].__name__
-        except AttributeError:
-            pass
-        else:
-            apple_score = len(bot[2])
-            self.results.write('%s,%s,%s,%s\n' % \
-                (self.game_id, name, apple_score, time_score))
-            self.results.flush()
-
     def update_snakes(self):
         self.game_ticks += 1
 
+from __future__ import absolute_import
+
+import pickle
+
+class Viewer(object):
+    def __init__(self, engine, sock):
+        self.sock = sock
+        self.engine = engine
+
+    def run(self):
+        for board in self.engine:
+            bots = {
+                letter: (None, colour, None, team)
+                for letter, (_, colour, _, team) in self.engine.bots.iteritems()
+            }
+
+            msg = pickle.dumps({
+                'bots': bots,
+                'board': board,
+            }, protocol=2)
+
+            self.sock.send(msg)
+
+class Engine(object):
+    def __init__(self, sock):
+        self.sock = sock
+
+    def __iter__(self):
+        while True:
+            if not self.sock.poll(timeout=2000):
+                break
+            msg = self.sock.recv()
+            obj = pickle.loads(msg)
+            self.bots = obj['bots']
+            yield obj['board']
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.