根据对局胜负更新用户积分

This commit is contained in:
flykhan 2023-03-09 16:11:18 +08:00
parent ebd3f3c610
commit dc2a352719
2 changed files with 24 additions and 1 deletions

View File

@ -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;

View File

@ -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(),