diff --git a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXGameController.java b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXGameController.java index 8444697..ae2b8a8 100644 --- a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXGameController.java +++ b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXGameController.java @@ -131,8 +131,8 @@ public class EXGameController extends GameController { try { String connecId = params.getString("connecId"); TaurusClient client = getCsMjGameServerConnection(connecId); - System.out.println("接收来自robot_mgr的加入房间协议 connecId: = "+connecId); - System.out.println("接收来自robot_mgr的加入房间协议 client: = "+client); + log.info("接收来自robot_mgr的加入房间协议 connecId: {}", connecId); + log.info("接收来自robot_mgr的加入房间协议 client: {}", client); if (client == null) { ITObject errorResponse = TObject.newInstance(); errorResponse.putString("status", "failed"); @@ -170,7 +170,7 @@ public class EXGameController extends GameController { params.del("connecId"); //发送加入房间请求到game_mj_cs client.send(Config.JOIN_ROOM_CS, params, response -> { - System.out.println("joinRoomController: " + response); + log.info("joinRoomController: {}", response); RobotUser roomInfo = new RobotUser(); roomInfo.setCurrentRoomId(Integer.parseInt(roomId)); roomInfo.setConnecId(connecId); @@ -200,8 +200,8 @@ public class EXGameController extends GameController { try { String connecId = params.getString("connecId"); TaurusClient client = getCsMjGameServerConnection(connecId); - System.out.println("接收来自robot_mgr的机器人准备协议 connecId: = "+connecId); - System.out.println("接收来自robot_mgr的机器人准备协议 client: = "+client); + log.info("接收来自robot_mgr的机器人准备协议 connecId: {}", connecId); + log.info("接收来自robot_mgr的机器人准备协议 client: {}", client); if (client == null) { ITObject errorResponse = TObject.newInstance(); errorResponse.putString("status", "failed"); @@ -214,7 +214,7 @@ public class EXGameController extends GameController { Thread.sleep(1000); //发送准备请求到game_mj_cs client.send(Config.GAME_READY_CS, params, response -> { - System.out.println("robotReadyRoom: " + response); + log.info("robotReadyRoom: {}", response); }); ITObject paramsReq = TObject.newInstance(); @@ -246,7 +246,7 @@ public class EXGameController extends GameController { if (!robotUsers.isEmpty()) { synchronized (robotUsers) { RobotUser robotUser = robotUsers.get(0); - System.err.println("房间{"+ roomId +"}中Redis未找到机器人{"+robotId+"},但本地映射存在{"+robotUser.getRobotId()+"},清理本地映射"); + log.warn("房间{}中Redis未找到机器人{},但本地映射存在{},清理本地映射", roomId, robotId, robotUser.getRobotId()); robotRoomMapping.remove(robotUser.getConnecId()); robotRoomMapping.remove(robotUser.getRobotId()); } @@ -261,16 +261,16 @@ public class EXGameController extends GameController { if (robotId != existingRobotId) { //不同机器人的冲突 - System.err.println("房间{"+ roomId +"}中Redis已存在机器人{"+existingRobotId+"},当前机器人{"+robotId+"}不执行加入逻辑"); + log.warn("房间{}中Redis已存在机器人{},当前机器人{}不执行加入逻辑", roomId, existingRobotId, robotId); return; } } } } - System.err.println("225开始进房间: "+"room:"+ roomId +"robot:"+robotId); + log.info("225开始进房间: room:{} robot:{}", roomId, robotId); //加入房间 joinRoomCommon(robotId, roomId, groupId, params); - System.err.println("225已进入房间准备成功: "+"room:"+ roomId +"robot:"+robotId); + log.info("225已进入房间准备成功: room:{} robot:{}", roomId, robotId); } /** @@ -280,10 +280,10 @@ public class EXGameController extends GameController { public void webGroupActive(Session session, ITObject params, int gid) { int robotId = params.getInt("robotid"); String roomId = params.getString("roomid"); - System.err.println("226开始进房间: " + "room:" + roomId + "robot:" + robotId); + log.info("226开始进房间: room:{} robot:{}", roomId, robotId); //加入房间 joinRoomCommon(params.getInt("robotid"), params.getString("roomid"), params.getInt("groupid"), params); - System.err.println("226已进入房间准备成功: " + "room:" + roomId + "robot:" + robotId); + log.info("226已进入房间准备成功: room:{} robot:{}", roomId, robotId); } /** @@ -310,12 +310,12 @@ public class EXGameController extends GameController { return; } - System.err.println("重启后开始进房间: " + "room:" + robotUser.getCurrentRoomId() + "robot:" + robotUser.getRobotId()); + log.info("重启后开始进房间: room:{} robot:{}", robotUser.getCurrentRoomId(), robotUser.getRobotId()); ITObject params = new TObject(); params.putString("session", "{user}:" + robotUser.getRobotId() + "," + robotSession); //加入房间 joinRoomCommon(Integer.parseInt(robotUser.getRobotId()), String.valueOf(robotUser.getCurrentRoomId()), Integer.parseInt(robotUser.getRobotGroupid()), params); - System.err.println("重启后已进入房间准备成功: " + "room:" + robotUser.getCurrentRoomId() + "robot:" + robotUser.getRobotId()); + log.info("重启后已进入房间准备成功: room:{} robot:{}", robotUser.getCurrentRoomId(), robotUser.getRobotId()); } catch (Exception e) { log.error("重启服务断线重连时发生错误", e); @@ -342,8 +342,8 @@ public class EXGameController extends GameController { } } - System.err.println("开始进房间: room:" + roomId); - System.err.println("开始进房间: {user}:" + robotId); + log.info("开始进房间: room:{}", roomId); + log.info("开始进房间: {user}:{}", robotId); TaurusClient client = getCsMjGameServerConnection(roomId + "_" + robotId); GroupRoomBusiness.joinRoom(groupId, "room:" + roomId, "{user}:" + robotId, null); @@ -373,11 +373,11 @@ public class EXGameController extends GameController { robotConnectionManager.reconnectToGameServer(response, robotUser, client); }); - System.err.println("已进入房间成功: " + robotUser.getConnecId()); + log.info("已进入房间成功: {}", robotUser.getConnecId()); Thread.sleep(1000); if (client.isConnected()) { client.send(Config.GAME_READY_CS, params, response -> { - System.out.println("1003:" + response); + log.info("1003:{}", response); }); jedis2.hset("gallrobot", String.valueOf(robotUser.getRobotId()), "1"); @@ -391,13 +391,13 @@ public class EXGameController extends GameController { scheduleDelay(() -> { //15秒后还没有建立映射关系 加入可能失败 if (robotRoomMapping.get(robotUser.getConnecId()) == null) { - System.err.println("机器人{"+robotId+"}加入房间{"+roomId+"}超时,清理临时状态"); + log.warn("机器人{}加入房间{}超时,清理临时状态", robotId, roomId); robotConnectionManager.disconnectFromGameServer(connecId); } }, 15, TimeUnit.SECONDS); //15秒后还没有建立映射关系 加入可能失败 if (robotRoomMapping.get(robotUser.getConnecId()) == null) { - System.err.println("机器人{"+robotId+"}加入房间{"+roomId+"}超时,清理临时状态"); + log.warn("机器人{}加入房间{}超时,清理临时状态", robotId, roomId); robotConnectionManager.disconnectFromGameServer(connecId); } } catch (Exception e) { @@ -405,7 +405,7 @@ public class EXGameController extends GameController { } }, ThreadPoolConfig.getBusinessThreadPool());//指定自定义线程池 robotUser.setIntoRoomTime(robotConnectionManager.getTime()); - System.err.println("已进入房间准备成功: " + robotUser.getConnecId()); + log.info("已进入房间准备成功: {}", robotUser.getConnecId()); } catch (Exception e) { log.error("加入房间时发生错误", e); } finally { @@ -460,7 +460,7 @@ public class EXGameController extends GameController { lastAccessTime.remove(removedUser.getConnecId()); } - System.out.println("清理机器人房间信息: " + robotId); + log.info("清理机器人房间信息: {}", robotId); } /** @@ -486,7 +486,7 @@ public class EXGameController extends GameController { for (String connecId : expiredConnections) { RobotUser robotUser = robotRoomMapping.get(connecId); if (robotUser != null) { - System.out.println("清理超时连接: " + connecId + ", 机器人ID: " + robotUser.getRobotId()); + log.info("清理超时连接: {}, 机器人ID: {}", connecId, robotUser.getRobotId()); robotConnectionManager.disconnectFromGameServer(connecId); } robotRoomMapping.remove(connecId); @@ -494,7 +494,7 @@ public class EXGameController extends GameController { } if (!expiredConnections.isEmpty()) { - System.out.println("本次清理了 " + expiredConnections.size() + " 个超时连接"); + log.info("本次清理了 {} 个超时连接", expiredConnections.size()); } } @@ -539,7 +539,7 @@ public class EXGameController extends GameController { */ public static TaurusClient getCsMjGameServerConnection(String connecId) { TaurusClient taurusClient = robotConnectionManager.getGameClient(connecId); - System.out.println("根据机器人ID和连接ID获取长沙麻将游戏服务器连接 client: = "+taurusClient); + log.info("根据机器人ID和连接ID获取长沙麻将游戏服务器连接 client: {}", taurusClient); if (taurusClient != null) { log.debug("成功获取游戏服务器连接,connecId: {}", connecId); return taurusClient; diff --git a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXMainServer.java b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXMainServer.java index 99d9196..a4a5184 100644 --- a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXMainServer.java +++ b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXMainServer.java @@ -64,7 +64,7 @@ public class EXMainServer extends MainServer{ String robotskey = "g{"+Config.DEFAULT_GROUP_ID+"}:play:"+Config.DEFAULT_PID; Map maprobot = jedis2.hgetAll(robotskey); for(Map.Entry entry : maprobot.entrySet()) { - System.out.println(entry.getKey() + ":" + entry.getValue()); + log.info("{}:{}", entry.getKey(), entry.getValue()); //是否创建 RobotUser robotUser = new RobotUser(); robotUser.setRobotId(entry.getKey()); @@ -88,7 +88,7 @@ public class EXMainServer extends MainServer{ log.info("长沙麻将机器人服务器已启动"); log.info("服务器将监听端口 {} 用于接收robot_mgr管理协议", gameSetting.port); - System.out.println("当前线程池配置: " + ThreadPoolConfig.getThreadPoolStatus()); + log.info("当前线程池配置: {}", ThreadPoolConfig.getThreadPoolStatus()); jedis2.close(); } @@ -123,16 +123,16 @@ public class EXMainServer extends MainServer{ //每30秒执行一次资源清理 Thread.sleep(30000); ResourceCleanupUtil.performCleanup(); - System.out.println("线程池状态: " + ThreadPoolConfig.getThreadPoolStatus()); + log.info("线程池状态: {}", ThreadPoolConfig.getThreadPoolStatus()); } catch (InterruptedException e) { break; } catch (Exception e) { - System.err.println("资源清理任务异常: " + e.getMessage()); + log.error("资源清理任务异常: {}", e.getMessage(), e); // 发生异常时尝试清理 try { ResourceCleanupUtil.performCleanup(); } catch (Exception cleanupEx) { - System.err.println("异常清理也失败: " + cleanupEx.getMessage()); + log.error("异常清理也失败: {}", cleanupEx.getMessage(), cleanupEx); } } } @@ -140,7 +140,7 @@ public class EXMainServer extends MainServer{ cleanupThread.setDaemon(true); cleanupThread.start(); - System.out.println("资源清理定时任务已启动"); + log.info("资源清理定时任务已启动"); } diff --git a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXPlayer.java b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXPlayer.java index 009e6a6..23ab902 100644 --- a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXPlayer.java +++ b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/EXPlayer.java @@ -2,6 +2,8 @@ package robot.mj; import com.robot.data.Player; import com.robot.data.Room; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @@ -10,10 +12,11 @@ import com.robot.data.Room; public class EXPlayer extends Player { + private static final Logger log = LoggerFactory.getLogger(EXPlayer.class); public EXPlayer(int playerid, Room table, String session_id) { super(playerid, table, session_id); - System.out.println("new robot "); + log.info("new robot"); } public EXRoom getRoom() { diff --git a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/RobotConnectionManager.java b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/RobotConnectionManager.java index 5f02a64..7e56b17 100644 --- a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/RobotConnectionManager.java +++ b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/RobotConnectionManager.java @@ -33,7 +33,7 @@ import static robot.mj.EXGameController.robotRoomMapping; */ public class RobotConnectionManager { - private Logger log = Logger.getLogger(RobotConnectionManager.class); + private final Logger log = Logger.getLogger(RobotConnectionManager.class); private static final Map huNanChangShaInstances = new ConcurrentHashMap<>(); //记录活跃连接 用于资源清理判断 @@ -103,13 +103,13 @@ public class RobotConnectionManager { //从Redis恢复状态 boolean restored = newInstance.restoreFromRedis(connecId); if (restored) { - System.out.println("从Redis恢复HuNanChangSha实例: " + connecId); + log.info("从Redis恢复HuNanChangSha实例: {}", connecId); } else { - System.out.println("创建新的HuNanChangSha实例: " + connecId); + log.info("创建新的HuNanChangSha实例: {}", connecId); } huNanChangShaInstances.put(connecId, newInstance); - System.out.println("当前HuNanChangSha实例总数: " + huNanChangShaInstances.size()); + log.info("当前HuNanChangSha实例总数: {}", huNanChangShaInstances.size()); return newInstance; } @@ -146,7 +146,7 @@ public class RobotConnectionManager { * 断开与游戏服务器的连接(主动断开) */ public void disconnectFromGameServer(String connecId) { - System.out.println("开始主动断开连接: {"+connecId+"}"); + log.info("开始主动断开连接: {}", connecId); RobotUser robotUser = robotRoomMapping.remove(connecId); //标记连接为非活跃 @@ -167,7 +167,7 @@ public class RobotConnectionManager { instance.getchowGroup().clear(); instance.getchangShaCardInhandgang().clear(); instance.getChuGuoPainum().clear(); - System.out.println("清空HuNanChangSha集合数据: " + connecId); + log.info("清空HuNanChangSha集合数据: {}", connecId); } //移除实例和相关数据 @@ -178,7 +178,7 @@ public class RobotConnectionManager { playermingsMapByConn.remove(connecId); playerzisMapByConn.remove(connecId); - System.out.println("清理完成,当前活跃连接数: " + activeConnections.size() + ", 实例数: " + huNanChangShaInstances.size()); + log.info("清理完成,当前活跃连接数: {}, 实例数: {}", activeConnections.size(), huNanChangShaInstances.size()); } if (robotUser != null) { @@ -188,12 +188,12 @@ public class RobotConnectionManager { if (client.isConnected()) { client.killConnection(); } - System.out.println("客户端主动断开连接完成: {"+connecId+"}"); + log.info("客户端主动断开连接完成: {}", connecId); } catch (Exception e) { - System.out.println("断开客户端连接时发生异常: " + connecId + ", 错误: " + e.getMessage()); + log.error("断开客户端连接时发生异常: {}, 错误: {}", connecId, e.getMessage(), e); } } else { - System.out.println("客户端连接不存在: {"+connecId+"}"); + log.warn("客户端连接不存在: {}", connecId); } //同时清理机器人房间映射 @@ -217,12 +217,12 @@ public class RobotConnectionManager { //清理过期连接 for (String connecId : expiredConnections) { - System.out.println("清理过期连接实例: " + connecId); + log.info("清理过期连接实例: {}", connecId); disconnectFromGameServer(connecId); } if (!expiredConnections.isEmpty()) { - System.out.println("本次清理了 " + expiredConnections.size() + " 个过期连接实例"); + log.info("本次清理了 {} 个过期连接实例", expiredConnections.size()); } } @@ -240,7 +240,7 @@ public class RobotConnectionManager { ITObject param = message.param; //回调协议号 String command = message.command; - System.out.println("csmj OnEvent msg: " + command); + log.debug("csmj OnEvent msg: {}", command); //根据玩法ID处理不同的回调 if (StringUtil.isNotEmpty(command)) { @@ -287,10 +287,10 @@ public class RobotConnectionManager { robotUser.setSeat(seat); } } - System.out.println("playerData:" + playerData); + log.info("playerData: {}", playerData); - System.out.println("obj:" + obj); - System.out.println("reloadInfo:" + reloadInfo); + log.info("obj: {}", obj); + log.info("reloadInfo: {}", reloadInfo); if (reloadInfo != null) { //重连回来的 int curren_outcard_seat = reloadInfo.getInt("curren_outcard_seat"); @@ -316,7 +316,7 @@ public class RobotConnectionManager { } } - System.out.println("hcard>0" + hcard); + log.info("hcard>0{}", hcard); if (hcard.size() > 0) { //同步手牌 HuNanChangSha currentInstance = getHuNanChangShaInstance(connecId); @@ -326,9 +326,9 @@ public class RobotConnectionManager { if (currentHand.isEmpty() || hcard.size() > currentHand.size()) { //手牌集合为空 或者 玩家出牌了 currentInstance.updateHandCard(hcard); - System.out.println("断线重连:同步手牌数据,服务器手牌:" + hcard); + log.info("断线重连:同步手牌数据,服务器手牌:{}", hcard); } else { - System.out.println("断线重连:使用Redis恢复的手牌数据,数量:" + currentHand.size()); + log.info("断线重连:使用Redis恢复的手牌数据,数量:{}", currentHand.size()); } if (outcard_list.size() > 0) { @@ -341,9 +341,9 @@ public class RobotConnectionManager { List currentOutCards = currentInstance.getChuGuoCardInhand(); if (currentOutCards.isEmpty() || outcards.size() > currentOutCards.size()) { currentInstance.updateOutCard(outcards); - System.out.println("断线重连:同步出牌数据,服务器出牌:" + outcards); + log.info("断线重连:同步出牌数据,服务器出牌:{}", outcards); } else { - System.out.println("断线重连:使用Redis恢复的出牌数据,数量:" + currentOutCards.size()); + log.info("断线重连:使用Redis恢复的出牌数据,数量:{}", currentOutCards.size()); } } @@ -359,7 +359,7 @@ public class RobotConnectionManager { Map> currentPlayerzisMap = getPlayerzisMap(connecId); reconnectedInstance.outCard(client, currentPlayerOutcardsMap, currentPlayerchisMap, currentPlayerpengsMap, currentPlayermingsMap, currentPlayerzisMap); - System.out.println("断线重连后成功执行出牌操作"); + log.info("断线重连后成功执行出牌操作"); } catch (Exception e) { log.error("断线重连后执行出牌操作时发生异常", e); //即使出牌失败,也要确保连接状态正确 @@ -373,7 +373,7 @@ public class RobotConnectionManager { } }, 2, TimeUnit.SECONDS); } else { - System.err.println("警告:重连时未获取到手牌数据"); + log.warn("警告:重连时未获取到手牌数据"); } } } @@ -396,7 +396,7 @@ public class RobotConnectionManager { EXGameController.updateLastAccessTime(connecId); if (robotUser == null) { - System.err.println("未找到机器人用户信息,连接ID: " + connecId); + log.error("未找到机器人用户信息,连接ID: {}", connecId); return; } @@ -585,7 +585,7 @@ public class RobotConnectionManager { //更新机器人剩余数量 updateLeftoverRobot(robotId); disconnectFromGameServer(connecId); - System.out.println("2001发送退出房间协议1005,robotId: {"+robotId+"}"); + log.info("2001发送退出房间协议1005,robotId: {}", robotId); }); } } @@ -599,7 +599,7 @@ public class RobotConnectionManager { } } }, 6, TimeUnit.SECONDS); - System.out.println("玩家{"+ robotUser.getCurrentRoomId()+"}加入房间:"+ param); + log.info("玩家{}加入房间:{}", robotUser.getCurrentRoomId(), param); } //2026.02.03修改 玩家退出房间也要检查 else if ("2002".equalsIgnoreCase(command)) { @@ -626,7 +626,7 @@ public class RobotConnectionManager { //更新机器人剩余数量 updateLeftoverRobot(robotId); disconnectFromGameServer(connecId); - System.out.println("2002发送退出房间协议1005,robotId: {"+robotId+"}"); + log.info("2002发送退出房间协议1005,robotId: {}", robotId); }); } } @@ -646,7 +646,7 @@ public class RobotConnectionManager { //更新机器人剩余数量 updateLeftoverRobot(robotId); disconnectFromGameServer(connecId); - System.out.println("2005玩家发送解散房间协议,robotId: {"+robotId+"}"); + log.info("2005玩家发送解散房间协议,robotId: {}", robotId); } //2026.02.03修改 解散房间时候恢复机器人账号可以使用 else if ("2008".equalsIgnoreCase(command)) { @@ -692,18 +692,18 @@ public class RobotConnectionManager { disconnectFromGameServer(connecId); //更新机器人剩余数量 updateLeftoverRobot(paramRobotId); - System.out.println("2009发送退出房间协议1005,robotId: {"+paramRobotId+"}"); + log.info("2009发送退出房间协议1005,robotId: {}", paramRobotId); }); } } } } } catch (NumberFormatException e) { - System.err.println("2009协议数字格式异常,robotId: " + param.get("aid") + ", connecId: " + connecId); + log.error("2009协议数字格式异常,robotId: {}, connecId: {}", param.get("aid"), connecId); } catch (NullPointerException e) { - System.err.println("2009协议空指针异常,connecId: " + connecId); + log.error("2009协议空指针异常,connecId: {}", connecId); } catch (Exception e) { - System.err.println("2009协议处理异常: " + e.getMessage() + ", connecId: " + connecId); + log.error("2009协议处理异常: {}, connecId: {}", e.getMessage(), connecId, e); } finally { if (jedis != null) { jedis.close(); @@ -796,7 +796,7 @@ public class RobotConnectionManager { jedis2.hset("{grobot}:" + robotId, "start", "0"); - System.out.println("机器人 {"+robotId+"} 退出房间,修改gallrobot为0"); + log.info("机器人 {} 退出房间,修改gallrobot为0", robotId); } finally { jedis2.close(); } @@ -806,14 +806,14 @@ public class RobotConnectionManager { * 机器人登录 */ public void login(RobotUser robotUser){ - System.out.println("login:"+robotUser.getRobotId()); + log.info("login:{}", robotUser.getRobotId()); ITObject object = null; AccountBusiness accountBusiness = null; accountBusiness = new AccountBusiness(); try { //先快速登录 object = accountBusiness.fastLogin(Integer.parseInt(robotUser.getRobotId())); - System.out.println("object:"+object); + log.info("object:{}", object); if(object==null){ object = accountBusiness.idPasswordLogin(Integer.parseInt(robotUser.getRobotId()), robotUser.getPassword()); } @@ -835,7 +835,7 @@ public class RobotConnectionManager { connectGame(robotUser); robotUser.setConnecId(robotUser.getCurrentRoomId()+"_"+robotUser.getRobotId()); - System.err.println("重启获取的机器人还有当前房间,准备加入: "+robotUser.getConnecId()); + log.info("重启获取的机器人还有当前房间,准备加入: {}", robotUser.getConnecId()); exGameController.webGroupJoinRoom(robotUser); } } @@ -862,12 +862,12 @@ public class RobotConnectionManager { robotUser.setClient(client); EXGameController.robotRoomMapping.put(robotUser.getCurrentRoomId()+"_"+robotUser.getRobotId(), robotUser); }else{ - System.out.println("reconnect"); - System.out.println("client.isConnected()"+robotUser.getClient().isConnected()); + log.info("reconnect"); + log.info("client.isConnected(){}", robotUser.getClient().isConnected()); if(robotUser.getClient().isConnected()){ robotUser.setIsconnect(true); }else{ - System.out.println("reconnect"+robotUser.getClient().getGameID()); + log.info("reconnect{}", robotUser.getClient().getGameID()); TaurusClient client = new TaurusClient(robotUser.getGameHost()+":"+robotUser.getGamePort(), "cm"+robotUser.getRobotId(), TaurusClient.ConnectionProtocol.Tcp); client.setSession(robotUser.getLoginsession()); client.connect(); @@ -914,7 +914,6 @@ public class RobotConnectionManager { //添加延迟 Thread.sleep(time); } catch (InterruptedException e) { - System.out.println(e); } } diff --git a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/business/AccountBusiness.java b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/business/AccountBusiness.java index a647e84..dfae5ca 100644 --- a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/business/AccountBusiness.java +++ b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/business/AccountBusiness.java @@ -145,11 +145,11 @@ public class AccountBusiness extends Controller { String idPwdBan = Redis.use("group1_db0").get(id + "_login_ban"); if (StringUtil.isNotEmpty(idPwdBan)) { - System.out.println("进入了77777777777777777777"); + logger.info("进入了77777777777777777777"); logger.error("id:" + id + " ban login"); //throw new WebException(ErrorCode.BAN_LOGIN); } - System.out.println("进入了9999999999999"); + logger.info("进入了9999999999999"); ITArray resultArray = null; try { @@ -172,13 +172,13 @@ public class AccountBusiness extends Controller { Redis.use("group1_db0").set(id + "_login_ban", "1"); Redis.use("group1_db0").expire(id + "_login_ban", 1800); logger.error("pwd error count:" + count + " not login"); - System.out.println("进入了00000000000"); + logger.info("进入了00000000000"); //throw new WebException(ErrorCode._NO_SESSION); } } - System.out.println("进入了111111111111"); + logger.info("进入了111111111111"); //throw new WebException(ErrorCode._FAILED); } @@ -204,13 +204,13 @@ public class AccountBusiness extends Controller { Redis.use("group1_db0").hset(token, "create_time", "" + System.currentTimeMillis() / 1000); Redis.use("group1_db0").expire(token, 172800); - System.out.println("进入了2222222222222"); + logger.info("进入了2222222222222"); long tokenNum = Redis.use("group1_db0").scard(session + "_token"); if (tokenNum >= 10) { logger.warn("id:" + accountid + " repeat login, token count:" + tokenNum); } - System.out.println("进入了33333333333333333332"); + logger.info("进入了33333333333333333332"); resData.putString("token", token); return resData; diff --git a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/handler/HuNanChangSha.java b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/handler/HuNanChangSha.java index 4cfe759..ebeac2c 100644 --- a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/handler/HuNanChangSha.java +++ b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/handler/HuNanChangSha.java @@ -7,6 +7,8 @@ import com.taurus.core.entity.TArray; import com.taurus.core.entity.TObject; import com.taurus.core.plugin.redis.Redis; import com.taurus.core.util.StringUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import taurus.client.Message; import taurus.client.TaurusClient; @@ -20,7 +22,7 @@ import com.google.gson.reflect.TypeToken; import static robot.mj.thread.ThreadPoolConfig.getBusinessThreadPool; public class HuNanChangSha { - + private static final Logger log = LoggerFactory.getLogger(HuNanChangSha.class); public int changShaCard = 0; public static boolean isTinPeng = false; private final List changShaCardInhandgang = new ArrayList<>(); @@ -108,9 +110,9 @@ public class HuNanChangSha { //1小时过期时间 jedis.expire(redisKey, 3600); - System.out.println("保存HuNanChangSha状态到Redis: " + connecId); + log.info("保存HuNanChangSha状态到Redis: {}", connecId); } catch (Exception e) { - System.err.println("保存HuNanChangSha状态到Redis失败: " + e.getMessage()); + log.error("保存HuNanChangSha状态到Redis失败: {}", e.getMessage()); } finally { jedis.close(); } @@ -128,7 +130,7 @@ public class HuNanChangSha { Map stateMap = jedis.hgetAll(redisKey); if (stateMap == null || stateMap.isEmpty()) { - System.out.println("Redis中没有找到HuNanChangSha状态数据: " + connecId); + log.info("Redis中没有找到HuNanChangSha状态数据{}", connecId); return false; } @@ -189,11 +191,10 @@ public class HuNanChangSha { session = stateMap.getOrDefault("session", ""); token = stateMap.getOrDefault("token", ""); - - System.out.println("从Redis恢复HuNanChangSha状态成功: " + connecId + ", 手牌数量: " + changShaCardInhand.size()); + log.info("从Redis恢复HuNanChangSha状态成功: {}, 手牌数量: {}", connecId, changShaCardInhand.size()); return true; } catch (Exception e) { - System.err.println("从Redis恢复HuNanChangSha状态失败: " + e.getMessage()); + log.error("从Redis恢复HuNanChangSha状态失败: {}", e.getMessage()); return false; } finally { jedis.close(); @@ -209,9 +210,9 @@ public class HuNanChangSha { try { String redisKey = "{csmj}:" + connecId; jedis.del(redisKey); - System.out.println("从Redis删除HuNanChangSha状态: " + connecId); + log.info("从Redis删除HuNanChangSha状态: {}", connecId); } catch (Exception e) { - System.err.println("从Redis删除HuNanChangSha状态失败: " + e.getMessage()); + log.error("从Redis删除HuNanChangSha状态失败: {}", e.getMessage()); } finally { jedis.close(); } @@ -228,8 +229,7 @@ public class HuNanChangSha { chowGroup.clear(); changShaCardInhandgang.clear(); chuGuoPainum.clear(); - - System.out.println("已清空HuNanChangSha所有集合数据"); + log.info("已清空HuNanChangSha所有集合数据"); } /** @@ -270,8 +270,8 @@ public class HuNanChangSha { int drawnCard = param.getInt("card"); changShaSuanFaTest.drawnCards = drawnCard;//存储摸到的牌 changShaCardInhand.add(drawnCard); - System.out.println("param.getInt(player)" + param.getInt("player") ); - System.out.println("摸到的牌 +++++++++ " + drawnCard); + log.info("param.getInt(player): {}",param.getInt("player")); + log.info("摸到的牌 +++++++++ : {}",drawnCard); } jedis222.close(); @@ -304,7 +304,7 @@ public class HuNanChangSha { changShaCardInhand.add(cardList.getInt(i)); } - System.out.println("changShaCardInhand" + changShaCardInhand); + log.info("changShaCardInhand: {}", changShaCardInhand); } return null; @@ -315,11 +315,13 @@ public class HuNanChangSha { * @param handCard */ public void updateHandCard(List handCard) { - System.out.println("updateHandCard同步手牌:"+ handCard); + log.info("updateHandCard同步手牌:{}", handCard); + + log.info("changShaCardInhand: {}", changShaCardInhand); changShaCardInhand.clear(); - System.out.println("updateHandCard同步手牌changShaCardInhand:"+ changShaCardInhand); + log.info("updateHandCard同步手牌changShaCardInhand:{}", changShaCardInhand); changShaCardInhand.addAll(handCard); - System.out.println("updateHandCard同步手牌add:"+ changShaCardInhand); + log.info("updateHandCard同步手牌add:{}", changShaCardInhand); } public void updateOutCard(List outCard) { @@ -337,14 +339,14 @@ public class HuNanChangSha { getBusinessThreadPool().execute(() -> { try { int delaySeconds = 1 + new Random().nextInt(2); - System.out.println("执行" + actionName + "动作,延迟" + delaySeconds + "秒"); + log.info("执行{}动作,延迟{}秒", actionName, delaySeconds); Thread.sleep(delaySeconds * 1000); client.send("612", params, response -> { - System.out.println(actionName + "动作发送完成"); + log.info("{}动作发送完成", actionName); }); } catch (Exception e) { - System.err.println("执行" + actionName + "动作时发生异常: " + e.getMessage()); + log.error("执行{}动作时发生异常: {}", actionName, e.getMessage(), e); } }); } @@ -413,12 +415,12 @@ public class HuNanChangSha { List shifoutingpai = changShaSuanFaTest.handscardshifoutingpai(changShaCardInhand, chowGroup, pongGroup, gangdepai); boolean beforelisten = false;//记录操作之前的下听状态 - System.out.println("shifoutingpai" + shifoutingpai); + log.info("shifoutingpai{}", shifoutingpai); if (shifoutingpai.size() > 0) { beforelisten = true; } - System.out.println("beforelisten" + beforelisten); + log.info("beforelisten{}", beforelisten); //如果杠了之后还能继续听牌则杠 //优先处理胡牌 @@ -453,7 +455,7 @@ public class HuNanChangSha { if (type == 3) { Util.removeCard(gangusecars, card, 3); List shifoutingpai3 = changShaSuanFaTest.handscardshifoutingpai(gangusecars, chowGroup, pongGroup, gangdepai); - System.out.println(shifoutingpai3); + log.info("shifoutingpai3: {}", shifoutingpai3); if (shifoutingpai3.size() > 0) { //开杠 @@ -564,7 +566,7 @@ public class HuNanChangSha { int beforeIsDahu = 0; beforeIsDahu = changShaSuanFaTest.checkDahu(yupanhandcard, chowGroup, pongGroup, gangdepai); - System.out.println("bef:" + beforeIsDahu); + log.info("bef:{}", beforeIsDahu); //5、门清 if (yupanhandcard.size() == 13 && beforelisten) { beforeIsDahu = 5;//门清 @@ -619,10 +621,10 @@ public class HuNanChangSha { idObject.put(id, sj); } //计算分数 - System.out.println(pingfenResult); + log.info("pingfenResult: {}", pingfenResult); if (pingfenResult.size() > 0) { int changeid = changShaSuanFaTest.suanfen(pingfenResult); - System.out.println("changeid:" + changeid); + log.info("changeid:{}", changeid); //选择最优的分数 if (changeid == 0) { params.putString("session", session + "," + token); @@ -636,7 +638,7 @@ public class HuNanChangSha { for (Map.Entry entry : idObject.entrySet()) { if (entry.getKey() == changeid) { tmp = entry.getValue(); - System.out.println("tmp ++++++++++= " + tmp); + log.debug("tmp ++++++++++= {}", tmp); if (tmp.getInt("type") == 2) { //碰 ITArray outcards = tmp.getTArray("opcard"); @@ -653,8 +655,8 @@ public class HuNanChangSha { for (int i = 0; i < outcards.size(); i++) { Util.removeCard(changShaCardInhand, outcards.getInt(i), 1); } - System.out.println("判断吃 +++++++++" + card); - System.out.println("判断吃 ==========" + outcards); + log.debug("判断吃 +++++++++{}", card); + log.debug("判断吃 ========== {}", outcards); chowGroup.add(card); chowGroup.add(outcards.getInt(0)); chowGroup.add(outcards.getInt(1)); @@ -686,7 +688,7 @@ public class HuNanChangSha { List tmpChangSch = new ArrayList<>(); tmpChangSch.addAll(yupanhandcard); ChangshaWinSplitCard.checkNormalHu(tmpChangSch, map); - System.out.println("要不要补map:" + map); + log.info("要不要补map:{}", map); Map map2 = new HashMap<>(); List tmpChangSch2 = new ArrayList<>(); @@ -700,12 +702,12 @@ public class HuNanChangSha { } ChangshaWinSplitCard.checkNormalHu(tmpChangSch2, map2); - System.out.println("要不要补map2:" + map2); - System.out.println(yupanhandcard); + log.info("要不要补map2:{}", map2); + log.info("yupanhandcard: {}", yupanhandcard); //假设补牌 //如果下听不能不能补牌 if (beforelisten) { - System.out.println("b补"); + log.info("b补"); params.putString("session", session + "," + token); params.putInt("qi", 0); params.putInt("id", 0); @@ -717,7 +719,7 @@ public class HuNanChangSha { Map> afterOpbg = changShaSuanFaTest.quyizhangTingPai(yupanhandcard); if (type == 4) { if (afterDahuOpbg.size() > 0 || afterOpbg.size() > 0) { - System.out.println("bs补"); + log.info("bs补"); params.putString("session", session + "," + token); params.putInt("qi", 0); params.putInt("id", 0); @@ -731,7 +733,7 @@ public class HuNanChangSha { if (map2.size() > 0) { //判断补牌后 if (Integer.parseInt(map2.get("remainingMelds").toString()) > Integer.parseInt(map.get("remainingMelds").toString()) && Integer.parseInt(map.get("remainingMelds").toString()) <= 2) { - System.out.println("b补"); + log.info("b补"); params.putString("session", session + "," + token); params.putInt("qi", 0); params.putInt("id", 0); @@ -742,7 +744,7 @@ public class HuNanChangSha { //判断七对 int dh = changShaSuanFaTest.checkDahu(yupanhandcard, chowGroup, pongGroup, gangdepai); if (dh > 0) { - System.out.println("不补"); + log.info("不补"); params.putString("session", session + "," + token); params.putInt("qi", 0); params.putInt("id", 0); @@ -750,7 +752,7 @@ public class HuNanChangSha { return "不开杠"; } - System.out.println("补"); + log.info("补"); params.putString("session", session + "," + token); params.putInt("qi", 0); @@ -812,7 +814,7 @@ public class HuNanChangSha { } } } - System.out.println("actchangShaCardInhand"+changShaCardInhand); + log.info("actchangShaCardInhand{}", changShaCardInhand); params.putString("session", session + "," + token); params.putInt("qi", 0); params.putInt("id", 0); @@ -865,10 +867,10 @@ public class HuNanChangSha { resultList.addAll(zigang); } - System.out.println("长沙麻将出牌" + changShaCardInhand); + log.info("长沙麻将出牌{}", changShaCardInhand); // 长沙麻将出牌 String changShaOutCard = changShaSuanFaTest.outCardSuanFa(changShaCardInhand, pongGroup, chowGroup, gangdepai, resultList); - System.out.println("长沙麻将出牌" + changShaCardInhand); + log.info("长沙麻将出牌{}", changShaCardInhand); ITObject params = TObject.newInstance(); int cardToOut; @@ -891,10 +893,10 @@ public class HuNanChangSha { // 将当前出的牌添加到历史出牌列表 changShachuguopai.add(cardToOut); - System.out.println("changShachuguopai ++++ :" + changShachuguopai); + log.info("changShachuguopai ++++ :{}", changShachuguopai); // 从手牌中移除 changShaCardInhand.remove(Integer.valueOf(cardToOut)); - System.out.println("打过后的手牌 +++ " + changShaCardInhand); + log.info("打过后的手牌 +++ {}", changShaCardInhand); params.putString("session", session + "," + token); //使用线程池替代CompletableFuture.runAsync + Thread.sleep @@ -906,7 +908,7 @@ public class HuNanChangSha { }); } catch (Exception e) { - System.out.println("Thread error"); + log.error("线程执行错误", e); } }); @@ -925,7 +927,7 @@ public class HuNanChangSha { * @return */ public String shanchuchuguopai(ITObject param) { - System.out.println("对面吃碰删除出过的牌组"); + log.info("对面吃碰删除出过的牌组"); if (param == null) { return null; } @@ -953,10 +955,10 @@ public class HuNanChangSha { } if (!robotIdsList.contains(playerid)) { - System.out.println("playerid "+playerid); - System.out.println("type "+type); + log.info("playerid {}", playerid); + log.info("type {}", type); if (type == 2 || type == 3 || type == 5 || type == 1) { // 碰,杠 - System.out.println("进入对面吃牌 机器人出的牌需要删除这张牌"); + log.info("进入对面吃牌 机器人出的牌需要删除这张牌"); getChuGuoCardInhand().remove(Integer.valueOf(card)); } } diff --git a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/thread/ResourceCleanupUtil.java b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/thread/ResourceCleanupUtil.java index a0d0f30..9a73cd6 100644 --- a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/thread/ResourceCleanupUtil.java +++ b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/thread/ResourceCleanupUtil.java @@ -1,5 +1,7 @@ package robot.mj.thread; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import robot.mj.EXGameController; import robot.mj.RobotConnectionManager; @@ -11,6 +13,7 @@ import java.util.concurrent.ConcurrentHashMap; * 用于管理和清理不再使用的资源 防止内存泄漏 */ public class ResourceCleanupUtil { + private static final Logger log = LoggerFactory.getLogger(ResourceCleanupUtil.class); //需要清理的资源 private static final Set pendingCleanupResources = ConcurrentHashMap.newKeySet(); @@ -26,7 +29,7 @@ public class ResourceCleanupUtil { return; } - System.out.println("开始执行资源清理,待清理资源数: " + pendingCleanupResources.size()); + log.info("开始执行资源清理,待清理资源数: {}", pendingCleanupResources.size()); int cleanedCount = 0; Set resourcesToClean = ConcurrentHashMap.newKeySet(); @@ -41,13 +44,13 @@ public class ResourceCleanupUtil { pendingCleanupResources.remove(resourceId); cleanedCount++; - System.out.println("已清理资源: " + resourceId); + log.info("已清理资源: {}", resourceId); } catch (Exception e) { - System.err.println("清理资源时发生异常: " + resourceId + ", 错误: " + e.getMessage()); + log.error("清理资源时发生异常: {}, 错误: {}", resourceId, e.getMessage(), e); } } - System.out.println("资源清理完成,共清理: " + cleanedCount + " 个资源"); + log.info("资源清理完成,共清理: {} 个资源", cleanedCount); //执行常规清理 performRegularCleanup(); @@ -62,11 +65,11 @@ public class ResourceCleanupUtil { EXGameController.cleanupExpiredConnections(); //输出当前系统状态 - System.out.println("=== 系统资源状态 ==="); - System.out.println(ThreadPoolConfig.getThreadPoolStatus()); + log.info("=== 系统资源状态 ==="); + log.info("{}", ThreadPoolConfig.getThreadPoolStatus()); } catch (Exception e) { - System.err.println("执行常规清理时发生异常: " + e.getMessage()); + log.error("执行常规清理时发生异常: {}", e.getMessage(), e); } } diff --git a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/thread/ThreadPoolConfig.java b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/thread/ThreadPoolConfig.java index af7bd35..b9cc8e1 100644 --- a/robots/majiang/robot_mj_cs/src/main/java/robot/mj/thread/ThreadPoolConfig.java +++ b/robots/majiang/robot_mj_cs/src/main/java/robot/mj/thread/ThreadPoolConfig.java @@ -1,5 +1,8 @@ package robot.mj.thread; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; @@ -8,6 +11,7 @@ import java.util.concurrent.atomic.AtomicInteger; */ public class ThreadPoolConfig { + private static final Logger log = LoggerFactory.getLogger(ThreadPoolConfig.class); //线程池配置 private static final ExecutorService BUSINESS_THREAD_POOL = new ThreadPoolExecutor( @@ -54,14 +58,14 @@ public class ThreadPoolConfig { * 执行延迟任务,替代Thread.sleep */ public static void scheduleDelay(Runnable task, long delay, TimeUnit unit) { - System.out.println("提交延迟任务: 延迟" + delay + " " + unit + ", 当前时间: " + System.currentTimeMillis()); + log.debug("提交延迟任务: 延迟{} {}, 当前时间: {}", delay, unit, System.currentTimeMillis()); SCHEDULED_EXECUTOR_SERVICE.schedule(() -> { try { - System.out.println("执行延迟任务开始: 当前时间: " + System.currentTimeMillis()); + log.debug("执行延迟任务开始: 当前时间: {}", System.currentTimeMillis()); task.run(); - System.out.println("执行延迟任务完成: 当前时间: " + System.currentTimeMillis()); + log.debug("执行延迟任务完成: 当前时间: {}", System.currentTimeMillis()); } catch (Exception e) { - System.err.println("延迟任务执行异常: " + e.getMessage()); + log.error("延迟任务执行异常: {}", e.getMessage(), e); } }, delay, unit); } @@ -70,13 +74,13 @@ public class ThreadPoolConfig { * 关闭线程池 释放资源 */ public static void shutdown() { - System.out.println("开始关闭线程池..."); + log.info("开始关闭线程池..."); //关闭定时任务线程池 SCHEDULED_EXECUTOR_SERVICE.shutdown(); try { if (!SCHEDULED_EXECUTOR_SERVICE.awaitTermination(3, TimeUnit.SECONDS)) { - System.out.println("定时任务线程池强制关闭"); + log.info("定时任务线程池强制关闭"); SCHEDULED_EXECUTOR_SERVICE.shutdownNow(); } } catch (InterruptedException e) { @@ -88,7 +92,7 @@ public class ThreadPoolConfig { BUSINESS_THREAD_POOL.shutdown(); try { if (!BUSINESS_THREAD_POOL.awaitTermination(5, TimeUnit.SECONDS)) { - System.out.println("业务线程池强制关闭"); + log.info("业务线程池强制关闭"); BUSINESS_THREAD_POOL.shutdownNow(); } } catch (InterruptedException e) { @@ -96,7 +100,7 @@ public class ThreadPoolConfig { Thread.currentThread().interrupt(); } - System.out.println("线程池关闭完成"); + log.info("线程池关闭完成"); } /** diff --git a/robots/majiang/robot_mj_cs/src/main/java/taurus/util/ChangShaSuanFaTest.java b/robots/majiang/robot_mj_cs/src/main/java/taurus/util/ChangShaSuanFaTest.java index 5e0ab90..548dc6a 100644 --- a/robots/majiang/robot_mj_cs/src/main/java/taurus/util/ChangShaSuanFaTest.java +++ b/robots/majiang/robot_mj_cs/src/main/java/taurus/util/ChangShaSuanFaTest.java @@ -39,7 +39,7 @@ public class ChangShaSuanFaTest { */ private void logInfo(String message) { // 注释掉日志输出,减少测试时的信息干扰 -// System.out.println(message); +// log.info(message); } private final static Logger log; @@ -179,21 +179,21 @@ public class ChangShaSuanFaTest { // boolean hasBigSuit = isAllSameSuit(handCards, pengCard); // 分析是否有可能的清一色花色 int hasBigSuit = checkDahu(handCards,chowGroup,pengCard,gangdepai); - System.out.println("resultList22 +++++++++++++++++++++++================" + hasBigSuit); + log.info("resultList22 +++++++++++++++++++++++================ {}", hasBigSuit); //特殊牌型处理 String tscard = teshuXuanPai(cardInhand, pengCard, chowGroup, gangdepai); if (tscard!=null){ - System.out.printf("特殊牌型处理111 +++++++++++++++++++++++++++++++++++"); + log.info("特殊牌型处理111 +++++++++++++++++++++++++++++++++++"); return tscard; } //循环去一张是否能大胡听牌 Map> afterDahuOp = quyizhangDahuTingPai(cardInhand,chowGroup,pengCard,gangdepai); - System.out.println("afterDahuOp:"+afterDahuOp); + log.info("afterDahuOp:{}", afterDahuOp); if (afterDahuOp.size()>0){ //执行 @@ -204,8 +204,8 @@ public class ChangShaSuanFaTest { tmpres.addAll(cardInhand); for (Map.Entry> entry : afterDahuOp.entrySet()) { - System.out.println(entry.getKey()); - System.out.println("num:"+getTingPainum(entry.getValue(),tmpres)); + log.debug("{}", entry.getKey()); + log.debug("num:{}", getTingPainum(entry.getValue(),tmpres)); if (caozuonum1==0){ caozuonum1 = getTingPainum(entry.getValue(),tmpres); xuanzecard1 = entry.getKey(); @@ -225,7 +225,7 @@ public class ChangShaSuanFaTest { //循环去一张还能听多牌 Map> afterOp = quyizhangTingPai(cardInhand); - System.out.println("afterOp:"+afterOp); + log.debug("afterOp:{}", afterOp); if (afterOp.size()>0){ //执行 @@ -255,7 +255,7 @@ public class ChangShaSuanFaTest { //六对 if (pisCardsCount >= 6 && pengCard.size() == 0 && chowGroup.size() == 0) { - System.out.println("七小对数量大于等于6+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); + log.info("七小对数量大于等于6+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); //单张牌 List danzhang = danzhang(handCards); isTin = true; @@ -299,21 +299,21 @@ public class ChangShaSuanFaTest { List zeroCountCards = threeCardsFromMax.stream() .filter(card3 -> danzhangCountMap.getOrDefault(card3, 0) == 0) .collect(Collectors.toList()); - System.out.println("出现次数为0的牌: " + zeroCountCards); + log.debug("出现次数为0的牌: {}", zeroCountCards); // 从maxCards中移除 if (!zeroCountCards.isEmpty()) { maxCards.removeAll(zeroCountCards); - System.out.println("移除后maxCards: " + maxCards); + log.debug("移除后maxCards: {}", maxCards); } } - System.out.println("maxCards中出现3次的牌: " + threeCardsFromMax); + log.debug("maxCards中出现3次的牌: {}", threeCardsFromMax); if (threeCardsFromMax.size() > 0) { return String.valueOf(threeCardsFromMax.get(0)); } Integer maxCard = maxCards.isEmpty() ? null : maxCards.get(0); - System.out.println("maxCard++++++++++++++++++++++++++++++" + maxCard); + log.debug("maxCard++++++++++++++++++++++++++++++{}", maxCard); System.out.printf("特殊牌型处理333 +++++++++++++++++++++++++++++++++++"); return String.valueOf(maxCard); @@ -348,7 +348,7 @@ public class ChangShaSuanFaTest { return String.valueOf(outcard); } - System.out.println("hasBigSuit:"+hasBigSuit); + log.debug("hasBigSuit:{}", hasBigSuit); if (hasBigSuit>0) { logInfo("执行清一色策略,尝试优化花色分布"); // 调用清一色特定出牌策略 @@ -435,7 +435,7 @@ public class ChangShaSuanFaTest { //调平胡递归 List integers = ChangshaWinSplitCard.analyzeBestDiscard(pinghuhandCards); - System.out.println("integers:"+integers); + log.debug("integers:{}", integers); if(integers.size()>0) { int integer = 0; @@ -456,7 +456,7 @@ public class ChangShaSuanFaTest { if (integers.size() > 0 && cardnum < 4) { - System.out.println("平胡最新出牌策略=============================================== 666 " + integer); + log.debug("平胡最新出牌策略=============================================== 666 {}", integer); List guzhangc = new ArrayList<>(); @@ -494,7 +494,7 @@ public class ChangShaSuanFaTest { } } - System.out.println("chaiduizi:" + chaiduizi); + log.debug("chaiduizi:{}", chaiduizi); List kydongduizi = new ArrayList<>(); List tmps = new ArrayList<>(); @@ -512,7 +512,7 @@ public class ChangShaSuanFaTest { } chaiduizi.removeAll(kydongduizi); - System.out.println("kydongduizi:" + chaiduizi); + log.debug("kydongduizi:{}", chaiduizi); if(chaiduizi.size()>0) { return String.valueOf(chaiduizi.get(0)); @@ -544,7 +544,7 @@ public class ChangShaSuanFaTest { } } - System.out.println("jiangcards:" + jiangcards); + log.debug("jiangcards:{}", jiangcards); if (jiangcards > 0&&chaiduizi.size()<2) { // //去掉将判断孤章 @@ -619,7 +619,7 @@ public class ChangShaSuanFaTest { //如果已经是听牌状态的情况 if (analysis.isTingPai) { - System.out.println("已经是听牌状态----" + drawnCards); + log.info("已经是听牌状态----{}", drawnCards); return String.valueOf(drawnCards); } @@ -627,7 +627,7 @@ public class ChangShaSuanFaTest { if (isJiangPai(outcard)&&integers.size()>0){ return String.valueOf(integers.get(0)); } - System.out.println("最终出的牌------" + outcard); + log.info("最终出的牌------{}", outcard); return String.valueOf(outcard); @@ -657,8 +657,8 @@ public class ChangShaSuanFaTest { } } - System.out.println("258将牌: " + is258Cards); - System.out.println("非258牌: " + non258Cards); + log.debug("258将牌: {}", is258Cards); + log.debug("非258牌: {}", non258Cards); @@ -763,7 +763,7 @@ public class ChangShaSuanFaTest { cardCountMap.put(card, cardCountMap.getOrDefault(card, 0) + 1); } - System.out.println("统计结果: " + cardCountMap); + log.debug("统计结果: {}", cardCountMap); List singleCards = new ArrayList<>(); @@ -951,7 +951,7 @@ public class ChangShaSuanFaTest { List handCards2 = new ArrayList<>(); handCards2.addAll(handCards); // handCards2.addAll(pengCard); - System.out.println("碰碰胡 handCards2 ++++++++++++++++" + handCards2); + log.debug("碰碰胡 handCards2 ++++++++++++++++{}", handCards2); // 按花色分组 Map> suitGroupMap = new HashMap<>(); @@ -2745,7 +2745,7 @@ public class ChangShaSuanFaTest { for (Integer item : cardInHand) { countMap.put(item, countMap.getOrDefault(item, 0) + 1); } - System.out.println("countMap: " + countMap); + log.debug("countMap: {}", countMap); int jiangnum = 0; for (int key : countMap.keySet()) { if (isJiangPai(key) && countMap.get(key) == 2) { @@ -2868,7 +2868,7 @@ public class ChangShaSuanFaTest { int suit = entry.getKey(); int count = entry.getValue(); - System.out.println("count:"+count); + log.debug("count:{}", count); if (handcards.size()==13||handcards.size()==14){ if (count >= 10) { @@ -2926,7 +2926,7 @@ public class ChangShaSuanFaTest { * @return */ public static int suanfen(Mapmap){ - System.out.println("suanfen"+map); + log.debug("suanfen{}", map); if (map.size()==0){ return 0; } @@ -2965,7 +2965,7 @@ public class ChangShaSuanFaTest { if (entry.getValue().getInt("guzhang")>0){ tmp= tmp+entry.getValue().getInt("guzhang")*1; } - System.out.println("tmp:"+tmp); + log.debug("tmp:{}", tmp); fell.put(entry.getKey(), tmp); } @@ -3028,7 +3028,7 @@ public class ChangShaSuanFaTest { chiob.putInt("isDaHu",beforeIsDahu); List checkCards = new ArrayList(); checkCards.addAll(yupanhandcard); - System.out.println("checkCards"+checkCards); + log.debug("checkCards{}", checkCards); //去掉三张牌是否还能听牌 List npeng = new ArrayList<>(); @@ -3228,7 +3228,7 @@ public class ChangShaSuanFaTest { } //检测是否有4对 int duizi = countPairs(yupanhandcard); - System.out.println("duizi:"+duizi); + log.debug("duizi:{}", duizi); System.out.println(pengGrop); if (duizi==4&&pengGrop.size()>=0){ diff --git a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXGameController.java b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXGameController.java index f017a0d..353fb8b 100644 --- a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXGameController.java +++ b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXGameController.java @@ -131,8 +131,8 @@ public class EXGameController extends GameController { try { String connecId = params.getString("connecId"); TaurusClient client = getCsMjGameServerConnection(connecId); - System.out.println("接收来自robot_mgr的加入房间协议 connecId: = "+connecId); - System.out.println("接收来自robot_mgr的加入房间协议 client: = "+client); + log.info("接收来自robot_mgr的加入房间协议 connecId: {}", connecId); + log.info("接收来自robot_mgr的加入房间协议 client: {}", client); if (client == null) { ITObject errorResponse = TObject.newInstance(); errorResponse.putString("status", "failed"); @@ -170,7 +170,7 @@ public class EXGameController extends GameController { params.del("connecId"); //发送加入房间请求到game_mj_cs client.send(Config.JOIN_ROOM_CS, params, response -> { - System.out.println("joinRoomController: " + response); + log.info("joinRoomController: {}", response); RobotUser roomInfo = new RobotUser(); roomInfo.setCurrentRoomId(Integer.parseInt(roomId)); roomInfo.setConnecId(connecId); @@ -200,8 +200,8 @@ public class EXGameController extends GameController { try { String connecId = params.getString("connecId"); TaurusClient client = getCsMjGameServerConnection(connecId); - System.out.println("接收来自robot_mgr的机器人准备协议 connecId: = "+connecId); - System.out.println("接收来自robot_mgr的机器人准备协议 client: = "+client); + log.info("接收来自robot_mgr的加入房间协议 connecId: {}", connecId); + log.info("接收来自robot_mgr的加入房间协议 client: {}", client); if (client == null) { ITObject errorResponse = TObject.newInstance(); errorResponse.putString("status", "failed"); @@ -214,7 +214,7 @@ public class EXGameController extends GameController { Thread.sleep(1000); //发送准备请求到game_mj_cs client.send(Config.GAME_READY_CS, params, response -> { - System.out.println("robotReadyRoom: " + response); + log.info("robotReadyRoom: {}", response); }); ITObject paramsReq = TObject.newInstance(); @@ -246,7 +246,7 @@ public class EXGameController extends GameController { if (!robotUsers.isEmpty()) { synchronized (robotUsers) { RobotUser robotUser = robotUsers.get(0); - System.err.println("房间{"+ roomId +"}中Redis未找到机器人{"+robotId+"},但本地映射存在{"+robotUser.getRobotId()+"},清理本地映射"); + log.error("房间{}中Redis未找到机器人{},但本地映射存在{}", roomId, robotId, robotId); robotRoomMapping.remove(robotUser.getConnecId()); robotRoomMapping.remove(robotUser.getRobotId()); } @@ -261,16 +261,16 @@ public class EXGameController extends GameController { if (robotId != existingRobotId) { //不同机器人的冲突 - System.err.println("房间{"+ roomId +"}中Redis已存在机器人{"+existingRobotId+"},当前机器人{"+robotId+"}不执行加入逻辑"); + log.error("房间{}中Redis已存在机器人{},当前机器人{}不执行加入逻辑", roomId, existingRobotId, robotId); return; } } } } - System.err.println("225开始进房间: "+"room:"+ roomId +"robot:"+robotId); + log.info("225开始进房间: room:{} robot:{}", roomId, robotId); //加入房间 joinRoomCommon(robotId, roomId, groupId, params); - System.err.println("225已进入房间准备成功: "+"room:"+ roomId +"robot:"+robotId); + log.info("225已进入房间准备成功: room:{} robot:{}", roomId, robotId); } /** @@ -280,10 +280,10 @@ public class EXGameController extends GameController { public void webGroupActive(Session session, ITObject params, int gid) { int robotId = params.getInt("robotid"); String roomId = params.getString("roomid"); - System.err.println("226开始进房间: " + "room:" + roomId + "robot:" + robotId); + log.info("226开始进房间 room: {} robot: {}", roomId, robotId); //加入房间 joinRoomCommon(params.getInt("robotid"), params.getString("roomid"), params.getInt("groupid"), params); - System.err.println("226已进入房间准备成功: " + "room:" + roomId + "robot:" + robotId); + log.info("226已进入房间准备成功: room:{} robot:{}", roomId, robotId); } /** @@ -310,15 +310,14 @@ public class EXGameController extends GameController { return; } - System.err.println("重启后开始进房间: " + "room:" + robotUser.getCurrentRoomId() + "robot:" + robotUser.getRobotId()); + log.info("重启后开始进房间: room:{}robot:{}", robotUser.getCurrentRoomId(), robotUser.getRobotId()); ITObject params = new TObject(); params.putString("session", "{user}:" + robotUser.getRobotId() + "," + robotSession); //加入房间 joinRoomCommon(Integer.parseInt(robotUser.getRobotId()), String.valueOf(robotUser.getCurrentRoomId()), Integer.parseInt(robotUser.getRobotGroupid()), params); - System.err.println("重启后已进入房间准备成功: " + "room:" + robotUser.getCurrentRoomId() + "robot:" + robotUser.getRobotId()); - + log.info("重启后已进入房间准备成功: room:{} robot:{}", robotUser.getCurrentRoomId(), robotUser.getRobotId()); } catch (Exception e) { - // throw new RuntimeException(e); + log.error("重启后进房间时发生错误", e); } finally { jedis0.close(); jedis2.close(); @@ -342,8 +341,8 @@ public class EXGameController extends GameController { } } - System.err.println("开始进房间: room:" + roomId); - System.err.println("开始进房间: {user}:" + robotId); + log.info("开始进房间: room:{}robot:{}", roomId, robotId); + log.info("开始进房间: {user}:{}", robotId); TaurusClient client = getCsMjGameServerConnection(roomId + "_" + robotId); GroupRoomBusiness.joinRoom(groupId, "room:" + roomId, "{user}:" + robotId, null); @@ -371,11 +370,11 @@ public class EXGameController extends GameController { robotRoomMapping.put(robotUser.getConnecId(), robotUser); robotConnectionManager.reconnectToGameServer(response, robotUser, client); }); - System.err.println("已进入房间成功: " + robotUser.getConnecId()); + log.info("已进入房间成功: {}", connecId); Thread.sleep(1000); if (client.isConnected()) { client.send(Config.GAME_READY_CS, params, response -> { - System.out.println("1003:" + response); + log.info("1003: {}", response); }); jedis2.hset("gallrobot", String.valueOf(robotUser.getRobotId()), "1"); @@ -389,13 +388,13 @@ public class EXGameController extends GameController { scheduleDelay(() -> { //15秒后还没有建立映射关系 加入可能失败 if (robotRoomMapping.get(robotUser.getConnecId()) == null) { - System.err.println("机器人{"+robotId+"}加入房间{"+roomId+"}超时,清理临时状态"); + log.info("机器人{{}}加入房间{{}}超时,清理临时状态", robotId, roomId); robotConnectionManager.disconnectFromGameServer(connecId); } }, 15, TimeUnit.SECONDS); //15秒后还没有建立映射关系 加入可能失败 if (robotRoomMapping.get(robotUser.getConnecId()) == null) { - System.err.println("机器人{"+robotId+"}加入房间{"+roomId+"}超时,清理临时状态"); + log.info("机器人{}加入房间{}超时,清理临时状态", robotId, roomId); robotConnectionManager.disconnectFromGameServer(connecId); } } catch (Exception e) { @@ -403,9 +402,9 @@ public class EXGameController extends GameController { } }, ThreadPoolConfig.getBusinessThreadPool());//指定自定义线程池 robotUser.setIntoRoomTime(robotConnectionManager.getTime()); - System.err.println("已进入房间准备成功: " + robotUser.getConnecId()); + log.info("已进入房间成功: {}", robotUser.getConnecId()); } catch (Exception e) { - // throw new RuntimeException(e); + log.error("机器人加入房间失败", e); } finally { jedis0.close(); jedis2.close(); @@ -452,13 +451,13 @@ public class EXGameController extends GameController { RobotUser removedUser = robotRoomMapping.remove(robotId); lastAccessTime.remove(robotId); - // 如果有连接ID,也一并清理 + //如果有连接ID 也一并清理 if (removedUser != null && removedUser.getConnecId() != null) { robotRoomMapping.remove(removedUser.getConnecId()); lastAccessTime.remove(removedUser.getConnecId()); } - System.out.println("清理机器人房间信息: " + robotId); + log.info("已删除机器人房间信息: {}", robotId); } /** @@ -484,7 +483,7 @@ public class EXGameController extends GameController { for (String connecId : expiredConnections) { RobotUser robotUser = robotRoomMapping.get(connecId); if (robotUser != null) { - System.out.println("清理超时连接: " + connecId + ", 机器人ID: " + robotUser.getRobotId()); + log.info("清理超时连接{}已超时,, 机器人ID: {}", connecId, robotUser.getRobotId()); robotConnectionManager.disconnectFromGameServer(connecId); } robotRoomMapping.remove(connecId); @@ -492,7 +491,7 @@ public class EXGameController extends GameController { } if (!expiredConnections.isEmpty()) { - System.out.println("本次清理了 " + expiredConnections.size() + " 个超时连接"); + log.info("本次清理了 {} 个超时连接", expiredConnections.size()); } } @@ -502,9 +501,9 @@ public class EXGameController extends GameController { */ public static TaurusClient getCsMjGameServerConnection(String connecId) { TaurusClient taurusClient = robotConnectionManager.getGameClient(connecId); - System.out.println("根据机器人ID和连接ID获取红中麻将游戏服务器连接 client: = "+taurusClient); + log.info("根据机器人ID和连接ID获取红中麻将游戏服务器连接 connecId: {}", connecId); if (taurusClient != null) { - log.debug("成功获取游戏服务器连接,connecId: {}", connecId); + log.info("成功获取游戏服务器连接,connecId: {}", connecId); return taurusClient; } taurusClient = robotConnectionManager.connectToGameServer(connecId); diff --git a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXMainServer.java b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXMainServer.java index 4e71b83..2a9f209 100644 --- a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXMainServer.java +++ b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXMainServer.java @@ -64,7 +64,7 @@ public class EXMainServer extends MainServer{ String robotskey = "g{"+Config.DEFAULT_GROUP_ID+"}:play:"+Config.DEFAULT_PID; Map maprobot = jedis2.hgetAll(robotskey); for(Map.Entry entry : maprobot.entrySet()) { - System.out.println(entry.getKey() + ":" + entry.getValue()); + log.info("机器人配置 - {}: {}", entry.getKey(), entry.getValue()); //是否创建 RobotUser robotUser = new RobotUser(); robotUser.setRobotId(entry.getKey()); @@ -89,7 +89,7 @@ public class EXMainServer extends MainServer{ //5、干活 log.info("红中麻将机器人服务器已启动"); log.info("服务器将监听端口 {} 用于接收robot_mgr管理协议", gameSetting.port); - System.out.println("当前线程池配置: " + ThreadPoolConfig.getThreadPoolStatus()); + log.info("当前线程池配置: {}", ThreadPoolConfig.getThreadPoolStatus()); jedis2.close(); } @@ -124,16 +124,16 @@ public class EXMainServer extends MainServer{ //每30秒执行一次资源清理 Thread.sleep(30000); ResourceCleanupUtil.performCleanup(); - System.out.println("线程池状态: " + ThreadPoolConfig.getThreadPoolStatus()); + log.info("线程池状态: {}", ThreadPoolConfig.getThreadPoolStatus()); } catch (InterruptedException e) { break; } catch (Exception e) { - System.err.println("资源清理任务异常: " + e.getMessage()); + log.error("资源清理任务异常: {}", e.getMessage()); // 发生异常时尝试清理 try { ResourceCleanupUtil.performCleanup(); } catch (Exception cleanupEx) { - System.err.println("异常清理也失败: " + cleanupEx.getMessage()); + log.error("异常清理也失败: {}", cleanupEx.getMessage()); } } } @@ -141,7 +141,7 @@ public class EXMainServer extends MainServer{ cleanupThread.setDaemon(true); cleanupThread.start(); - System.out.println("资源清理定时任务已启动"); + log.info("资源清理定时任务已启动"); } @Override diff --git a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXPlayer.java b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXPlayer.java index 024692c..e74ee05 100644 --- a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXPlayer.java +++ b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/EXPlayer.java @@ -2,16 +2,19 @@ package robot.mj; import com.robot.data.Player; import com.robot.data.Room; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * * */ public class EXPlayer extends Player { + private static final Logger log = LoggerFactory.getLogger(EXPlayer.class); public EXPlayer(int playerid, Room table, String session_id) { super(playerid, table, session_id); - System.out.println("new robot "); + log.info("创建新的机器人玩家"); } public EXRoom getRoom() { diff --git a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/RobotConnectionManager.java b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/RobotConnectionManager.java index f81d9c2..b6f1c36 100644 --- a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/RobotConnectionManager.java +++ b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/RobotConnectionManager.java @@ -72,13 +72,13 @@ public class RobotConnectionManager { //从Redis恢复状态 boolean restored = newInstance.restoreFromRedis(connecId); if (restored) { - System.out.println("从Redis恢复HuNanHongZhong实例: " + connecId); + log.info("从Redis恢复HuNanHongZhong实例: " + connecId); } else { - System.out.println("创建新的HuNanHongZhong实例: " + connecId); + log.info("创建新的HuNanHongZhong实例: " + connecId); } huNanHongZhongInstances.put(connecId, newInstance); - System.out.println("当前HuNanHongZhong实例总数: " + huNanHongZhongInstances.size()); + log.info("当前HuNanHongZhong实例总数: " + huNanHongZhongInstances.size()); return newInstance; } @@ -114,7 +114,7 @@ public class RobotConnectionManager { * 断开与游戏服务器的连接(主动断开) */ public void disconnectFromGameServer(String connecId) { - System.out.println("开始主动断开连接: {"+connecId+"}"); + log.info("开始主动断开连接: {"+connecId+"}"); RobotUser robotUser = robotRoomMapping.remove(connecId); //标记连接为非活跃 @@ -130,13 +130,13 @@ public class RobotConnectionManager { //清理所有集合数据以释放内存 instance.getHongZhongCardInhand().clear(); instance.getChuGuoCardInhand().clear(); - System.out.println("清空HuNanHongZhong集合数据: " + connecId); + log.info("清理HuNanHongZhong集合数据: " + connecId); } //移除实例 huNanHongZhongInstances.remove(connecId); - System.out.println("清理完成,当前活跃连接数: " + activeConnections.size() + ", 实例数: " + huNanHongZhongInstances.size()); + log.info("清理完成,当前活跃连接数: " + activeConnections.size() + ", 实例数: " + huNanHongZhongInstances.size()); } if (robotUser != null) { @@ -146,12 +146,12 @@ public class RobotConnectionManager { if (client.isConnected()) { client.killConnection(); } - System.out.println("客户端主动断开连接完成: {"+connecId+"}"); + log.info("客户端主动断开连接完成: {"+connecId+"}"); } catch (Exception e) { - System.out.println("断开客户端连接时发生异常: " + connecId + ", 错误: " + e.getMessage()); + log.error("断开客户端连接时发生异常: " + connecId + ", 错误: " + e.getMessage()); } } else { - System.out.println("客户端连接不存在: {"+connecId+"}"); + log.info("客户端连接不存在: {"+connecId+"}"); } //同时清理机器人房间映射 @@ -175,12 +175,12 @@ public class RobotConnectionManager { //清理过期连接 for (String connecId : expiredConnections) { - System.out.println("清理过期连接实例: " + connecId); + log.info("清理过期连接实例: " + connecId); disconnectFromGameServer(connecId); } if (!expiredConnections.isEmpty()) { - System.out.println("本次清理了 " + expiredConnections.size() + " 个过期连接实例"); + log.info("本次清理了 " + expiredConnections.size() + " 个过期连接实例"); } } @@ -198,7 +198,6 @@ public class RobotConnectionManager { ITObject param = message.param; //回调协议号 String command = message.command; - System.out.println("csmj OnEvent msg: " + command); //根据玩法ID处理不同的回调 if (StringUtil.isNotEmpty(command)) { @@ -245,10 +244,10 @@ public class RobotConnectionManager { robotUser.setSeat(seat); } } - System.out.println("playerData:" + playerData); + log.info("playerData:" + playerData); - System.out.println("obj:" + obj); - System.out.println("reloadInfo:" + reloadInfo); + log.info("obj:" + obj); + log.info("reloadInfo:" + reloadInfo); if (reloadInfo != null) { //重连回来的 int curren_outcard_seat = reloadInfo.getInt("curren_outcard_seat"); @@ -274,7 +273,7 @@ public class RobotConnectionManager { } } - System.out.println("hcard>0" + hcard); + log.info("hcard>0" + hcard); if (hcard.size() > 0) { //同步手牌 HuNanHongZhong currentInstance = getHuNanHongZhongInstance(connecId); @@ -284,9 +283,9 @@ public class RobotConnectionManager { if (currentHand.isEmpty() || hcard.size() > currentHand.size()) { //手牌集合为空 或者 玩家出牌了 currentInstance.updateHandCard(hcard); - System.out.println("断线重连:同步手牌数据,服务器手牌:" + hcard); + log.info("断线重连:同步手牌数据,服务器手牌:" + hcard); } else { - System.out.println("断线重连:使用Redis恢复的手牌数据,数量:" + currentHand.size()); + log.info("断线重连:使用Redis恢复的手牌数据,数量:" + currentHand.size()); } if (outcard_list.size() > 0) { @@ -299,9 +298,9 @@ public class RobotConnectionManager { List currentOutCards = currentInstance.getChuGuoCardInhand(); if (currentOutCards.isEmpty() || outcards.size() > currentOutCards.size()) { currentInstance.updateOutCard(outcards); - System.out.println("断线重连:同步出牌数据,服务器出牌:" + outcards); + log.info("断线重连:同步出牌数据,服务器出牌:" + outcards); } else { - System.out.println("断线重连:使用Redis恢复的出牌数据,数量:" + currentOutCards.size()); + log.info("断线重连:使用Redis恢复的出牌数据,数量:" + currentOutCards.size()); } } @@ -311,7 +310,7 @@ public class RobotConnectionManager { scheduleDelay(() -> { try { currentInstance.outCard(client); - System.out.println("断线重连后成功执行出牌操作"); + log.info("断线重连后成功执行出牌操作"); } catch (Exception e) { log.error("断线重连后执行出牌操作时发生异常", e); //即使出牌失败,也要确保连接状态正确 @@ -325,7 +324,7 @@ public class RobotConnectionManager { } }, 2, TimeUnit.SECONDS); } else { - System.err.println("警告:重连时未获取到手牌数据"); + log.info("警告:重连时未获取到手牌数据"); } } } @@ -348,7 +347,7 @@ public class RobotConnectionManager { EXGameController.updateLastAccessTime(connecId); if (robotUser == null) { - System.err.println("未找到机器人用户信息,连接ID: " + connecId); + log.info("未找到机器人用户信息,连接ID: " + connecId); return; } @@ -392,7 +391,7 @@ public class RobotConnectionManager { else if ("817".equalsIgnoreCase(command)) { huNanHongZhong.getHongZhongCardInhand().clear(); huNanHongZhong.getChuGuoCardInhand().clear(); - System.out.println("红中结算"); + log.info("红中结算"); Integer type = param.getInt("type"); if (type == 1 || type == 2) { //为1 为大结算 为2为解散,都需要恢复数据 //更新机器人剩余数量 @@ -453,7 +452,7 @@ public class RobotConnectionManager { //更新机器人剩余数量 updateLeftoverRobot(robotId); disconnectFromGameServer(connecId); - System.out.println("2002发送退出房间协议1005,robotId: {"+robotId+"}"); + log.info("2002发送退出房间协议1005,robotId: {"+robotId+"}"); }); } } @@ -462,7 +461,7 @@ public class RobotConnectionManager { log.error("处理玩家加入房间检查时发生异常", e); } }, 6, TimeUnit.SECONDS); - System.out.println("玩家{"+ robotUser.getCurrentRoomId()+"}加入房间:"+ param); + log.info("玩家{"+ robotUser.getCurrentRoomId()+"}加入房间:"+ param); } //玩家退出房间也要检查 else if ("2002".equalsIgnoreCase(command)) { @@ -490,7 +489,7 @@ public class RobotConnectionManager { //更新机器人剩余数量 updateLeftoverRobot(robotId); disconnectFromGameServer(connecId); - System.out.println("2002发送退出房间协议1005,robotId: {"+robotId+"}"); + log.info("2002发送退出房间协议1005,robotId: {"+robotId+"}"); }); } } @@ -510,7 +509,7 @@ public class RobotConnectionManager { //更新机器人剩余数量 updateLeftoverRobot(robotId); disconnectFromGameServer(connecId); - System.out.println("2005玩家发送解散房间协议,robotId: {"+robotId+"}"); + log.info("2005玩家发送解散房间协议,robotId: {"+robotId+"}"); } //解散房间时候恢复机器人账号可以使用 else if ("2008".equalsIgnoreCase(command)) { @@ -528,7 +527,7 @@ public class RobotConnectionManager { //查询该房间的玩家信息 String playersStr = jedis.hget(roomKey, "players"); - if (!playersStr.equals("[]")) { + if (playersStr != null && !playersStr.equals("[]")) { String players = playersStr.substring(1, playersStr.length() - 1); String[] playerIds = players.split(","); @@ -544,18 +543,18 @@ public class RobotConnectionManager { disconnectFromGameServer(connecId); //更新机器人剩余数量 updateLeftoverRobot(paramRobotId); - System.out.println("2009发送退出房间协议1005,robotId: {"+paramRobotId+"}"); + log.info("2009发送退出房间协议1005,robotId: {"+paramRobotId+"}"); }); } } } } } catch (NumberFormatException e) { - System.err.println("2009协议数字格式异常,robotId: " + param.get("aid") + ", connecId: " + connecId); + log.error("2009协议数字格式异常,robotId: " + param.get("aid") + ", connecId: " + connecId); } catch (NullPointerException e) { - System.err.println("2009协议空指针异常,connecId: " + connecId); + log.error("2009协议空指针异常,connecId: " + connecId); } catch (Exception e) { - System.err.println("2009协议处理异常: " + e.getMessage() + ", connecId: " + connecId); + log.error("2009协议处理异常: " + e.getMessage() + ", connecId: " + connecId); } finally { if (jedis != null) { jedis.close(); @@ -582,7 +581,7 @@ public class RobotConnectionManager { jedis2.hset("{grobot}:" + robotId, "start", "0"); - System.out.println("机器人 {"+robotId+"} 退出房间,修改gallrobot为0"); + log.info("机器人 {"+robotId+"} 退出房间,修改gallrobot为0"); } finally { jedis2.close(); } @@ -592,14 +591,14 @@ public class RobotConnectionManager { * 机器人登录 */ public void login(RobotUser robotUser){ - System.out.println("login:"+robotUser.getRobotId()); + log.info("开始机器人登录,robotId: {"+robotUser.getRobotId()+"}"); ITObject object = null; AccountBusiness accountBusiness = null; accountBusiness = new AccountBusiness(); try { //先快速登录 object = accountBusiness.fastLogin(Integer.parseInt(robotUser.getRobotId())); - System.out.println("object:"+object); + log.info("机器人登录成功,robotId: {"+robotUser.getRobotId()+"}"); if(object==null){ object = accountBusiness.idPasswordLogin(Integer.parseInt(robotUser.getRobotId()), robotUser.getPassword()); } @@ -621,7 +620,7 @@ public class RobotConnectionManager { connectGame(robotUser); robotUser.setConnecId(robotUser.getCurrentRoomId()+"_"+robotUser.getRobotId()); - System.err.println("重启获取的机器人还有当前房间,准备加入: "+robotUser.getConnecId()); + log.info("重启获取的机器人还有当前房间,准备加入: "+robotUser.getConnecId()); exGameController.webGroupJoinRoom(robotUser); } } @@ -648,12 +647,12 @@ public class RobotConnectionManager { robotUser.setClient(client); EXGameController.robotRoomMapping.put(robotUser.getCurrentRoomId()+"_"+robotUser.getRobotId(), robotUser); }else{ - System.out.println("reconnect"); - System.out.println("client.isConnected()"+robotUser.getClient().isConnected()); + log.info("机器人已连接,准备加入房间"); + log.info("client.isConnected()"+robotUser.getClient().isConnected()); if(robotUser.getClient().isConnected()){ robotUser.setIsconnect(true); }else{ - System.out.println("reconnect"+robotUser.getClient().getGameID()); + log.info("reconnect"+robotUser.getClient().getGameID()); TaurusClient client = new TaurusClient(robotUser.getGameHost()+":"+robotUser.getGamePort(), "hz"+robotUser.getRobotId(), TaurusClient.ConnectionProtocol.Tcp); client.setSession(robotUser.getLoginsession()); client.connect(); diff --git a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/business/AccountBusiness.java b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/business/AccountBusiness.java index 3d05078..56d9858 100644 --- a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/business/AccountBusiness.java +++ b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/business/AccountBusiness.java @@ -28,8 +28,7 @@ import java.util.Map.Entry; import java.util.Set; public class AccountBusiness extends Controller { - private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(AccountBusiness.class); - private static Logger logger = Logger.getLogger(AccountBusiness.class); + private static final Logger log = Logger.getLogger(AccountBusiness.class); private final ITObject fillLoginData(String session, int accountid) { ITObject resData = TObject.newInstance(); @@ -149,11 +148,11 @@ public class AccountBusiness extends Controller { String idPwdBan = Redis.use("group1_db0").get(id + "_login_ban"); if (StringUtil.isNotEmpty(idPwdBan)) { - System.out.println("进入了77777777777777777777"); + log.info("用户登录被禁止进入了77777777777777777777: {}", id); logger.error("id:" + id + " ban login"); //throw new WebException(ErrorCode.BAN_LOGIN); } - System.out.println("进入了9999999999999"); + log.debug("执行密码登录流程进入了9999999999999"); ITArray resultArray = null; try { @@ -176,13 +175,13 @@ public class AccountBusiness extends Controller { Redis.use("group1_db0").set(id + "_login_ban", "1"); Redis.use("group1_db0").expire(id + "_login_ban", 1800); logger.error("pwd error count:" + count + " not login"); - System.out.println("进入了00000000000"); + log.warn("密码错误次数过多,禁止登录: {}进入了00000000000", id); //throw new WebException(ErrorCode._NO_SESSION); } } - System.out.println("进入了111111111111"); + log.debug("密码验证完成进入了111111111111"); //throw new WebException(ErrorCode._FAILED); } @@ -226,13 +225,13 @@ public class AccountBusiness extends Controller { // logger.info("delte timeout token:" + temp); // } // } - System.out.println("进入了2222222222222"); + log.debug("生成登录token完成进入了2222222222222"); long tokenNum = Redis.use("group1_db0").scard(session + "_token"); if (tokenNum >= 10) { logger.warn("id:" + accountid + " repeat login, token count:" + tokenNum); } - System.out.println("进入了33333333333333333332"); + log.debug("登录流程结束进入了33333333333333333332"); resData.putString("token", token); return resData; diff --git a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/handler/HuNanHongZhong.java b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/handler/HuNanHongZhong.java index 48f1a95..2cbc3e0 100644 --- a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/handler/HuNanHongZhong.java +++ b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/handler/HuNanHongZhong.java @@ -7,6 +7,8 @@ import com.taurus.core.entity.ITObject; import com.taurus.core.entity.TObject; import com.taurus.core.plugin.redis.Redis; import com.taurus.core.util.StringUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import taurus.client.Message; import taurus.client.TaurusClient; @@ -15,11 +17,11 @@ import taurus.util.HongZhongSuanFaTest; import taurus.util.Util; import java.util.*; -import java.util.concurrent.CompletableFuture; import static robot.mj.thread.ThreadPoolConfig.getBusinessThreadPool; public class HuNanHongZhong { + private static final Logger log = LoggerFactory.getLogger(HuNanHongZhong.class); public static int hongZhongCard = 0; @@ -74,9 +76,9 @@ public class HuNanHongZhong { //1小时过期时间 jedis.expire(redisKey, 3600); - System.out.println("保存HuNanHongZhong状态到Redis: " + connecId); + log.info("保存HuNanHongZhong状态到Redis: {}", connecId); } catch (Exception e) { - System.err.println("保存HuNanHongZhong状态到Redis失败: " + e.getMessage()); + log.error("保存HuNanHongZhong状态到Redis失败: {}", e.getMessage()); } finally { jedis.close(); } @@ -94,7 +96,7 @@ public class HuNanHongZhong { Map stateMap = jedis.hgetAll(redisKey); if (stateMap == null || stateMap.isEmpty()) { - System.out.println("Redis中没有找到HuNanHongZhong状态数据: " + connecId); + log.warn("Redis中没有找到HuNanHongZhong状态数据: {}", connecId); return false; } @@ -117,10 +119,10 @@ public class HuNanHongZhong { session = stateMap.getOrDefault("session", ""); token = stateMap.getOrDefault("token", ""); - System.out.println("从Redis恢复HuNanHongZhong状态成功: " + connecId + ", 手牌数量: " + hongZhongchuguopai.size()); + log.info("从Redis恢复HuNanHongZhong状态成功: {}, 手牌数量: {}", connecId, hongZhongchuguopai.size()); return true; } catch (Exception e) { - System.err.println("从Redis恢复HuNanHongZhong状态失败: " + e.getMessage()); + log.error("从Redis恢复HuNanHongZhong状态失败: {}", e.getMessage()); return false; } finally { jedis.close(); @@ -136,9 +138,9 @@ public class HuNanHongZhong { try { String redisKey = "{hzmj}:" + connecId; jedis.del(redisKey); - System.out.println("从Redis删除HuNanHongZhong状态: " + connecId); + log.info("从Redis删除HuNanHongZhong状态: {}", connecId); } catch (Exception e) { - System.err.println("从Redis删除HuNanHongZhong状态失败: " + e.getMessage()); + log.error("从Redis删除HuNanHongZhong状态失败: {}", e.getMessage()); } finally { jedis.close(); } @@ -149,11 +151,11 @@ public class HuNanHongZhong { * @param handCard */ public void updateHandCard(List handCard) { - System.out.println("updateHandCard同步手牌:"+ handCard); + log.info("updateHandCard同步手牌: {}", handCard); hongZhongCardInhand.clear(); - System.out.println("updateHandCard同步手牌hongZhongCardInhand:"+ hongZhongCardInhand); + log.debug("updateHandCard同步手牌hongZhongCardInhand: {}", hongZhongCardInhand); hongZhongCardInhand.addAll(handCard); - System.out.println("updateHandCard同步手牌add:"+ hongZhongCardInhand); + log.info("updateHandCard同步手牌add: {}", hongZhongCardInhand); } public void updateOutCard(List outCard) { @@ -175,8 +177,8 @@ public class HuNanHongZhong { return null; } hongZhongCard = param.getInt("card"); - System.out.println("出牌广播" + hongZhongCard); - System.out.println("座位号:" + param.getInt("seat") + "的用户出牌:" + param.getInt("card")); + log.info("出牌广播: {}", hongZhongCard); + log.info("座位号:{}的用户出牌:{}", param.getInt("seat"), param.getInt("card")); } return null; } @@ -190,28 +192,28 @@ public class HuNanHongZhong { * @return */ public String getCard(String command, Message message) { - System.out.println("摸牌协议-----" + command + "message---" + message); + log.info("摸牌协议-----{} message---{}", command, message); if (command.equalsIgnoreCase("819")) { ITObject param = message.param; if (param == null) { return null; } // {seat=2, Ishupai=0, isBaoTing=-1, tingcard=0, isgang=0, card=101, left_count=106} - System.out.println("轮到用户:" + param.getInt("player") + "的用户摸牌" + ",牌为:" + param.getInt("card")); - System.out.println("用户id" + playerId); - System.out.println("座位号" + param.getInt("seat")); + log.info("轮到用户:{}的用户摸牌, 牌为:{}", param.getInt("player"), param.getInt("card")); + log.debug("用户id: {}", playerId); + log.debug("座位号: {}", param.getInt("seat")); if (param.getInt("player") != null) { int drawnCard = param.getInt("card"); hongZhongSuanFaTest.drawnCards = drawnCard; hongZhongCardInhand.add(drawnCard); - System.out.println("摸牌后手牌" + hongZhongCardInhand); + log.info("摸牌后手牌: {}", hongZhongCardInhand); // 创建包含摸牌后的完整手牌 List newHand = new ArrayList<>(hongZhongCardInhand); // 调用分离分析方法,将刻子、顺子、红中单独拎出后分析剩余牌 - System.out.println("[HuNanHongZhong] 开始分离分析手牌结构..."); + log.info("[HuNanHongZhong] 开始分离分析手牌结构..."); hongZhongSuanFaTest.separateAndAnalyzeHand(newHand); // 直接调用hongZhongSuanFaTest中的analyzeDrawCard方法分析摸牌后是否可听牌 @@ -219,15 +221,15 @@ public class HuNanHongZhong { // 调用新添加的findDiscardToTing方法,分析打出哪张牌可以听牌 - System.out.println("[HuNanHongZhong] 开始分析打出哪张牌可以听牌..."); + log.info("[HuNanHongZhong] 开始分析打出哪张牌可以听牌..."); Map> discardOptions = hongZhongSuanFaTest.findDiscardToTing(newHand); // 如果有可打出后听牌的选项,记录信息 if (!discardOptions.isEmpty()) { - System.out.println("[HuNanHongZhong] 发现" + discardOptions.size() + "个可打出后听牌的选项"); + log.info("[HuNanHongZhong] 发现{}个可打出后听牌的选项", discardOptions.size()); // 这些信息将在出牌决策时被考虑 } else { - System.out.println("[HuNanHongZhong] 当前没有可以打出后听牌的牌"); + log.info("[HuNanHongZhong] 当前没有可以打出后听牌的牌"); } } } @@ -241,7 +243,7 @@ public class HuNanHongZhong { * @return 是否应该碰牌 */ public boolean shouldPong(int proposedCard) { - System.out.println("判断是否应该碰牌: " + proposedCard); + log.debug("判断是否应该碰牌: {}", proposedCard); // 直接调用hongZhongSuanFaTest中的shouldPong方法,它已经包含了所有需要的规则 return hongZhongSuanFaTest.shouldPong(proposedCard, hongZhongCardInhand); @@ -267,9 +269,9 @@ public class HuNanHongZhong { } if (hongZhongCardInhand.size() > 13) { outCard(client); - System.out.println("机器人:" + param.getInt("seat") + "为庄家,需要出牌" + ",牌为:" + hongZhongCardInhand.get(0)); + log.info("机器人:{}为庄家,需要出牌, 牌为:{}", param.getInt("seat"), hongZhongCardInhand.get(0)); } - System.out.println("机器人:" + param.getInt("seat") + "初始化手牌" + ",牌为:" + hongZhongCardInhand.toString()); + log.info("机器人:{}初始化手牌, 牌为:{}", param.getInt("seat"), hongZhongCardInhand.toString()); } return null; @@ -293,8 +295,8 @@ public class HuNanHongZhong { id = firstTip.getInt("id"); type = firstTip.getInt("type"); opcard = firstTip.getTArray("opcard").getInt(0); - System.out.println("id ++ " + id); - System.out.println("type ++ " + type); + log.debug("id ++ {}", id); + log.debug("type ++ {}", type); } //弃 是根据算法选择是否要弃掉 不进行碰杠胡 //params.putInt("qi", 0); @@ -308,12 +310,12 @@ public class HuNanHongZhong { params.putInt("qi", 0); params.putInt("id", 1); Util.removeCard(hongZhongCardInhand, opcard, 2); - System.out.println("执行碰牌并删除碰的牌"); + log.info("执行碰牌并删除碰的牌"); } else { params.putString("session", session + "," + token); params.putInt("qi", 1); // 放弃碰牌 params.putInt("id", 0); - System.out.println("根据规则,决定不碰牌: " + opcard); + log.info("根据规则,决定不碰牌: {}", opcard); } // Global.logger.info("删除碰的牌"); //执行胡牌 @@ -321,46 +323,46 @@ public class HuNanHongZhong { params.putString("session", session + "," + token); params.putInt("qi", 0); params.putInt("id", 1); - System.out.println("执行胡牌"); + log.info("执行胡牌"); //执行吃杠 } else if (type == 3) { params.putString("session", session + "," + token); params.putInt("qi", 0); params.putInt("id", 1); Util.removeCard(hongZhongCardInhand, opcard, 3); - System.out.println("执行吃杠"); + log.info("执行吃杠"); //执行自杠 } else if (type == 4) { params.putString("session", session + "," + token); params.putInt("qi", 0); params.putInt("id", 1); Util.removeCard(hongZhongCardInhand, opcard, 4); - System.out.println("执行自杠"); + log.info("执行自杠"); // 碰后补杠 } else if (type == 5) { params.putString("session", session + "," + token); params.putInt("qi", 0); params.putInt("id", 1); Util.removeCard(hongZhongCardInhand, opcard, 1); - System.out.println("执行碰后补杠"); + log.info("执行碰后补杠"); } // cardInhand.remove(0); // cardInhand.remove(1); - System.out.println("执行id为:" + 0 + "的操作"); + log.info("执行id为:{}的操作", 0); //使用线程池执行延迟动作 getBusinessThreadPool().execute(() -> { try { int delaySeconds = 1 + new Random().nextInt(2); - System.out.println("执行延迟" + delaySeconds + "秒"); + log.debug("执行延迟{}秒", delaySeconds); Thread.sleep(delaySeconds * 1000); client.send("612", params, response -> { }); } catch (Exception e) { - System.err.println("执行动作时发生异常: " + e.getMessage()); + log.error("执行动作时发生异常: {}", e.getMessage()); } }); return null; @@ -373,7 +375,7 @@ public class HuNanHongZhong { if (param == null) { return null; } - System.out.println("出牌权转移到座位号:" + param.getInt("seat") + "的用户"); + log.info("出牌权转移到座位号:{}的用户", param.getInt("seat")); } return null; } @@ -385,7 +387,7 @@ public class HuNanHongZhong { public String outCard(TaurusClient client) { // 调用分离分析方法,将刻子、顺子、红中单独拎出后分析剩余牌 try { - System.out.println("[HuNanHongZhong] 出牌前分离分析手牌结构..."); + log.info("[HuNanHongZhong] 出牌前分离分析手牌结构..."); hongZhongSuanFaTest.separateAndAnalyzeHand(hongZhongCardInhand); // 红中麻将出牌 @@ -409,13 +411,13 @@ public class HuNanHongZhong { params.putTArray("outcard_list", CardUtil.maJiangToTArray(cardsToSend)); } params.putTArray("card_list", CardUtil.maJiangToTArray(hongZhongCardInhand)); - System.out.println("机器人牌============" + params); + log.info("机器人牌============{}", params); // 将当前出的牌添加到历史出牌列表 hongZhongchuguopai.add(cardToOut); // 从手牌中移除 hongZhongCardInhand.remove(Integer.valueOf(cardToOut)); - System.out.println("出牌: " + cardToOut); - System.out.println("目前机器人剩余手牌:" + hongZhongCardInhand.toString()); + log.info("出牌: {}", cardToOut); + log.info("目前机器人剩余手牌:{}", hongZhongCardInhand.toString()); params.putString("session", session + "," + token); //使用线程池替代CompletableFuture.runAsync + Thread.sleep @@ -427,7 +429,7 @@ public class HuNanHongZhong { }); } catch (Exception e) { - System.out.println("Thread error"); + log.error("线程执行错误"); } }); @@ -451,14 +453,14 @@ public class HuNanHongZhong { Integer card = param.getInt("card"); // 操作牌值 Integer type = param.getInt("type"); // 操作类型 Integer from_seat = param.getInt("from_seat"); // 牌来源座位 - System.out.println("删除出过的牌组 card " + card); - System.out.println("删除出过的牌组 type " + type); - System.out.println("删除出过的牌组 from_seat " + from_seat); - System.out.println("机器人 seat " + seat); + log.debug("删除出过的牌组 card: {}", card); + log.debug("删除出过的牌组 type: {}", type); + log.debug("删除出过的牌组 from_seat: {}", from_seat); + log.debug("机器人 seat: {}", seat); if (type == 2 || type == 3 || type == 5) { // 碰,杠 getChuGuoCardInhand().remove(Integer.valueOf(card)); - System.out.println("删除出过的牌组 成功"); + log.info("删除出过的牌组 成功"); } return null; } diff --git a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/thread/ResourceCleanupUtil.java b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/thread/ResourceCleanupUtil.java index c10eb50..5a9ce64 100644 --- a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/thread/ResourceCleanupUtil.java +++ b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/thread/ResourceCleanupUtil.java @@ -2,6 +2,8 @@ package robot.mj.thread; import robot.mj.EXGameController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -11,6 +13,7 @@ import java.util.concurrent.ConcurrentHashMap; * 用于管理和清理不再使用的资源 防止内存泄漏 */ public class ResourceCleanupUtil { + private static final Logger log = LoggerFactory.getLogger(ResourceCleanupUtil.class); //需要清理的资源 private static final Set pendingCleanupResources = ConcurrentHashMap.newKeySet(); @@ -26,7 +29,7 @@ public class ResourceCleanupUtil { return; } - System.out.println("开始执行资源清理,待清理资源数: " + pendingCleanupResources.size()); + log.info("开始执行资源清理,待清理资源数: {}", pendingCleanupResources.size()); int cleanedCount = 0; Set resourcesToClean = ConcurrentHashMap.newKeySet(); @@ -41,13 +44,13 @@ public class ResourceCleanupUtil { pendingCleanupResources.remove(resourceId); cleanedCount++; - System.out.println("已清理资源: " + resourceId); + log.info("已清理资源: {}", resourceId); } catch (Exception e) { - System.err.println("清理资源时发生异常: " + resourceId + ", 错误: " + e.getMessage()); + log.error("清理资源时发生异常: {}, 错误: {}", resourceId, e.getMessage()); } } - System.out.println("资源清理完成,共清理: " + cleanedCount + " 个资源"); + log.info("资源清理完成,共清理: {} 个资源", cleanedCount); //执行常规清理 performRegularCleanup(); @@ -62,11 +65,11 @@ public class ResourceCleanupUtil { EXGameController.cleanupExpiredConnections(); //输出当前系统状态 - System.out.println("=== 系统资源状态 ==="); - System.out.println(ThreadPoolConfig.getThreadPoolStatus()); + log.info("=== 系统资源状态 ==="); + log.info("{}", ThreadPoolConfig.getThreadPoolStatus()); } catch (Exception e) { - System.err.println("执行常规清理时发生异常: " + e.getMessage()); + log.error("执行常规清理时发生异常: {}", e.getMessage()); } } } \ No newline at end of file diff --git a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/thread/ThreadPoolConfig.java b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/thread/ThreadPoolConfig.java index 8c3004c..4247268 100644 --- a/robots/majiang/robot_mj_hz/src/main/java/robot/mj/thread/ThreadPoolConfig.java +++ b/robots/majiang/robot_mj_hz/src/main/java/robot/mj/thread/ThreadPoolConfig.java @@ -1,5 +1,8 @@ package robot.mj.thread; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; @@ -7,6 +10,7 @@ import java.util.concurrent.atomic.AtomicInteger; * 线程池配置类 */ public class ThreadPoolConfig { + private static final Logger log = LoggerFactory.getLogger(ThreadPoolConfig.class); //线程池配置 private static final ExecutorService BUSINESS_THREAD_POOL = @@ -54,14 +58,14 @@ public class ThreadPoolConfig { * 执行延迟任务,替代Thread.sleep */ public static void scheduleDelay(Runnable task, long delay, TimeUnit unit) { - System.out.println("提交延迟任务: 延迟" + delay + " " + unit + ", 当前时间: " + System.currentTimeMillis()); + log.debug("提交延迟任务: 延迟{} {}, 当前时间: {}", delay, unit, System.currentTimeMillis()); SCHEDULED_EXECUTOR_SERVICE.schedule(() -> { try { - System.out.println("执行延迟任务开始: 当前时间: " + System.currentTimeMillis()); + log.debug("执行延迟任务开始: 当前时间: {}", System.currentTimeMillis()); task.run(); - System.out.println("执行延迟任务完成: 当前时间: " + System.currentTimeMillis()); + log.debug("执行延迟任务完成: 当前时间: {}", System.currentTimeMillis()); } catch (Exception e) { - System.err.println("延迟任务执行异常: " + e.getMessage()); + log.error("延迟任务执行异常: {}", e.getMessage()); } }, delay, unit); } @@ -70,13 +74,13 @@ public class ThreadPoolConfig { * 关闭线程池 释放资源 */ public static void shutdown() { - System.out.println("开始关闭线程池..."); + log.info("开始关闭线程池..."); //关闭定时任务线程池 SCHEDULED_EXECUTOR_SERVICE.shutdown(); try { if (!SCHEDULED_EXECUTOR_SERVICE.awaitTermination(3, TimeUnit.SECONDS)) { - System.out.println("定时任务线程池强制关闭"); + log.warn("定时任务线程池强制关闭"); SCHEDULED_EXECUTOR_SERVICE.shutdownNow(); } } catch (InterruptedException e) { @@ -88,7 +92,7 @@ public class ThreadPoolConfig { BUSINESS_THREAD_POOL.shutdown(); try { if (!BUSINESS_THREAD_POOL.awaitTermination(5, TimeUnit.SECONDS)) { - System.out.println("业务线程池强制关闭"); + log.warn("业务线程池强制关闭"); BUSINESS_THREAD_POOL.shutdownNow(); } } catch (InterruptedException e) { @@ -96,7 +100,7 @@ public class ThreadPoolConfig { Thread.currentThread().interrupt(); } - System.out.println("线程池关闭完成"); + log.info("线程池关闭完成"); } /** diff --git a/robots/majiang/robot_mj_hz/src/main/java/taurus/util/HongZhongSuanFaTest.java b/robots/majiang/robot_mj_hz/src/main/java/taurus/util/HongZhongSuanFaTest.java index d925ee1..4da2b68 100644 --- a/robots/majiang/robot_mj_hz/src/main/java/taurus/util/HongZhongSuanFaTest.java +++ b/robots/majiang/robot_mj_hz/src/main/java/taurus/util/HongZhongSuanFaTest.java @@ -1,9 +1,13 @@ package taurus.util; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.*; public class HongZhongSuanFaTest { private static final int HONG_ZHONG = 412; + private static final Logger log = LoggerFactory.getLogger(HongZhongSuanFaTest.class); public int drawnCards; // 权重配置 - @@ -57,7 +61,7 @@ public class HongZhongSuanFaTest { // 关键步骤1:摸牌分析 - 检测是否已经听牌或可听牌 boolean isTingAfterDraw = analyzeDrawCard(cardInhand,drawnCards); - System.out.println("是否听牌: " + isTingAfterDraw); + log.info("是否听牌: " + isTingAfterDraw); // 分析手牌结构 - 获取完整的手牌分析结果 HandAnalysis analysis = analyzeHand(handCards); @@ -103,9 +107,9 @@ public class HongZhongSuanFaTest { // 执行时间统计 long endTime = System.currentTimeMillis(); // logInfo("算法执行时间: " + (endTime - startTime) + "ms"); - System.out.println("听的牌----"+analysis.tingCards); + log.info("听的牌----"+analysis.tingCards); if (isTingAfterDraw && drawnCards!=412 ) { - System.out.println("1111"); + log.info("1111"); return String.valueOf(drawnCards); } @@ -626,7 +630,7 @@ public class HongZhongSuanFaTest { * 统一日志输出方法 */ private void logInfo(String message) { - System.out.println(message); + log.info(message); } /** @@ -1881,13 +1885,13 @@ public class HongZhongSuanFaTest { * @return 当前手牌是否已经听牌 */ public boolean analyzeDrawCard(List currentHand, int drawnCard) { - System.out.println("机器人手牌" + currentHand); + log.info("机器人手牌" + currentHand); // 创建包含摸牌后的手牌 List newHand = new ArrayList<>(currentHand); // newHand.add(drawnCard); newHand.sort(Integer::compareTo); drawnCard = drawnCards; - System.out.println("drawnCards ======= " + drawnCards); + log.info("drawnCards ======= " + drawnCards); logInfo("\n===== 开始摸牌分析 ====="); logInfo("摸牌: " + drawnCard + ",摸牌后手牌: " + newHand); @@ -2130,8 +2134,7 @@ public class HongZhongSuanFaTest { * @param hand 当前手牌 */ public void separateAndAnalyzeHand(List hand) { - System.out.println("\n开始分离分析手牌..."); - + log.info("开始分离分析手牌"); // 分析手牌 HandAnalysis analysis = analyzeHand(hand); @@ -2195,12 +2198,11 @@ public class HongZhongSuanFaTest { } // 输出分离结果 - System.out.println("分离结果:"); - System.out.println("- 刻子: " + kziCards); - System.out.println("- 顺子: " + shunziCards); - System.out.println("- 红中: " + hongzhongCards); - System.out.println("- 剩余待分析的牌: " + remainingCards); - + log.info("分离结果:"); + log.info("刻子: " + kziCards); + log.info("顺子: " + shunziCards); + log.info("红中: " + hongzhongCards); + log.info("剩余待分析的牌: " + remainingCards); // 更新analysis中的剩余牌 analysis.remainingCards = remainingCards; @@ -2215,7 +2217,7 @@ public class HongZhongSuanFaTest { */ private void analyzeRemainingCards(List remainingCards, HandAnalysis analysis) { if (remainingCards.isEmpty()) { - System.out.println("没有剩余需要分析的牌,手牌结构完整"); + log.info("没有剩余需要分析的牌,手牌结构完整"); return; } @@ -2339,21 +2341,21 @@ public class HongZhongSuanFaTest { // 更新analysis中的孤张牌信息,确保顺子中多余的牌被正确识别为孤张 analysis.isolatedCards = isolatedCards; - System.out.println("剩余牌分析:"); - System.out.println("- 三张: " + triples); - System.out.println("- 对子: " + pairs); - System.out.println("- 搭子: " + 搭子); - System.out.println("- 孤张: " + isolatedCards); + log.info("剩余牌分析结果: "); + log.info("三张: {}", triples); + log.info("对子: {}", pairs); + log.info("搭子: {}", 搭子); + log.info("孤张: {}", isolatedCards); // 计算剩余牌需要的张数来完成牌型 int neededMelds = 4 - analysis.meldCount; int neededPairs = (analysis.pairCount > 0) ? 0 : 1; - System.out.println("还需要完成的面子数: " + neededMelds); - System.out.println("还需要的对子数: " + neededPairs); + log.info("剩余牌需要完成的面子数: " + neededMelds); + log.info("还需要的对子数: " + neededPairs); // 分析牌的冗余情况 if (!isolatedCards.isEmpty()) { - System.out.println("存在孤张牌,建议考虑打出:" + isolatedCards); + log.info("存在孤张牌,建议考虑打出:{}", isolatedCards); } } @@ -2423,28 +2425,28 @@ public class HongZhongSuanFaTest { public boolean shouldPong(int proposedCard, List currentHand) { // 分析当前手牌 HandAnalysis analysis = analyzeHand(currentHand); - System.out.println("----"+analysis.pairCount); + log.info("当前手牌分析结果: "+analysis.pairCount); // 不能碰听牌组中的牌 if (analysis.tingCards.contains(proposedCard)) { - System.out.println("不能碰: 不能碰听牌组中的牌 " + proposedCard); + log.info("不能碰: 不能碰听牌组中的牌 " + proposedCard); return false; } if (analysis.isTingPai){ - System.out.println("听牌中不能碰" + proposedCard); + log.info("听牌中不能碰" + proposedCard); return false; } // 规则2: 有5个对子时不能碰已成对子的牌 if (analysis.hasLongQiDuiPotential==true && analysis.pairs.contains(proposedCard)) { - System.out.println("不能碰: 已有5个或更多对子时不能碰已成对子的牌 " + proposedCard); + log.info("不能碰: 已有5个或更多对子时不能碰已成对子的牌 " + proposedCard); return false; } // 规则3: 顺子的情况下不能碰 if (isCardInSequence(proposedCard, analysis)) { - System.out.println("不能碰: 牌在顺子中的情况下不能碰 " + proposedCard); + log.info("不能碰: 牌在顺子中的情况下不能碰 " + proposedCard); return false; } @@ -2457,7 +2459,7 @@ public class HongZhongSuanFaTest { } boolean canPong = cardCount >= 2; - System.out.println("是否可以碰牌 " + proposedCard + ": " + canPong); + log.info("是否可以碰牌 " + proposedCard + ": "+ canPong); return canPong; }