Ejemplo n.º 1
0
  def eventLoop(self):
    for line in self.serial:
      line = line.rstrip()
      print("<a-", repr(line))
      sys.stdout.flush()

      h = proto.MessageHandler()

      @h.handles(proto.HIT)
      def hit(sentTeam, sentPlayer, damage): # pylint: disable=W0612
        self.logic.hit(self.gameState, self.player, Player(sentTeam, sentPlayer), damage)
        return True

      @h.handles(proto.FULL_AMMO)
      def fullAmmo(): # pylint: disable=W0612
        self.logic.fullAmmo(self.gameState, self.player)
        return True

      @h.handles(proto.TRIGGER)
      def trigger(): # pylint: disable=W0612
        if (self.player and self.logic.trigger(self.gameState, self.player)):
          self.serialWrite(proto.FIRE.create(self.player.teamID, self.player.playerID, self.player.gunDamage))
        return True

      #TODO be more discerning about unparseable input here.
      h.handle(line)

      if (self.player):
        msg = proto.RECV.create(self.player.teamID, self.player.playerID, line)
      else:
        msg = proto.RECV.create(0, 0, line)
      self._sendToServer(msg)
Ejemplo n.º 2
0
    def recv(recvTeamStr, recvPlayerStr, line): # pylint: disable=W0612
      recvTeam = int(recvTeamStr)
      recvPlayer = int(recvPlayerStr)

      h2 = proto.MessageHandler()

      @h2.handles(proto.HIT)
      def hit(sentTeamStr, sentPlayerStr, damage): # pylint: disable=W0612
        sentTeam = int(sentTeamStr)
        sentPlayer = int(sentPlayerStr)

        serverTime = connection.clientTimeToServer(event.time)

        gameEvent = HitEvent(serverTime, recvTeam, recvPlayer, sentTeam, sentPlayer, damage)
        gameState.addEvent(gameEvent)

      @h2.handles(proto.TRIGGER)
      def trigger(): # pylint: disable=W0612
        #TODO: change this to Fire consistently.
        
        serverTime = connection.clientTimeToServer(event.time)

        gameEvent = FireEvent(serverTime, recvTeam, recvPlayer)
        gameState.addEvent(gameEvent)

      @h2.handles(proto.FULL_AMMO)
      def fullAmmo(): # pylint: disable=W0612
        serverTime = connection.clientTimeToServer(event.time)

        gameEvent = FullAmmoEvent(serverTime, recvTeam, recvPlayer)
        gameState.addEvent(gameEvent)

      return h2.handle(line)
Ejemplo n.º 3
0
    def handleMsg(self, fullLine):
        event = proto.parseEvent(fullLine)
        msgStr = event.msgStr

        h = proto.MessageHandler()

        @h.handles(proto.TEAMPLAYER)
        def teamPlayer(teamID, playerID):
            self.main.player = Player(teamID, playerID)
            return True

        @h.handles(proto.STARTGAME)
        def startGame(duration):
            self.main.gameState.setGameTime(int(duration))
            self.main.gameState.startGame()
            return True

        @h.handles(proto.STOPGAME)
        def stopGame():
            self.main.gameState.stopGame()
            return True

        @h.handles(proto.DELETED)
        def deleted():
            #just treat this as the game stopping for us.
            self.main.gameState.stopGame()
            return True

        return h.handle(msgStr)
Ejemplo n.º 4
0
        def recv(recvTeamStr, recvPlayerStr, line):
            recvTeam = int(recvTeamStr)
            recvPlayer = int(recvPlayerStr)
            player = gameState.getOrCreatePlayer(recvTeam, recvPlayer)
            player.lastContact = time.time()

            h2 = proto.MessageHandler()

            @h2.handles(proto.HIT)
            def hit(sentTeamStr, sentPlayerStr, damage):
                sentTeam = int(sentTeamStr)
                sentPlayer = int(sentPlayerStr)

                #TODO: add some sanity checks in here. The shooting player shouldn't be dead at this point
                self.logic.hit(gameState, player, sentTeam, sentPlayer, damage)
                gameState.playerUpdated.emit(recvTeam, recvPlayer)

                return True

            @h2.handles(proto.TRIGGER)
            def trigger():
                if (self.logic.trigger(gameState, player)):
                    gameState.playerUpdated.emit(recvTeam, recvPlayer)

                return True

            @h2.handles(proto.FULL_AMMO)
            def fullAmmo():
                if (self.logic.fullAmmo(gameState, player)):
                    gameState.playerUpdated.emit(recvTeam, recvPlayer)

                return True

            return h2.handle(line)
Ejemplo n.º 5
0
  def handleMsg(self, fullLine):
    event = proto.parseEvent(fullLine)
    msgStr = event.msgStr

    h = proto.MessageHandler()

    @h.handles(proto.TEAMPLAYER)
    def teamPlayer(teamID, playerID): # pylint: disable=W0612
      self.main.setPlayer(Player(teamID, playerID))
      
    @h.handles(proto.STARTGAME)
    def startGame(duration): # pylint: disable=W0612
      self.main.gameState.setGameTime(int(duration))
      self.main.gameState.startGame()
    
    @h.handles(proto.STOPGAME)
    def stopGame(): # pylint: disable=W0612
      self.main.gameState.stopGame()
    
    @h.handles(proto.DELETED)
    def deleted(): # pylint: disable=W0612
      #just treat this as the game stopping for us.
      self.main.gameState.stopGame()
      #then shutdown as the server won't want us back.
      self.main.shutdown()
    
    @h.handles(proto.RESETGAME)
    def resetGame(): # pylint: disable=W0612
      self.main.player.reset()
    
    @h.handles(proto.PING)
    def ping(): # pylint: disable=W0612
      self.queueMessage(proto.PONG.create(event.time, 0))
          
    @h.handles(proto.PONG)
    def pong(startTime, reply): # pylint: disable=W0612
      if int(reply):
        self.queueMessage(proto.PONG.create(event.time, 0))

    return h.handle(msgStr)
Ejemplo n.º 6
0
  def __handleEvent(self, event, gameState, connection):
    """handle an event, you must be holding self.eventLock before calling this"""
    msgStr = event.msgStr

    h1 = proto.MessageHandler()

    @h1.handles(proto.RECV)
    def recv(recvTeamStr, recvPlayerStr, line): # pylint: disable=W0612
      recvTeam = int(recvTeamStr)
      recvPlayer = int(recvPlayerStr)

      h2 = proto.MessageHandler()

      @h2.handles(proto.HIT)
      def hit(sentTeamStr, sentPlayerStr, damage): # pylint: disable=W0612
        sentTeam = int(sentTeamStr)
        sentPlayer = int(sentPlayerStr)

        serverTime = connection.clientTimeToServer(event.time)

        gameEvent = HitEvent(serverTime, recvTeam, recvPlayer, sentTeam, sentPlayer, damage)
        gameState.addEvent(gameEvent)

      @h2.handles(proto.TRIGGER)
      def trigger(): # pylint: disable=W0612
        #TODO: change this to Fire consistently.
        
        serverTime = connection.clientTimeToServer(event.time)

        gameEvent = FireEvent(serverTime, recvTeam, recvPlayer)
        gameState.addEvent(gameEvent)

      @h2.handles(proto.FULL_AMMO)
      def fullAmmo(): # pylint: disable=W0612
        serverTime = connection.clientTimeToServer(event.time)

        gameEvent = FullAmmoEvent(serverTime, recvTeam, recvPlayer)
        gameState.addEvent(gameEvent)

      return h2.handle(line)

    @h1.handles(proto.HELLO)
    def hello(): # pylint: disable=W0612
      clientId = event.id
      existingIds = self.listeningThread.isConnected(clientId)
      if existingIds:
        #TODO maintain the state of this client by sending it an update (taking our send queue into account).
        #For now, simply remove the ghost player from the game.
        self.gameState.deletePlayer(existingIds[0], existingIds[1])
        player = gameState.getOrCreatePlayer(existingIds[0], existingIds[1])
      else:
        player = gameState.createNewPlayer()
      connection.queueMessage(proto.TEAMPLAYER.create(player.teamID, player.playerID))

      self.listeningThread.establishConnection(connection, player, clientId)

      if self.gameState.isGameStarted():
        connection.queueMessage(proto.STARTGAME.create(self.gameState.gameTimeRemaining()))

    @h1.handles(proto.PING)
    def ping(): # pylint: disable=W0612
      connection.queueMessage(proto.PONG.create(event.time, 1))
          
    @h1.handles(proto.PONG)
    def pong(startTime, reply): # pylint: disable=W0612
      now = connection.timeProvider()
      latency = (now - int(startTime)) / 2
      connection.setLatency(latency)
      connection.setClientClockDrift(event.time - (now - latency))

      if int(reply):
        connection.queueMessage(proto.PONG.create(event.time, 0))
    
    return h1.handle(msgStr)
Ejemplo n.º 7
0
    def __handleEvent(self, event, gameState, connection):
        """handle an event, you must be holding self.eventLock before calling this"""
        msgStr = event.msgStr

        h1 = proto.MessageHandler()

        @h1.handles(proto.RECV)
        def recv(recvTeamStr, recvPlayerStr, line):
            recvTeam = int(recvTeamStr)
            recvPlayer = int(recvPlayerStr)
            player = gameState.getOrCreatePlayer(recvTeam, recvPlayer)
            player.lastContact = time.time()

            h2 = proto.MessageHandler()

            @h2.handles(proto.HIT)
            def hit(sentTeamStr, sentPlayerStr, damage):
                sentTeam = int(sentTeamStr)
                sentPlayer = int(sentPlayerStr)

                #TODO: add some sanity checks in here. The shooting player shouldn't be dead at this point
                self.logic.hit(gameState, player, sentTeam, sentPlayer, damage)
                gameState.playerUpdated.emit(recvTeam, recvPlayer)

                return True

            @h2.handles(proto.TRIGGER)
            def trigger():
                if (self.logic.trigger(gameState, player)):
                    gameState.playerUpdated.emit(recvTeam, recvPlayer)

                return True

            @h2.handles(proto.FULL_AMMO)
            def fullAmmo():
                if (self.logic.fullAmmo(gameState, player)):
                    gameState.playerUpdated.emit(recvTeam, recvPlayer)

                return True

            return h2.handle(line)

        @h1.handles(proto.HELLO)
        def hello():
            clientId = event.id
            existingIds = self.listeningThread.isConnected(clientId)
            if existingIds:
                #TODO maintain the state of this client by sending it an update (taking our send queu into account).
                #For now, simply remove the ghost player from the game.
                self.gameState.deletePlayer(existingIds[0], existingIds[1])

            player = gameState.createNewPlayer()
            connection.queueMessage(
                proto.TEAMPLAYER.create(player.teamID, player.playerID))

            self.listeningThread.establishConnection(connection, player,
                                                     clientId)

            if self.gameState.isGameStarted():
                connection.queueMessage(
                    proto.STARTGAME.create(self.gameState.gameTimeRemaining()))

            return True

        return h1.handle(msgStr)