示例#1
0
 def __init__(self, width, height, port):
     pygame.init()
     pygame.mixer.init()
     self.__width = width
     self.__height = height
     self.__screen = pygame.display.set_mode((width, height), pygame.DOUBLEBUF)
     self.__sprite_manager = ClientManager(port)
     self.__clock = pygame.time.Clock()
     self.__fps = 120
     self.__is_working = True
示例#2
0
def test_closeClientSession():
    cm = ClientManager(10)
    cm.addClient("s1")
    cm.updateClient("s1", "A")

    cm.closeClientSession("s1")
    clientA = cm.getClientByUsername("A")
    assert len(clientA["sessions"]) == 1
    assert clientA["currSession"] == None
示例#3
0
    def __init__(self, _server):
        print "Loaded Stream Manager"

        # Server ref
        self.server = _server

        # Connection Manager
        self.connectionMgr = ConnectionManager(self)
        self.connectionMgr.start()

        # init Sub Managers
        self.clientManager = ClientManager(self)
        self.moveManager = MoveManager(self)
        self.ghostManager = GhostManager(self)
        self.datablockManager = DatablockManager(self)

        # Setup opcodes and handlers
        self.unpackOpcodes = {
            MSG_SERVER_MOVE_CMD: self.moveManager.readStreamPacket
        }
示例#4
0
class Game:
    def __init__(self, width, height, port):
        pygame.init()
        pygame.mixer.init()
        self.__width = width
        self.__height = height
        self.__screen = pygame.display.set_mode((width, height), pygame.DOUBLEBUF)
        self.__sprite_manager = ClientManager(port)
        self.__clock = pygame.time.Clock()
        self.__fps = 120
        self.__is_working = True

    def run_game(self):
        while self.__is_working:
            self.__clock.tick(self.__fps)
            control = self.__get_control()
            sprites = self.__update(control)
            self.__render(sprites)

    def __get_control(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.__close_game()
        return pygame.key.get_pressed()

    def __update(self, control):
        sprites = self.__sprite_manager.update_objects(control)
        return sprites

    def __render(self, sprites):
        self.__screen.fill([0, 0, 0])
        sprites.draw(self.__screen)
        pygame.display.update()

    def __close_game(self):
        self.__is_working = False
示例#5
0
def test_whoElseSince_withOfflineClients():

    startTime = time.time()

    cm = ClientManager(10)
    cm.addClient("s1")
    cm.updateClient("s1", "A")

    cm.addClient("s2")
    cm.updateClient("s2", "B")

    cm.addClient("s3")
    cm.updateClient("s3", "C")

    assert len(cm.getClientsActiveSince(startTime)) == 3

    cm.closeClientSession("s1")
    assert len(cm.getClientsActiveSince(startTime)) == 3
示例#6
0
def test_whoElse():
    cm = ClientManager(10)
    cm.addClient("s1")
    cm.updateClient("s1", "A")

    cm.addClient("s2")
    cm.updateClient("s2", "B")

    cm.addClient("s3")
    cm.updateClient("s3", "C")

    assert len(cm.getActiveClients()) == 3

    cm.closeClientSession("s1")
    assert len(cm.getActiveClients()) == 2
示例#7
0
def test_authenticateClient():
    cm = ClientManager(10)
    # Success
    assert cm.authenticateClient("jenn", "ifer") == "success"

    # Currently Active
    cm.addClient("socket")
    cm.updateClient("socket", "jenn")
    assert cm.authenticateClient("jenn", "ifer") == "alreadyActive"

    # Unsuccessful
    assert cm.authenticateClient("asd", "asd") == "wrongCredentials"

    # Blocked
    cm.authenticateClient("qwe", "qwe")
    cm.authenticateClient("qwe", "qwe")
    cm.authenticateClient("qwe", "qwe")
    assert cm.authenticateClient("qwe", "qwe") == "blocked"
示例#8
0
def test_blocking():

    cm = ClientManager(10)
    cm.addClient("s1")
    cm.updateClient("s1", "A")

    cm.addClient("s2")
    cm.updateClient("s2", "B")

    cm.block("A", "B")
    clientA = cm.getClientByUsername("A")
    print("CLIENT: " + str(clientA["blockedUsers"]))

    assert ("B" in clientA["blockedUsers"]) == True

    cm.block("A", "B", "unblock")
    assert ("B" in clientA["blockedUsers"]) == False
示例#9
0
class MqttCallback:
    def __init__(self, mqttClient):
        self.mClientManager = ClientManager()
        self.mPlayerManager = PlayerManager(self.mClientManager)
        self.mNotifyToClient = NotifyToClient(mqttClient)
        self.mKeyboardClicker = KeyboardClicker()

    def on_connect(self, client, userdata, flags, rc):
        # print("on_connect", client, userdata, flags, rc)
        if rc == 0:  #Successful
            print("Connection on Success")
        print("my client id -", client)

    def on_message(self, client, userdata, msg):
        # print("on_message",client, userdata, msg)
        print("message -", msg.payload.decode("utf-8"))

        # message = msg.payload.decode('utf-8')
        message = json.loads(msg.payload.decode("utf-8"))
        # mlist = []
        # mlist = message.split()

        clientId = message["clientId"]

        # if mlist[0]== '12':
        # 	pSwitcher = "keyup"
        # else:

        # String uniqueID = UUID.randomUUID().toString(); #java android unique Id

        if self.mClientManager.isNewClient(clientId):
            print("new client connected -", clientId)
            self.mClientManager.appendNewClient(clientId)
            self.mNotifyToClient.updateClientNum()

        if self.mPlayerManager.isPlayer(clientId) == True:
            if message["purpose"] == END_CONNECT:
                self.mClientManager.removeClient(clientId)
                self.mNotifyToClient.notifyOppositePlayerDisconnect()
                self.mNotifyToClient.updateClientNum()

                if self.mPlayerManager.isPlayerIsEnough():
                    self.mNotifyToClient.notifyYourPlayer(
                        self.mClientManager.clientList[MAXIMUM_PLAYER_NUM])
                else:
                    self.mNotifyToClient.notifyPlayerIsNotEnough()

            elif message["purpose"] == ACTION:
                if not self.mPlayerManager.isPlayerIsEnough():
                    self.mNotifyToClient.notifyPlayerIsNotEnough()
                self.mKeyboardClicker.clickBroker(message["virtualKey"],
                                                  message["clickType"])

        else:
            if message["purpose"] == END_CONNECT:
                self.mClientManager.removeClient(clientId)

        #####################################
        # playerId = getPlayerId(clientId)
        # pSwitcher = msgToAscii(playerId)
        #
        # actionInt = message["actionInt"]
        # if actionInt in [9,10,11]:
        #     buttonClick(pSwitcher[actionInt])
        # elif actionInt in [1,2,3,4,5,6,7,8]:
        #     KeyboardClick(playerId, pSwitcher[actionInt])
        # elif actionInt in [12]:
        #     KeyUp(playerId, pSwitcher[actionInt])
        # elif actionInt in [99]:
        #     playerManager(playerId, clientId, pSwitcher[actionInt])

        # KeyboardClick([0x0002])
        # Delay(0.05)
        # client.publish(topicAp, None,0,True)

    def on_disconnect(self, client, userdata, rc):
        if rc != 0:
            print("Unexpected disconnection.")

    def on_subscribe(self, client, userdata, mid, granted_qos):
        # print("on_subscribe",client, userdata, mid, granted_qos)
        print("Subscribed to", TOPIC_AK)

    def on_unsubscribe(self, client, userdata, mid):
        # if self.mPlayerManager.isPlayer(client) == True:
        #     self.mClientManager.removeClient(client)
        #     self.mNotifyToClient.notifyOppositePlayerDisconnect()
        #     self.mNotifyToClient.updateClientNum()
        #
        #     if self.mPlayerManager.isPlayerIsEnough():
        #         self.mNotifyToClient.notifyYourPlayer(self.mClientManager.clientList[MAXIMUM_PLAYER_NUM])
        #     else:
        #         self.mNotifyToClient.notifyPlayerIsNotEnough()
        #
        # else:
        #     self.mClientManager.removeClient(client)
        pass

    def on_publish(self, client, userdata, mid):
        # print("on_publish",client, userdata, mid)
        # print("published")
        pass
示例#10
0
 def __init__(self, mqttClient):
     self.mClientManager = ClientManager()
     self.mPlayerManager = PlayerManager(self.mClientManager)
     self.mNotifyToClient = NotifyToClient(mqttClient)
     self.mKeyboardClicker = KeyboardClicker()