Exemplo n.º 1
0
 /**
  * Sends a message to all players in the current game.<br>
  * The message will not be sent to players with the {@link Player#isAi} flag.
  *
  * @param message The message to be sent.
  */
 public void sendMessageToAllPlayers(Message message) {
   for (Player p : players) {
     if (p.isAi() == false || p.getSocket() != null) {
       message.send(p.getSocket(), p);
     }
   }
 }
Exemplo n.º 2
0
  private void tell(String message) throws IOException {
    String recipient;

    if (message.indexOf(' ') != -1) {
      recipient = message.substring(0, message.indexOf(' '));
      message = message.substring(message.indexOf(' ') + 1);
    } else {
      Telnet.writeLine(cs, "<fgred>Tell what to whom?<reset>\n");
      return;
    }

    Player p;

    if ((p = s.getPlayerDB().searchName(recipient)) != null) {
      Telnet.writeLine(
          p.getSocket(),
          "<bright><fgyellow>" + player.Name() + " tells you \"" + message + "\"<reset>\n");
    } else {
      Telnet.writeLine(cs, "<fgred>No player " + recipient + " found<reset>\n");
      return;
    }
  }
Exemplo n.º 3
0
 /**
  * Sends a message to the current player. The message will not be sent to players with the {@link
  * Player#isAi} flag.
  *
  * @param message The message to be sent.
  */
 public void sendMessageToCurrentPlayer(Message message) {
   if (currentPlayer.isAi() == false) {
     message.send(currentPlayer.getSocket(), currentPlayer);
   }
 }
Exemplo n.º 4
0
 /**
  * Sends a message to a single player. The message will not be sent to players with the {@link
  * Player#isAi} flag.
  *
  * @param message The message to be sent.
  * @param player The player who will receive the message.
  */
 public void sendMessageToPlayer(Message message, Player player) {
   if (player.isAi() == false) {
     message.send(player.getSocket(), player);
   }
 }
  @Override
  public void run() {
    while (true) {
      try {
        is = new BufferedReader(new InputStreamReader(player.getSocket().getInputStream()));
        line = is.readLine();
        if (line != null) {
          int type = Message.getMessageType(line);
          System.out.println("Message type : " + type);
          List<String> message = Message.getMessage(line);

          switch (type) {

              // USER CONNECT WITH USERNAME
            case 0:
              {
                player.setNickName(message.get(1));
                System.out.println("Player set name " + message.get(1));
                player.sendMessage("user," + player.getId());

                // Client State --> Room Screen, Server Send List Room
                String roomList = "";
                for (Room r : Server.rooms) {
                  roomList +=
                      "," + r.getId() + "," + r.getName() + "," + r.getClients().size() + ", true";
                }
                player.sendMessage("roomlist" + roomList);
                break;
              }

              // USER MOVE
            case 1:
              {
                List<Player> clients = player.getRoom().getClients();
                Room room = player.getRoom();

                int row = Integer.parseInt(message.get(1));
                int col = Integer.parseInt(message.get(2));

                room.Board[row][col] = player.getId();
                List<int[]> res = room.checkWin(row, col, player.getId());

                // This user win
                String converted = "";
                if (!res.isEmpty()) {
                  for (int[] cell : res) {
                    converted += "," + cell[0] + "," + cell[1];
                  }
                  //				room.broadcastMessageWithEx(player, "win," + player.getNickName()+converted
                  // );
                  player.sendMessage("youwin" + converted);
                  player
                      .getRoom()
                      .broadcastMessageWithEx(player, "win," + player.getNickName() + converted);
                  System.out.println("Hasil :" + converted);
                }

                Player turn = clients.get((clients.indexOf(player) + 1) % clients.size());
                room.broadcastMessageToAll(
                    "move,"
                        + message.get(1)
                        + ","
                        + message.get(2)
                        + ","
                        + player.getId()
                        + ","
                        + turn.getId()
                        + ","
                        + turn.getNickName());
                System.out.println(
                    player.getNickName() + " Move to ROW : " + row + " COL : " + col);
                break;
              }

              // USER JOIN ROOM
            case 2:
              {
                Room theRoom = Server.getRoomById(Integer.parseInt(message.get(1)));
                player.setRoom(theRoom);
                player.sendMessage("user," + player.getId());
                theRoom.broadcastMessageWithEx(
                    player, "message,Server," + player.getNickName() + " joins this room ");
                if (theRoom.isStart()) {
                  theRoom.getCreator().sendMessage("canstart");
                }
                System.out.println(
                    "USer  " + player.getNickName() + " join room, room : " + theRoom.getName());
                break;
              }
              // USER CREATE ROOM
            case 3:
              {
                String roomName = message.get(1);
                player.createRoom(roomName);
                player.sendMessage("user," + player.getId());
                System.out.println(player.getNickName() + " create room, room name " + roomName);
                String roomList = "";
                for (Room r : Server.rooms) {
                  roomList +=
                      "," + r.getId() + "," + r.getName() + "," + r.getClients().size() + ", true";
                }
                Server.broadcastMessage("roomlist" + roomList);

                Room theRoom = player.getRoom();
                if (theRoom.isStart()) {
                  theRoom.getCreator().sendMessage("canstart");
                }
                break;
              }
              // PLAYER DISCONNECT
            case 4:
              {
                Room room = player.getRoom();
                try {
                  room.deleteClient(player);
                  room.broadcastMessageToAll("disconnect," + player.getNickName());
                  Server.deleteClient(this);
                  if (room.getClients().isEmpty()) {
                    Server.deletRoom(room);
                    System.out.println("Current Rooms on Server :" + Server.rooms);
                  } else if (message.get(1).equals("ingame")) {
                    List<Player> clients = room.getClients();
                    Player turn = clients.get((clients.indexOf(player) + 1) % clients.size());
                    room.broadcastMessageToAll("turn," + turn.getId() + "," + turn.getNickName());
                  } else if (room.isCreator(player)) {
                    // Change the creator
                    Player newCreator =
                        room.getClients().get(room.getClients().indexOf(player) + 1);
                    room.setCreator(newCreator);
                    newCreator.sendMessage("newcreator");
                  } else {
                    // Check the number to start still sufficient
                    if (room.isStart()) {
                      room.getCreator().sendMessage("canstart");
                    } else {
                      room.getCreator().sendMessage("cantstart");
                    }
                  }

                } catch (NullPointerException ex) {
                  System.out.println(player.getNickName() + " has Disconnected");
                }
                System.out.println(player.getNickName() + " Has DISCONNECTED");
                break;
              }

              // GET START SIGNAL FROM CREATOR
            case 5:
              {
                System.out.println("Get Start Game Signal");
                Room room = player.getRoom();
                room.broadcastMessageToAll("start," + player.getId() + "," + player.getNickName());
                break;
              }

              //			 JOIN ROOM TO SPECTATE
            case 6:
              {
                Room theRoom = Server.getRoomById(Integer.parseInt(message.get(1)));
                player.setRoomAsSpectator(theRoom);
                player.sendMessage("spectate," + player.getId());
                theRoom.broadcastMessageToSpectatorsWithEx(
                    player, "message,Server," + player.getNickName() + " Spectates This Room ");
                System.out.println(
                    "Uuer  "
                        + player.getNickName()
                        + " spectate room, room : "
                        + theRoom.getName());
                break;
              }

              // Spectator Disconnect
            case 7:
              {
                Room room = player.getRoom();
                try {
                  room.deleteSpectator(player);
                  room.broadcastMessageToSpectators("disconnect," + player.getNickName());
                  Server.deleteClient(this);
                } catch (NullPointerException ex) {
                  System.out.println(player.getNickName() + " has Disconnected");
                }
                System.out.println(player.getNickName() + " Has DISCONNECTED");
                break;
              }

              // CHAT WITH OTHER SPECTATORS
            case 8:
              {
                Room room = player.getRoom();
                room.broadcastMessageToSpectators(
                    "message," + player.getNickName() + "," + message.get(1));
                System.out.println("BroadCast Message SPECTATOR " + message.get(1));
                break;
              }

            case -1:
              {
                Room room = player.getRoom();
                room.broadcastMessageToPlayers(
                    "message," + player.getNickName() + "," + message.get(0));
                System.out.println("BroadCast Message " + message.get(0));
                break;
              }
          }
          // Stop thread
          if (type == 4 || type == 7) {
            break;
          }
        }
      } catch (IOException ex) {
        ex.printStackTrace();
        System.out.println("I/O fail");
        System.exit(-1);
      }
    }
  }
 public void sendMessage(String message) throws IOException {
   is = new BufferedReader(new InputStreamReader(player.getSocket().getInputStream()));
   this.player.sendMessage(message);
 }