实现bot代码执行的微服务 botrunningsystem, 主服务前后端做出对应修改

This commit is contained in:
2023-03-09 10:46:31 +08:00
parent fffb1d5a60
commit ebd3f3c610
31 changed files with 627 additions and 45 deletions
@@ -21,7 +21,8 @@ public class MatchingController {
public String addPlayer(@RequestParam MultiValueMap<String, String> data) {
Integer userId = Integer.parseInt(Objects.requireNonNull(data.getFirst("user_id")));
Integer rating = Integer.parseInt(Objects.requireNonNull(data.getFirst("rating")));
return matchingService.addPlayer(userId, rating);
Integer botId = Integer.parseInt(Objects.requireNonNull(data.getFirst("bot_id")));
return matchingService.addPlayer(userId, rating,botId);
}
@PostMapping("/player/remove/")
@@ -3,7 +3,7 @@ package com.kob.matchingsystem.service;
public interface MatchingService {
// 给匹配池添加一名玩家
String addPlayer(Integer userId, Integer rating);
String addPlayer(Integer userId, Integer rating,Integer botId);
// 从匹配池删除一名玩家
String removePlayer(Integer userId);
@@ -11,10 +11,10 @@ public class MatchingServiceImpl implements MatchingService {
public final static MatchingPool matchingPool = new MatchingPool();
@Override
public String addPlayer(Integer userId, Integer rating) {
public String addPlayer(Integer userId, Integer rating, Integer botId) {
System.out.println("add player: " + userId + " " + rating);
// 向匹配池添加一名玩家
matchingPool.addPlayer(userId, rating);
matchingPool.addPlayer(userId, rating, botId);
return "add player success";
}
@@ -27,11 +27,11 @@ public class MatchingPool extends Thread {
MatchingPool.restTemplate = restTemplate;
}
public void addPlayer(Integer userId, Integer rating) {
public void addPlayer(Integer userId, Integer rating,Integer botId) {
lock.lock();
try {
// 一开始匹配等待时间是 0
playerList.add(new Player(userId, rating, 0));
playerList.add(new Player(userId, rating,botId, 0));
} finally {
lock.unlock();
}
@@ -77,8 +77,11 @@ public class MatchingPool extends Thread {
// 返回 a 和 b 的匹配结果
private void sendResult(Player a, Player b) {
MultiValueMap<String, String> data = new LinkedMultiValueMap<>();
// 返回两名玩家的 userId 和 botId
data.add("a_id", a.getUserId().toString());
data.add("a_bot_id",a.getBotId().toString());
data.add("b_id", b.getUserId().toString());
data.add("b_bot_id",b.getBotId().toString());
restTemplate.postForObject(startGameUrl, data, String.class);
}
@@ -11,5 +11,6 @@ import lombok.NoArgsConstructor;
public class Player {
private Integer userId;
private Integer rating;
private Integer botId;
private Integer waitingTime; // 等待时间
}