From dc2a352719bec64b1e57c1e69b80e9631a0f89b8 Mon Sep 17 00:00:00 2001 From: flykhan Date: Thu, 9 Mar 2023 16:11:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=AF=B9=E5=B1=80=E8=83=9C?= =?UTF-8?q?=E8=B4=9F=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7=E7=A7=AF=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kob/backend/consumer/WebSocketServer.java | 2 +- .../com/kob/backend/consumer/utils/Game.java | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) 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(),