Exemplo n.º 1
0
  public void stop() {
    if (roomCreated) {
      closeRoom();
    }

    quitGlobal();
    disconnect(false);

    ((PreGameScreen) graphwar.getUI().getScreen(Constants.PRE_GAME_SCREEN)).refreshGlobalButton();
    ((GameScreen) graphwar.getUI().getScreen(Constants.GAME_SCREEN)).refreshGlobalButton();
  }
Exemplo n.º 2
0
  public void joinGlobalServer(String ip, int port, String playerName) throws IOException {
    connection = new Connection(ip, port);

    connection.sendMessage(URLEncoder.encode(playerName, "UTF-8"));

    this.localPlayer = playerName;

    this.running = true;
    new Thread(this).start();

    ((PreGameScreen) graphwar.getUI().getScreen(Constants.PRE_GAME_SCREEN)).refreshGlobalButton();
    ((GameScreen) graphwar.getUI().getScreen(Constants.GAME_SCREEN)).refreshGlobalButton();
  }
Exemplo n.º 3
0
  private void disconnect(boolean kick) {
    if (kick && running) {
      ((GlobalScreen) graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN))
          .showDisconnectMessage("You have been disconnected.");
    }

    running = false;

    ((GameScreen) graphwar.getUI().getScreen(Constants.GAME_SCREEN)).refreshGlobalButton();

    try {
      connection.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 4
0
  public synchronized void mouseMoved(MouseEvent e) {
    if (roomFocused) {
      int lastFocus = focusedRoomNum;

      focusedRoomNum = e.getY() / entryHeight;

      if (focusedRoomNum != lastFocus) {
        graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN).repaint();
      }

    } else {
      roomFocused = true;
      focusedRoomNum = e.getY() / entryHeight;
      graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN).repaint();
    }
  }
Exemplo n.º 5
0
  private void addRoom(
      String name, int roomID, String ipAddress, int port, int mode, int numPlayers) {
    Room room = new Room(name, roomID, ipAddress, port, mode, numPlayers);

    this.rooms.add(room);

    ((GlobalScreen) graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN)).refreshRooms();
  }
Exemplo n.º 6
0
  private void addPlayer(String name, int id) {
    LobbyPlayer player = new LobbyPlayer(name, id);

    synchronized (players) {
      this.players.add(player);
    }

    ((GlobalScreen) graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN)).refreshPlayers();
  }
Exemplo n.º 7
0
  private void updateRoom(int id, int gameMode, int numPlayers) {
    ListIterator<Room> itr = rooms.listIterator();

    while (itr.hasNext()) {
      Room room = itr.next();

      if (room.getRoomID() == id) {
        room.updateRoom(numPlayers, gameMode);
        break;
      }
    }

    ((GlobalScreen) graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN)).refreshRooms();
  }
Exemplo n.º 8
0
  private void removeRoom(int id) {
    ListIterator<Room> itr = rooms.listIterator();

    while (itr.hasNext()) {
      Room room = itr.next();

      if (room.getRoomID() == id) {
        rooms.remove(room);
        break;
      }
    }

    ((GlobalScreen) graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN)).refreshRooms();
  }
Exemplo n.º 9
0
  public void mouseReleased(MouseEvent e) {
    if (graphwar.getGameData().getGameState() == Constants.NONE) {
      int roomNum = e.getY() / entryHeight;

      if (roomNum < graphwar.getGlobalClient().getRooms().size()) {
        Room room = graphwar.getGlobalClient().getRooms().get(roomNum);

        ((GlobalScreen) graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN))
            .showMessage("Connecting...");

        try {
          graphwar.joinGame(room.getIp(), room.getPort());
          graphwar.getGameData().addPlayer(graphwar.getGlobalClient().getLocalPlayerName());
          graphwar.getUI().setScreen(Constants.PRE_GAME_SCREEN);
        } catch (IOException e1) {
          ((GlobalScreen) graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN))
              .showMessage("Could not connect.");
          graphwar.getGameData().disconnect();
          e1.printStackTrace();
        }
      }
    }
  }
Exemplo n.º 10
0
  private void removePlayer(int id) {
    synchronized (players) {
      ListIterator<LobbyPlayer> itr = players.listIterator();

      while (itr.hasNext()) {
        LobbyPlayer player = itr.next();

        if (player.getID() == id) {
          players.remove(player);
          break;
        }
      }
    }

    ((GlobalScreen) graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN)).refreshPlayers();
  }
Exemplo n.º 11
0
  private void addToChat(int playerId, String chatMessage) {
    String playerName = "Anon";

    synchronized (players) {
      ListIterator<LobbyPlayer> itr = players.listIterator();

      while (itr.hasNext()) {
        LobbyPlayer player = itr.next();

        if (player.getID() == playerId) {
          playerName = player.getName();
          break;
        }
      }
    }

    ((GlobalScreen) graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN))
        .addChat(playerName, chatMessage);
  }
Exemplo n.º 12
0
  private void handleMessage(String message) {
    // System.out.println("Received from "+"server: "+message);

    String[] info = new String[0];

    if (message != null) {
      info = message.split("&");
    }

    if (info.length > 0) {
      int code = Integer.parseInt(info[0]);

      switch (code) {
        case NetworkProtocol.NO_INFO:
          {
          }
          break;

        case NetworkProtocol.JOIN:
          {
            if (info.length == 3) {
              try {
                String name = URLDecoder.decode(info[1], "UTF-8");
                int id = Integer.parseInt(info[2]);

                addPlayer(name, id);
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          }
          break;

        case NetworkProtocol.SAY_CHAT:
          {
            if (info.length == 3) {
              try {
                int id = Integer.parseInt(info[1]);
                String chatMessage = URLDecoder.decode(info[2], "UTF-8");

                addToChat(id, chatMessage);
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          }
          break;

        case NetworkProtocol.ROOM_STATUS:
          {
            if (info.length == 4) {
              int roomID = Integer.parseInt(info[1]);
              int gameMode = Integer.parseInt(info[2]);
              int numPlayers = Integer.parseInt(info[3]);

              updateRoom(roomID, gameMode, numPlayers);
            }
          }
          break;

        case NetworkProtocol.CREATE_ROOM:
          {
            if (info.length == 5) {
              try {
                String roomName = URLDecoder.decode(info[1], "UTF-8");
                int roomID = Integer.parseInt(info[2]);
                String ipAddress = URLDecoder.decode(info[3], "UTF-8");
                int port = Integer.parseInt(info[4]);

                addRoom(roomName, roomID, ipAddress, port, Constants.NORMAL_FUNC, 0);
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          }
          break;

        case NetworkProtocol.LIST_PLAYERS:
          {
            if (info.length >= 2) {
              players.clear();

              int numPlayers = Integer.parseInt(info[1]);

              for (int i = 0; i < numPlayers; i++) {
                try {
                  String name = URLDecoder.decode(info[2 + 2 * i], "UTF-8");
                  int id = Integer.parseInt(info[3 + 2 * i]);

                  addPlayer(name, id);
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
            }
          }
          break;

        case NetworkProtocol.LIST_ROOMS:
          {
            if (info.length >= 2) {
              rooms.clear();

              int numRooms = Integer.parseInt(info[1]);

              for (int i = 0; i < numRooms; i++) {
                try {
                  String roomName = URLDecoder.decode(info[2 + 6 * i], "UTF-8");
                  int roomID = Integer.parseInt(info[3 + 6 * i]);
                  String ipAddress = URLDecoder.decode(info[4 + 6 * i], "UTF-8");
                  int port = Integer.parseInt(info[5 + 6 * i]);
                  int mode = Integer.parseInt(info[6 + 6 * i]);
                  int numPlayers = Integer.parseInt(info[7 + 6 * i]);

                  addRoom(roomName, roomID, ipAddress, port, mode, numPlayers);
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
            }
          }
          break;

        case NetworkProtocol.CLOSE_ROOM:
          {
            if (info.length == 2) {
              int roomID = Integer.parseInt(info[1]);

              removeRoom(roomID);
            }
          }
          break;

        case NetworkProtocol.ROOM_INVALID:
          {
            this.roomInvalid = true;

            graphwar.getUI().getScreen(Constants.PRE_GAME_SCREEN).repaint();
          }
          break;

        case NetworkProtocol.QUIT:
          {
            if (info.length == 2) {
              int playerID = Integer.parseInt(info[1]);

              removePlayer(playerID);
            }
          }
          break;
      }
    }
  }
Exemplo n.º 13
0
  public void mouseExited(MouseEvent e) {
    roomFocused = false;

    graphwar.getUI().getScreen(Constants.GLOBAL_ROOM_SCREEN).repaint();
  }