Source

pygamegsoc12 / alchemymadness / client.py

import sys
from threading import Thread, Event
from PodSixNet.Connection import ConnectionListener, connection

class RemoteObject(object):
    synch = ()
    
    def __init__(self):
        self.changed = False
        self.updating = Event()
        self.__setattr__ = self._setattr
    
    def _synch_var(self):
        return ((k,v) for k,v in self.__dict__.iteritems() if k in self.synch)
        
    def _setattr(self, name, value):
        object.__setattr__(self, name, value)
        if not self.updating.is_set():
            object.__setattr__(self, '_changed', self.changed or name in self.synch)

class Player(RemoteObject):
    synch = ('test',)
    def __init__(self):
        super(Player, self).__init__()
        self.test = 'aaa'

class GameClient(ConnectionListener):
    def __init__(self, host, port):
        self.player = Player()
        self.others = {}
        self._stop = Event()
        self.input_thread = Thread(target = self.input_handler)
        self.input_thread.daemon = True
        self.input_thread.start()
        self.Connect((host, port))
        
    def step(self, *args):
        connection.Pump()
        if self.player.changed:
            self.Send(dict(self.player._synch_var(), action='update'))
            self.player.changed = False
        self.Pump()
        
    #def Network(self, data):
    #    print data
        
    def Network_connected(self, data):
        print '# Connected'
        self._stop.clear()
        if not self.input_thread.is_alive():
            self.input_thread.start()

    def Network_error(self, data):
        print '# Error:', data['error'][1]
        self.connection.Close()
        self._stop.set()

    def Network_disconnected(self, data):
        print '# Disconnected'
        self._stop.set()
        
    def Network_info(self, data):
        for msg in data['info'].split('/n'):
            print '#', msg
        
    def Network_msg(self, data):
        print '%s: %s' % (data['id'], data['msg'])
        
    def Network_init(self, data):
        self.player.updating.set()
        self.player.test = data['test']
        self.player.updating.clear()
        
    def Network_add(self, data):
        p = Player()
        self.others[data['id']] = p
        
    def Network_del(self, data):
        del self.others[data['id']]
        
    def Network_update(self, data):
        p = self.others[data['id']]
        del data['action']
        p.test = data['test']
    
    def input_handler(self):
        while not self._stop.is_set():
            msg = raw_input()
            if len(msg) > 0:
                self.Send({'action': 'msg', 'msg': msg[:255]})
                
    def run(self):
        while not self._stop.wait(0.001):
            self.step()

if __name__ == '__main__':
    addr = 'localhost'
    if len(sys.argv) > 1:
        addr = sys.argv[1]
    GameClient(addr, 10000).run()
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.