Esempio n. 1
0
    @Override
    public void actionPerformed(ActionEvent e) {
      switch (e.getActionCommand()) {
        case "Cancel":
          screen.show("menu");
          break;

        case "Delete game":
          Game deleteGame = new Game();
          deleteGame.setName(screen.getDeleteGamePanel().getSelectedGame());
          for (Game g : deleteGames) {
            if (g.getName().equals(screen.getDeleteGamePanel().getSelectedGame())) {
              deleteGame = g;
            }
          }
          // Besked om at spillet er slettet
          String message = api.deleteGame(deleteGame.getGameId());
          JOptionPane.showMessageDialog(screen, message);
          if (message.equals("Game was deleted")) {
            screen.getDeleteGamePanel().removeGame();
          }
          break;
      }
    }
Esempio n. 2
0
  // Method that allows the user to join a game and at the same time starting, determining who's the
  // winner
  public void JoinGame(
      ScreenFrame frame, ServerConnection server, User currentUser, Parsers parser) {

    // Try/catch for error handling through exceptions
    try {

      // Defines the variables gameId for the game and controls for the joining user
      // GamId is set as type long because the server apparently sends back long type variables
      long gameId = frame.getJoin().getTfGameId();
      String controls = frame.getJoin().getTfControls().getText();

      // Checks whether the opponent typed controls are legitimate
      if (!controls.equals("")) {

        // Instansierer/create new objects of game and gamer class
        Game game = new Game();
        Gamer gamer = new Gamer();

        // Sets the gamer id equal to the current users id
        gamer.setId(currentUser.getId());

        // Sets the typed in controls equal to gamer controls
        gamer.setControls(controls);

        // Sets the gameId equal to the typed gameId
        game.setGameId(gameId);

        // Sets the gamer to being the opponent
        game.setOpponent(gamer);

        String json = new Gson().toJson(game);

        String message = parser.messageParser(server.put("games/join/", json));

        // Checks whether the PUT-request succeeded
        if (message.equals("Game was joined")) {

          //
          Game joined = parser.joinParser(server.put("games/start/", json));

          joined = parser.joinParser(server.get("game/" + joined.getGameId() + "/"));

          // If the SnakeMasterId is equal to the opponent (current user)
          // the panel shows a message confirming that the opponent won
          if (joined.getSnakeMasterId() == currentUser.getId()) {

            JOptionPane.showMessageDialog(
                frame, "You joined the game and won!", "WINNER!", JOptionPane.INFORMATION_MESSAGE);

            // If the SnakeMasterId isn't equal to the current users
            // the panel shows a message confirming that the host won
          } else if (joined.getSnakeMasterId() != currentUser.getId()) {

            JOptionPane.showMessageDialog(
                frame,
                "You joined the game and lost!\nBuhuu..",
                "LOSER",
                JOptionPane.INFORMATION_MESSAGE);
          }

          // If the PUT-request message equals "Game closed"
          // the panel shows error message to the user
        } else if (message.equals("Game closed")) {

          JOptionPane.showMessageDialog(
              frame,
              "The Game is closed. Check your Game Id again",
              "NOPE",
              JOptionPane.ERROR_MESSAGE);
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }