Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,64 @@ public LeaderboardResponseDTO getLeaderboard(String currentEmail) {
int total = (int) userRepository.count();

List<User> topUsers =
userRepository.findAllByOrderByRatingDescIdAsc(PageRequest.of(0, EDGE_SIZE));
userRepository.findAllByOrderByRatingDescIdAsc(PageRequest.of(
0,
EDGE_SIZE
));

List<User> bottomUsers;
if (total <= EDGE_SIZE) {
bottomUsers = List.of();
} else {
int bottomCount = Math.min(EDGE_SIZE, total - EDGE_SIZE);
int bottomCount = Math.min(
EDGE_SIZE,
total - EDGE_SIZE
);
List<User> bottomAsc =
userRepository.findAllByOrderByRatingAscIdDesc(PageRequest.of(0, bottomCount));
userRepository.findAllByOrderByRatingAscIdDesc(PageRequest.of(
0,
bottomCount
));
bottomUsers = new ArrayList<>(bottomAsc);
Collections.reverse(bottomUsers);
}

List<LeaderboardEntryDTO> top = mapWithRank(topUsers, 1, currentEmail);
List<LeaderboardEntryDTO> top = mapWithRank(
topUsers,
1,
currentEmail
);
int bottomStartRank = total - bottomUsers.size() + 1;
List<LeaderboardEntryDTO> bottom = mapWithRank(bottomUsers, bottomStartRank, currentEmail);
List<LeaderboardEntryDTO> bottom = mapWithRank(
bottomUsers,
bottomStartRank,
currentEmail
);

User me = userRepository.findByEmail(currentEmail).orElse(null);
LeaderboardEntryDTO currentUser = null;
if (me != null) {
long above = userRepository.countByRatingGreaterThan(me.getRating());
int myRank = (int) above + 1;
currentUser = new LeaderboardEntryDTO(
myRank, me.getNickname(), me.getRating(),
avatarUrl(me.getEmail()), true);
myRank,
me.getNickname(),
me.getRating(),
avatarUrl(me.getEmail()),
true
);
}

return new LeaderboardResponseDTO(top, bottom, currentUser, total);
return new LeaderboardResponseDTO(
top,
bottom,
currentUser,
total
);
}

private List<LeaderboardEntryDTO> mapWithRank(List<User> users, int startRank, String currentEmail) {
private List<LeaderboardEntryDTO> mapWithRank(List<User> users, int startRank,
String currentEmail) {
List<LeaderboardEntryDTO> result = new ArrayList<>(users.size());
int rank = startRank;
for (User u : users) {
Expand All @@ -84,13 +111,20 @@ private List<LeaderboardEntryDTO> mapWithRank(List<User> users, int startRank, S

private String avatarUrl(String email) {
GetObjectRequest objectRequest = GetObjectRequest.builder()
.bucket(s3Settings.bucketName())
.key("icons/" + email)
.build();
.bucket(s3Settings.bucketName())
.key("icons/" + email)
.overrideConfiguration(cfg -> cfg.putHeader(
"Host",
"localhost:9000"
))
.build();
GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(10))
.getObjectRequest(objectRequest)
.build();
return presigner.presignGetObject(presignRequest).url().toExternalForm();
.signatureDuration(Duration.ofMinutes(10))
.getObjectRequest(objectRequest)
.build();
return presigner.presignGetObject(presignRequest).url().toExternalForm().replaceFirst(
"minio",
"localhost"
);
}
}
43 changes: 38 additions & 5 deletions src/main/java/com/codzilla/backend/MatchRoom/MatchController.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
package com.codzilla.backend.MatchRoom;

import com.codzilla.backend.MatchRoom.dto.MatchHistoryEntryDTO;
import com.codzilla.backend.PreMatch.model.Category;
import com.codzilla.backend.User.User;
import lombok.RequiredArgsConstructor;
import com.codzilla.backend.judge.problem.ProblemService;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;

import java.util.UUID;
import java.util.List;

@RestController
@Controller
@RequestMapping("/match")
@RequiredArgsConstructor
public class MatchController {

private final MatchService matchService;
private final ProblemService problemService;

public MatchController(MatchService matchService, ProblemService problemService) {
this.matchService = matchService;
this.problemService = problemService;
}

@GetMapping("/history")
@GetMapping("/{matchId}/options")
ResponseEntity<?> getMatchOptions(@AuthenticationPrincipal User user, @PathVariable UUID matchId) {
var match = matchService.getMatchById(matchId);
if (match == null) {
return ResponseEntity.notFound().build();
}
if (!(match.getFirstUserId().equals(user.getId()) ||
match.getSecondUserId().equals(user.getId()))) {
return ResponseEntity.status(HttpStatusCode.valueOf(404)).build();
}
try {
var statement = problemService.getStatementOfProblem(match.getProblem().getId());
var problem = problemService.getArtefactsOfProblem(match.getProblem().getId());
var options = match.getOptions();
var response = new MatchOptions(problem.getName(), statement, options.get(Category.Language), options.get(Category.ProblemType), options.get(Category.ProblemLevel));
return ResponseEntity.ok(response);
} catch (RuntimeException e) {
return ResponseEntity.notFound().build();
}
}

@GetMapping("/history")
public ResponseEntity<List<MatchHistoryEntryDTO>> getMatchHistory(@AuthenticationPrincipal User user) {
return ResponseEntity.ok(matchService.getMatchHistory(user.getId()));
}

}

11 changes: 11 additions & 0 deletions src/main/java/com/codzilla/backend/MatchRoom/MatchOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.codzilla.backend.MatchRoom;


public record MatchOptions(
String title,
String statement,
String language,
String problemType,
String problemLevel
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Problem pickProblemOfOptions(Map<Category, String> options) {
log.info("Pick problem of options: language={}, type={}, level={}",
language, problemType, problemLevel);

if (language != null && language.equalsIgnoreCase(Language.SQL.name())) {
if (language == null) {
log.info("Choosing SQL problem");
Problem sqlProblem = problemService.getOrCreateRandomSqlProblem(problemLevel);
log.info("Selected SQL problem: id={}, type={}", sqlProblem.getId(), sqlProblem.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
public Map<String, Category> categorySequence() {
return Map.of(
Language.PY.name(),
Category.ProblemType,
Category.ProblemLevel,
Language.CPP.name(),
Category.ProblemType,
Category.ProblemLevel,
Language.JAVA.name(),
Category.ProblemType,
Language.SQL.name(),
Category.ProblemLevel,

ProblemType.ALGORITHM.name(),
Category.ProblemLevel,
Category.Language,
ProblemType.MATH.name(),
Category.ProblemLevel,
Category.Language,
ProblemType.DATA_STRUCTURES.name(),
Category.Language,
ProblemType.SQL.name(),
Category.ProblemLevel
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public DraftSession startDraftSession(UUID matchId, UUID firstUserId, UUID secon
matchId,
firstUserId,
secondUserId,
Category.Language
Category.ProblemType
);
draftSessionRepository.save(draftSession);
startTimer(draftSession.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
public enum Language implements Option {
CPP(54),
PY(71),
SQL(0),
JAVA(63);

private final int value;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/codzilla/backend/User/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private String createPresignedGetUrl(String bucketName, String keyName) {
GetObjectRequest objectRequest = GetObjectRequest.builder()
.bucket(bucketName)
.key(keyName)
.overrideConfiguration(cfg -> cfg.putHeader("Host", "localhost:9000"))
.build();

GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder()
Expand All @@ -130,7 +131,7 @@ private String createPresignedGetUrl(String bucketName, String keyName) {
presignedRequest.httpRequest().method()
);

return presignedRequest.url().toExternalForm();
return presignedRequest.url().toExternalForm().replaceFirst("minio", "localhost");

}

Expand Down
Loading
Loading