feyerobot/libs/robot_common/src/main/java/taurus/newRobot/AddRoomRobot.java

320 lines
12 KiB
Java
Raw Normal View History

2026-01-28 03:54:13 +08:00
package taurus.newRobot;
import com.taurus.core.plugin.redis.Redis;
import redis.clients.jedis.*;
import java.util.*;
2026-01-30 16:04:50 +08:00
import taurus.client.TaurusClient;
2026-01-28 03:54:13 +08:00
public class AddRoomRobot {
2026-01-28 04:23:18 +08:00
2026-01-28 03:54:13 +08:00
public void addRoom() {
Jedis jedis2 = Redis.use("group1_db2").getJedis();
Jedis jedis11 = Redis.use("group1_db11").getJedis();
2026-01-30 16:04:50 +08:00
System.out.println("进入1111111111111111111111111");
//给玩法分配机器人
if (jedis11.keys("group:*") != null) {
Set<String> groupIds = jedis11.keys("group:*");
for (String group : groupIds) {
String groupId = group.substring(group.indexOf(":") + 1);
String groupKey = "g{" + groupId + "}:play:*";
if (jedis11.keys(groupKey) != null) {
Set<String> playIds = jedis11.keys(groupKey);
// 遍历圈子下面的玩法
for (String pid : playIds) {
String playId = pid.substring(pid.indexOf("play:") + 5);
String playKey = "g{" + groupId + "}:play:" + playId;
String leftover_robot = "0";
if (jedis11.hget(playKey, "leftover_robot") != null) {
leftover_robot = jedis11.hget(playKey, "leftover_robot");
if (Integer.parseInt(leftover_robot) > 0) {
//给圈子玩法分配机器人创建到redis
addPlayRobot(groupId, playId, leftover_robot);
2026-01-28 03:54:13 +08:00
}
}
}
}
}
2026-01-30 16:04:50 +08:00
}
System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
//机器人主动找真人
joinzhenrenRoom();
System.out.println("进入22222222222222222222222");
2026-01-28 03:54:13 +08:00
2026-01-30 16:04:50 +08:00
//机器人主动进入退出, 这里进入的是中括号机器人退出的房间
zhongJoninRoom();
// System.out.println("进入3333333333333333333");
2026-01-28 03:54:13 +08:00
2026-01-30 16:04:50 +08:00
//机器人创建房间 进入房间
List<String> gKeys = scanKeysByPattern(jedis2, "g{*}:play:*");
try {
2026-01-28 03:54:13 +08:00
Map<String, List<String>> stringListMap = analyzePlayStructure(gKeys);
//取出 圈子玩法里的 机器人使用所有的状态
Map<String, Map<String, Map<String, String>>> playDetails = getPlayDetails(jedis2, stringListMap);
System.out.println("result" + playDetails);
//统计 玩法里 等待中的次数 为1的有几个 ,然后需要建立几个房间
Map<String, Map<String, Integer>> stringMapMap = analyzeAndMarkRoomsWithCircle(playDetails);
System.out.println("stringIntegerMap" + stringMapMap);
// 按圈子ID排序处理
List<String> circleIds = new ArrayList<>(stringMapMap.keySet());
Collections.sort(circleIds);
for (String circleId : circleIds) {
Map<String, Integer> playRoomMap = stringMapMap.get(circleId);
2026-01-30 16:04:50 +08:00
System.out.println("创建房间,进入房间");
jiqiren jiqiren = new jiqiren();
System.out.println("circleId +++" + circleId);
System.out.println("playRoomMap +++ " + playRoomMap);
jiqiren.createRoomsForCircle(circleId, playRoomMap);
2026-01-28 03:54:13 +08:00
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis2.close();
jedis11.close();
}
}
2026-01-30 16:04:50 +08:00
//机器人进入中括号房间
public void zhongJoninRoom() {
2026-01-28 03:54:13 +08:00
Jedis jedis0 = Redis.use().getJedis();
try {
2026-01-30 16:04:50 +08:00
//获取所有房间
Set<String> roomIds = jedis0.keys("room:*");
for (String id : roomIds) {
jiqiren jiqiren = new jiqiren();
jiqiren.tcp_jiqirezhongkouhao(id);
}
} catch (Exception e) {
2026-01-28 03:54:13 +08:00
e.printStackTrace();
2026-01-30 16:04:50 +08:00
} finally {
jedis0.close();
2026-01-28 03:54:13 +08:00
}
}
//统计 玩法里 等待中的次数
// 方法1返回 Map<圈子ID, Map<玩法ID, 房间需求>>
public Map<String, Map<String, Integer>> analyzeAndMarkRoomsWithCircle(
Map<String, Map<String, Map<String, String>>> result) {
Map<String, Map<String, Integer>> circlePlayRoomMap = new HashMap<>();
for (Map.Entry<String, Map<String, Map<String, String>>> circleEntry : result.entrySet()) {
String circleId = circleEntry.getKey();
Map<String, Map<String, String>> playData = circleEntry.getValue();
Map<String, Integer> playRoomMark = new HashMap<>();
for (Map.Entry<String, Map<String, String>> playEntry : playData.entrySet()) {
String playId = playEntry.getKey();
Map<String, String> robotData = playEntry.getValue();
// 统计值为"1"的机器人数量
int countOfOnes = 0;
for (String value : robotData.values()) {
if ("1".equals(value)) {
countOfOnes++;
}
}
// 根据逻辑进行标记
if (countOfOnes == 0) {
playRoomMark.put(playId, 2); // 需要建立2个房间
} else if (countOfOnes == 1) {
playRoomMark.put(playId, 1); // 标记为1
} else if (countOfOnes >= 2) {
playRoomMark.put(playId, 0); // 标记为0
}
}
circlePlayRoomMap.put(circleId, playRoomMark);
}
return circlePlayRoomMap;
}
// 统计每个玩法的机器人总数
public Map<String, Map<String, Map<String, String>>> getPlayDetails(Jedis jedis,
Map<String, List<String>> circleToPlays) {
Map<String, Map<String, Map<String, String>>> result = new HashMap<>();
for (Map.Entry<String, List<String>> entry : circleToPlays.entrySet()) {
String circleId = entry.getKey();
List<String> playIds = entry.getValue();
Map<String, Map<String, String>> playDetails = new HashMap<>();
for (String playId : playIds) {
// 构建Redis键
String redisKey = String.format("g{%s}:play:%s", circleId, playId);
// 获取Hash中的所有字段和值
Map<String, String> robotData = jedis.hgetAll(redisKey);
playDetails.put(playId, robotData);
}
result.put(circleId, playDetails);
}
return result;
}
//分割圈子和玩法
public static Map<String, List<String>> analyzePlayStructure(List<String> gKeys) {
Map<String, List<String>> circleToPlays = new HashMap<>();
for (String key : gKeys) {
// 解析键g{圈子ID}:play:玩法ID
String[] parts = key.split(":");
if (parts.length >= 3) {
// 提取圈子ID去掉 g{ 和 }
String circlePart = parts[0]; // g{330800}
String circleId = circlePart.substring(2, circlePart.length() - 1);
// 提取玩法ID
String playId = parts[2]; // 玩法ID
// 将玩法ID添加到对应圈子的列表中
circleToPlays.computeIfAbsent(circleId, k -> new ArrayList<>())
.add(playId);
}
}
return circleToPlays;
}
/**
* 使SCANkey
*/
private static List<String> scanKeysByPattern(Jedis jedis, String pattern) {
List<String> keys = new ArrayList<>();
String cursor = ScanParams.SCAN_POINTER_START;
ScanParams scanParams = new ScanParams();
scanParams.match(pattern);
scanParams.count(1000); // 每次扫描数量
do {
ScanResult<String> scanResult = jedis.scan(cursor, scanParams);
cursor = String.valueOf(scanResult.getCursor());
keys.addAll(scanResult.getResult());
} while (!cursor.equals(ScanParams.SCAN_POINTER_START));
return keys;
}
//给圈子玩法分配机器人创建到redis
public void addPlayRobot(String groupId, String playId, String leftover_robot) {
Jedis jedis2 = Redis.use("group1_db2").getJedis();
// 为这个玩法创建机器人配置
String robotPlayKey = "g{" + groupId + "}:play:" + playId;
boolean exists = jedis2.exists(robotPlayKey);
if (!exists) {
// 计算需要配置的机器人数量
int num = Integer.parseInt(leftover_robot) * 2;
// 获取所有可用的机器人
List<String> availableRobots1 = getAvailableRobots();
List<String> availableRobots = new ArrayList<>();
if (availableRobots1.size() > 0) {
for (String key : availableRobots1) {
String robotId = key.substring(key.indexOf(":") + 1);
availableRobots.add(robotId);
}
// 检查是否有足够的机器人
if (availableRobots.size() < num) {
System.out.println("警告:玩法 " + playId + " 需要 " + num + " 个机器人,但只有 " + availableRobots.size() + " 个可用");
return;
}
}
// 存储机器人信息使用Hash结构
Map<String, String> robotConfigs = new HashMap<>();
for (int i = 0; i < num; i++) {
String robotId = availableRobots.get(i);
// key格式robot:机器人IDvalue使用状态0-未使用)
robotConfigs.put(robotId, "0");
String robotKey = "{robot}:" + robotId;
jedis2.hset(robotKey, "start", "1");
}
// 批量设置机器人配置
if (!robotConfigs.isEmpty()) {
jedis2.hmset(robotPlayKey, robotConfigs);
System.out.println("玩法 " + playId + " 已配置 " + num + " 个机器人");
}
jedis2.close();
}
}
/**
* start=0
*
* @return ID
*/
public List<String> getAvailableRobots() {
Jedis jedis2 = Redis.use("group1_db2").getJedis();
Set<String> allRobotKeys = jedis2.keys("{robot}:*");
List<String> availableRobots = new ArrayList<>();
// 使用Pipeline提高效率
Pipeline pipeline = jedis2.pipelined();
List<Response<String>> responses = new ArrayList<>();
for (String robotKey : allRobotKeys) {
responses.add(pipeline.hget(robotKey, "start"));
}
pipeline.sync();
// 提取机器人ID并筛选
int index = 0;
for (String robotKey : allRobotKeys) {
String startStatus = responses.get(index).get();
if ("0".equals(startStatus)) {
String robotId = robotKey.substring(robotKey.indexOf(":") + 1);
availableRobots.add(robotId);
}
index++;
}
jedis2.close();
return availableRobots;
}
2026-01-30 16:04:50 +08:00
public void joinzhenrenRoom() {
Jedis jedis0 = Redis.use().getJedis();
Jedis jedis11 = Redis.use("group1_db11").getJedis();
Jedis jedis2 = Redis.use("group1_db2").getJedis();
try {
//获取所有房间
Set<String> roomIds = jedis0.keys("room:*");
for (String id : roomIds) {
jiqiren jiqiren = new jiqiren();
jiqiren.tcp_jiqirenzhenren(id);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis0.close();
jedis11.close();
jedis2.close();
}
}
2026-01-28 03:54:13 +08:00
}