Skip to content

Commit

Permalink
refactor: 솝탬프 랭킹 로직 개선 (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
kseysh authored Jul 17, 2024
2 parents a57d244 + 32f1073 commit 7768dfb
Show file tree
Hide file tree
Showing 51 changed files with 769 additions and 580 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sopt.app.application.service;
package org.sopt.app.application.app_service;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sopt.app.application.service;
package org.sopt.app.application.app_service;

import java.util.Comparator;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.sopt.app.common.utils.AnonymousNameGenerator;
import org.sopt.app.domain.entity.Friend;
import org.sopt.app.domain.enums.Friendship;
import org.sopt.app.interfaces.postgres.FriendRepository;
import org.sopt.app.interfaces.postgres.friend.FriendRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -25,6 +25,7 @@
@RequiredArgsConstructor
public class FriendService {

private final Random random = new Random();
private final FriendRepository friendRepository;

public List<Long> findAllFriendIdsByUserIdRandomly(Long userId, int limitNum) {
Expand Down Expand Up @@ -164,8 +165,6 @@ public Long getPokeFriendIdRandomly(Long userId) {
throw new NotFoundException(ErrorCode.FRIENDSHIP_NOT_FOUND.getMessage());
}

Random random = new Random();

return friends.get(random.nextInt(friends.size())).getFriendUserId();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.sopt.app.application.rank;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.sopt.app.application.soptamp.SoptampPointInfo.PartPoint;
import org.sopt.app.application.soptamp.SoptampPointInfo.PartRank;


@RequiredArgsConstructor
public class SoptampPartRankCalculator {

private final List<PartPoint> partPoints;

public List<PartRank> findAllPartRanks() {

return partPoints.stream().map(this::createPartRank).toList();
}

private PartRank createPartRank(PartPoint targetPartPoint) {

return PartRank.builder()
.part(targetPartPoint.part().getPartName())
.rank(getTargetPartRank(targetPartPoint))
.points(targetPartPoint.points())
.build();
}

private int getTargetPartRank(PartPoint targetPartPoint) {
int rankPoint = 1;

for (PartPoint comparisonPartPoint : partPoints) {
if (targetPartPoint.points() < comparisonPartPoint.points()) {
rankPoint++;
}
}
return rankPoint;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.sopt.app.application.rank;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.sopt.app.application.slack.SlackService;
import org.sopt.app.application.soptamp.SoptampPointInfo.Main;
import org.sopt.app.application.soptamp.SoptampPointInfo.Point;
import org.sopt.app.application.soptamp.SoptampUserInfo;

@RequiredArgsConstructor
public class SoptampUserRankCalculator {

private final List<SoptampUserInfo> soptampUsers;
private final List<Main> ranking = new ArrayList<>();
private int rankPoint = 1;

public List<Main> calculateRanking(List<Point> soptampPointList) {
soptampPointList.stream()
.sorted(Comparator.comparing(Point::getPoints).reversed())
.forEach(point ->
findSoptampUserInfo(point.getSoptampUserId())
.ifPresentOrElse(
user -> addUserToRanking(user, point.getPoints()),
() -> SlackService.sendSlackMessage(
"Warning",
"soptamp_point에 해당하지 않는 soptamp_user가 확인되었습니다.\n"
+ "soptampPointId: " + point.getId() + "\n"
+ "soptampUserId: " + point.getSoptampUserId()
)
));
return ranking;
}

private void addUserToRanking(SoptampUserInfo user, Long userPoint) {
ranking.add(Main.builder()
.rank(getCurrentRankPointAndIncrement())
.nickname(user.getNickname())
.profileMessage(user.getProfileMessage())
.point(userPoint)
.build());
}

private Optional<SoptampUserInfo> findSoptampUserInfo(Long soptampUserId) {
return soptampUsers.stream()
.filter(user -> user.getId().equals(soptampUserId))
.findAny();
}

private int getCurrentRankPointAndIncrement() {
return rankPoint++;
}

@Deprecated
public List<Main> calculateRank() {
return soptampUsers.stream().sorted(
Comparator.comparing(SoptampUserInfo::getTotalPoints).reversed())
.map(user -> Main.of(getCurrentRankPointAndIncrement(), user))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@

import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.sopt.app.domain.entity.Mission;
import org.sopt.app.domain.entity.SoptampPoint;
import org.sopt.app.domain.enums.Part;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SoptampPointInfo {

@Getter
@Builder
@ToString
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public static class Main {

private Integer rank;
private String nickname;
private Long point;
private String profileMessage;

public static Main of(Integer rank, SoptampUserInfo soptampUserInfo) {
return new Main(
rank,
soptampUserInfo.getNickname(),
soptampUserInfo.getTotalPoints(),
soptampUserInfo.getProfileMessage()
);
}
}

@Getter
@Builder
@ToString
public static class Detail {

private String nickname;
Expand All @@ -34,23 +43,33 @@ public static class Detail {

@Getter
@Builder
@ToString
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public static class Point {
private Long id;
private Long generation;
private Long soptampUserId;
private Long points;

public static Point of(Long id, Long generation, Long soptampUserId, Long points) {
return new Point(id, generation, soptampUserId, points);
public static Point of(SoptampPoint soptampPoint) {
return new Point(
soptampPoint.getId(),
soptampPoint.getGeneration(),
soptampPoint.getSoptampUserId(),
soptampPoint.getPoints()
);
}
}

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public static class PartRank {
private String part;
private Integer rank;
private Long points;
}

public record PartPoint(Part part, Long points) {

}
}
Loading

0 comments on commit 7768dfb

Please sign in to comment.