Commits

Owen Nelson  committed 6231702

first attempt at sending game data down the pipe

  • Participants
  • Parent commits cc2ba18
  • Branches twisted

Comments (0)

Files changed (2)

         it = np.nditer(state, flags=['multi_index'])
         while not it.finished:
             (x, y) = it.multi_index
-            val = it[0]
+            val = int(it[0])
             count = living_neighbors(x, y, state)
             # print "%d,%d: %d" % (x, y, count)
 
         it = np.nditer(self._state, flags=['multi_index'])
         while not it.finished:
             (x, y) = it.multi_index
-            v = it[0]
+            v = int(it[0])
             yield (x, y, v)
             it.iternext()
 
+#!/usr/bin/env python
+
+import sys
+import json
+from twisted.python import log
+from twisted.internet.protocol import ClientFactory
+from twisted.protocols.basic import LineReceiver
+from twisted.internet import reactor
+from cgol import Game
+
+
+game = None
+
+
+class GOLClient(LineReceiver):
+
+    def connectionMade(self):
+        print "connected"
+        self.start_game()
+
+    def lineReceived(self, data):
+        print "receive:", repr(data)
+
+    def start_game(self):
+        print 'starting game'
+        self.sendLine(json.dumps({'shape': game.state.shape}))
+        while 1:
+            self.sendLine(json.dumps({'frame': list(game.next())}))
+
+
+class GOLClientFactory(ClientFactory):
+    protocol = GOLClient
+
+    def clientConnectionFailed(self, connector, reason):
+        print 'connection failed:', reason.getErrorMessage()
+        reactor.stop()
+
+    def clientConnectionLost(self, connector, reason):
+        print 'connection lost:', reason.getErrorMessage()
+        reactor.stop()
+
+
+def main():
+    try:
+        port = int(sys.argv[1])
+        shape = int(sys.argv[2]), int(sys.argv[3])
+    except:
+        port = 9999
+        shape = (50, 50)
+
+    global game
+    game = Game(size=shape)
+
+    log.startLogging(sys.stdout)
+    factory = GOLClientFactory()
+    reactor.connectTCP('localhost', port, factory)
+    reactor.run()
+
+
+if __name__ == '__main__':
+    main()