Skip to content

Commit

Permalink
Refactored names of requests and responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
FabriDeCastelli committed Aug 21, 2023
1 parent 6ceb512 commit 60b5316
Show file tree
Hide file tree
Showing 23 changed files with 184 additions and 187 deletions.
60 changes: 30 additions & 30 deletions src/main/java/client/WordleClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import java.net.Socket;
import java.util.Optional;
import java.util.Properties;
import model.ServerResponse;
import model.Response;
import model.StreamHandler;
import model.User;
import model.UserRequest;
import model.enums.Request;
import model.enums.RequestType;

/**
* WordleClientMain is the client for the Wordle game.
Expand All @@ -27,8 +27,8 @@ public class WordleClientMain {
private static ObjectInputStream in;
private static ObjectOutputStream out;

private static final ServerResponse errorResponse =
new ServerResponse(-1, "Error sending request to server.");
private static final Response errorResponse =
new Response(-1, "Error sending requestType to server.");


/**
Expand Down Expand Up @@ -58,79 +58,79 @@ public static void main(String[] args) {


/**
* Sends a login request to the server.
* Sends a login requestType to the server.
*
* @param username the username of the user sending the request
* @param password the password of the user sending the request
* @param username the username of the user sending the requestType
* @param password the password of the user sending the requestType
*/
public static Optional<ServerResponse> login(String username, String password) {
public static Optional<Response> login(String username, String password) {
final PasswordHashingService passwordHashingService = PasswordHashingService.getInstance();
final String hashedPassword = passwordHashingService.hashPassword(password);
final User user = new User(username, hashedPassword);
final UserRequest userRequest = new UserRequest(Request.LOGIN, user);
final UserRequest userRequest = new UserRequest(RequestType.LOGIN, user);
if (StreamHandler.sendData(out, userRequest)) {
return StreamHandler.getData(in, ServerResponse.class);
return StreamHandler.getData(in, Response.class);
}
return Optional.of(errorResponse);
}

/**
* Sends a register request to the server.
* Sends a register requestType to the server.
*
* @param username the username of the user sending the request
* @param password the password of the user sending the request
* @param username the username of the user sending the requestType
* @param password the password of the user sending the requestType
*/
public static Optional<ServerResponse> register(String username, String password) {
public static Optional<Response> register(String username, String password) {
final PasswordHashingService passwordHashingService = PasswordHashingService.getInstance();
final String hashedPassword = passwordHashingService.hashPassword(password);
final User user = new User(username, hashedPassword);
final UserRequest userRequest = new UserRequest(Request.REGISTER, user);
final UserRequest userRequest = new UserRequest(RequestType.REGISTER, user);
if (StreamHandler.sendData(out, userRequest)) {
return StreamHandler.getData(in, ServerResponse.class);
return StreamHandler.getData(in, Response.class);
}
return Optional.of(errorResponse);
}

/**
* Sends a logout request to the server.
* Sends a logout requestType to the server.
*
* @param username the username of the user sending the request
* @param username the username of the user sending the requestType
*/
public static Optional<ServerResponse> logout(String username) {
public static Optional<Response> logout(String username) {
final User user = new User(username, "");
final UserRequest userRequest = new UserRequest(Request.LOGOUT, user);
final UserRequest userRequest = new UserRequest(RequestType.LOGOUT, user);
if (StreamHandler.sendData(out, userRequest)) {
return StreamHandler.getData(in, ServerResponse.class);
return StreamHandler.getData(in, Response.class);
}
return Optional.of(errorResponse);
}

/**
* Sends a play request to the server.
* Sends a play requestType to the server.
*
* @param username the username of the user sending the request
* @param username the username of the user sending the requestType
*/
public static Optional<ServerResponse> play(String username) {
public static Optional<Response> play(String username) {
final User user = new User(username, "");
final UserRequest userRequest = new UserRequest(Request.PLAY, user);
final UserRequest userRequest = new UserRequest(RequestType.PLAY, user);
if (StreamHandler.sendData(out, userRequest)) {
return StreamHandler.getData(in, ServerResponse.class);
return StreamHandler.getData(in, Response.class);
}
return Optional.of(errorResponse);
}

/**
* Sends a word to the server.
*
* @param username the username of the user sending the request
* @param username the username of the user sending the requestType
* @param word the word to send
* @return the response from the server
*/
public static Optional<ServerResponse> sendWord(String username, String word) {
public static Optional<Response> sendWord(String username, String word) {
final User user = new User(username, "");
final UserRequest userRequest = new UserRequest(Request.SENDWORD, user, word);
final UserRequest userRequest = new UserRequest(RequestType.SENDWORD, user, word);
if (StreamHandler.sendData(out, userRequest)) {
return StreamHandler.getData(in, ServerResponse.class);
return StreamHandler.getData(in, Response.class);
}
return Optional.of(errorResponse);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/client/gui/AuthenticationDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import model.ServerResponse;
import model.Response;
import model.enums.AuthType;

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ private void addActionEvent() {
@Override
public void actionPerformed(ActionEvent e) {

final Optional<ServerResponse> response;
final Optional<Response> response;

if (this.authType == AuthType.Login) {
response = WordleClientMain.login(usernameField.getText(),
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/client/gui/HomePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import model.ServerResponse;
import model.Response;

/**
* Interface for the home page.
Expand Down Expand Up @@ -44,7 +44,7 @@ public HomePage(String username) {

final JButton playButton = new JButton("Play");
playButton.addActionListener(e -> {
final Optional<ServerResponse> response = WordleClientMain.play(username);
final Optional<Response> response = WordleClientMain.play(username);
if (response.isEmpty()) {
JOptionPane.showMessageDialog(HomePage.this, "Server could not respond.");
} else if (response.get().status() == 0) {
Expand All @@ -61,7 +61,7 @@ public HomePage(String username) {

@Override
public void actionPerformed(ActionEvent e) {
final Optional<ServerResponse> response = WordleClientMain.logout(username);
final Optional<Response> response = WordleClientMain.logout(username);
if (response.isEmpty()) {
JOptionPane.showMessageDialog(this, "Server could not respond.");
} else if (response.get().status() == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/client/gui/play/PlayPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import model.ServerResponse;
import model.Response;
import model.WordHints;

/**
Expand Down Expand Up @@ -115,7 +115,7 @@ public void actionPerformed(ActionEvent e) {
Arrays.stream(inputFields[currentAttempt])
.forEach(field -> guess.append(field.getText()));

final Optional<ServerResponse> response =
final Optional<Response> response =
WordleClientMain.sendWord(username, guess.toString());

if (response.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@
/**
* Represents a response from the server.
*/
public record ServerResponse(int status, String message, Object data) implements Serializable {
public record Response(int status, String message, Object data) implements Serializable {

/**
* Constructor for ServerResponse. Sets data to null.
* Constructor for Response. Sets data to null.
*
* @param status the status of the response
* @param message the message of the response
*/
public ServerResponse(int status, String message) {
public Response(int status, String message) {
this(status, message, null);
}

/**
* Single argument constructor for ServerResponse.
* Single argument constructor for Response.
*
* @param status the status of the response
*/
public ServerResponse(int status) {
public Response(int status) {
this(status, "", null);
}

/**
* Two argument constructor for ServerResponse.
* Two argument constructor for Response.
*
* @param status the status of the response
* @param data the data of the response
*/
public ServerResponse(int status, Object data) {
public Response(int status, Object data) {
this(status, "", data);
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/model/UserRequest.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package model;

import java.io.Serializable;
import model.enums.Request;
import model.enums.RequestType;

/**
* Represents a user request.
* Represents a user requestType.
*/
public record UserRequest(Request request, User user, String word) implements Serializable {
public record UserRequest(RequestType requestType, User user, String word) implements Serializable {

/**
* Constructor for the UserRequest.
*
* @param request the request type
* @param user the user sending the request
* @param requestType the requestType type
* @param user the user sending the requestType
*/
public UserRequest(Request request, User user) {
this(request, user, null);
public UserRequest(RequestType requestType, User user) {
this(requestType, user, null);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package model.enums;

/**
* Represents a request.
* Represents a requestType.
*/
public enum Request {
public enum RequestType {

LOGIN,
REGISTER,
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/server/controller/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import model.ServerResponse;
import model.Response;
import model.StreamHandler;
import model.UserRequest;
import model.enums.Request;
import model.enums.RequestType;
import org.jetbrains.annotations.NotNull;
import server.model.Command;
import server.service.AuthenticationService;
Expand All @@ -22,23 +22,23 @@
import server.service.SendWordCommand;

/**
* Handles a request from a client.
* Handles a requestType from a client.
*/
public class RequestHandler implements Runnable, AutoCloseable {

private final Socket socket;
private final ObjectInputStream in;
private final ObjectOutputStream out;
private static final Map<Request, Command> commandMap;
private static final Map<RequestType, Command> commandMap;

static {
commandMap = new HashMap<>();
final AuthenticationService authenticationService = new AuthenticationService();
commandMap.put(Request.LOGIN, new LoginCommand(authenticationService));
commandMap.put(Request.LOGOUT, new LogoutCommand(authenticationService));
commandMap.put(Request.REGISTER, new RegisterCommand(authenticationService));
commandMap.put(Request.PLAY, new PlayCommand(new PlayWordleService()));
commandMap.put(Request.SENDWORD, new SendWordCommand(new PlayWordleService()));
commandMap.put(RequestType.LOGIN, new LoginCommand(authenticationService));
commandMap.put(RequestType.LOGOUT, new LogoutCommand(authenticationService));
commandMap.put(RequestType.REGISTER, new RegisterCommand(authenticationService));
commandMap.put(RequestType.PLAY, new PlayCommand(new PlayWordleService()));
commandMap.put(RequestType.SENDWORD, new SendWordCommand(new PlayWordleService()));
}


Expand All @@ -62,14 +62,14 @@ public void run() {
break;
}

final Command command = commandMap.get(userRequest.get().request());
final Command command = commandMap.get(userRequest.get().requestType());
if (command == null) {
throw new IllegalStateException("Command not found.");
}

final ServerResponse serverResponse = command.handle(userRequest.get());
final Response response = command.handle(userRequest.get());

if (!StreamHandler.sendData(out, serverResponse)) {
if (!StreamHandler.sendData(out, response)) {
break;
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/server/model/Command.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package server.model;

import java.io.IOException;
import java.io.ObjectOutputStream;
import model.ServerResponse;
import model.Response;
import model.UserRequest;
import org.jetbrains.annotations.NotNull;

Expand All @@ -12,12 +10,12 @@
public interface Command {

/**
* Handles a user request.
* Handles a user requestType.
*
* @param userRequest the user request
* @param userRequest the user requestType
* @return the server response
*/
ServerResponse handle(@NotNull UserRequest userRequest);
Response handle(@NotNull UserRequest userRequest);



Expand Down
Loading

0 comments on commit 60b5316

Please sign in to comment.