|
@@ -101,53 +101,54 @@ public class RiceService {
|
|
|
|
|
|
|
|
//获取当前等级
|
|
//获取当前等级
|
|
|
public Map<String, Object> getCurrentLevel(Long empiricalValue) {
|
|
public Map<String, Object> getCurrentLevel(Long empiricalValue) {
|
|
|
|
|
+ String jsonString = sysConfigService.getString("rice_level");
|
|
|
User authenticatedUser = SecurityUtils.getAuthenticatedUser();
|
|
User authenticatedUser = SecurityUtils.getAuthenticatedUser();
|
|
|
Long authId = authenticatedUser.getId();
|
|
Long authId = authenticatedUser.getId();
|
|
|
|
|
+ Optional<Rice> byUserId = riceRepo.findByUserId(authId);
|
|
|
|
|
+ if (byUserId.isPresent()) {
|
|
|
|
|
+ Rice rice = byUserId.get();
|
|
|
|
|
+ JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
|
|
|
|
|
+ JsonArray jsonArray = jsonReader.readArray();
|
|
|
|
|
+ Long currentLevel = null;
|
|
|
|
|
+ Long currentStart = null;
|
|
|
|
|
+ Long nextStart = null;
|
|
|
|
|
+ for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
|
|
+ JsonObject jsonObject = jsonArray.getJsonObject(i);
|
|
|
|
|
+ Long start = Long.valueOf(jsonObject.getInt("start"));
|
|
|
|
|
+ if (empiricalValue >= start && empiricalValue < (i < jsonArray.size() - 1 ? Long.valueOf(jsonArray.getJsonObject(i + 1).getInt("start")) : Long.MAX_VALUE)) {
|
|
|
|
|
+ String currentLevelStr = jsonObject.getString("name").replace("Lv", "");
|
|
|
|
|
+ currentLevel = Long.parseLong(currentLevelStr);
|
|
|
|
|
+ rice.setLevel(currentLevel);
|
|
|
|
|
+ riceRepo.save(rice);
|
|
|
|
|
+ currentStart = Long.valueOf(jsonObject.getInt("start"));
|
|
|
|
|
+ if (i < jsonArray.size() - 1) {
|
|
|
|
|
+ JsonObject nextObject = jsonArray.getJsonObject(i + 1);
|
|
|
|
|
+ nextStart = nextObject.getJsonNumber("start").longValue();
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- Optional<Rice> optionalRice = riceRepo.findByUserId(authId);
|
|
|
|
|
- Rice rice = optionalRice.orElseThrow(() -> new BusinessException("用户不存在"));
|
|
|
|
|
-
|
|
|
|
|
- Map<String, Object> result = getRiceLevelJsonArray().stream()
|
|
|
|
|
- .map(JsonObject.class::cast)
|
|
|
|
|
- .map(jsonObject -> {
|
|
|
|
|
- Long start = Long.valueOf(jsonObject.getInt("start"));
|
|
|
|
|
- String currentLevelStr = jsonObject.getString("name").replace("Lv", "");
|
|
|
|
|
- Long currentLevel = Long.parseLong(currentLevelStr);
|
|
|
|
|
- return new AbstractMap.SimpleEntry<>(start, currentLevel);
|
|
|
|
|
- })
|
|
|
|
|
- .filter(entry -> empiricalValue >= entry.getKey())
|
|
|
|
|
- .reduce((a, b) -> b)
|
|
|
|
|
- .map(entry -> {
|
|
|
|
|
- Long currentStart = entry.getKey();
|
|
|
|
|
- Long currentLevel = entry.getValue();
|
|
|
|
|
- rice.setLevel(currentLevel);
|
|
|
|
|
- riceRepo.save(rice);
|
|
|
|
|
- Long nextStart = getRiceLevelJsonArray().stream()
|
|
|
|
|
- .skip(1)
|
|
|
|
|
- .findFirst()
|
|
|
|
|
- .map(JsonObject.class::cast)
|
|
|
|
|
- .map(jsonObject -> jsonObject.getJsonNumber("start").longValue())
|
|
|
|
|
- .orElse(null);
|
|
|
|
|
-
|
|
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
|
|
- map.put("currentLevel", currentLevel);
|
|
|
|
|
- double levelUpPercentage = nextStart != null ? (double) (empiricalValue - currentStart) / (nextStart - currentStart) : 1.0;
|
|
|
|
|
- map.put("levelUpPercentage", levelUpPercentage);
|
|
|
|
|
- return map;
|
|
|
|
|
- })
|
|
|
|
|
- .orElseThrow(() -> new BusinessException("没有配置对应的等级对应经验值"));
|
|
|
|
|
-
|
|
|
|
|
- return result;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private JsonArray getRiceLevelJsonArray() {
|
|
|
|
|
- String jsonString = sysConfigService.getString("rice_level");
|
|
|
|
|
- JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
|
|
|
|
|
- return jsonReader.readArray();
|
|
|
|
|
|
|
+ if (currentLevel != null && currentStart != null) {
|
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
|
+ result.put("currentLevel", currentLevel);
|
|
|
|
|
+ double levelUpPercentage;
|
|
|
|
|
+ if (nextStart != null) {
|
|
|
|
|
+ levelUpPercentage = (double) (empiricalValue - currentStart) / (nextStart - currentStart);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ levelUpPercentage = 1.0;
|
|
|
|
|
+ }
|
|
|
|
|
+ result.put("levelUpPercentage", levelUpPercentage);
|
|
|
|
|
+ return result;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new BusinessException("没有配置对应的等级对应经验值");
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new BusinessException("用户不存在");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 任务初始化.显示各个任务能否点击
|
|
* 任务初始化.显示各个任务能否点击
|
|
|
*
|
|
*
|
|
@@ -220,21 +221,41 @@ public class RiceService {
|
|
|
public List<RiceDTO> getTop100(Long userId) {
|
|
public List<RiceDTO> getTop100(Long userId) {
|
|
|
// this.updateLvRank();
|
|
// this.updateLvRank();
|
|
|
List<Rice> top100Rices = riceRepo.findTop100OrderByEmpiricalValueDesc();
|
|
List<Rice> top100Rices = riceRepo.findTop100OrderByEmpiricalValueDesc();
|
|
|
- boolean isOnTop100 = top100Rices.size() < 100 ? true : false;
|
|
|
|
|
- List<RiceDTO> result = top100Rices.stream().map(rice -> {
|
|
|
|
|
|
|
+ List<RiceDTO> result = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ // 计算自己的排名
|
|
|
|
|
+// int selfRank = 0;
|
|
|
|
|
+// for (int i = 0; i < top100Rices.size(); i++) {
|
|
|
|
|
+// if (top100Rices.get(i).getId().equals(userId)) {
|
|
|
|
|
+// selfRank = i + 1;
|
|
|
|
|
+// break;
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i < top100Rices.size(); i++) {
|
|
|
|
|
+ Rice rice = top100Rices.get(i);
|
|
|
RiceDTO riceDTO = new RiceDTO();
|
|
RiceDTO riceDTO = new RiceDTO();
|
|
|
BeanUtils.copyProperties(rice, riceDTO);
|
|
BeanUtils.copyProperties(rice, riceDTO);
|
|
|
- riceDTO.setScoreRank(top100Rices.indexOf(rice) + 1);
|
|
|
|
|
- riceDTO.setOnTop100(isOnTop100);
|
|
|
|
|
|
|
+ riceDTO.setScoreRank(i + 1);
|
|
|
|
|
+ // 判断是否上榜
|
|
|
|
|
+ if (i < 100) {
|
|
|
|
|
+ riceDTO.setOnTop100(true);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ riceDTO.setOnTop100(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 如果是自己,则设置自己的头像、昵称、等级和离上榜还差多少名
|
|
|
if (rice.getId().equals(userId)) {
|
|
if (rice.getId().equals(userId)) {
|
|
|
riceDTO.setAvatar(rice.getAvatar());
|
|
riceDTO.setAvatar(rice.getAvatar());
|
|
|
riceDTO.setNickname(rice.getNickname());
|
|
riceDTO.setNickname(rice.getNickname());
|
|
|
riceDTO.setLevel(rice.getLevel());
|
|
riceDTO.setLevel(rice.getLevel());
|
|
|
- riceDTO.setRankGap(isOnTop100 ? 0 : top100Rices.indexOf(rice) + 1 - 100);
|
|
|
|
|
|
|
+ if (i < 100) {
|
|
|
|
|
+ riceDTO.setRankGap(0);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ riceDTO.setRankGap(i + 1 - 100);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- return riceDTO;
|
|
|
|
|
- }).collect(Collectors.toList());
|
|
|
|
|
-
|
|
|
|
|
|
|
+ result.add(riceDTO);
|
|
|
|
|
+ }
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -262,16 +283,43 @@ public class RiceService {
|
|
|
return R.error("用户不存在");
|
|
return R.error("用户不存在");
|
|
|
}
|
|
}
|
|
|
List<Rice> top100Rices = riceRepo.findTop100OrderByEmpiricalValueDesc();
|
|
List<Rice> top100Rices = riceRepo.findTop100OrderByEmpiricalValueDesc();
|
|
|
- int selfRank = top100Rices.indexOf(rice1.get()) + 1;
|
|
|
|
|
- if (selfRank == 0) {
|
|
|
|
|
|
|
+ List<RiceDTO> result = new ArrayList<>();
|
|
|
|
|
+ RiceDTO dto = new RiceDTO();
|
|
|
|
|
+ // 计算自己的排名
|
|
|
|
|
+ int selfRank = 0;
|
|
|
|
|
+ for (int i = 0; i < top100Rices.size(); i++) {
|
|
|
|
|
+ if (top100Rices.get(i).getUserId().equals(userId)) {
|
|
|
|
|
+ selfRank = i + 1;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ for (int i = 0; i < top100Rices.size(); i++) {
|
|
|
|
|
+ Rice rice = top100Rices.get(i);
|
|
|
|
|
+ RiceDTO riceDTO = new RiceDTO();
|
|
|
|
|
+ BeanUtils.copyProperties(rice, riceDTO);
|
|
|
|
|
+ riceDTO.setScoreRank(i + 1);
|
|
|
|
|
+ // 判断是否上榜
|
|
|
|
|
+ if (i < 100) {
|
|
|
|
|
+ riceDTO.setOnTop100(true);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ riceDTO.setOnTop100(false);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 如果是自己,则设置自己的头像、昵称、等级和离上榜还差多少名
|
|
|
|
|
+ if (rice.getUserId().equals(userId)) {
|
|
|
|
|
+ riceDTO.setAvatar(rice.getAvatar());
|
|
|
|
|
+ riceDTO.setNickname(rice.getNickname());
|
|
|
|
|
+ riceDTO.setLevel(rice.getLevel());
|
|
|
|
|
+ if (i < 100) {
|
|
|
|
|
+ riceDTO.setRankGap(0);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ riceDTO.setRankGap(i + 1 - 100);
|
|
|
|
|
+ }
|
|
|
|
|
+ BeanUtils.copyProperties(riceDTO, dto);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dto == null) {
|
|
|
return R.error("用户不存在");
|
|
return R.error("用户不存在");
|
|
|
}
|
|
}
|
|
|
- RiceDTO dto = new RiceDTO();
|
|
|
|
|
- Rice selfRice = top100Rices.get(selfRank - 1);
|
|
|
|
|
- BeanUtils.copyProperties(selfRice, dto);
|
|
|
|
|
- dto.setScoreRank(selfRank);
|
|
|
|
|
- dto.setOnTop100(selfRank <= 100);
|
|
|
|
|
- dto.setRankGap(Math.max(0, selfRank - 100));
|
|
|
|
|
return R.success(dto);
|
|
return R.success(dto);
|
|
|
}
|
|
}
|
|
|
|
|
|