diff --git a/backendcloud/backend/src/main/java/com/kob/backend/consumer/WebSocketServer.java b/backendcloud/backend/src/main/java/com/kob/backend/consumer/WebSocketServer.java index 7ecf455..cfc443e 100644 --- a/backendcloud/backend/src/main/java/com/kob/backend/consumer/WebSocketServer.java +++ b/backendcloud/backend/src/main/java/com/kob/backend/consumer/WebSocketServer.java @@ -36,7 +36,7 @@ public class WebSocketServer { // 用于和 MatchingSystem 进行通信 public static RestTemplate restTemplate; // 在 WebSocketServer 中注入数据库的方法演示-> 使用 static 定义为独一份的变量 - private static UserMapper userMapper; + public static UserMapper userMapper; private static BotMapper botMapper; // 注入 RecordMapper 用于调用实现游戏数据到数据库的存储 public static RecordMapper recordMapper; diff --git a/backendcloud/backend/src/main/java/com/kob/backend/consumer/utils/Game.java b/backendcloud/backend/src/main/java/com/kob/backend/consumer/utils/Game.java index 1b3ff9a..f83b960 100644 --- a/backendcloud/backend/src/main/java/com/kob/backend/consumer/utils/Game.java +++ b/backendcloud/backend/src/main/java/com/kob/backend/consumer/utils/Game.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONObject; import com.kob.backend.consumer.WebSocketServer; import com.kob.backend.pojo.Bot; import com.kob.backend.pojo.Record; +import com.kob.backend.pojo.User; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -356,8 +357,30 @@ public class Game extends Thread { } } + // 更新玩家积分 + private void updateUserRating(Player player, Integer rating) { + User user = WebSocketServer.userMapper.selectById(player.getId()); + user.setRating(rating); // 修改用户 rating 信息 + WebSocketServer.userMapper.updateById(user); // 更新数据库的 rating 信息 + } + + // 将游戏结果存到数据库中 private void saveToDataBase() { + // 取出用户天梯积分并更新 + Integer ratingA = WebSocketServer.userMapper.selectById(playerA.getId()).getRating(); + Integer ratingB = WebSocketServer.userMapper.selectById(playerB.getId()).getRating(); + if ("A".equals(loser)) { // A 是 Loser 则: 玩家A积分-2,同时玩家B积分+5 + ratingA -= 2; + ratingB += 5; + } else if ("B".equals(loser)) { + ratingA += 5; + ratingB -= 2; + } + updateUserRating(playerA, ratingA); + updateUserRating(playerB, ratingB); + + Record record = new Record( null, playerA.getId(),