From c0cb4b636aaad2de685e11b1c56470c7d66ea895 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sun, 18 Dec 2011 18:55:08 +0100 Subject: [PATCH] Add simple integrator for websockets-based mass-pong --- tools/game/bong/integrator.py | 77 +++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tools/game/bong/integrator.py diff --git a/tools/game/bong/integrator.py b/tools/game/bong/integrator.py new file mode 100644 index 0000000..4d30212 --- /dev/null +++ b/tools/game/bong/integrator.py @@ -0,0 +1,77 @@ +import r0ketrem0te.game +import r0ketrem0te.bridge +import r0ketrem0te.packets +import time +import Queue +import tornado.escape +import tornado.ioloop +import tornado.options +import tornado.web +import tornado.websocket +import os.path +import logging +import threading + +from tornado.options import define, options + +define("port", default=8888, help="run on the given port", type=int) + +pong=None +pos=0.5 + +def sendpos(): + if pong: + pong.write_message("{ \"right\": "+str(pos)+" }") + threading.Timer(.5,sendpos).start() + +sendpos() + +def receivedPacket(packet): + global pos + if isinstance(packet,r0ketrem0te.packets.Button): + if packet.button == 2: + pos+=.01 + if packet.button == 1: + pos-=.01 + if pos <0: + pos=0 + if pos >1: + pos=1 + +game = r0ketrem0te.game.Game('COM4', "testgame", 83, + 81, [ord(x) for x in 'REM0T'], 2, True) +game.bridge.registerCallback(receivedPacket) + +class Application(tornado.web.Application): + def __init__(self): + handlers = [ + (r"/data", DataSocketHandler), + ] + settings = dict( + cookie_secret="43oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=", + template_path=os.path.join(os.path.dirname(__file__), "templates"), + static_path=os.path.join(os.path.dirname(__file__), "static"), + xsrf_cookies=True, + autoescape=None, + ) + tornado.web.Application.__init__(self, handlers, **settings) + +class DataSocketHandler(tornado.websocket.WebSocketHandler): + waiters = set() + def open(self): + global pong + print "WebSocket: Open" + DataSocketHandler.waiters.add(self) + pong=self + + def on_close(self): + print "WebSocket: Close" + DataSocketHandler.waiters.remove(self) + + def on_message(self, message): + print "Got message: ",message + +tornado.options.parse_command_line() +app = Application() +app.listen(options.port,'127.0.0.1') +tornado.ioloop.IOLoop.instance().start()