Redis to achieve a small game

The SpringBoot project implements swagger-UI online API documentation

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;
}
1234567891011121314151617

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();
    }

}
123456789101112131415161718192021222324252627282930313233343536373839

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;
    }

    /**
     * 闯关成功,Java群:1142951706 欢迎大家进来一起学习 一起吹水(进群即可领取最新Java全套学习资料)
     * @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);
    }

}
Copy the code