Exemplo n.º 1
0
class Race(object):
    def __init__(self):
        self.server = SocketDaemon('', PORT, self.__new_conn)
        self.conns = set()
        self.leaving = set()
        self.names = set()
        self.reset_current()

    def reset_current(self):
        self.current = ''.join([get_letter() for i in range(7)])
        self.wordcast()

    def __new_conn(self, conn):
        rc = RaceConn(conn, self)
        self.conns.add(rc)
        conn.set_close_cb(self.close_cb, [rc])

    def close_cb(self, conn):
        conn.remove()
        self.leaving.add(conn)
        if conn.name:
            self.names.remove(conn.name)
            self.broadcast(["LEAVE", conn.name])

    def alertcast(self, msg):
        self.broadcast(["ALERT", msg])

    def joincast(self, player):
        self.names.add(player.name)
        self.broadcast(["JOIN", [player.name, player.score]])

    def scorecast(self, player):
        self.broadcast(["SCORE", [player.name, player.score]])

    def wordcast(self):
        self.broadcast(["WORD", self.current])

    def broadcast(self, msg):
        msg = encode(msg)
        for conn in self.leaving:
            self.conns.remove(conn)
        self.leaving.clear()
        for conn in self.conns:
            conn.send(msg)

    def start(self):
        self.server.start()
Exemplo n.º 2
0
 def __init__(self):
     self.server = SocketDaemon('', PORT, self.__new_conn)
     self.conns = set()
     self.leaving = set()
     self.names = set()
     self.reset_current()