改绘制错误 bug + 将地图行,列,障碍物数量改为后端获取

This commit is contained in:
2023-03-01 11:13:30 +08:00
parent e6c49ad600
commit 1224f16414
7 changed files with 235 additions and 92 deletions
@@ -21,10 +21,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
@Component
@ServerEndpoint("/websocket/{token}") // 注意不要以'/'结尾
public class WebSocketServer {
// 后端向前端发送信息,首先需要创建一个 session
private Session session = null;
// 用户信息:定义一个成员变量
private User user;
/*
存储所有链接:对所有 websocket 可见的全局变量,存储为 static 静态变量
因为每个 websocket 实例都在一个独立的线程里,所以该公共变量应该是线程安全的
@@ -36,6 +32,10 @@ public class WebSocketServer {
private static final CopyOnWriteArraySet<User> matchPool = new CopyOnWriteArraySet<>();
// 在 WebSocketServer 中注入数据库的方法演示-> 使用 static 定义为独一份的变量
private static UserMapper userMapper;
// 后端向前端发送信息,首先需要创建一个 session
private Session session = null;
// 用户信息:定义一个成员变量
private User user;
// 注入方法
@Autowired
@@ -85,6 +85,54 @@ public class WebSocketServer {
}
}
// 开始匹配的逻辑部分
private void startMatching() {
System.out.println("start matching");
matchPool.add(this.user);
while (matchPool.size()>=2){
// 迭代器用于枚举前两个人进行匹配
Iterator<User> it = matchPool.iterator();
User a = it.next(), b = it.next();
// 取出两个人之后,从匹配池中将他们删除
matchPool.remove(a);
matchPool.remove(b);
// 匹配成功时,创建联机地图
Game game = new Game(13,14,20);
game.createMap(); // 初始化地图
JSONObject respGameData = new JSONObject();
respGameData.put("game_map",game.getG());
respGameData.put("rows",game.getRows());
respGameData.put("cols",game.getCols());
respGameData.put("inner_walls_count",game.getInnerWallsCount());
// 将 a 配对成功的消息传回客户端
JSONObject respA = new JSONObject();
respA.put("event","start-matching");
respA.put("opponent_username",b.getUsername());
respA.put("opponent_photo",b.getPhoto());
respA.put("game_data",respGameData);
// 获取 a 的链接,并通过 sendMessage 将消息传给前端
users.get(a.getId()).sendMessage(respA.toJSONString());
// 同理,将 b 的匹配成功信息传回给前端
JSONObject respB = new JSONObject();
respB.put("event","start-matching");
respB.put("opponent_username",a.getUsername());
respB.put("opponent_photo",a.getPhoto());
respB.put("game_data",respGameData);
users.get(b.getId()).sendMessage(respB.toJSONString());
}
}
// 取消匹配的逻辑部分
private void stopMatching() {
System.out.println("stop matching");
matchPool.remove(this.user);
}
// @OnMessage 用于从前端接收请求: 一般 onMessage 当成路由使用,做为消息判断处理的中间部分
@OnMessage
public void onMessage(String message, Session session) {
@@ -121,47 +169,6 @@ public class WebSocketServer {
}
}
// 开始匹配的逻辑部分
private void startMatching() {
System.out.println("start matching");
matchPool.add(this.user);
while (matchPool.size()>=2){
// 迭代器用于枚举前两个人进行匹配
Iterator<User> it = matchPool.iterator();
User a = it.next(), b = it.next();
// 取出两个人之后,从匹配池中将他们删除
matchPool.remove(a);
matchPool.remove(b);
// 匹配成功时,创建联机地图
Game game = new Game(13,14,20);
game.createMap(); // 初始化地图
// 将 a 配对成功的消息传回客户端
JSONObject respA = new JSONObject();
respA.put("event","start-matching");
respA.put("opponent_username",b.getUsername());
respA.put("opponent_photo",b.getPhoto());
respA.put("game_map",game.getG());
// 获取 a 的链接,并通过 sendMessage 将消息传给前端
users.get(a.getId()).sendMessage(respA.toJSONString());
// 同理,将 b 的匹配成功信息传回给前端
JSONObject respB = new JSONObject();
respB.put("event","start-matching");
respB.put("opponent_username",a.getUsername());
respB.put("opponent_photo",a.getPhoto());
respB.put("game_map",game.getG());
users.get(b.getId()).sendMessage(respB.toJSONString());
}
}
// 取消匹配的逻辑部分
private void stopMatching() {
System.out.println("stop matching");
matchPool.remove(this.user);
}
}
@@ -17,15 +17,51 @@ public class Game {
public Game(Integer rows, Integer cols, Integer inner_walls_count) {
this.rows = rows;
this.cols = cols;
this.inner_walls_count = rows;
this.inner_walls_count = inner_walls_count;
this.g = new int[rows][cols];
}
public int getRows(){
return rows;
}
public int getCols(){
return cols;
}
public int getInnerWallsCount(){
return inner_walls_count;
}
// 返回生成的地图
public int[][] getG() {
return g;
}
// 联通检测方法---true(联通)---false(不通),参数: 起点坐标 sx,sy ,终点坐标 tx,ty
private boolean check_connectivity(int sx, int sy, int tx, int ty) {
// 起点就是终点时,结果联通,直接返回 true
if (sx == tx && sy == ty) return true;
g[sx][sy] = 1;
//枚举"上右下左"四个方向,求当前点下一个相邻点的坐标
for (int i = 0; i < 4; i++) {
int x = sx + dx[i];
int y = sy + dy[i];
// 判断是否撞到障碍物( g[x][y] == 0 表示没有碰到障碍物 ),如果没有赚到障碍物,且可以找到重点的话,返回 true(联通)
if (x >= 0 && x < this.rows && y >= 0 && y < this.cols && g[x][y] == 0) {
if (check_connectivity(x, y, tx, ty)) {
// 还原状态
g[sx][sy] = 0;
return true;
}
}
}
// 还原状态
g[sx][sy] = 0;
return false;
}
// 画地图
public boolean draw() {
// 一开始现将所有障碍物初始化为 false
@@ -74,32 +110,6 @@ public class Game {
return check_connectivity(this.rows - 2, 1, 1, this.cols - 2);
}
// 联通检测方法---true(联通)---false(不通),参数: 起点坐标 sx,sy ,终点坐标 tx,ty
private boolean check_connectivity(int sx, int sy, int tx, int ty) {
// 起点就是终点时,结果联通,直接返回 true
if (sx == tx && sy == ty) return true;
g[sx][sy] = 1;
//枚举"上右下左"四个方向,求当前点下一个相邻点的坐标
for (int i = 0; i < 4; i++) {
int x = sx + dx[i];
int y = sy + dy[i];
// 判断是否撞到障碍物( g[x][y] == 0 表示没有碰到障碍物 ),如果没有赚到障碍物,且可以找到重点的话,返回 true(联通)
if (x >= 0 && x < this.rows && y >= 0 && y < this.cols && g[x][y] == 0) {
if (check_connectivity(x, y, tx, ty)) {
// 还原状态
g[sx][sy] = 0;
return true;
}
}
}
// 还原状态
g[sx][sy] = 0;
return true;
}
public void createMap() {
// 循环绘制:如果发现哪次循环中画地图成功了,则跳出循环,绘制结束
for (int i = 0; i < 1000; i++) {
@@ -6,11 +6,10 @@ import io.jsonwebtoken.Claims;
//Jwt 验证配置工具类
public class JwtAuthenticationUtil {
public static Integer getUserId(String token) {
// 核心验证逻辑
// 默认 userid 赋值为 -1 ,表示不存在
// 默认 userid 赋值为 -1 ,表示不存在
int userId = -1;
try {
// 将 token 解析,如果能成功解析出 userid 表示合法,否则表示不合法
// 将 token 解析,如果能成功解析出 userid 表示合法,否则表示不合法
Claims claims = JwtUtil.parseJWT(token);
userId = Integer.parseInt(claims.getSubject());
} catch (Exception e) {