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
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