修改红中部分log信息

master
zhouwei 2026-03-04 17:18:21 +08:00
parent 4ccc1f3382
commit f05b0c2641
4 changed files with 76 additions and 41 deletions

View File

@ -392,11 +392,6 @@ public class EXGameController extends GameController {
robotConnectionManager.disconnectFromGameServer(connecId);
}
}, 15, TimeUnit.SECONDS);
//15秒后还没有建立映射关系 加入可能失败
if (robotRoomMapping.get(robotUser.getConnecId()) == null) {
log.info("机器人{}加入房间{}超时,清理临时状态", robotId, roomId);
robotConnectionManager.disconnectFromGameServer(connecId);
}
} catch (Exception e) {
log.error("机器人加入房间超时", e);
}

View File

@ -9,7 +9,8 @@ import com.taurus.core.events.IEventListener;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.ICallback;
import com.taurus.core.util.StringUtil;
import org.apache.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import robot.mj.business.AccountBusiness;
import robot.mj.handler.HuNanHongZhong;
import robot.mj.info.RobotUser;
@ -31,8 +32,8 @@ import static robot.mj.thread.ThreadPoolConfig.scheduleDelay;
*/
public class RobotConnectionManager {
private static final Map<String, HuNanHongZhong> huNanHongZhongInstances = new ConcurrentHashMap<>();
private static final Logger log = Logger.getLogger(RobotConnectionManager.class);
private final Map<String, HuNanHongZhong> huNanHongZhongInstances = new ConcurrentHashMap<>();
private static final Logger log = LoggerFactory.getLogger(RobotConnectionManager.class);
//记录活跃连接 用于资源清理判断
private static final Set<String> activeConnections = ConcurrentHashMap.newKeySet();
@ -106,6 +107,7 @@ public class RobotConnectionManager {
return client;
} catch (Exception e) {
log.error("连接到游戏服务器时发生异常: " + connecId, e);
return null;
}
}
@ -114,7 +116,7 @@ public class RobotConnectionManager {
*
*/
public void disconnectFromGameServer(String connecId) {
log.info("开始主动断开连接: {"+connecId+"}");
log.info("开始主动断开连接{}", connecId);
RobotUser robotUser = robotRoomMapping.remove(connecId);
//标记连接为非活跃
@ -124,34 +126,37 @@ public class RobotConnectionManager {
//清理连接数据
if (connecId != null) {
HuNanHongZhong.removeFromRedis(connecId);
HuNanHongZhong instance = huNanHongZhongInstances.get(connecId);
if (instance != null) {
//清理所有集合数据以释放内存
instance.getHongZhongCardInhand().clear();
instance.getChuGuoCardInhand().clear();
log.info("清理HuNanHongZhong集合数据: " + connecId);
log.info("清理 HuNanHongZhong 集合数据:{}", connecId);
}
//移除实例
huNanHongZhongInstances.remove(connecId);
log.info("清理完成,当前活跃连接数: " + activeConnections.size() + ", 实例数: " + huNanHongZhongInstances.size());
log.info("清理完成,当前活跃连接数{}, 实例数:{}", activeConnections.size(), huNanHongZhongInstances.size());
}
if (robotUser != null) {
TaurusClient client = robotUser.getClient();
if (client != null) {
try {
//先检查连接状态,再断开
if (client.isConnected()) {
client.killConnection();
log.info("客户端连接已强制断开:{}", connecId);
} else {
log.info("客户端连接已经断开:{}", connecId);
}
log.info("客户端主动断开连接完成: {"+connecId+"}");
} catch (Exception e) {
log.error("断开客户端连接时发生异常: " + connecId + ", 错误: " + e.getMessage());
log.error("断开客户端连接时发生异常{}, 错误:{}", connecId, e.getMessage());
}
} else {
log.info("客户端连接不存在: {"+connecId+"}");
log.info("客户端连接不存在{}", connecId);
}
//同时清理机器人房间映射
@ -192,17 +197,21 @@ public class RobotConnectionManager {
IEventListener messageListener = new IEventListener() {
@Override
public void handleEvent(Event event) {
//获取 msg
Message message = (Message) event.getParameter("msg");
ITObject param = message.param;
//回调协议号
String command = message.command;
//根据玩法ID处理不同的回调
if (StringUtil.isNotEmpty(command)) {
//直接处理协议
handleProtocol(command, message, client, connecId);
try {
//获取 msg
Message message = (Message) event.getParameter("msg");
ITObject param = message.param;
//回调协议号
String command = message.command;
//根据玩法 ID 处理不同的回调
if (StringUtil.isNotEmpty(command)) {
//直接处理协议
handleProtocol(command, message, client, connecId);
}
} catch (Exception e) {
log.error("处理游戏协议时发生异常connecId: {}, command: {}", connecId, ((Message)event.getParameter("msg")).command, e);
}
}
};
@ -214,6 +223,9 @@ public class RobotConnectionManager {
Message message = (Message) event.getParameter("msg");
SocketCode code = (SocketCode) event.getParameter("code");
if (code == SocketCode.Connect) {
log.info("连接建立成功connecId: {}", connecId);
}
}
};
@ -393,10 +405,10 @@ public class RobotConnectionManager {
huNanHongZhong.getChuGuoCardInhand().clear();
log.info("红中结算");
Integer type = param.getInt("type");
if (type == 1 || type == 2) { //为1 为大结算 为2为解散,都需要恢复数据
if (type == 1 || type == 2) { //为1为大结算 为2为解散
//更新机器人剩余数量
updateLeftoverRobot(Integer.parseInt(robotUser.getRobotId()));
//游戏结束后主动断开连接
disconnectFromGameServer(connecId);
}

View File

@ -23,7 +23,7 @@ import static robot.mj.thread.ThreadPoolConfig.getBusinessThreadPool;
public class HuNanHongZhong {
private static final Logger log = LoggerFactory.getLogger(HuNanHongZhong.class);
public static int hongZhongCard = 0;
public int hongZhongCard = 0;
//红中麻将手牌
private final List<Integer> hongZhongCardInhand = new ArrayList<>();
@ -32,15 +32,14 @@ public class HuNanHongZhong {
private final List<Integer> hongZhongchuguopai = new ArrayList<>();
// 玩家座位号
public static int seat = 0;
public static int playerId = 0;
public int seat = 0;
public int playerId = 0;
// 会话标识
public static String session = "";
public String session = "";
// 访问令牌
public static String token = "";
public String token = "";
private static HongZhongSuanFaTest hongZhongSuanFaTest = new HongZhongSuanFaTest();
@ -54,6 +53,30 @@ public class HuNanHongZhong {
public List<Integer> getChuGuoCardInhand() {
return hongZhongchuguopai;
}
public int getSeat() {
return seat;
}
public void setSeat(int seat) {
this.seat = seat;
}
public int getPlayerId() {
return playerId;
}
public void setPlayerId(int playerId) {
this.playerId = playerId;
}
public int getHongZhongCard() {
return hongZhongCard;
}
public void setHongZhongCard(int hongZhongCard) {
this.hongZhongCard = hongZhongCard;
}
@ -176,8 +199,9 @@ public class HuNanHongZhong {
if (param == null) {
return null;
}
hongZhongCard = param.getInt("card");
log.info("出牌广播: {}", hongZhongCard);
//使用实例变量
setHongZhongCard(param.getInt("card"));
log.info("出牌广播:{}", getHongZhongCard());
log.info("座位号:{}的用户出牌:{}", param.getInt("seat"), param.getInt("card"));
}
return null;
@ -267,6 +291,10 @@ public class HuNanHongZhong {
for (int i = 0; i < cardList.size(); i++) {
hongZhongCardInhand.add(cardList.getInt(i));
}
//设置座位号
setSeat(param.getInt("seat"));
setPlayerId(param.getInt("player"));
if (hongZhongCardInhand.size() > 13) {
outCard(client);
log.info("机器人:{}为庄家,需要出牌, 牌为:{}", param.getInt("seat"), hongZhongCardInhand.get(0));
@ -391,7 +419,7 @@ public class HuNanHongZhong {
hongZhongSuanFaTest.separateAndAnalyzeHand(hongZhongCardInhand);
// 红中麻将出牌
String hongzhongOutCard = hongZhongSuanFaTest.outCardSuanFa(hongZhongCardInhand, hongZhongCard);
String hongzhongOutCard = hongZhongSuanFaTest.outCardSuanFa(hongZhongCardInhand, getHongZhongCard());
// String hongzhongOutCard = hongZhongSuanFaTest.outCardSuanFa(list, hongZhongCard);
ITObject params = TObject.newInstance();
int cardToOut;
@ -456,7 +484,7 @@ public class HuNanHongZhong {
log.debug("删除出过的牌组 card: {}", card);
log.debug("删除出过的牌组 type: {}", type);
log.debug("删除出过的牌组 from_seat: {}", from_seat);
log.debug("机器人 seat: {}", seat);
log.debug("机器人 seat: {}", getSeat());
if (type == 2 || type == 3 || type == 5) { // 碰,杠
getChuGuoCardInhand().remove(Integer.valueOf(card));

View File

@ -35,7 +35,7 @@ public class ThreadPoolConfig {
//添加定时任务线程池
private static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE =
new ScheduledThreadPoolExecutor(2, new ThreadFactory() {
new ScheduledThreadPoolExecutor(10, new ThreadFactory() {
private final AtomicInteger threadNumber = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {