mirror of https://github.com/hykilpikonna/AquaDX
[O] Rewrite chusan item handler
parent
b41f3b9370
commit
15002c45d6
|
@ -1,67 +0,0 @@
|
|||
package icu.samnyan.aqua.sega.chusan.handler;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import icu.samnyan.aqua.sega.general.BaseHandler;
|
||||
import icu.samnyan.aqua.sega.chusan.model.userdata.UserItem;
|
||||
import icu.samnyan.aqua.sega.chusan.service.UserItemService;
|
||||
import icu.samnyan.aqua.sega.util.jackson.StringMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Handler for getting user item.
|
||||
* This get call before profile create.
|
||||
*
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
@Component("ChusanGetUserItemHandler")
|
||||
public class GetUserItemHandler implements BaseHandler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GetUserItemHandler.class);
|
||||
|
||||
private final StringMapper mapper;
|
||||
|
||||
private final UserItemService userItemService;
|
||||
|
||||
public GetUserItemHandler(StringMapper mapper, UserItemService userItemService) {
|
||||
this.mapper = mapper;
|
||||
this.userItemService = userItemService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String handle(Map<String, Object> request) throws JsonProcessingException {
|
||||
String userId = (String) request.get("userId");
|
||||
Long nextIndexVal = Long.parseLong((String) request.get("nextIndex"));
|
||||
int maxCount = Integer.parseInt((String) request.get("maxCount"));
|
||||
|
||||
Long mul = 10000000000L;
|
||||
|
||||
int kind = (int) (nextIndexVal / mul);
|
||||
int nextIndex = (int) (nextIndexVal % mul);
|
||||
int pageNum = nextIndex / maxCount;
|
||||
|
||||
Page<UserItem> userItemPage = userItemService.getByUserAndItemKind(userId, kind, pageNum, maxCount);
|
||||
|
||||
List<UserItem> userItemList = userItemPage.getContent();
|
||||
|
||||
long currentIndex = kind * mul + maxCount * pageNum + userItemPage.getNumberOfElements();
|
||||
|
||||
Map<String, Object> resultMap = new LinkedHashMap<>();
|
||||
resultMap.put("userId", userId);
|
||||
resultMap.put("length", userItemPage.getNumberOfElements());
|
||||
resultMap.put("nextIndex", userItemPage.getNumberOfElements() < maxCount ? -1 : currentIndex);
|
||||
resultMap.put("itemKind", kind);
|
||||
resultMap.put("userItemList", userItemList);
|
||||
|
||||
String json = mapper.write(resultMap);
|
||||
logger.info("Response: " + json);
|
||||
return json;
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package icu.samnyan.aqua.sega.chusan.handler
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException
|
||||
import ext.int
|
||||
import ext.long
|
||||
import icu.samnyan.aqua.sega.chusan.service.UserItemService
|
||||
import icu.samnyan.aqua.sega.general.BaseHandler
|
||||
import icu.samnyan.aqua.sega.util.jackson.StringMapper
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
/**
|
||||
* Handler for getting user item.
|
||||
* This get call before profile create.
|
||||
*
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
@Component("ChusanGetUserItemHandler")
|
||||
class GetUserItemHandler(private val mapper: StringMapper, private val userItemService: UserItemService) : BaseHandler {
|
||||
@Throws(JsonProcessingException::class)
|
||||
override fun handle(request: Map<String, Any>): String {
|
||||
val userId = request["userId"].toString()
|
||||
val nextIndexVal = request["nextIndex"]?.long() ?: 0
|
||||
val maxCount = request["maxCount"]?.int() ?: 100
|
||||
|
||||
val mul = 10000000000L
|
||||
|
||||
val kind = (nextIndexVal / mul).toInt()
|
||||
val nextIndex = (nextIndexVal % mul).toInt()
|
||||
val pageNum = nextIndex / maxCount
|
||||
|
||||
val userItemPage = userItemService.getByUserAndItemKind(userId, kind, pageNum, maxCount)
|
||||
val userItemList = userItemPage.content
|
||||
val currentIndex = kind * mul + maxCount * pageNum + userItemPage.numberOfElements
|
||||
|
||||
// TODO: Music unlock
|
||||
|
||||
val resultMap = mapOf(
|
||||
"userId" to userId,
|
||||
"length" to userItemPage.numberOfElements,
|
||||
"nextIndex" to if (userItemPage.numberOfElements < maxCount) -1 else currentIndex,
|
||||
"itemKind" to kind,
|
||||
"userItemList" to userItemList
|
||||
)
|
||||
|
||||
val json = mapper.write(resultMap)
|
||||
logger.info("Response: $json")
|
||||
return json
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val logger: Logger = LoggerFactory.getLogger(GetUserItemHandler::class.java)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue