mirror of https://github.com/hykilpikonna/AquaDX
[chusan] Implement GetUserFavoriteItemHandler
parent
355d3a0011
commit
c242b74077
|
@ -30,5 +30,13 @@
|
||||||
- Can be disabled dynamically on GetGameSettingHandler
|
- Can be disabled dynamically on GetGameSettingHandler
|
||||||
- NAT hole punching, direct connection between cab to cab?
|
- NAT hole punching, direct connection between cab to cab?
|
||||||
- Session management required
|
- Session management required
|
||||||
- Fallback to CPU competitors if conditions met
|
- Fill player slots with CPU competitors if at least two player exist in a room
|
||||||
- ReflectorUri, Related?
|
- ReflectorUri, Related?
|
||||||
|
|
||||||
|
## User favorite kind types
|
||||||
|
| Kind | Name | Notes |
|
||||||
|
|------|-------------|----------------------------|
|
||||||
|
| 1 | Music | |
|
||||||
|
| 2 | Rival | Query ID to Rival Handlers |
|
||||||
|
| 3 | Character | |
|
||||||
|
| 4 | Unknown | |
|
|
@ -2,44 +2,86 @@ package icu.samnyan.aqua.sega.chusan.handler.impl;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import icu.samnyan.aqua.sega.chusan.handler.BaseHandler;
|
import icu.samnyan.aqua.sega.chusan.handler.BaseHandler;
|
||||||
|
import icu.samnyan.aqua.sega.chusan.model.response.data.UserFavorite;
|
||||||
|
import icu.samnyan.aqua.sega.chusan.model.userdata.UserGeneralData;
|
||||||
|
import icu.samnyan.aqua.sega.chusan.service.UserGeneralDataService;
|
||||||
import icu.samnyan.aqua.sega.util.jackson.StringMapper;
|
import icu.samnyan.aqua.sega.util.jackson.StringMapper;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
/**
|
|
||||||
* Handle GetUserFavoriteItem request
|
|
||||||
* @author yueou (yueou.xu@gmail.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Component("ChusanGetUserFavoriteItemHandler")
|
@Component("ChusanGetUserFavoriteItemHandler")
|
||||||
public class GetUserFavoriteItemHandler implements BaseHandler {
|
public class GetUserFavoriteItemHandler implements BaseHandler {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(GetUserFavoriteItemHandler.class);
|
private static final Logger logger = LoggerFactory.getLogger(GetUserFavoriteItemHandler.class);
|
||||||
|
|
||||||
private final StringMapper mapper;
|
private final StringMapper mapper;
|
||||||
|
|
||||||
|
private final UserGeneralDataService userGeneralDataService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public GetUserFavoriteItemHandler(StringMapper mapper) {
|
public GetUserFavoriteItemHandler(StringMapper mapper, UserGeneralDataService userGeneralDataService) {
|
||||||
this.mapper = mapper;
|
this.mapper = mapper;
|
||||||
|
this.userGeneralDataService = userGeneralDataService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String handle(Map<String, Object> request) throws JsonProcessingException {
|
public String handle(Map<String, Object> request) throws JsonProcessingException {
|
||||||
String userId = (String) request.get("userId");
|
String userId = (String) request.get("userId");
|
||||||
String kind = (String) request.get("kind");
|
int kind = Integer.parseInt((String) request.get("kind"));
|
||||||
|
Long nextIndexVal = Long.parseLong((String) request.get("nextIndex"));
|
||||||
|
int maxCount = Integer.parseInt((String) request.get("maxCount")); // Fixed to 50?
|
||||||
|
|
||||||
|
List<UserFavorite> userFavorites = new LinkedList<>();
|
||||||
|
Optional<UserGeneralData> favOptional;
|
||||||
|
|
||||||
|
switch (kind) {
|
||||||
|
case 1: // Music
|
||||||
|
favOptional = userGeneralDataService.getByUserIdAndKey(userId, "favorite_music");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3: // Chara
|
||||||
|
favOptional = userGeneralDataService.getByUserIdAndKey(userId, "favorite_chara");
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
favOptional = Optional.empty();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let's assume data is in following format:
|
||||||
|
// 1111,2222,3333,4444 ...
|
||||||
|
if(favOptional.isPresent()) {
|
||||||
|
String val = favOptional.get().getPropertyValue();
|
||||||
|
if(StringUtils.isNotBlank(val) && val.contains(",")) {
|
||||||
|
String[] records = val.split(",");
|
||||||
|
for (String record : records) {
|
||||||
|
userFavorites.add(new UserFavorite(Integer.parseInt(record)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// While client seems capable to handle more than 50 items, let's limit it to 50
|
||||||
|
// Reasons: 1. VARCHAR is limited to 255 chars (theoretically <= 51 entries), 2. Multiple pagination is troublesome
|
||||||
|
if (userFavorites.size() > 50) {
|
||||||
|
userFavorites = userFavorites.subList(0, 50);
|
||||||
|
}
|
||||||
|
|
||||||
Map<String, Object> resultMap = new LinkedHashMap<>();
|
Map<String, Object> resultMap = new LinkedHashMap<>();
|
||||||
resultMap.put("userId", userId);
|
resultMap.put("userId", userId);
|
||||||
resultMap.put("length", 0);
|
|
||||||
resultMap.put("kind", kind);
|
resultMap.put("kind", kind);
|
||||||
|
resultMap.put("length", userFavorites.size());
|
||||||
resultMap.put("nextIndex", -1);
|
resultMap.put("nextIndex", -1);
|
||||||
resultMap.put("userFavoriteItemList", List.of());
|
resultMap.put("userFavoriteItemList", userFavorites);
|
||||||
|
|
||||||
String json = mapper.write(resultMap);
|
String json = mapper.write(resultMap);
|
||||||
logger.info("Response: " + json);
|
logger.info("Response: " + json);
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
package icu.samnyan.aqua.sega.chusan.model.response.data;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author samnyan (privateamusement@protonmail.com)
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class UserFavorite {
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue