Esempio n. 1
0
  /**
   * Sends the server time to the network player
   *
   * @param networkPlayer the player being contacted
   * @throws IOException if there is an error writing to the output stream
   */
  private void broadcastServerTime(NetworkPlayer networkPlayer) throws IOException {

    DataOutputStream out = networkPlayer.getOutStream();

    synchronized (out) {

      // Message type
      out.writeInt(NetworkConstants.S2C.SET_SERVER_TIME.ordinal());
      // Server time
      out.writeLong(getTime());
    }
  }
Esempio n. 2
0
  /**
   * Called when a player connects to the server.
   *
   * @param socket player socket
   * @throws IOException
   */
  public void onConnectReceived(Socket socket) throws IOException {

    System.out.println("Received connect, send info");

    DataOutputStream out = new DataOutputStream(socket.getOutputStream());

    synchronized (out) {

      // Create player object
      Player player = new Player(this, 0, 0);
      player.setPlayerClass(getRandomAnimalClass());

      // Add to team
      Team team = getTeamToJoin();
      team.addPlayer(player);

      // Send to spawnpoint
      player.setPosition(team.getSpawnPointCopy());

      // Register player
      player.register();

      // Store player+socket
      NetworkPlayer networkPlayer = new NetworkPlayer(socket, player);
      this.newNetworkPlayers.add(networkPlayer);

      // Message type
      out.writeInt(NetworkConstants.S2C.CREATE_ENTITIES.ordinal());
      // Number of entities
      out.writeInt(getEntities().size());

      // Entities
      synchronized (getEntities()) {
        for (Entity e : getEntities()) {

          // entity class name (without shared.entities. prefix)
          out.writeUTF(e.getClass().getSimpleName());
          // entity properties
          e.writeToNetStream(out);
        }
      }

      // Send teams info
      out.writeInt(NetworkConstants.S2C.CREATE_TEAM.ordinal());
      out.writeInt(getTeams().size());
      for (Team _team : getTeams()) {
        out.writeInt(_team.getID());
        out.writeUTF(_team.getTeamName());
        out.writeInt(_team.getTeamScore());
        out.writeInt(_team.getFlagCaptureRegion().getID());
      }

      // Send background texture
      out.writeInt(NetworkConstants.S2C.SET_BACKGROUND_TEXTURE.ordinal());
      out.writeUTF(mapFile.getBackgroundTexture());

      // Send server time
      out.writeInt(NetworkConstants.S2C.SET_SERVER_TIME.ordinal());
      out.writeLong(getTime());

      // Send player entity ID
      out.writeInt(NetworkConstants.S2C.SET_PLAYER_ID.ordinal());
      out.writeInt(player.getID());
    }
  }