排行榜接口

This commit is contained in:
石皮幼鸟 2024-04-12 09:51:21 +08:00
parent 792493119f
commit e89d6d089f
4 changed files with 48 additions and 20 deletions

View File

@ -37,14 +37,14 @@ public class LevelController {
@ApiOperation(value = "关卡详情") @ApiOperation(value = "关卡详情")
@GetMapping("/detail") @GetMapping("/detail")
@ApiParam(name = "id", value = "关卡ID") @ApiParam(name = "id", value = "关卡ID")
public R<Level> detail(Long id) { public R<Level> detail(String id) {
return levelService.levelDetail(id); return levelService.levelDetail(id);
} }
@ApiOperation(value = "关卡创建web前端管理员提交") @ApiOperation(value = "关卡创建web前端管理员提交")
@PostMapping("/create") @PostMapping("/create")
public R create(@RequestBody Level level) { public R create(@RequestBody Level level) {
return null; return levelService.LevelCreate(level) ? R.ok() : R.fail();
} }
@ApiOperation(value = "关卡修改web前端管理员提交") @ApiOperation(value = "关卡修改web前端管理员提交")
@ -55,15 +55,15 @@ public class LevelController {
@ApiOperation(value = "关卡删除web前端管理员提交") @ApiOperation(value = "关卡删除web前端管理员提交")
@PostMapping("/delete") @PostMapping("/delete")
public R delete(Long id) { public R delete(String id) {
return null; return null;
} }
@ApiOperation(value = "获取某个关卡排行榜数据") @ApiOperation(value = "获取某个关卡排行榜数据")
@GetMapping("/rank") @GetMapping("/rank")
@ApiParam(name = "id", value = "关卡ID") @ApiParam(name = "id", value = "关卡ID")
public R<RankVo> rank(Long id) { public R<RankVo> rank(String id, @RequestHeader(value = "token", required = false)String token) {
return null; return R.ok(levelService.getRankInfo(id, token));
} }
@ApiOperation(value = "关卡结算游戏结束时提交一定要鉴别token") @ApiOperation(value = "关卡结算游戏结束时提交一定要鉴别token")

View File

@ -2,7 +2,9 @@ package com.example.catchTheLetters.service;
import com.example.catchTheLetters.entity.Level; import com.example.catchTheLetters.entity.Level;
import com.example.catchTheLetters.entity.ScoreInfo; import com.example.catchTheLetters.entity.ScoreInfo;
import com.example.catchTheLetters.model.vo.RankVo;
import com.example.catchTheLetters.utils.R; import com.example.catchTheLetters.utils.R;
import java.util.List; import java.util.List;
/** /**
@ -13,31 +15,42 @@ import java.util.List;
*/ */
public interface LevelService { public interface LevelService {
/** /**
* 关卡详情
* *
* @param id * @param id 关卡ID
* @return token * @return token
*/ */
R<Level> levelDetail(String id);
R<Level> levelDetail(Long id);
/** /**
* 关卡列表
* *
* @return token * @return token
*/ */
R<List<Level>> list(); R<List<Level>> list();
/**
* 结算
*
* @param scoreInfo 分数信息
* @param token token
* @return token
*/
R<ScoreInfo> settle(ScoreInfo scoreInfo, String token); R<ScoreInfo> settle(ScoreInfo scoreInfo, String token);
/** /**
* @param levelId * 获取关卡排行信息
*
* @param levelId 关卡ID
* @param token token
* @return 关卡排行信息 * @return 关卡排行信息
*/ */
Integer getRankInfo(String levelId); RankVo getRankInfo(String levelId, String token);
/** /**
* 创建关卡
* *
* @param level * @param level 关卡信息
* @return 关卡创建是否成功 * @return 关卡创建是否成功
*/ */
boolean LevelCreate(Level level); boolean LevelCreate(Level level);

View File

@ -2,6 +2,7 @@ package com.example.catchTheLetters.service.impl;
import com.example.catchTheLetters.entity.Level; import com.example.catchTheLetters.entity.Level;
import com.example.catchTheLetters.entity.ScoreInfo; import com.example.catchTheLetters.entity.ScoreInfo;
import com.example.catchTheLetters.model.vo.RankVo;
import com.example.catchTheLetters.service.LevelService; import com.example.catchTheLetters.service.LevelService;
import com.example.catchTheLetters.utils.JwtUtil; import com.example.catchTheLetters.utils.JwtUtil;
import com.example.catchTheLetters.utils.R; import com.example.catchTheLetters.utils.R;
@ -26,7 +27,7 @@ public class LevelServiceImpl implements LevelService {
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
@Override @Override
public R<Level> levelDetail(Long id) { public R<Level> levelDetail(String id) {
Level level = mongoTemplate.findOne(new Query(Criteria.where("id").is(id)), Level.class); Level level = mongoTemplate.findOne(new Query(Criteria.where("id").is(id)), Level.class);
if (level == null){ if (level == null){
return R.fail("查询失败,请重试"); return R.fail("查询失败,请重试");
@ -60,14 +61,28 @@ public class LevelServiceImpl implements LevelService {
} }
@Override @Override
public Integer getRankInfo(String levelId) { public RankVo getRankInfo(String levelId, String token) {
ScoreInfo sc=mongoTemplate.findById(2,ScoreInfo.class); // 在ScoreInfo表当中查询levelId为levelId的所有数据按照分数降序排列取前10条数据
Integer infor = null; var info = mongoTemplate.find(new Query(Criteria.where("levelId").is(levelId)).limit(10), ScoreInfo.class);
if(sc!=null) {
infor = sc.getScore();
}
return infor;
var rankVo = new RankVo();
rankVo.setScoreInfos(info);
// 如果有token就查自己的分数数据
if (token != null) {
var map = JwtUtil.getPayload(token);
String id = null;
if (map != null) {
id = map.get("id").asString().replaceAll("\"", "");
}
if (id != null) {
var myScore = mongoTemplate.findOne(new Query(Criteria.where("userId").is(id).and("levelId").is(levelId)), ScoreInfo.class);
rankVo.setMyScore(myScore);
}
}
return rankVo;
} }
@Override @Override