Example #1
0
  @Override
  public void handleMessage(Socket socket) {
    log(Level.DEBUG, MSG_TYPE.REC, socket, "JOIN_GAME");

    String startLocation =
        PropertiesLoader.getInstance().load("server.properties").getProperty("startlocation", "");
    String startMap = startLocation;
    int startX = 5, startY = 5;

    if (startLocation.contains(",")) {
      startMap = startLocation.split(",")[0].trim();
      startX = Integer.valueOf(startLocation.split(",")[1].trim());
      startY = Integer.valueOf(startLocation.split(",")[2].trim());
    }

    // Load the player from the database
    String playerName = socket.getInetAddress().getHostAddress();
    Player yourPlayer = new Player(playerName, "knightImage", startMap);
    if ((int) (Math.random() * 2) == 0) yourPlayer.setRef("mageImage");
    yourPlayer.setLocation(startX, startY);

    try {
      sendTo(socket, "SET_REF:YOU," + yourPlayer.getRef());
      sendTo(socket, "SET_NAME:YOU," + yourPlayer.getName());
      synchronized (this) {
        // Add the player to the game
        players.put(socket, yourPlayer);
        String joinGameMessage =
            "CHAT:BROADCAST,#00FF00," + yourPlayer.getName() + " has joined the game!";
        String welcomeMessage = "CHAT:BROADCAST,#00FFFF,Welcome to SimpleOrpg!";
        sendTo(socket, welcomeMessage);
        WhoHandler whoHandler = new WhoHandler();
        whoHandler.handleMessage(socket);
        sendAll(joinGameMessage);
      }
      MessageHandler joinMapHandler = new JoinMapHandler();
      joinMapHandler.handleMessage(socket);

    } catch (Exception ex) {
      log(Level.ERROR, socket, ex.getMessage(), ex.getCause());
    }
  }