Compare commits

...

3 Commits

Author SHA1 Message Date
石皮幼鸟 e89d6d089f 排行榜接口 2024-04-12 09:51:21 +08:00
石皮幼鸟 792493119f Merge branch 'refs/heads/dev-czt-11'
# Conflicts:
#	src/main/java/com/example/catchTheLetters/service/LevelService.java
#	src/main/java/com/example/catchTheLetters/service/impl/LevelServiceImpl.java
2024-04-12 09:26:09 +08:00
czt d5e38c8da2 关卡创建,获取排行榜信息 2024-04-12 08:56:48 +08:00
6 changed files with 213 additions and 9 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

@ -34,6 +34,8 @@ public class Level implements Serializable {
* 限时可选毫秒数 * 限时可选毫秒数
*/ */
private Long timeLimit; private Long timeLimit;
/** /**
* 目标分数type=1时有效 * 目标分数type=1时有效
*/ */
@ -42,4 +44,83 @@ public class Level implements Serializable {
* 单词列表 * 单词列表
*/ */
private List<String> words; private List<String> words;
public Level(String id,Integer order,String name,Integer type,Long timeLimit,Integer targetScore, List<String> words){
this.id=id;
this.order=order;
this.name=name;
this.type=type;
this.timeLimit=timeLimit;
this.targetScore=targetScore;
this.words=words;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Long getTimeLimit() {
return timeLimit;
}
public void setTimeLimit(Long timeLimit) {
this.timeLimit = timeLimit;
}
public Integer getTargetScore() {
return targetScore;
}
public void setTargetScore(Integer targetScore) {
this.targetScore = targetScore;
}
public List<String> getWords() {
return words;
}
public void setWords(List<String> words) {
this.words = words;
}
@Override
public String toString(){
return "Level{" +
"id=" + id +
", order= '"+ order +'\''+
", name= '"+ name +'\''+
", type= '"+ type +'\''+
", timeLimit= '"+ timeLimit +'\''+
", targetScore= '"+ targetScore +'\''+
", words= '"+ words +'\''+'}';
}
} }

View File

@ -31,4 +31,53 @@ public class ScoreInfo implements Serializable {
* 时间UNIX时间戳 * 时间UNIX时间戳
*/ */
private Long time; private Long time;
public ScoreInfo(Long userId,Long levelId,String username,Integer score, Long time){
this.userId=userId;
this.levelId=levelId;
this.username=username;
this.score=score;
this.time=time;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Long getLevelId() {
return levelId;
}
public void setLevelId(Long levelId) {
this.levelId = levelId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
} }

View File

@ -2,6 +2,7 @@ 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,10 +14,45 @@ import java.util.List;
* @description * @description
*/ */
public interface LevelService { public interface LevelService {
/**
* 关卡详情
*
* @param id 关卡ID
* @return token
*/
R<Level> levelDetail(String id);
R<Level> levelDetail(Long id); /**
* 关卡列表
*
* @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 关卡ID
* @param token token
* @return 关卡排行信息
*/
RankVo getRankInfo(String levelId, String token);
/**
* 创建关卡
*
* @param level 关卡信息
* @return 关卡创建是否成功
*/
boolean LevelCreate(Level level);
} }

View File

@ -2,11 +2,11 @@ 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;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.data.annotation.QueryAnnotation;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
@ -27,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("查询失败,请重试");
@ -59,4 +59,42 @@ public class LevelServiceImpl implements LevelService {
} }
return R.fail("添加失败,请重试"); return R.fail("添加失败,请重试");
} }
@Override
public RankVo getRankInfo(String levelId, String token) {
// 在ScoreInfo表当中查询levelId为levelId的所有数据按照分数降序排列取前10条数据
var info = mongoTemplate.find(new Query(Criteria.where("levelId").is(levelId)).limit(10), ScoreInfo.class);
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
public boolean LevelCreate(Level level) {
String s=level.toString();
if (s != null) {
mongoTemplate.insert(s);
return true;
}
return false;
}
} }