实现游戏数据到数据库的存储

This commit is contained in:
flykhan 2023-03-03 22:40:37 +08:00
parent 069a207b23
commit d1bc4c1ba4
6 changed files with 129 additions and 29 deletions

View File

@ -5,6 +5,7 @@ package com.kob.backend.consumer;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.kob.backend.consumer.utils.Game; import com.kob.backend.consumer.utils.Game;
import com.kob.backend.consumer.utils.JwtAuthenticationUtil; import com.kob.backend.consumer.utils.JwtAuthenticationUtil;
import com.kob.backend.mapper.RecordMapper;
import com.kob.backend.mapper.UserMapper; import com.kob.backend.mapper.UserMapper;
import com.kob.backend.pojo.User; import com.kob.backend.pojo.User;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -32,6 +33,8 @@ public class WebSocketServer {
private static final CopyOnWriteArraySet<User> matchPool = new CopyOnWriteArraySet<>(); private static final CopyOnWriteArraySet<User> matchPool = new CopyOnWriteArraySet<>();
// WebSocketServer 中注入数据库的方法演示-> 使用 static 定义为独一份的变量 // WebSocketServer 中注入数据库的方法演示-> 使用 static 定义为独一份的变量
private static UserMapper userMapper; private static UserMapper userMapper;
// 注入 RecordMapper 用于调用实现游戏数据到数据库的存储
public static RecordMapper recordMapper;
// 后端向前端发送信息,首先需要创建一个 session // 后端向前端发送信息,首先需要创建一个 session
private Session session = null; private Session session = null;
// 用户信息:定义一个成员变量 // 用户信息:定义一个成员变量
@ -45,6 +48,11 @@ public class WebSocketServer {
WebSocketServer.userMapper = userMapper; WebSocketServer.userMapper = userMapper;
} }
@Autowired
public void setRecordMapper(RecordMapper recordMapper) {
WebSocketServer.recordMapper = recordMapper;
}
// @OnOpen 创建链接时自动触发这个函数 // @OnOpen 创建链接时自动触发这个函数
@OnOpen @OnOpen
@ -150,8 +158,7 @@ public class WebSocketServer {
// user.getId() 是获取当前链接的用户 id // user.getId() 是获取当前链接的用户 id
if (game.getPlayerA().getId().equals(user.getId())) { if (game.getPlayerA().getId().equals(user.getId())) {
game.setNextStepA(direction); game.setNextStepA(direction);
} } else if (game.getPlayerB().getId().equals(user.getId())) {
else if(game.getPlayerB().getId().equals(user.getId())){
game.setNextStepB(direction); game.setNextStepB(direction);
} }
} }

View File

@ -2,12 +2,11 @@ package com.kob.backend.consumer.utils;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.kob.backend.consumer.WebSocketServer; import com.kob.backend.consumer.WebSocketServer;
import com.kob.backend.pojo.Record;
import javax.swing.event.InternalFrameEvent; import javax.swing.event.InternalFrameEvent;
import java.util.ArrayList; import java.sql.Time;
import java.util.List; import java.util.*;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
// 用来管理整个游戏流程:这里需要多线程 // 用来管理整个游戏流程:这里需要多线程
@ -62,6 +61,23 @@ public class Game extends Thread {
return inner_walls_count; return inner_walls_count;
} }
private String getGameMapString(){
StringBuilder res = new StringBuilder();
// 将地图数据展开成一维
/* for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
res.append(g[i][j]);
}
}*/
for (int[] row:g){
for(int col:row){
res.append(col);
}
}
return res.toString();
}
public void setNextStepA(Integer nextStepA) { public void setNextStepA(Integer nextStepA) {
// 为了防止两方读写冲突,需要加线程锁之后操作 nextStep // 为了防止两方读写冲突,需要加线程锁之后操作 nextStep
lock.lock(); lock.lock();
@ -273,6 +289,27 @@ public class Game extends Thread {
} }
} }
// 将游戏结果存到数据库中
private void saveToDataBase(){
Record record = new Record(
null,
playerA.getId(),
playerA.getSx(),
playerA.getSy(),
playerB.getId(),
playerB.getSx(),
playerB.getSy(),
playerA.getStepsString(),
playerB.getStepsString(),
getGameMapString(),
loser,
new Date()
);
WebSocketServer.recordMapper.insert(record);
}
// 向两个 Client 公布结果 // 向两个 Client 公布结果
private void sendResult() { private void sendResult() {
lock.lock(); lock.lock();
@ -283,6 +320,8 @@ public class Game extends Thread {
// 将最后碰撞时的蛇的眼睛指向传给前端 // 将最后碰撞时的蛇的眼睛指向传给前端
resp.put("a_eyes_finally_direction", nextStepA); resp.put("a_eyes_finally_direction", nextStepA);
resp.put("b_eyes_finally_direction", nextStepB); resp.put("b_eyes_finally_direction", nextStepB);
// 发送结果之前,先将结果存到数据库中
saveToDataBase();
sendAllMessage(resp.toJSONString()); sendAllMessage(resp.toJSONString());
} finally { } finally {
lock.unlock(); lock.unlock();

View File

@ -48,4 +48,14 @@ public class Player {
} }
return res; return res;
} }
// steps 转换为 string 的辅助函数
public String getStepsString() {
StringBuilder res = new StringBuilder();
for (int d : steps) {
res.append(d);
}
return res.toString();
}
} }

View File

@ -0,0 +1,9 @@
package com.kob.backend.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kob.backend.pojo.Record;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface RecordMapper extends BaseMapper<Record> {
}

View File

@ -1,4 +1,5 @@
// 用于将 Mysql Bot 转换为 class // 用于将 Mysql Bot 转换为 class
// 数据库中 user_id 这种在 pojo 里要命名为 userId
package com.kob.backend.pojo; package com.kob.backend.pojo;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;

View File

@ -0,0 +1,34 @@
package com.kob.backend.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.Timer;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Record {
@TableId(type = IdType.AUTO)
private Integer id;
private Integer aId;
private Integer aSx;
private Integer aSy;
private Integer bId;
private Integer bSx;
private Integer bSy;
private String aSteps;
private String bSteps;
private String map;
private String loser;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
private Date createtime;
}