/* (non-Javadoc) * @see com.jme3.network.MessageListener#messageReceived(java.lang.Object, com.jme3.network.Message) */ @Override public void messageReceived(HostedConnection source, Message message) { if (message instanceof StringMessage) { // do something with the message StringMessage stringMessage = (StringMessage) message; final String msg = "Server received '" + stringMessage.getMessage() + "' from client #" + source.getId(); System.out.println(msg); logger.log(Level.INFO, msg, stringMessage); } else if (message instanceof ChatMessage) { ChatMessage chatMessage = (ChatMessage) message; ChatMessage aPlayerSays = new ChatMessage(chatMessage.getPlayerID(), chatMessage.getMessage()); gameServer.broadcast(Filters.notEqualTo(source), aPlayerSays); final String chatMsg = "#" + chatMessage.getPlayerID() + " says " + chatMessage.getMessage(); logger.log(Level.INFO, chatMsg, chatMessage); } else if (message instanceof PlayerLeavingMessage) { PlayerLeavingMessage plm = (PlayerLeavingMessage) message; Player p = plm.getPlayer(); p.setLeaver(true); ServerHub.getInstance().removePlayer(p); gameServer.getConnection(source.getId()).close("You wanted to leave! Shame on you..."); gameServer.broadcast(Filters.notEqualTo(source), plm); } }
/* (non-Javadoc) * @see com.jme3.network.MessageListener#messageReceived(java.lang.Object, com.jme3.network.Message) */ @Override public void messageReceived(HostedConnection source, Message message) { if (message instanceof PlanetActionMessage) { PlanetActionMessage clientMessage = (PlanetActionMessage) message; PlanetActionMessage serverMessage = new PlanetActionMessage( clientMessage.getClientTime(), System.currentTimeMillis(), clientMessage.getActionName(), clientMessage.getPlayerID(), clientMessage.getPlayerState(), clientMessage.getPlanetID()); gameServer.broadcast(Filters.notEqualTo(source), serverMessage); final String planetActionMsg = "Client@" + new Date(clientMessage.getClientTime()).toString() + " | " + clientMessage.getActionName() + " from #" + clientMessage.getPlayerID() + "/" + clientMessage.getPlayerState().name + " moves ships to planet #" + clientMessage.getPlanetID(); logger.log(Level.INFO, planetActionMsg, serverMessage); } else if (message instanceof GeneralActionMessage) { GeneralActionMessage clientMessage = (GeneralActionMessage) message; GeneralActionMessage serverMessage = new GeneralActionMessage( clientMessage.getActionName(), clientMessage.getSender(), clientMessage.getSenderState(), clientMessage.getReciever(), clientMessage.getRecieverState()); gameServer.broadcast(Filters.notEqualTo(source), serverMessage); // final String generalMsg = // clientMessage.getActionName() + " from #" // + clientMessage.getSender() // + "/" + clientMessage.getSenderState().name // + " to #" + clientMessage.getReciever() // + "/" + clientMessage.getRecieverState().name; logger.log(Level.INFO, serverMessage.getActionName(), serverMessage); } }
/** * Syncronize level. * * @param tpf the tpf */ public void syncronizeLevel(float tpf) { if (serverState != ServerState.INGAME) { return; } LevelActionMessage message = new LevelActionMessage(seed, System.currentTimeMillis(), DeathmatchGameplay.getGameTick()); this.gameServer.broadcast(Filters.notEqualTo(host), message); }
/** * Respond player. * * @param p the p * @param connecting the connecting flag */ public void respondPlayer(Player p, boolean connecting) { if (!connectedPlayers.containsKey(p)) { return; } if (connecting) { boolean isHost = p.isHost(); HostedConnection hc = connectedPlayers.get(p); checkPlayersName(p); PlayerAcceptedMessage joiningPlayer = new PlayerAcceptedMessage(p, ServerHub.getPlayers(), isHost, true); hc.setAttribute("PlayerObject", p); hc.setAttribute("PlayerID", p.getID()); hc.setAttribute("PlayerName", p.getName()); gameServer.broadcast(Filters.equalTo(hc), joiningPlayer); final String connectMsg = "#" + p.getID() + "/" + p.getName() + " joined server."; logger.log(Level.INFO, connectMsg, joiningPlayer); // Collection<HostedConnection> connections = gameServer.getConnections(); for (Map.Entry<Player, HostedConnection> entrySet : connectedPlayers.entrySet()) { Player player = entrySet.getKey(); HostedConnection connection = entrySet.getValue(); if (player != null && !player.equals(p) && connection != null) { isHost = player.isHost(); PlayerAcceptedMessage otherPlayer = new PlayerAcceptedMessage(p, ServerHub.getPlayers(), isHost, false); gameServer.broadcast(Filters.equalTo(connection), otherPlayer); final String othersMsg = "Told #" + player.getID() + "/" + player.getName() + " that #" + p.getID() + "/" + p.getName() + " joined server."; logger.log(Level.INFO, othersMsg, joiningPlayer); } } } else { HostedConnection hc = connectedPlayers.get(p); PlayerLeavingMessage plm = new PlayerLeavingMessage(p); gameServer.broadcast(Filters.notEqualTo(hc), plm); final String leavingMsg = "#" + p.getID() + "/" + p.getName() + " left server."; logger.log(Level.INFO, leavingMsg, plm); connectedPlayers.remove(p); } }