红中机器人调整

master
zhouwei 2026-02-26 12:54:24 +08:00
parent 3e74b5cc43
commit 562a4678df
4 changed files with 119 additions and 16 deletions

View File

@ -26,4 +26,26 @@ public class Config {
*/ */
public static final String JOIN_ROOM_CS = "1002"; public static final String JOIN_ROOM_CS = "1002";
/** Web组加入房间协议 */
public static final String WEB_GROUP_JOIN_ROOM = "225";
/** Web组主动重连协议 */
public static final String WEB_GROUP_ACTIVE_RECONNECT = "226";
//==================== 游戏服务器配置 ====================
/** 游戏服务器主机地址 */
public static final String GAME_SERVER_HOST = "127.0.0.1";
/** 游戏服务器端口 */
public static final String GAME_SERVER_PORT = "6421";
/** 默认密码 */
public static final String DEFAULT_PASSWORD = "123456";
/** 默认PID */
public static final String DEFAULT_PID = "22";
/** 默认群组ID */
public static final String DEFAULT_GROUP_ID = "330800";
} }

View File

@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
@ -219,7 +220,7 @@ public class EXGameController extends GameController {
/** /**
* web_group * web_group
*/ */
@ActionKey(value = "225", validate = GameInterceptor.NOT_PLAYER) @ActionKey(value = Config.WEB_GROUP_JOIN_ROOM, validate = GameInterceptor.NOT_PLAYER)
public void webGroup(Session session, ITObject params, int gid) { public void webGroup(Session session, ITObject params, int gid) {
int robotId = params.getInt("robotid"); int robotId = params.getInt("robotid");
String roomId = params.getString("roomid"); String roomId = params.getString("roomid");
@ -245,10 +246,38 @@ public class EXGameController extends GameController {
/** /**
* web_group * web_group
*/ */
@ActionKey(value = "226", validate = GameInterceptor.NOT_PLAYER) @ActionKey(value = Config.WEB_GROUP_ACTIVE_RECONNECT, validate = GameInterceptor.NOT_PLAYER)
public void webGroupActive(Session session, ITObject params, int gid) { public void webGroupActive(Session session, ITObject params, int gid) {
int robotId = params.getInt("robotid"); int robotId = params.getInt("robotid");
String roomId = params.getString("roomid"); String roomId = params.getString("roomid");
//检查Redis中该房间是否真的包含当前机器人
if (!checkRobotInRoomRedis(roomId, String.valueOf(robotId))) {
//Redis中不存在该机器人 清理本地可能的错误映射
List<RobotUser> robotUsers = getRobotUsersByRoomId(Integer.parseInt(roomId));
if (!robotUsers.isEmpty()) {
synchronized (robotUsers) {
RobotUser robotUser = robotUsers.get(0);
System.err.println("房间{"+ roomId +"}中Redis未找到机器人{"+robotId+"},但本地映射存在{"+robotUser.getRobotId()+"},清理本地映射");
robotRoomMapping.remove(robotUser.getConnecId());
robotRoomMapping.remove(robotUser.getRobotId());
}
}
} else {
//Redis中存在该机器人 检查是否是不同机器人的冲突
List<RobotUser> robotUsers = getRobotUsersByRoomId(Integer.parseInt(roomId));
if (!robotUsers.isEmpty()) {
synchronized (robotUsers) {
RobotUser robotUser = robotUsers.get(0);
int existingRobotId = Integer.parseInt(robotUser.getRobotId());
if (robotId != existingRobotId) {
//不同机器人的冲突
System.err.println("房间{"+ roomId +"}中Redis已存在机器人{"+existingRobotId+"},当前机器人{"+robotId+"}不执行加入逻辑");
return;
}
}
}
}
System.err.println("226开始进房间: " + "room:" + roomId + "robot:" + robotId); System.err.println("226开始进房间: " + "room:" + roomId + "robot:" + robotId);
//加入房间 //加入房间
joinRoomCommon(params.getInt("robotid"), params.getString("roomid"), params.getInt("groupid"), params); joinRoomCommon(params.getInt("robotid"), params.getString("roomid"), params.getInt("groupid"), params);
@ -325,13 +354,16 @@ public class EXGameController extends GameController {
robotUser.setClient(client); robotUser.setClient(client);
robotUser.setConnecId(connecId); robotUser.setConnecId(connecId);
} }
robotRoomMapping.put(robotUser.getConnecId(), robotUser); //先不放入映射 等确认加入成功后再放入
//robotRoomMapping.put(robotUser.getConnecId(), robotUser);
robotRoomMapping.remove(robotUser.getRobotId()); robotRoomMapping.remove(robotUser.getRobotId());
Thread.sleep(2000); Thread.sleep(2000);
params.putString("session", "{user}:" + robotId + "," + robotSession); params.putString("session", "{user}:" + robotId + "," + robotSession);
//发送加入房间请求到game_mj_cs //发送加入房间请求到game_mj_cs
client.send(Config.JOIN_ROOM_CS, params, response -> { client.send(Config.JOIN_ROOM_CS, params, response -> {
//成功响应后才建立映射关系
robotRoomMapping.put(robotUser.getConnecId(), robotUser);
robotConnectionManager.reconnectToGameServer(response, robotUser, client); robotConnectionManager.reconnectToGameServer(response, robotUser, client);
}); });
System.err.println("已进入房间成功: " + robotUser.getConnecId()); System.err.println("已进入房间成功: " + robotUser.getConnecId());
@ -345,6 +377,20 @@ public class EXGameController extends GameController {
robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_READY); robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_READY);
robotConnectionManager.setSessionAndToken("{user}:" + robotId, robotSession, robotUser.getConnecId()); robotConnectionManager.setSessionAndToken("{user}:" + robotId, robotSession, robotUser.getConnecId());
} }
//添加超时检查机制
CompletableFuture.runAsync(() -> {
try {
Thread.sleep(15000);
//15秒后还没有建立映射关系 加入可能失败
if (robotRoomMapping.get(robotUser.getConnecId()) == null) {
System.err.println("机器人{"+robotId+"}加入房间{"+roomId+"}超时,清理临时状态");
//清理可能残留的状态
robotConnectionManager.disconnectFromGameServer(connecId);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
robotUser.setIntoRoomTime(robotConnectionManager.getTime()); robotUser.setIntoRoomTime(robotConnectionManager.getTime());
System.err.println("已进入房间准备成功: " + robotUser.getConnecId()); System.err.println("已进入房间准备成功: " + robotUser.getConnecId());
} catch (Exception e) { } catch (Exception e) {
@ -363,11 +409,11 @@ public class EXGameController extends GameController {
if (robotUser ==null) { if (robotUser ==null) {
RobotUser robotUserCopy = new RobotUser(); RobotUser robotUserCopy = new RobotUser();
robotUserCopy.setRobotId(robotId); robotUserCopy.setRobotId(robotId);
robotUserCopy.setPassword("123456"); robotUserCopy.setPassword(Config.DEFAULT_PASSWORD);
robotUserCopy.setGameHost("127.0.0.1"); robotUserCopy.setGameHost(Config.GAME_SERVER_HOST);
robotUserCopy.setGamePort("6421"); robotUserCopy.setGamePort(Config.GAME_SERVER_PORT);
robotUserCopy.setRobotGroupid("330800"); robotUserCopy.setRobotGroupid(Config.DEFAULT_GROUP_ID);
robotUserCopy.setRobotPid("22"); robotUserCopy.setRobotPid(Config.DEFAULT_PID);
return robotUserCopy; return robotUserCopy;
} }
return robotRoomMapping.get(robotId); return robotRoomMapping.get(robotId);
@ -410,4 +456,39 @@ public class EXGameController extends GameController {
return taurusClient; return taurusClient;
} }
/**
* Redis
* @param roomId ID
* @param robotId ID
* @return
*/
private boolean checkRobotInRoomRedis(String roomId, String robotId) {
Jedis jedis = Redis.use().getJedis();
try {
//查询该房间的玩家信息
String playersStr = jedis.hget("room:" + roomId, "players");
if (playersStr == null || playersStr.equals("[]")) {
return false;
}
String players = playersStr.substring(1, playersStr.length() - 1);
String[] playerIds = players.split(",");
//检查是否包含该机器人
int targetRobotId = Integer.parseInt(robotId);
for (String playerIdStr : playerIds) {
int playerId = Integer.parseInt(playerIdStr.trim());
if (playerId == targetRobotId) {
return true;
}
}
return false;
} catch (Exception e) {
log.error("检查Redis房间玩家信息时发生错误roomId: {}, robotId: {}", roomId, robotId, e);
return false;
} finally {
jedis.close();
}
}
} }

View File

@ -35,18 +35,18 @@ public class EXMainServer extends MainServer{
//startConnectionCheckScheduler(); //startConnectionCheckScheduler();
//测试 //测试
Jedis jedis2 = Redis.use("group1_db2").getJedis(); Jedis jedis2 = Redis.use("group1_db2").getJedis();
String robotskey = "g{"+762479+"}:play:"+22; String robotskey = "g{"+Config.DEFAULT_GROUP_ID+"}:play:"+Config.DEFAULT_PID;
Map<String, String> maprobot = jedis2.hgetAll(robotskey); Map<String, String> maprobot = jedis2.hgetAll(robotskey);
for(Map.Entry<String, String> entry : maprobot.entrySet()) { for(Map.Entry<String, String> entry : maprobot.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue()); System.out.println(entry.getKey() + ":" + entry.getValue());
//是否创建 //是否创建
RobotUser robotUser = new RobotUser(); RobotUser robotUser = new RobotUser();
robotUser.setRobotId(entry.getKey()); robotUser.setRobotId(entry.getKey());
robotUser.setPassword("123456"); robotUser.setPassword(Config.DEFAULT_PASSWORD);
robotUser.setGameHost("127.0.0.1"); robotUser.setGameHost(Config.GAME_SERVER_HOST);
robotUser.setGamePort("6421"); robotUser.setGamePort(Config.GAME_SERVER_PORT);
robotUser.setRobotGroupid("762479"); robotUser.setRobotGroupid(Config.DEFAULT_GROUP_ID);
robotUser.setRobotPid("22"); robotUser.setRobotPid(Config.DEFAULT_PID);
robotRoomMapping.put(entry.getKey(), robotUser); robotRoomMapping.put(entry.getKey(), robotUser);
} }

View File

@ -31,8 +31,8 @@ public class RobotConnectionManager {
private static final Map<String, HuNanHongZhong> huNanHongZhongInstances = new ConcurrentHashMap<>(); private static final Map<String, HuNanHongZhong> huNanHongZhongInstances = new ConcurrentHashMap<>();
private final EXGameController exGameController; private final EXGameController exGameController;
private final String host="127.0.0.1"; private final String host= Config.GAME_SERVER_HOST;
private final int port=6421; private final int port= Integer.parseInt(Config.GAME_SERVER_PORT);
public RobotConnectionManager() { public RobotConnectionManager() {