365 lines
13 KiB
Java
365 lines
13 KiB
Java
package taurus.newRobot;
|
||
|
||
import com.taurus.core.plugin.redis.Redis;
|
||
import com.taurus.core.util.Logger;
|
||
import redis.clients.jedis.*;
|
||
|
||
import java.util.*;
|
||
import taurus.client.TaurusClient;
|
||
|
||
|
||
public class AddRoomRobot {
|
||
|
||
|
||
private final static Logger log;
|
||
|
||
static {
|
||
log = Logger.getLogger(AddRoomRobot.class);
|
||
}
|
||
|
||
|
||
public void addRoom() {
|
||
Jedis jedis2 = Redis.use("group1_db2").getJedis();
|
||
Jedis jedis11 = Redis.use("group1_db11").getJedis();
|
||
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 shangxian_robot = "0";
|
||
if (jedis11.hget(playKey, "shangxian_robot") != null) {
|
||
shangxian_robot = jedis11.hget(playKey, "shangxian_robot");
|
||
if (Integer.parseInt(shangxian_robot) > 0) {
|
||
|
||
|
||
|
||
//分配完机器之后去看有没有初始化“leftover_robot”字段
|
||
if (jedis11.hget(playKey, "leftover_robot") == null){
|
||
jedis11.hset(playKey, "leftover_robot", shangxian_robot);
|
||
}
|
||
|
||
|
||
Map<String, String> stringStringMap = jedis2.hgetAll(playKey);
|
||
log.info("靠靠靠靠靠靠靠靠靠靠靠靠靠靠靠靠靠靠靠" + stringStringMap);
|
||
//循环stringStringMap
|
||
boolean b = areAllValuesZero(stringStringMap);
|
||
log.info("iiiiiiiiiiiiiiiiiiiiiiiiiiiiii" + b);
|
||
if (b){
|
||
jedis11.hset(playKey, "leftover_robot", shangxian_robot);
|
||
}
|
||
|
||
//给圈子玩法分配机器人,创建到redis
|
||
addPlayRobot(groupId, playId, shangxian_robot);
|
||
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
|
||
//机器人主动找真人
|
||
joinzhenrenRoom();
|
||
System.out.println("进入22222222222222222222222");
|
||
|
||
//机器人主动进入退出, 这里进入的是中括号机器人退出的房间
|
||
zhongJoninRoom();
|
||
// System.out.println("进入3333333333333333333");
|
||
|
||
//机器人创建房间 进入房间
|
||
List<String> gKeys = scanKeysByPattern(jedis2, "g{*}:play:*");
|
||
try {
|
||
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);
|
||
System.out.println("创建房间,进入房间");
|
||
jiqiren jiqiren = new jiqiren();
|
||
System.out.println("circleId +++" + circleId);
|
||
System.out.println("playRoomMap +++ " + playRoomMap);
|
||
jiqiren.createRoomsForCircle(circleId, playRoomMap);
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
} finally {
|
||
jedis2.close();
|
||
jedis11.close();
|
||
}
|
||
}
|
||
|
||
|
||
//机器人进入中括号房间
|
||
public void zhongJoninRoom() {
|
||
Jedis jedis0 = Redis.use().getJedis();
|
||
try {
|
||
//获取所有房间
|
||
Set<String> roomIds = jedis0.keys("room:*");
|
||
for (String id : roomIds) {
|
||
jiqiren jiqiren = new jiqiren();
|
||
jiqiren.tcp_jiqirezhongkouhao(id);
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
} finally {
|
||
jedis0.close();
|
||
}
|
||
}
|
||
|
||
// 判断玩家状态是不是都为0
|
||
public static boolean areAllValuesZero(Map<String, String> map) {
|
||
if (map == null || map.isEmpty()) {
|
||
return false; // 或者 true,根据业务需求
|
||
}
|
||
|
||
for (String value : map.values()) {
|
||
if (!"0".equals(value)) {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
//统计 玩法里 等待中的次数
|
||
// 方法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;
|
||
}
|
||
|
||
/**
|
||
* 使用SCAN命令安全地扫描匹配模式的key
|
||
*/
|
||
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 shangxian_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(shangxian_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:机器人ID,value:使用状态(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;
|
||
}
|
||
|
||
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();
|
||
}
|
||
|
||
}
|
||
}
|