├── README.md ├── .gitignore ├── src └── main │ ├── java │ └── com │ │ └── yizhuoyan │ │ └── gameforprogrammer │ │ ├── util │ │ ├── StringUtil.java │ │ └── AlgorithmUtil.java │ │ ├── domain │ │ └── PlayerGameStatus.java │ │ ├── GameForProgrammerApplication.java │ │ └── web │ │ └── controller │ │ ├── GameControllController.java │ │ ├── GameInterceptor.java │ │ └── GameLevelController.java │ └── resources │ ├── META-INF │ └── additional-spring-configuration-metadata.json │ ├── templates │ ├── warning.html │ ├── 06.html │ ├── 05.html │ ├── timeout.html │ ├── 07.html │ ├── 08.html │ ├── 09.html │ ├── restart.html │ ├── 01.html │ ├── 10.html │ ├── 11.html │ ├── win.html │ ├── 03.html │ ├── 04.html │ ├── 02.html │ ├── welcome.html │ └── 12.html │ ├── static │ └── error │ │ ├── 500.html │ │ └── 404.html │ └── application.yml ├── pom.xml └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # game-for-programer -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | 3 | .idea/ 4 | *.iml 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/main/java/com/yizhuoyan/gameforprogrammer/util/StringUtil.java: -------------------------------------------------------------------------------- 1 | package com.yizhuoyan.gameforprogrammer.util; 2 | 3 | public interface StringUtil { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/additional-spring-configuration-metadata.json: -------------------------------------------------------------------------------- 1 | {"properties": [{ 2 | "name": "app.name", 3 | "type": "java.lang.String", 4 | "description": "A description for 'app.name'" 5 | }]} -------------------------------------------------------------------------------- /src/main/resources/templates/warning.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 警告 6 | 7 | 8 | 9 |

酒要一口一口地喝,路要一步一步走,步子迈得太大,会扯着蛋!

10 | 11 | 返回 12 | -------------------------------------------------------------------------------- /src/main/resources/static/error/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 500 6 | 7 | 8 | 9 |

500

10 | 11 |

12 | 系统被你整冒烟了!!! 13 | 返回重试 14 |

15 | -------------------------------------------------------------------------------- /src/main/resources/templates/06.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 第6关 6 | 7 | 8 | 9 |

第6关/共12关

10 |

当初指雁为羹,充饥画饼,道无情却有情。

11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/resources/templates/05.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 第5关 6 | 7 | 8 | 9 |

第5关/共12关

10 | 11 |

此情无计可消除,才下眉头,却上心头。

12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/resources/static/error/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 404 6 | 7 | 8 | 9 |

404

10 | 11 |

12 | 中国有一座无人敢提及的城市,没有名称,只有一个代号叫404 13 | 14 |

15 |

16 | 返回 17 |

18 | -------------------------------------------------------------------------------- /src/main/resources/templates/timeout.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 超时了 6 | 7 | 8 |

你的session超时了,你只有重来了

9 | 10 |

11 | 好的,重来 12 |

13 | 14 | -------------------------------------------------------------------------------- /src/main/resources/templates/07.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 第7关 6 | 7 | 8 | 9 |

第7关/共12关

10 | 11 |

这个提示够明显了吧

12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/resources/templates/08.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 第8关 6 | 7 | 8 | 9 |

第8关/共12关

10 | 11 |

明修栈道,暗度陈仓

12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/main/resources/templates/09.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 第9关 6 | 7 | 8 | 9 |

第9关/共12关

10 | 11 |

看山不是山,看水不是水

12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | # for ignore utf-8 bom 2 | server: 3 | port: 1024 4 | spring: 5 | thymeleaf: 6 | prefix: classpath:/templates 7 | encoding: utf-8 8 | mvc: 9 | locale: zh_CN 10 | application: 11 | name: ${app.name} 12 | session: 13 | timeout: 15m 14 | 15 | app: 16 | day: 庆余生 17 | name: ${app.day}-程序员闯关大赛 -------------------------------------------------------------------------------- /src/main/resources/templates/restart.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |

你确定要重来?

9 |

10 | 11 | 确定重来 12 | 13 | 算了接着刚才关卡继续 14 |

15 | 16 | -------------------------------------------------------------------------------- /src/main/resources/templates/01.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 第1关 6 | 7 | 8 | 9 |

第1关/共12关

10 |

11 | 落霞与孤鹜齐飞 ,秋水共长天一色。 12 |

13 | 第二关入口 14 | 15 | -------------------------------------------------------------------------------- /src/main/resources/templates/10.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 第10关 6 | 7 | 8 | 9 |

第10关/共12关

10 | 11 |

12 | 绰约娇波二八春,几时飘谪下红尘。 13 |

14 | 15 |

16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/resources/templates/11.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 第11关 6 | 7 | 8 | 9 |

第11关/共12关

10 | 11 |

12 | 句法洪徐可逼真,摩挲老眼见斯人。 13 |

14 | 15 |

16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/resources/templates/win.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Win 6 | 7 | 8 | 9 |

恭喜你,闯关成功。

10 | 11 |

12 | 发送邮件,主题为“您的大名+”到 13 | yizhuoyan@hotmail.com 14 | 进行成绩统计。 15 |

16 | 17 | -------------------------------------------------------------------------------- /src/main/resources/templates/03.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 第3关 6 | 7 | 8 | 9 |

第3关/共12关

10 | 11 |

空山不见人,但闻人语响。

12 | 13 |
14 | 15 | 16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/resources/templates/04.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 第4关 6 | 7 | 8 | 9 |

第4关/共12关

10 | 11 |

返景入深林,复照青苔上。

12 | 13 |
14 | 15 | 16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /src/main/resources/templates/02.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 第2关 6 | 7 | 8 |

第2关/共12关

9 | 10 |

11 | 色不异空,空不异色;
12 | 色即是空,空即是色。 13 |

14 | 15 |
16 | 17 | 18 |
19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/main/resources/templates/welcome.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |

9 |
10 |
11 |

闯关方法:

12 |
13 |

14 | 请根据每个关卡的提示找到进入下一关的方法。 15 |

16 |
17 |

18 | 警告:每个关卡的通关时间只有15分钟 19 |

20 |

21 | 说明:您的浏览器必须启动JavaScript以及启用Cookie 22 |

23 |

24 | 25 |

26 |
27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/main/java/com/yizhuoyan/gameforprogrammer/domain/PlayerGameStatus.java: -------------------------------------------------------------------------------- 1 | package com.yizhuoyan.gameforprogrammer.domain; 2 | 3 | import java.util.Objects; 4 | 5 | import com.yizhuoyan.gameforprogrammer.util.AlgorithmUtil; 6 | 7 | /** 8 | * Created by ben on 10/27/18. 9 | */ 10 | public class PlayerGameStatus implements AlgorithmUtil{ 11 | public static final int MAX_LEVEL=12; 12 | int currentLevel; 13 | String currentLevelKey; 14 | 15 | 16 | public PlayerGameStatus() { 17 | this.currentLevelKey=uuid32(); 18 | this.currentLevel = 1; 19 | } 20 | 21 | public boolean passCurrentLevel(String key){ 22 | if(Objects.equals(key,currentLevelKey)){ 23 | this.currentLevel++; 24 | this.currentLevelKey= uuid32(); 25 | return true; 26 | } 27 | return false; 28 | } 29 | 30 | public String newCurrentLevelKey(){ 31 | this.currentLevelKey= uuid32(); 32 | return this.currentLevelKey; 33 | } 34 | 35 | 36 | public int getCurrentLevel() { 37 | return currentLevel; 38 | } 39 | 40 | public String getCurrentLevelKey() { 41 | return currentLevelKey; 42 | } 43 | 44 | public String getCurrentLevelViewName(){ 45 | if(this.currentLevel>=10){ 46 | return this.currentLevel+""; 47 | } 48 | return "0"+this.currentLevel; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/yizhuoyan/gameforprogrammer/util/AlgorithmUtil.java: -------------------------------------------------------------------------------- 1 | package com.yizhuoyan.gameforprogrammer.util; 2 | 3 | import java.util.Arrays; 4 | import java.util.Base64; 5 | import java.util.UUID; 6 | 7 | /** 8 | * Created by ben on 10/27/18. 9 | */ 10 | public interface AlgorithmUtil { 11 | String[] MORSE_LETTER_CODES = { "01", "1000", "1010", "100", "0", "0010", "110", "0000", "00", "0111", "101", 12 | "0100", "11", "10", "111", "0110", "1101", "010", "000", "1", "001", "0001", "011", "1001", "1011", 13 | "1100" }; 14 | String[] MORSE_NUMBER_CODES = { "11111", "01111", "00111", "00011", "00001", "00000", "10000", "11000", "11100", 15 | "11110" }; 16 | 17 | default public String uuid32() { 18 | String uuid = UUID.randomUUID().toString(); 19 | char[] cs = new char[32]; 20 | char c = 0; 21 | for (int i = uuid.length(), j = 0; i-- > 0;) { 22 | if ((c = uuid.charAt(i)) != '-') { 23 | cs[j++] = c; 24 | } 25 | } 26 | return new String(cs); 27 | } 28 | 29 | default public String base64(String s) { 30 | return Base64.getEncoder().encodeToString(s.getBytes()); 31 | } 32 | 33 | default public String to8bitsString(char c) { 34 | char[] cs = new char[8]; 35 | Arrays.fill(cs, '0'); 36 | String result = Integer.toBinaryString(c); 37 | for (int i = result.length(), j = cs.length - 1; i-- > 0;) { 38 | cs[j--] = result.charAt(i); 39 | } 40 | return new String(cs); 41 | } 42 | 43 | default public String morseCode(char c) { 44 | if (c >= 'a' && c <= 'z') { 45 | return MORSE_LETTER_CODES[c - 'a']; 46 | } 47 | 48 | if (c >= 'A' && c <= 'Z') { 49 | return MORSE_LETTER_CODES[c - 'A']; 50 | } 51 | if (c >= '0' && c <= '9') { 52 | return MORSE_NUMBER_CODES[c - '0']; 53 | } 54 | return "" + c; 55 | 56 | } 57 | 58 | default public String morseCode(String s) { 59 | StringBuilder result = new StringBuilder(); 60 | for (int i = 0; i < s.length(); i++) { 61 | result.append(morseCode(s.charAt(i))).append(' '); 62 | } 63 | result.deleteCharAt(result.length() - 1); 64 | return result.toString(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/resources/templates/12.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Apache Tomcat/9.0.44 - Error report 4 | 66 | 67 |

HTTP Status 404 - /12

68 |
69 |

type Status report

70 |

message /12

71 |

description The requested resource is not available.

72 |
73 |

Apache Tomcat/9.0.44

74 | -------------------------------------------------------------------------------- /src/main/java/com/yizhuoyan/gameforprogrammer/GameForProgrammerApplication.java: -------------------------------------------------------------------------------- 1 | package com.yizhuoyan.gameforprogrammer; 2 | 3 | import java.util.Arrays; 4 | import java.util.stream.Collectors; 5 | import java.util.stream.IntStream; 6 | 7 | import javax.servlet.ServletContext; 8 | 9 | import org.springframework.boot.SpringApplication; 10 | import org.springframework.boot.autoconfigure.SpringBootApplication; 11 | import org.springframework.boot.web.context.WebServerInitializedEvent; 12 | import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent; 13 | import org.springframework.context.ApplicationListener; 14 | import org.springframework.context.ConfigurableApplicationContext; 15 | import org.springframework.context.annotation.Configuration; 16 | import org.springframework.core.env.ConfigurableEnvironment; 17 | import org.springframework.stereotype.Component; 18 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 19 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 20 | 21 | import com.yizhuoyan.gameforprogrammer.domain.PlayerGameStatus; 22 | import com.yizhuoyan.gameforprogrammer.web.controller.GameInterceptor; 23 | 24 | @SpringBootApplication 25 | public class GameForProgrammerApplication 26 | implements WebMvcConfigurer, ApplicationListener { 27 | 28 | public void addInterceptors(InterceptorRegistry registry) { 29 | registry.addInterceptor(new GameInterceptor()).addPathPatterns(IntStream.range(1, PlayerGameStatus.MAX_LEVEL) 30 | .mapToObj(i -> i > 9 ? "/" + i : "/0" + i).collect(Collectors.toList())); 31 | } 32 | 33 | public void onApplicationEvent(ServletWebServerInitializedEvent event) { 34 | ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment(); 35 | ServletContext servletContext = event.getApplicationContext().getServletContext(); 36 | Arrays.asList("app.name").forEach(k -> { 37 | servletContext.setAttribute(k, environment.getProperty(k)); 38 | }); 39 | } 40 | 41 | public static void main(String[] args) { 42 | System.setProperty("file.encoding", "UTF-8"); 43 | SpringApplication.run(GameForProgrammerApplication.class, args); 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.yizhuoyan 7 | game-for-programmer 8 | 1.0.2 9 | jar 10 | 11 | 程序员闯关类游戏 12 | 13 | 14 | org.springframework.boot 15 | spring-boot-starter-parent 16 | 2.0.6.RELEASE 17 | 18 | 19 | 20 | 21 | UTF-8 22 | UTF-8 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-devtools 35 | runtime 36 | true 37 | 38 | 39 | net.glxn.qrgen 40 | javase 41 | 2.0 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-thymeleaf 46 | 47 | 48 | org.projectlombok 49 | lombok 50 | true 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-starter-test 55 | test 56 | 57 | 58 | 59 | 60 | 61 | 62 | org.springframework.boot 63 | spring-boot-maven-plugin 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /src/main/java/com/yizhuoyan/gameforprogrammer/web/controller/GameControllController.java: -------------------------------------------------------------------------------- 1 | /** 2 | * GameControllController.java 3 | */ 4 | package com.yizhuoyan.gameforprogrammer.web.controller; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpSession; 8 | 9 | import org.springframework.beans.factory.annotation.Value; 10 | import org.springframework.stereotype.Controller; 11 | import org.springframework.web.bind.annotation.GetMapping; 12 | 13 | import com.yizhuoyan.gameforprogrammer.domain.PlayerGameStatus; 14 | 15 | /** 16 | * @author yijun@neusoft.com 17 | * 18 | */ 19 | @Controller 20 | public class GameControllController { 21 | 22 | 23 | @GetMapping("/need-warning") 24 | public String dd() { 25 | return "/warning.html"; 26 | } 27 | 28 | @GetMapping("/") 29 | public String welcome(HttpServletRequest req) { 30 | HttpSession session=req.getSession(false); 31 | if(session!=null) {//说明之前玩过 32 | //跳到当前关卡 33 | return "forward:/continue"; 34 | }else { 35 | req.getSession().setAttribute(PlayerGameStatus.class.getName(), new PlayerGameStatus()); 36 | } 37 | //第一次游戏 38 | return "/welcome.html"; 39 | } 40 | 41 | 42 | @GetMapping("/timeout") 43 | public String timeout(HttpServletRequest req) { 44 | 45 | return "/timeout.html"; 46 | } 47 | 48 | 49 | @GetMapping("/restart") 50 | public String restart(HttpServletRequest req) { 51 | HttpSession session=req.getSession(false); 52 | if(session!=null) { 53 | //请求重玩 54 | session.invalidate(); 55 | } 56 | req.getSession().setAttribute(PlayerGameStatus.class.getName(), new PlayerGameStatus()); 57 | return "/welcome.html"; 58 | } 59 | 60 | @GetMapping("/need-restart") 61 | public String needRestart() { 62 | return "/restart.html"; 63 | } 64 | @GetMapping("/win") 65 | public String win(HttpServletRequest req) { 66 | PlayerGameStatus pgs =(PlayerGameStatus) req.getSession().getAttribute(PlayerGameStatus.class.getName()); 67 | if(pgs==null) { 68 | return "forward:/timeout"; 69 | } 70 | if(pgs.getCurrentLevel()>=PlayerGameStatus.MAX_LEVEL) { 71 | req.getSession().invalidate(); 72 | req.setAttribute("code", String.valueOf(Math.random()).subSequence(2, 5)); 73 | return "/win.html"; 74 | } 75 | return "forward:/need-warning"; 76 | 77 | } 78 | @GetMapping("/continue") 79 | public String levelContinue(HttpServletRequest req) { 80 | PlayerGameStatus pgs =(PlayerGameStatus) req.getSession().getAttribute(PlayerGameStatus.class.getName()); 81 | if(pgs==null) { 82 | return "redirect:/01"; 83 | } 84 | return "redirect:/" + pgs.getCurrentLevelViewName(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/com/yizhuoyan/gameforprogrammer/web/controller/GameInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.yizhuoyan.gameforprogrammer.web.controller; 2 | 3 | import org.springframework.web.servlet.HandlerInterceptor; 4 | import org.springframework.web.servlet.ModelAndView; 5 | 6 | import com.yizhuoyan.gameforprogrammer.domain.PlayerGameStatus; 7 | 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import javax.servlet.http.HttpSession; 11 | 12 | /** 13 | * Created by ben on 10/27/18. 14 | */ 15 | public class GameInterceptor implements HandlerInterceptor { 16 | 17 | static Integer parseInt(String s) { 18 | int numberEndIndex = 0; 19 | char c = 0; 20 | for (int i = 0; i < s.length(); i++) { 21 | c = s.charAt(i); 22 | if ('0' <= c && c <= '9') { 23 | numberEndIndex++; 24 | } else { 25 | break; 26 | } 27 | } 28 | s = s.substring(0, numberEndIndex); 29 | if(s.length()==0)return null; 30 | return Integer.parseInt(s, 10); 31 | } 32 | 33 | private Integer getRequestLevel(HttpServletRequest req){ 34 | String uri = req.getRequestURI(); 35 | uri = uri.substring(req.getContextPath().length() + 1); 36 | try { 37 | Integer requestLevel = parseInt(uri); 38 | return requestLevel; 39 | }catch (NumberFormatException e){ 40 | return null; 41 | } 42 | } 43 | @Override 44 | 45 | public boolean preHandle(HttpServletRequest req, HttpServletResponse response, Object handler) throws Exception { 46 | //第一次进入 47 | if(req.getRequestedSessionId()==null){ 48 | response.sendRedirect("/"); 49 | return false; 50 | } 51 | 52 | HttpSession session = req.getSession(false); 53 | //session超时 54 | if(session==null){ 55 | req.getRequestDispatcher("/timeout").forward(req, response); 56 | return false; 57 | } 58 | //获取请求关卡 59 | Integer requestLevel=getRequestLevel(req); 60 | if(requestLevel==null) { 61 | //继续当前关卡 62 | req.getRequestDispatcher("/continue").forward(req, response); 63 | return false; 64 | } 65 | PlayerGameStatus pgs = (PlayerGameStatus) session.getAttribute(PlayerGameStatus.class.getName()); 66 | // 无当前关卡信息 67 | if (pgs == null) { 68 | // timeout,从第一关进行 69 | req.getRequestDispatcher("/timeout").forward(req, response); 70 | return false; 71 | } 72 | // 请求关卡和实际关卡一样,通过 73 | if (requestLevel == pgs.getCurrentLevel()) { 74 | return true; 75 | // 请求关卡小于实际关卡,询问是否重来? 76 | } else if (requestLevel < pgs.getCurrentLevel()) { 77 | req.getRequestDispatcher("/need-restart").forward(req, response); 78 | return false; 79 | } else { 80 | // 请求关卡大于实际关卡,警告并返回当前关卡 81 | req.getRequestDispatcher("/need-warning").forward(req, response); 82 | return false; 83 | } 84 | } 85 | 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/yizhuoyan/gameforprogrammer/web/controller/GameLevelController.java: -------------------------------------------------------------------------------- 1 | package com.yizhuoyan.gameforprogrammer.web.controller; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.IOException; 5 | import java.util.zip.ZipEntry; 6 | import java.util.zip.ZipOutputStream; 7 | 8 | import javax.servlet.ServletOutputStream; 9 | import javax.servlet.http.Cookie; 10 | import javax.servlet.http.HttpServletRequest; 11 | import javax.servlet.http.HttpServletResponse; 12 | import javax.servlet.http.HttpSession; 13 | 14 | import org.springframework.beans.factory.annotation.Value; 15 | import org.springframework.stereotype.Controller; 16 | import org.springframework.web.bind.annotation.ExceptionHandler; 17 | import org.springframework.web.bind.annotation.GetMapping; 18 | import org.springframework.web.bind.annotation.RequestMapping; 19 | import org.springframework.web.context.request.RequestContextHolder; 20 | import org.springframework.web.context.request.ServletRequestAttributes; 21 | 22 | import com.yizhuoyan.gameforprogrammer.domain.PlayerGameStatus; 23 | import com.yizhuoyan.gameforprogrammer.util.AlgorithmUtil; 24 | 25 | import net.glxn.qrgen.core.AbstractQRCode; 26 | import net.glxn.qrgen.core.image.ImageType; 27 | import net.glxn.qrgen.javase.QRCode; 28 | 29 | /** 30 | * Created by ben on 10/25/18. 31 | */ 32 | @Controller 33 | public class GameLevelController implements AlgorithmUtil { 34 | @Value("${spring.application.name}") 35 | String appName; 36 | 37 | public PlayerGameStatus getPlayerGameStatus() { 38 | ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); 39 | HttpSession session = sra.getRequest().getSession(); 40 | PlayerGameStatus pgs = (PlayerGameStatus) session.getAttribute(PlayerGameStatus.class.getName()); 41 | // 没有则新建一个 42 | if (pgs == null) { 43 | pgs = new PlayerGameStatus(); 44 | session.setAttribute(PlayerGameStatus.class.getName(), pgs); 45 | } 46 | return pgs; 47 | } 48 | 49 | public String newCurrentLevelKey() { 50 | ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); 51 | HttpSession session = sra.getRequest().getSession(); 52 | PlayerGameStatus pgs = (PlayerGameStatus) session.getAttribute(PlayerGameStatus.class.getName()); 53 | session.setAttribute("key", pgs.newCurrentLevelKey()); 54 | return pgs.getCurrentLevelKey(); 55 | } 56 | 57 | // 文字和背景同色 58 | @GetMapping("/01") 59 | public String level01(String key) { 60 | PlayerGameStatus pgs = getPlayerGameStatus(); 61 | if (pgs.passCurrentLevel(key)) { 62 | return "redirect:/02"; 63 | } 64 | // 失败,重新生成key 65 | newCurrentLevelKey(); 66 | return "/01.html"; 67 | } 68 | 69 | // 注释 70 | @GetMapping("/02") 71 | public String levelInComment(String key) { 72 | PlayerGameStatus pgs = getPlayerGameStatus(); 73 | if (pgs.passCurrentLevel(key)) { 74 | return "redirect:/03"; 75 | } 76 | // 失败,重新生成key 77 | newCurrentLevelKey(); 78 | 79 | return "/02.html"; 80 | } 81 | 82 | // 必须get 83 | @RequestMapping("/03") 84 | public String levelMustGetMethod(String key, HttpServletRequest req) { 85 | PlayerGameStatus pgs = getPlayerGameStatus(); 86 | if (req.getMethod().equalsIgnoreCase("GET")) { 87 | if (pgs.passCurrentLevel(key)) { 88 | return "redirect:/04"; 89 | } 90 | } 91 | // 失败,重新生成key 92 | newCurrentLevelKey(); 93 | return "/03.html"; 94 | } 95 | // 必须post 96 | @RequestMapping("/04") 97 | public String levelMustPostMethod(String key, HttpServletRequest req) { 98 | PlayerGameStatus pgs = getPlayerGameStatus(); 99 | if (req.getMethod().equalsIgnoreCase("POST")) { 100 | if (pgs.passCurrentLevel(key)) { 101 | return "redirect:/05"; 102 | } 103 | } 104 | // 失败,重新生成key 105 | newCurrentLevelKey(); 106 | return "/04.html"; 107 | } 108 | 109 | // head 110 | @GetMapping("/05") 111 | public String levelKeyInResponceHeader(String key, HttpServletResponse resp) { 112 | PlayerGameStatus pgs = getPlayerGameStatus(); 113 | if (pgs.passCurrentLevel(key)) { 114 | return "redirect:/06"; 115 | } 116 | // 失败,重新生成key 117 | resp.setHeader("key", newCurrentLevelKey()); 118 | return "/05.html"; 119 | } 120 | 121 | // cookie 122 | @GetMapping("/06") 123 | public String levelCookie(String key, HttpServletRequest req, HttpServletResponse resp) { 124 | 125 | PlayerGameStatus pgs = getPlayerGameStatus(); 126 | // key-cookie消失 127 | if (!containsKeyCookie(req)) { 128 | if (pgs.passCurrentLevel(key)) { 129 | return "redirect:/07"; 130 | } 131 | } 132 | // 失败,重新生成key 133 | Cookie cookie = new Cookie("key", newCurrentLevelKey()); 134 | // 保证浏览器不失效 135 | cookie.setMaxAge(Integer.MAX_VALUE); 136 | cookie.setPath("/deleteMePass"); 137 | cookie.setVersion(1); 138 | resp.addCookie(cookie); 139 | 140 | return "/06.html"; 141 | } 142 | 143 | private boolean containsKeyCookie(HttpServletRequest req) { 144 | 145 | Cookie[] cookies = req.getCookies(); 146 | if (cookies == null) 147 | return false; 148 | for (Cookie c : cookies) { 149 | if ("key".equals(c.getName())) { 150 | return true; 151 | } 152 | } 153 | return false; 154 | } 155 | 156 | /** 157 | * 二维码扫码 158 | */ 159 | @GetMapping("/07") 160 | public String levelScan(String key) { 161 | PlayerGameStatus pgs = getPlayerGameStatus(); 162 | if (pgs.passCurrentLevel(key)) { 163 | return "redirect:/08"; 164 | } 165 | // 失败,重新生成key 166 | newCurrentLevelKey(); 167 | return "/07.html"; 168 | } 169 | 170 | @GetMapping("/07.png") 171 | public void generateBase64QRCodeImg(HttpServletResponse resp) throws IOException { 172 | PlayerGameStatus pgs = getPlayerGameStatus(); 173 | resp.setContentType("image/png"); 174 | // 失败,重新生成key 175 | String key = pgs.getCurrentLevelKey(); 176 | key = base64(key); 177 | AbstractQRCode qrCode = QRCode.from(key); 178 | // 设置字符集,支持中文 179 | qrCode.withCharset("utf-8"); 180 | // 设置生成的二维码图片大小 181 | qrCode.withSize(260, 260); 182 | ByteArrayOutputStream data = qrCode.to(ImageType.PNG).stream(); 183 | 184 | try (ServletOutputStream out = resp.getOutputStream();) { 185 | out.write(data.toByteArray()); 186 | } 187 | } 188 | 189 | /** 190 | * 图片16进制隐藏 191 | * 192 | * @param resp 193 | * 194 | * @throws IOException 195 | */ 196 | @GetMapping("/08") 197 | public String levelHideInImg(String key, HttpServletResponse resp) throws IOException { 198 | PlayerGameStatus pgs = getPlayerGameStatus(); 199 | if (pgs.passCurrentLevel(key)) { 200 | return "redirect:/09"; 201 | } 202 | // 失败,重新生成key 203 | newCurrentLevelKey(); 204 | return "/08.html"; 205 | 206 | } 207 | 208 | @GetMapping("/08.png") 209 | public void generateHideBytesInQRImg(HttpServletResponse resp) throws IOException { 210 | PlayerGameStatus pgs = getPlayerGameStatus(); 211 | resp.setContentType("image/png"); 212 | AbstractQRCode qrCode = QRCode.from("怎么可能还这么简单?"); 213 | // 设置字符集,支持中文 214 | qrCode.withCharset("utf-8"); 215 | // 设置生成的二维码图片大小 216 | qrCode.withSize(260, 260); 217 | ByteArrayOutputStream data = qrCode.to(ImageType.PNG).stream(); 218 | 219 | try (ServletOutputStream out = resp.getOutputStream();) { 220 | out.write(data.toByteArray()); 221 | String key = pgs.getCurrentLevelKey(); 222 | out.write(new byte[8]);//给8个空字节分割 223 | out.write(key.getBytes("iso-8859-1")); 224 | } 225 | } 226 | 227 | /** 228 | * 种子图片方式 229 | * 230 | * @param key 231 | */ 232 | @GetMapping("/09") 233 | public String levelSeedImg(String key) { 234 | PlayerGameStatus pgs = getPlayerGameStatus(); 235 | if (pgs.passCurrentLevel(key)) { 236 | return "redirect:/10"; 237 | } 238 | // 失败,重新生成key 239 | newCurrentLevelKey(); 240 | return "/09.html"; 241 | } 242 | 243 | @GetMapping("/09.png") 244 | public void genereateSeedImg(HttpServletResponse resp) throws IOException { 245 | PlayerGameStatus pgs = getPlayerGameStatus(); 246 | resp.setContentType("image/jpeg"); 247 | String key = pgs.getCurrentLevelKey(); 248 | try (ServletOutputStream out = resp.getOutputStream();) { 249 | AbstractQRCode qrCode = QRCode.from("你居然还这么试?"); 250 | // 设置字符集,支持中文 251 | qrCode.withCharset("utf-8"); 252 | // 设置生成的二维码图片大小 253 | qrCode.withSize(260, 260); 254 | ByteArrayOutputStream data = qrCode.to(ImageType.PNG).stream(); 255 | out.write(data.toByteArray()); 256 | // yasuo 257 | byte[] zipData = createZipFile(key); 258 | out.write(zipData); 259 | } 260 | } 261 | /** 262 | * 创建压缩文件 263 | * @param key 264 | * @return 265 | * @throws IOException 266 | */ 267 | private byte[] createZipFile(String key) throws IOException { 268 | ByteArrayOutputStream data = new ByteArrayOutputStream(); 269 | try (ZipOutputStream out = new ZipOutputStream(data);) { 270 | out.putNextEntry(new ZipEntry("1")); 271 | out.write(key.substring(0, key.length() / 2).getBytes()); 272 | out.putNextEntry(new ZipEntry("2")); 273 | out.write(key.substring(key.length() / 2).getBytes()); 274 | out.finish(); 275 | return data.toByteArray(); 276 | } 277 | } 278 | 279 | // 2进制形式 280 | @GetMapping("/10") 281 | public String levelShowBinary(String key, HttpServletRequest req, HttpServletResponse resp) throws Exception { 282 | PlayerGameStatus pgs = getPlayerGameStatus(); 283 | if (pgs.passCurrentLevel(key)) { 284 | return "redirect:/11"; 285 | } 286 | // 失败,重新生成key 287 | key = newCurrentLevelKey(); 288 | StringBuilder result = new StringBuilder(); 289 | for (int i = 0; i < key.length(); i++) { 290 | if(i<=8) { 291 | result.append(to8bitsString(key.charAt(i))); 292 | }else { 293 | result.append(key.charAt(i)); 294 | } 295 | } 296 | req.setAttribute("tips", result.toString()); 297 | return "/10.html"; 298 | } 299 | 300 | // morse code 301 | @GetMapping("/11") 302 | public String levelShowMoreCode(String key, HttpServletRequest req) { 303 | PlayerGameStatus pgs = getPlayerGameStatus(); 304 | if (pgs.passCurrentLevel(key)) { 305 | return "redirect:/12"; 306 | } 307 | // 失败,重新生成key 308 | key=newCurrentLevelKey(); 309 | String moreCode = morseCode(key.substring(0,16)); 310 | 311 | req.setAttribute("tips", moreCode+" "+key.substring(16)); 312 | return "/11.html"; 313 | } 314 | 315 | // 404方式 316 | @GetMapping("/12") 317 | public String level404(String key, HttpServletRequest req) { 318 | 319 | PlayerGameStatus pgs = getPlayerGameStatus(); 320 | if (pgs.passCurrentLevel(key)) { 321 | return "redirect:/win"; 322 | } 323 | // 失败,重新生成key 324 | newCurrentLevelKey(); 325 | return "/12.html"; 326 | } 327 | 328 | @ExceptionHandler 329 | public void hanldeException(Exception e) { 330 | e.printStackTrace(); 331 | } 332 | 333 | } 334 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------