Skip to content

Commit

Permalink
Refactored as command depend on the logged user.
Browse files Browse the repository at this point in the history
  • Loading branch information
FabriDeCastelli committed Aug 27, 2023
1 parent 89958fe commit c4b3435
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 22 deletions.
19 changes: 13 additions & 6 deletions src/main/java/server/service/command/PlayCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import model.enums.Status;
import org.jetbrains.annotations.NotNull;
import server.model.Command;
import server.service.AuthenticationService;
import server.service.PlayWordleService;
import server.service.UserStatisticsService;
import server.service.WordExtractionService;
Expand All @@ -18,18 +19,22 @@ public class PlayCommand implements Command {

private final PlayWordleService playWordleService;
private final UserStatisticsService userStatisticsService;
private final AuthenticationService authenticationService;

/**
* Constructor for PlayCommand.
*
* @param playWordleService the play wordle service
* @param playWordleService the play wordle service
* @param userStatisticsService the user statistics service
* @param authenticationService the authentication service
*/
public PlayCommand(
PlayWordleService playWordleService,
UserStatisticsService userStatisticsService) {
@NotNull PlayWordleService playWordleService,
@NotNull UserStatisticsService userStatisticsService,
@NotNull AuthenticationService authenticationService) {
this.playWordleService = playWordleService;
this.userStatisticsService = userStatisticsService;
this.authenticationService = authenticationService;
}

/**
Expand All @@ -41,13 +46,15 @@ public PlayCommand(
public Response handle(@NotNull Request request) {

if (request.requestType() != RequestType.PLAY) {
throw new IllegalArgumentException("Cannot handle a non-play requestType");
throw new IllegalArgumentException("Cannot handle a non-play request");
} else if (request.username() == null) {
throw new IllegalArgumentException("Cannot play a null user");
}

final String currentWord = WordExtractionService.getCurrentWord();
final String username = request.username();
final String username = authenticationService.getLoggedUser()
.orElseThrow(IllegalStateException::new)
.getUsername();

if (playWordleService.hasPlayed(username, currentWord)) {
return new Response(Status.FAILURE, "You have already played the game for this word.");
Expand All @@ -62,4 +69,4 @@ public Response handle(@NotNull Request request) {
return new Response(Status.FAILURE, "Error while saving the played game.");
}

}
}
2 changes: 1 addition & 1 deletion src/main/java/server/service/command/RegisterCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Response handle(@NotNull Request request) {

return user.getPasswordHash().isEmpty()
? new Response(Status.FAILURE, "Password cannot be empty.")
: authenticationService.registerUser(user)
: authenticationService.register(user)
? new Response(Status.SUCCESS, "Registration successful.")
: new Response(Status.FAILURE, "User already registered.");

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/server/service/command/SendMeStatisticsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import model.Request;
import model.Response;
import model.User;
import model.UserStatistics;
import model.enums.Status;
import org.jetbrains.annotations.NotNull;
import server.model.Command;
import server.service.AuthenticationService;
import server.service.UserStatisticsService;

/**
Expand All @@ -14,20 +16,27 @@
public class SendMeStatisticsCommand implements Command {

private final UserStatisticsService userStatisticsService;
private final AuthenticationService authenticationService;

/**
* Constructor for the SendMeStatisticsCommand.
*
* @param userStatisticsService the user statistics service
* @param authenticationService the authentication service
*/
public SendMeStatisticsCommand(@NotNull UserStatisticsService userStatisticsService) {
public SendMeStatisticsCommand(
@NotNull UserStatisticsService userStatisticsService,
@NotNull AuthenticationService authenticationService) {
this.userStatisticsService = userStatisticsService;
this.authenticationService = authenticationService;
}

@Override
public Response handle(@NotNull Request request) {
final User loggedUser =
authenticationService.getLoggedUser().orElseThrow(IllegalStateException::new);
final UserStatistics userStatistics =
userStatisticsService.getStatisticsByUsername(request.username());
userStatisticsService.getStatisticsByUsername(loggedUser.getUsername());
return new Response(Status.SUCCESS, userStatistics);
}
}
40 changes: 27 additions & 13 deletions src/main/java/server/service/command/SendWordCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import model.enums.Status;
import org.jetbrains.annotations.NotNull;
import server.model.Command;
import server.service.AuthenticationService;
import server.service.PlayWordleService;
import server.service.UserStatisticsService;
import server.service.WordExtractionService;
Expand All @@ -23,21 +24,25 @@ public class SendWordCommand implements Command {

private final PlayWordleService playWordleService;
private final UserStatisticsService userStatisticsService;
private final List<WordHints> wordHintsHistory = new ArrayList<>();
private final List<WordHints> wordHintsHistory;
private final AuthenticationService authenticationService;


/**
* Constructor for GuessWordCommand.
*
* @param playWordleService the play wordle service
* @param playWordleService the play wordle service
* @param userStatisticsService the user statistics service
* @param authenticationService the authentication service
*/
public SendWordCommand(
PlayWordleService playWordleService,
UserStatisticsService userStatisticsService) {
@NotNull PlayWordleService playWordleService,
@NotNull UserStatisticsService userStatisticsService,
@NotNull AuthenticationService authenticationService) {
this.playWordleService = playWordleService;
this.userStatisticsService = userStatisticsService;

this.wordHintsHistory = new ArrayList<>();
this.authenticationService = authenticationService;
}

@Override
Expand All @@ -49,16 +54,21 @@ public Response handle(@NotNull Request request) {
throw new IllegalArgumentException("Cannot send a null word");
}


final WordAttempt wordAttempt = getWordAttempt(request);


if (wordAttempt.attemptNumber() - 11 > 0) {
final String loggedUsername =
authenticationService.getLoggedUser()
.orElseThrow(IllegalStateException::new)
.getUsername();
final UserStatistics userStatistics =
userStatisticsService.getStatisticsByUsername(request.username());
userStatisticsService.getStatisticsByUsername(loggedUsername);
userStatistics.resetCurrentStreak();
userStatisticsService.updateStatistics(request.username(), userStatistics);
final GameResult gameResult =
new GameResult(request.username(), wordHintsHistory, userStatistics);
userStatisticsService.updateStatistics(loggedUsername, userStatistics);
final GameResult gameResult = new GameResult(
loggedUsername, new ArrayList<>(wordHintsHistory), userStatistics);
wordHintsHistory.clear();
return new Response(Status.SUCCESS,
"You have exceeded the maximum number of attempts.", gameResult);
Expand All @@ -68,15 +78,18 @@ public Response handle(@NotNull Request request) {
wordHintsHistory.add(wordHints);

if (wordAttempt.word().equals(WordExtractionService.getCurrentWord())) {
final String loggedUsername =
authenticationService.getLoggedUser()
.orElseThrow(IllegalStateException::new)
.getUsername();
final UserStatistics userStatistics =
userStatisticsService.getStatisticsByUsername(request.username());
userStatisticsService.getStatisticsByUsername(loggedUsername);
userStatistics.incrementGamesWon();
userStatistics.incrementCurrentStreak();
userStatistics.addTrials(wordAttempt.attemptNumber());
userStatisticsService.updateStatistics(request.username(), userStatistics);

userStatisticsService.updateStatistics(loggedUsername, userStatistics);
final GameResult gameResult = new GameResult(
request.username(), new ArrayList<>(wordHintsHistory), userStatistics);
loggedUsername, new ArrayList<>(wordHintsHistory), userStatistics);
wordHintsHistory.clear();
return new Response(Status.GUESS, "You guessed the word!", gameResult);
}
Expand All @@ -86,6 +99,7 @@ public Response handle(@NotNull Request request) {
}



/**
* Gets the word attempt from the request.
*
Expand Down

0 comments on commit c4b3435

Please sign in to comment.