Пример #1
0
 def newPlayer(self, playerData):
     for clientId, _, _ in self.players.values():
         if clientId == hnet.getClientId():
             raise PlayerAlreadyAdded("You are already a player in that game") # make custom exception for this 
     playerId = self.playerIds.nextId()
     self.players[playerId] = (hnet.getClientId(), hnet.getHandler(), playerData)
     self.clients[hnet.getClientId()] = playerId
     hnet.getHandler().runOnClose.append(lambda : self.delPlayer(playerId, hnet.getClientId()))
     self.startGame()
     self.sendGamePacket(("PlayerJoined", playerData))
     return playerId
Пример #2
0
 def joinGame(self, gameName, playerData):
     clientId = hnet.getClientId()
     if clientId in self.clients:
       raise PlayerAlreadyInGame(self.clients[clientId][0])
     if gameName not in self.games:
         self.games[gameName] = Game(gameName)
     playerId = self.games[gameName].newPlayer(playerData)
     self.clients[hnet.getClientId()] = (gameName, playerId)
     hnet.getHandler().runOnClose.append(lambda : self.exitGame(playerData))
     print "Player %s joined game: %s:" % (playerData, gameName) 
     return playerId
Пример #3
0
 def exitGame(self, playerData):
     clientId = hnet.getClientId()
     gameName, playerId = self.clients[clientId]
     print "Player %s left game: %s:" % (playerData, gameName)
     del self.clients[clientId]
     if not self.games[gameName].players:
       del self.games[gameName]
Пример #4
0
 def sendGamePacket(self, packet, srcPlayerId = None, dstPlayerId = None):
     if srcPlayerId == None:
       srcPlayerId = self.clients[hnet.getClientId()]
     with self.frame.lock: # we don't want the server to send out the frame end packet before another packet in that frame
         if dstPlayerId == None:        
           for clientId, handler, _ in self.players.values():
             handler.send((srcPlayerId, self.frame.value, packet))
         else:
           clientId, handler, _ = self.players[dstPlayerId]
           handler.send((srcPlayerId, self.frame.value, packet))