コード例 #1
0
  // Method that allows the user to create a new game in the database
  public void CreateGame(
      ScreenFrame frame, ServerConnection server, User currentUser, Parsers parser) {

    try {

      // Defining variables from typed values in the CreateScreen panel
      String gamename = frame.getCreate().getTfGameName().getText();
      int mapSize = frame.getCreate().getTfMapSize();
      String controls = frame.getCreate().getTfControls().getText();

      // Checks whether the typed in values are legitimate
      // Strings controls and gamename can't be empty and the map size can't be 0
      if (!controls.equals("") && mapSize != 0 && !gamename.equals("")) {

        // Creates objects of/instansialize Game and Gamer classes
        Game game = new Game();
        Gamer gamer = new Gamer();

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

        // Sets id equal to the logged in user (the current user)
        gamer.setId(currentUser.getId());

        // Sets the above defined id as the host of the game
        game.setHost(gamer);

        // Sets the gamename and mapsize equal to the typed values
        game.setName(gamename);
        game.setMapSize(mapSize);

        // All of the above setters are used to @post the variables into the database
        //
        String json = new Gson().toJson(game);
        String message = parser.createParser(server.post(json, "games/"));

        // Checks if the received list of games contains the newly created game
        // If so a confirmation will be shown to the user
        if (message.equals(game.getName())) {

          JOptionPane.showMessageDialog(
              frame,
              "Game was created!\nIt's called " + game.getName(),
              "Success!",
              JOptionPane.INFORMATION_MESSAGE);
        }
      }

      // Prints a stacktrace when catching an unforeseen error
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #2
0
  // Method that makes it possible for the user to log in
  public User Login(ScreenFrame frame, ServerConnection server, User currentUser, Parsers parser) {

    // Sets variables username and password equal to the typed values in the LoginScreen panel
    String username = frame.getLoginScreen().getTfUsername().getText();
    String password = frame.getLoginScreen().getTfPassword().getText();

    // Try/catch for error handling through exceptions
    try {
      // If-statement for checking the typed values.
      // If the typed values aren't equal to "" (empty textfields) the method will be executed
      if (!username.equals("") & !password.equals("")) {

        User user = new User();

        // Sets the username and password for the logged in user equal to the typed values
        user.setUsername(username);
        user.setPassword(password);

        // Sends
        String json = new Gson().toJson(user);

        // Sends
        String message = parser.loginParser((server.post(json, "login/")), user);

        // Checks whether the received message is equal to the wanted one
        if (message.equals("Login successful")) {

          currentUser = user;

          // Uses the userParser method to get ..
          parser.userParser(server.get("users/" + currentUser.getId() + "/"), currentUser);

          // Leads the user to the usermenu
          frame.show(frame.USERSCREEN);

          // Returns the value/variable/object currentUser to define who's logged in
          return currentUser;

          // If the server can't fit the typed values
          // Responds following received message with a JOptionPane that will warn the user
        } else if (message.equals("Wrong username or password")) {

          JOptionPane.showMessageDialog(
              frame,
              "Wrong username or password. Please try again",
              "Error",
              JOptionPane.ERROR_MESSAGE);

          // If the error is elsewhere than in the typed values
          // Responds following received message with a JOptionPane that will warn the user
        } else if (message.equals("Error in JSON")) {

          JOptionPane.showMessageDialog(frame, "Error", "Error", JOptionPane.ERROR_MESSAGE);
        }
      }

      // Prints a stacktrace when catching an unforeseen error
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }