Merge branch 'dev' into 'master'

[ongeki] GetUserEventRankingApi supports rank calculating

See merge request domeori/aqua!19
pull/1/head
Dom Eori 2023-03-09 13:53:24 +00:00
commit e65f050f48
3 changed files with 31 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package icu.samnyan.aqua.sega.ongeki.dao.userdata;
import icu.samnyan.aqua.sega.ongeki.model.userdata.UserData;
import icu.samnyan.aqua.sega.ongeki.model.userdata.UserEventPoint;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@ -21,4 +22,7 @@ public interface UserEventPointRepository extends JpaRepository<UserEventPoint,
@Transactional
void deleteByUser(UserData user);
@Query(value = "SELECT rank from (SELECT user_id , DENSE_RANK() OVER (ORDER BY point DESC) as rank from ongeki_user_event_point where event_id = :eventId) where user_id == :userId limit 1", nativeQuery = true)
int calculateRankByUserAndEventId(long userId, int eventId);
}

View File

@ -40,17 +40,15 @@ public class GetUserEventRankingHandler implements BaseHandler {
@Override
public String handle(Map<String, Object> request) throws JsonProcessingException {
long userId = ((Number) request.get("userId")).longValue();
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.0"));
// TODO: query ranking from database
List<UserEventPoint> eventPointList = userEventPointRepository.findByUser_Card_ExtId(userId);
List<UserEventRankingItem> rankingItemList = new LinkedList<>();
eventPointList.forEach(x -> rankingItemList.add(new UserEventRankingItem(
x.getEventId(),
1, // Type 1 is latest ranking
time,
1,
userEventPointRepository.calculateRankByUserAndEventId(x.getUser().getId(), x.getEventId()),
x.getPoint()
)));

View File

@ -12,6 +12,7 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import java.util.List;
import static icu.samnyan.aqua.util.CardHelper.getCard;
import static icu.samnyan.aqua.util.CardHelper.getRandomCard;
import static org.assertj.core.api.Assertions.assertThat;
/**
@ -175,6 +176,26 @@ class OngekiRepositoryTest {
assertThat(aL).hasSize(2);
}
@Test
void userEventPoint_Rank() {
var u1 = getNewRandomValidUser();
var u2 = getNewRandomValidUser();
var u3 = getNewRandomValidUser();
final var eventId = 2857;
userEventPointRepository.saveAll(List.of(
getEventPoint(u1, eventId, 500),
getEventPoint(u2, eventId, 600),
getEventPoint(u3, eventId, 2857)
));
var eventPointData = userEventPointRepository.findByUser_Card_ExtId(u1.getCard().getExtId()).get(0);
var rank = userEventPointRepository.calculateRankByUserAndEventId(eventPointData.getUser().getId(), eventPointData.getEventId());
assertThat(rank).isEqualTo(3);
}
@Test
void userGeneralData_SaveLoad() {
var c = cardRepository.save(getCard());
@ -402,7 +423,11 @@ class OngekiRepositoryTest {
}
private UserEventPoint getEventPoint(UserData u, Integer eventId) {
return new UserEventPoint(-1, u, eventId, 1, false);
return getEventPoint(u, eventId, 1);
}
private UserEventPoint getEventPoint(UserData u, Integer eventId, Integer point) {
return new UserEventPoint(-1, u, eventId, point, false);
}
private UserGeneralData getGeneralData(UserData u, String key, String value) {