From 9b6c58269c4f658620ceccc8de8caf5fd1a01fe9 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Wed, 6 Aug 2025 13:00:08 +0300 Subject: [PATCH] feat: backend to send game status for the user --- .../wesal/controller/PuzzleController.java | 20 +++++++++-- .../wesal/dto/DailyChallengeResponse.java | 34 ++++++++++++++++++- .../wesal/wesal/dto/LeaderboardEntry.java | 12 ++++++- .../wesal/wesal/service/PuzzleService.java | 7 ++++ 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/backend/src/main/java/online/wesal/wesal/controller/PuzzleController.java b/backend/src/main/java/online/wesal/wesal/controller/PuzzleController.java index 9fdc997..538853b 100644 --- a/backend/src/main/java/online/wesal/wesal/controller/PuzzleController.java +++ b/backend/src/main/java/online/wesal/wesal/controller/PuzzleController.java @@ -14,7 +14,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.time.LocalDateTime; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @RestController @@ -27,13 +29,24 @@ public class PuzzleController { private PuzzleService puzzleService; @GetMapping("/dailyChallenge") - @Operation(summary = "Get daily challenge", description = "Returns today's Wordle word and whether user has solved it") + @Operation(summary = "Get daily challenge", description = "Returns today's Wordle word and user's attempt details") public ResponseEntity> getDailyChallenge() { try { Puzzle todaysPuzzle = puzzleService.getTodaysPuzzle(); - boolean solved = puzzleService.hasUserSolvedToday(); + Optional userAttempt = puzzleService.getUserTodaysAttempt(); - DailyChallengeResponse response = new DailyChallengeResponse(todaysPuzzle.getWord(), solved); + boolean attempted = userAttempt.isPresent(); + boolean solved = attempted && userAttempt.get().isSolved(); + Integer attempts = attempted ? userAttempt.get().getAttempts() : null; + LocalDateTime solveTime = attempted ? userAttempt.get().getSubmittedAt() : null; + + DailyChallengeResponse response = new DailyChallengeResponse( + todaysPuzzle.getWord(), + attempted, + solved, + attempts, + solveTime + ); return ResponseEntity.ok(ApiResponse.success(response)); } catch (RuntimeException e) { return ResponseEntity.ok(ApiResponse.error(e.getMessage())); @@ -64,6 +77,7 @@ public class PuzzleController { .map(attempt -> new LeaderboardEntry( attempt.getUser().getDisplayName(), attempt.getUser().getUsername(), + attempt.getUser().getAvatar(), attempt.getAttempts(), attempt.isSolved(), attempt.getSubmittedAt() diff --git a/backend/src/main/java/online/wesal/wesal/dto/DailyChallengeResponse.java b/backend/src/main/java/online/wesal/wesal/dto/DailyChallengeResponse.java index 6f74cc9..524b4c5 100644 --- a/backend/src/main/java/online/wesal/wesal/dto/DailyChallengeResponse.java +++ b/backend/src/main/java/online/wesal/wesal/dto/DailyChallengeResponse.java @@ -1,15 +1,23 @@ package online.wesal.wesal.dto; +import java.time.LocalDateTime; + public class DailyChallengeResponse { private String wordle; + private boolean attempted; private boolean solved; + private Integer attempts; + private LocalDateTime solveTime; public DailyChallengeResponse() {} - public DailyChallengeResponse(String wordle, boolean solved) { + public DailyChallengeResponse(String wordle, boolean attempted, boolean solved, Integer attempts, LocalDateTime solveTime) { this.wordle = wordle; + this.attempted = attempted; this.solved = solved; + this.attempts = attempts; + this.solveTime = solveTime; } public String getWordle() { @@ -20,6 +28,14 @@ public class DailyChallengeResponse { this.wordle = wordle; } + public boolean isAttempted() { + return attempted; + } + + public void setAttempted(boolean attempted) { + this.attempted = attempted; + } + public boolean isSolved() { return solved; } @@ -27,4 +43,20 @@ public class DailyChallengeResponse { public void setSolved(boolean solved) { this.solved = solved; } + + public Integer getAttempts() { + return attempts; + } + + public void setAttempts(Integer attempts) { + this.attempts = attempts; + } + + public LocalDateTime getSolveTime() { + return solveTime; + } + + public void setSolveTime(LocalDateTime solveTime) { + this.solveTime = solveTime; + } } \ No newline at end of file diff --git a/backend/src/main/java/online/wesal/wesal/dto/LeaderboardEntry.java b/backend/src/main/java/online/wesal/wesal/dto/LeaderboardEntry.java index 3066e44..09ac6a1 100644 --- a/backend/src/main/java/online/wesal/wesal/dto/LeaderboardEntry.java +++ b/backend/src/main/java/online/wesal/wesal/dto/LeaderboardEntry.java @@ -6,15 +6,17 @@ public class LeaderboardEntry { private String displayName; private String username; + private String avatar; private int attempts; private boolean solved; private LocalDateTime submittedAt; public LeaderboardEntry() {} - public LeaderboardEntry(String displayName, String username, int attempts, boolean solved, LocalDateTime submittedAt) { + public LeaderboardEntry(String displayName, String username, String avatar, int attempts, boolean solved, LocalDateTime submittedAt) { this.displayName = displayName; this.username = username; + this.avatar = avatar; this.attempts = attempts; this.solved = solved; this.submittedAt = submittedAt; @@ -36,6 +38,14 @@ public class LeaderboardEntry { this.username = username; } + public String getAvatar() { + return avatar; + } + + public void setAvatar(String avatar) { + this.avatar = avatar; + } + public int getAttempts() { return attempts; } diff --git a/backend/src/main/java/online/wesal/wesal/service/PuzzleService.java b/backend/src/main/java/online/wesal/wesal/service/PuzzleService.java index 4ab1e18..b2d16fc 100644 --- a/backend/src/main/java/online/wesal/wesal/service/PuzzleService.java +++ b/backend/src/main/java/online/wesal/wesal/service/PuzzleService.java @@ -113,6 +113,13 @@ public class PuzzleService { return attempt.isPresent(); } + public Optional getUserTodaysAttempt() { + User currentUser = userService.getCurrentUser(); + Puzzle todaysPuzzle = getTodaysPuzzle(); + + return puzzleAttemptRepository.findByUserAndPuzzle(currentUser, todaysPuzzle); + } + public PuzzleAttempt submitAttempt(int attempts, boolean solved) { User currentUser = userService.getCurrentUser(); Puzzle todaysPuzzle = getTodaysPuzzle();