feat: backend to send game status for the user

This commit is contained in:
sBubshait 2025-08-06 13:00:08 +03:00
parent 28fbdfba07
commit 9b6c58269c
4 changed files with 68 additions and 5 deletions

View File

@ -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<ApiResponse<DailyChallengeResponse>> getDailyChallenge() {
try {
Puzzle todaysPuzzle = puzzleService.getTodaysPuzzle();
boolean solved = puzzleService.hasUserSolvedToday();
Optional<PuzzleAttempt> 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()

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -113,6 +113,13 @@ public class PuzzleService {
return attempt.isPresent();
}
public Optional<PuzzleAttempt> 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();