[chuni] Save recent rating to database

[ongeki] Save recent rating to database
This commit is contained in:
samnyan
2020-03-29 16:10:35 +09:00
parent 528b2f8b4d
commit 0cf510fb02
10 changed files with 223 additions and 20 deletions

View File

@@ -0,0 +1,20 @@
package icu.samnyan.aqua.sega.chunithm.dao.userdata;
import icu.samnyan.aqua.sega.chunithm.model.userdata.UserData;
import icu.samnyan.aqua.sega.chunithm.model.userdata.UserGeneralData;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
/**
* @author samnyan (privateamusement@protonmail.com)
*/
@Repository("ChuniUserGeneralDataRepository")
public interface UserGeneralDataRepository extends JpaRepository<UserGeneralData, Long> {
Optional<UserGeneralData> findByUserAndPropertyKey(UserData user, String key);
Optional<UserGeneralData> findByUser_Card_ExtIdAndPropertyKey(int extId, String key);
}

View File

@@ -2,18 +2,19 @@ package icu.samnyan.aqua.sega.chunithm.handler.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import icu.samnyan.aqua.sega.chunithm.handler.BaseHandler;
import icu.samnyan.aqua.sega.chunithm.model.userdata.UserGeneralData;
import icu.samnyan.aqua.sega.chunithm.service.UserGeneralDataService;
import icu.samnyan.aqua.sega.general.model.response.UserRecentRating;
import icu.samnyan.aqua.sega.chunithm.model.userdata.UserPlaylog;
import icu.samnyan.aqua.sega.chunithm.service.UserPlaylogService;
import icu.samnyan.aqua.sega.util.jackson.StringMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -29,25 +30,48 @@ public class GetUserRecentRatingHandler implements BaseHandler {
private final StringMapper mapper;
private final UserPlaylogService userPlaylogService;
private final UserGeneralDataService userGeneralDataService;
@Autowired
public GetUserRecentRatingHandler(StringMapper mapper, UserPlaylogService userPlaylogService) {
public GetUserRecentRatingHandler(StringMapper mapper, UserPlaylogService userPlaylogService, UserGeneralDataService userGeneralDataService) {
this.mapper = mapper;
this.userPlaylogService = userPlaylogService;
this.userGeneralDataService = userGeneralDataService;
}
@Override
public String handle(Map<String, Object> request) throws JsonProcessingException {
String userId = (String) request.get("userId");
List<UserPlaylog> top = userPlaylogService.getRecent30Plays(userId);
List<UserRecentRating> rating = top.stream().map(log -> new UserRecentRating(log.getMusicId(), log.getLevel(), "1030000", log.getScore()))
.collect(Collectors.toList());
Optional<UserGeneralData> recentOptional = userGeneralDataService.getByUserIdAndKey(userId, "recent_rating_list");
List<UserRecentRating> ratingList;
if(recentOptional.isPresent()) {
ratingList = new LinkedList<>();
String val = recentOptional.get().getPropertyValue();
if(StringUtils.isNotBlank(val) && val.contains(",")) {
String[] records = val.split(",");
for (String record :
records) {
String[] value = record.split(":");
ratingList.add(new UserRecentRating(
Integer.parseInt(value[0]),
Integer.parseInt(value[1]),
"1030000",
Integer.parseInt(value[2])
));
}
}
} else {
List<UserPlaylog> top = userPlaylogService.getRecent30Plays(userId);
ratingList = top.stream().map(log -> new UserRecentRating(log.getMusicId(), log.getLevel(), "1030000", log.getScore()))
.collect(Collectors.toList());
}
Map<String, Object> resultMap = new LinkedHashMap<>();
resultMap.put("userId", userId);
resultMap.put("length", rating.size());
resultMap.put("userRecentRatingList", rating);
resultMap.put("length", ratingList.size());
resultMap.put("userRecentRatingList", ratingList);
String json = mapper.write(resultMap);
logger.info("Response: " + json);

View File

@@ -7,6 +7,7 @@ import icu.samnyan.aqua.sega.chunithm.model.response.CodeResp;
import icu.samnyan.aqua.sega.chunithm.model.userdata.*;
import icu.samnyan.aqua.sega.chunithm.service.*;
import icu.samnyan.aqua.sega.general.model.Card;
import icu.samnyan.aqua.sega.general.model.response.UserRecentRating;
import icu.samnyan.aqua.sega.general.service.CardService;
import icu.samnyan.aqua.sega.util.jackson.StringMapper;
import org.slf4j.Logger;
@@ -44,9 +45,10 @@ public class UpsertUserAllHandler implements BaseHandler {
private final UserDataExService userDataExService;
private final UserCourseService userCourseService;
private final UserDuelService userDuelService;
private final UserGeneralDataService userGeneralDataService;
@Autowired
public UpsertUserAllHandler(StringMapper mapper, CardService cardService, UserDataService userDataService, UserCharacterService userCharacterService, UserGameOptionService userGameOptionService, UserGameOptionExService userGameOptionExService, UserMapService userMapService, UserItemService userItemService, UserMusicDetailService userMusicDetailService, UserActivityService userActivityService, UserPlaylogService userPlaylogService, UserChargeService userChargeService, UserDataExService userDataExService, UserCourseService userCourseService, UserDuelService userDuelService) {
public UpsertUserAllHandler(StringMapper mapper, CardService cardService, UserDataService userDataService, UserCharacterService userCharacterService, UserGameOptionService userGameOptionService, UserGameOptionExService userGameOptionExService, UserMapService userMapService, UserItemService userItemService, UserMusicDetailService userMusicDetailService, UserActivityService userActivityService, UserPlaylogService userPlaylogService, UserChargeService userChargeService, UserDataExService userDataExService, UserCourseService userCourseService, UserDuelService userDuelService, UserGeneralDataService userGeneralDataService) {
this.mapper = mapper;
this.cardService = cardService;
this.userDataService = userDataService;
@@ -62,6 +64,7 @@ public class UpsertUserAllHandler implements BaseHandler {
this.userDataExService = userDataExService;
this.userCourseService = userCourseService;
this.userDuelService = userDuelService;
this.userGeneralDataService = userGeneralDataService;
}
@@ -232,6 +235,23 @@ public class UpsertUserAllHandler implements BaseHandler {
}
// userRecentRatingList
if(upsertUserAll.getUserRecentRatingList() != null) {
List<UserRecentRating> userRecentRatingList = upsertUserAll.getUserRecentRatingList();
StringBuilder sb = new StringBuilder();
userRecentRatingList.forEach(userRecentRating -> {
sb.append(userRecentRating.getMusicId()).append(":");
sb.append(userRecentRating.getDifficultId()).append(":");
sb.append(userRecentRating.getScore()).append(",");
});
if(sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
}
UserGeneralData userGeneralData = userGeneralDataService.getByUserAndKey(newUserData, "recent_rating_list")
.orElseGet(() -> new UserGeneralData(newUserData, "recent_rating_list"));
userGeneralData.setPropertyValue(sb.toString());
userGeneralDataService.save(userGeneralData);
}
// userChargeList
if (upsertUserAll.getUserChargeList() != null) {

View File

@@ -0,0 +1,43 @@
package icu.samnyan.aqua.sega.chunithm.model.userdata;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
/**
* This is for storing the other data that doesn't need to save it in a separate table
* @author samnyan (privateamusement@protonmail.com)
*/
@Entity(name = "ChuniUserGeneralData")
@Table(name = "chuni_user_general_data")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserGeneralData implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore
private long id;
@JsonIgnore
@ManyToOne
@JoinColumn(name = "user_id")
private UserData user;
private String propertyKey;
private String propertyValue;
public UserGeneralData(UserData userData, String key) {
this.user = userData;
this.propertyKey = key;
this.propertyValue = "";
}
}

View File

@@ -0,0 +1,34 @@
package icu.samnyan.aqua.sega.chunithm.service;
import icu.samnyan.aqua.sega.chunithm.dao.userdata.UserGeneralDataRepository;
import icu.samnyan.aqua.sega.chunithm.model.userdata.UserData;
import icu.samnyan.aqua.sega.chunithm.model.userdata.UserGeneralData;
import org.springframework.stereotype.Service;
import java.util.Optional;
/**
* @author samnyan (privateamusement@protonmail.com)
*/
@Service("ChuniUserGeneralDataService")
public class UserGeneralDataService {
private final UserGeneralDataRepository userGeneralDataRepository;
public UserGeneralDataService(UserGeneralDataRepository userGeneralDataRepository) {
this.userGeneralDataRepository = userGeneralDataRepository;
}
public UserGeneralData save(UserGeneralData userGeneralData) {
return userGeneralDataRepository.save(userGeneralData);
}
public Optional<UserGeneralData> getByUserAndKey(UserData user, String key) {
return userGeneralDataRepository.findByUserAndPropertyKey(user, key);
}
public Optional<UserGeneralData> getByUserIdAndKey(String userId, String key) {
return userGeneralDataRepository.findByUser_Card_ExtIdAndPropertyKey(Integer.parseInt(userId), key);
}
}