Redis实现闯关小游戏---让你在同事面前装X
程序员文章站
2022-06-23 10:43:54
Redis实现闯关小游戏看本篇之前可看springboot项目整合Swagger-UI实现在线API文档package com.macro.mall.grade.controller;import com.macro.mall.common.api.CommonResult;import com.macro.mall.grade.domain.SucessResult;import io.swagger.annotations.Api;import io.swagger.annotations...
Redis实现闯关小游戏
看本篇之前可看springboot项目整合Swagger-UI实现在线API文档
package com.macro.mall.grade.domain;
import lombok.Data;
import java.util.ArrayList;
import java.util.Set;
/**
* @Author: liuxiang
* @Date: 2020/6/5
* @Description:
*/
@Data
public class SucessResult {
private Set<String> userScore1;
private ArrayList<String> strings1;
}
package com.macro.mall.grade.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
```java
/**
* Swagger2API文档的配置
* Created by macro on 2018/4/26.
*/
@Configuration
@EnableSwagger2
public class GradeSwaggerConfig {
@Bean
public Docket createRestSeckillApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.macro.mall.grade.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("mall学习闯关")
.description("mall闯关模块")
.version("1.0")
.build();
}
}
package com.macro.mall.grade.controller;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.grade.domain.SucessResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Set;
/**
* @Author: liuxiang
* @Date: 2020/6/4
* @Description: 闯关控制器
*/
@RestController
@Api(tags = "WordController", description = "闯关单词")
@RequestMapping("/word")
public class WordController {
private static String[] word = null;
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 给用户添加单词库
* @param userId
* @return
*/
@ApiOperation("给用户添加单词库")
@RequestMapping(value = "/putWord", method = RequestMethod.GET)
@ResponseBody
public CommonResult putWord(@RequestParam String userId) {
String userPerfix = "user:word:" + userId;
//绑定hashKey
BoundHashOperations<String, Object, Object> stringObjectObjectBoundHashOperations = stringRedisTemplate.boundHashOps(userPerfix);
//添加单词库
for (int i = 0; i < 100; i++) {
stringObjectObjectBoundHashOperations.put(String.valueOf(i), "十全十美");
}
return CommonResult.success("ok");
}
/**
* 获取本关单词
* @param userId
* @param id
* @return
*/
@ApiOperation("获取本关单词")
@RequestMapping(value = "/getWord", method = RequestMethod.GET)
@ResponseBody
public CommonResult getWord(@NotNull @RequestParam String userId, @NotNull @RequestParam String[] id) {
//获取用户闯关单词
ArrayList<String> strings1 = getUserWord(userId, id);
//将成语id保存起来
word = id;
//显示过关的排名
ZSetOperations<String, String> stringStringZSetOperations = stringRedisTemplate.opsForZSet();
//获取闯关的等级
stringStringZSetOperations.incrementScore("userScore", userId, id.length);
return strings1 == null ? CommonResult.failed("该用户未注册") : CommonResult.success(strings1);
}
/**
* 获取用户闯关单词
*
* @param userId
* @param id
* @return
*/
private ArrayList<String> getUserWord(@NotNull @RequestParam String userId, @NotNull @RequestParam @NotNull String[] id) {
String userPerfix = "user:word:" + userId;
BoundHashOperations<String, Object, Object> stringObjectObjectBoundHashOperations = stringRedisTemplate.boundHashOps(userPerfix);
ArrayList<String> strings1 = new ArrayList<>();
for (String i : id) {
Object o1 = stringObjectObjectBoundHashOperations.get(i);
strings1.add((String) o1);
}
return strings1;
}
/**
* 闯关成功
* @param userId
* @return
*/
@ApiOperation("闯关成功")
@RequestMapping(value = "/success", method = RequestMethod.GET)
@ResponseBody
public CommonResult<SucessResult> success(@NotNull @RequestParam String userId) {
String userPerfix = "user:word:" + userId;
BoundHashOperations<String, Object, Object> stringObjectObjectBoundHashOperations = stringRedisTemplate.boundHashOps(userPerfix);
//展示已成功的单词
ArrayList<String> strings1 = new ArrayList<>();
for (String i : word) {
Object o1 = stringObjectObjectBoundHashOperations.get(i);
strings1.add((String) o1);
}
SucessResult sucessResult = new SucessResult();
sucessResult.setStrings1(strings1);
//删除已闯关成功的单词
stringObjectObjectBoundHashOperations.delete(word);
//获取闯关排名
Double userScore = stringRedisTemplate.opsForZSet().score("userScore", userId);
if (userScore == null) {
Set<String> userScore1 = stringRedisTemplate.opsForZSet().reverseRange("userScore", 0, -1);
sucessResult.setUserScore1(userScore1);
return CommonResult.success(sucessResult);
}
Set<String> userScore1 = stringRedisTemplate.opsForZSet().reverseRange("userScore", 0, userScore.longValue());
sucessResult.setUserScore1(userScore1);
//清除数组中的内容
word = null;
return CommonResult.success(sucessResult);
}
}
本文地址:https://blog.csdn.net/weixin_44302240/article/details/112866768
下一篇: 雪梨个人独资企业被列为经营异常