本地调试成功

This commit is contained in:
2023-03-12 20:08:43 +08:00
parent 304f2fbd0c
commit cc95f82b7f
29 changed files with 132 additions and 74 deletions
+22
View File
@@ -12,6 +12,28 @@
<groupId>com.kob.matchingsystem</groupId>
<artifactId>matchingsystem</artifactId>
<!-- 项目打包类型 : jar -->
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--这里写上main方法所在类的路径-->
<configuration>
<mainClass>com.kob.matchingsystem.MatchingSystemApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
@@ -18,7 +18,7 @@ public class MatchingPool extends Thread {
// 定义一个锁, Reentrant Lock 是可重入锁
private final ReentrantLock lock = new ReentrantLock();
// 定义微服务传值 URL
private static final String startGameUrl = "http://127.0.0.1:3000/pk/start/";
private static final String startGameUrl = "http://127.0.0.1:3001/pk/start/";
// 定义 RestTemplate 用来微服务数据通信
private static RestTemplate restTemplate;
@@ -27,11 +27,11 @@ public class MatchingPool extends Thread {
MatchingPool.restTemplate = restTemplate;
}
public void addPlayer(Integer userId, Integer rating,Integer botId) {
public void addPlayer(Integer userId, Integer rating, Integer botId) {
lock.lock();
try {
// 一开始匹配等待时间是 0
playerList.add(new Player(userId, rating,botId, 0));
playerList.add(new Player(userId, rating, botId, 0));
} finally {
lock.unlock();
}
@@ -65,7 +65,7 @@ public class MatchingPool extends Thread {
// 判断两名玩家是否匹配
private boolean checkMatched(Player a, Player b) {
int ratingDelta = Math.abs(a.getRating() - b.getRating()); // 两名玩家的天梯积分之差
int ratingDelta = Math.abs(a.getRating() - b.getRating()); // 两名玩家的天梯积分之差
// a b 两名玩家的等待时间最小值
// min 是两方任意一方接受等待时间就可以匹配, max 是两方都接受的匹配
int minWaitingTime = Math.min(a.getWaitingTime(), b.getWaitingTime());
@@ -79,29 +79,31 @@ public class MatchingPool extends Thread {
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("a_bot_id", a.getBotId().toString());
data.add("b_id", b.getUserId().toString());
data.add("b_bot_id",b.getBotId().toString());
data.add("b_bot_id", b.getBotId().toString());
restTemplate.postForObject(startGameUrl, data, String.class);
}
// 尝试匹配所有玩家
private void matchPlayers() {
//TODO 后端调试
System.out.println("match players: " + playerList.toString());
// TODO 后端调试
// System.out.println("match players: " + playerList.toString());
// havaMatched 表示玩家已经匹配过了
boolean[] haveMatched = new boolean[playerList.size()];
for (int i = 0; i < playerList.size(); i++) {
if (haveMatched[i]) continue; // 如果当前枚举到的玩家已经匹配过了,则跳过改玩家
for (int j = i + 1; j < playerList.size(); j++) { // j 从 i+1 开始枚举
if (haveMatched[j]) continue;
if (haveMatched[i])
continue; // 如果当前枚举到的玩家已经匹配过了,则跳过改玩家
for (int j = i + 1; j < playerList.size(); j++) { // j 从 i+1 开始枚举
if (haveMatched[j])
continue;
// 如果 i 和 j 都没有匹配,则将 a 和 b 玩家取出来
Player a = playerList.get(i);
Player b = playerList.get(j);
// 判断 a 和 b 能否匹配:如果匹配,则将结果返回,并将 a 和 b 的位置置为 true
if (checkMatched(a, b)) {
haveMatched[i] = haveMatched[j] = true; // 置为已匹配
sendResult(a, b); // 返回匹配玩家结果
sendResult(a, b); // 返回匹配玩家结果
break;
}
}
@@ -1 +1 @@
server.port=3001
server.port=3002