Esempio n. 1
0
  Node createDebugGeometry() {
    Line l = new Line(from.getPlanet().getPosition(), to.getPlanet().getPosition());

    line = new Geometry("Line #" + from.getPlanet().getID() + " to #" + to.getPlanet().getID(), l);

    Material material =
        new Material(
            SolarWarsApplication.getInstance().getAssetManager(),
            "Common/MatDefs/Misc/Unshaded.j3md");

    Player p = from.getPlanet().getOwner();
    ColorRGBA c;
    if (p == null) {
      c = ColorRGBA.White.clone();
      c.a = 0.5f;
      material.setColor("Color", c);
    } else {
      c = p.getColor();
      c.a = 0.5f;
      material.setColor("Color", c);
    }
    material.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha);
    line.setMaterial(material);

    createLabel();
    Node lineNode = new Node(line.getName() + "_Node");
    lineNode.attachChild(line);
    lineNode.attachChild(label);
    //        Vector3f pos = to.getPlanet().getPosition().
    //                subtract(from.getPlanet().getPosition());
    //        lineNode.setLocalTranslation(pos.mult(0.5f));
    return lineNode;
  }
Esempio n. 2
0
    /* (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);
      }
    }
Esempio n. 3
0
  private void checkPlayersName(Player p) {
    String name = p.getName();
    for (Map.Entry<Player, HostedConnection> entrySet : connectedPlayers.entrySet()) {
      Player player = entrySet.getKey();

      if (player.getName().equals(name) && p != player) {
        long rand = System.currentTimeMillis() % 2000;
        String s = rand + player.getName();
        s = s.substring(0, s.length() / 2);
        s = "" + s.hashCode();
        s = s.substring(s.length() / 2);
        p.getState().name = ("" + s);
        return;
      }
    }
  }
Esempio n. 4
0
  /** Enter level. */
  public void enterLevel() {
    Player hostPlayer = ServerHub.getHostPlayer();
    host = connectedPlayers.get(hostPlayer);
    final String hostMsg = "#" + hostPlayer.getID() + "/" + hostPlayer.getName() + " is host!";
    logger.log(Level.INFO, hostMsg, hostPlayer);

    for (Map.Entry<Player, HostedConnection> entrySet : connectedPlayers.entrySet()) {
      Player player = entrySet.getKey();
      final String playerMsg = "#" + player.getID() + "/" + player.getName();
      logger.log(Level.INFO, playerMsg, player);
    }
    serverState = ServerState.INGAME;
    gameServer.addMessageListener(
        gameplayListener, PlanetActionMessage.class, GeneralActionMessage.class);
  }
Esempio n. 5
0
  /**
   * 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);
    }
  }