diff --git a/robots/puke/robot_pk_pdk/src/main/java/robot/mj/RobotConnectionManager.java b/robots/puke/robot_pk_pdk/src/main/java/robot/mj/RobotConnectionManager.java index 53c0e4b..5a42905 100644 --- a/robots/puke/robot_pk_pdk/src/main/java/robot/mj/RobotConnectionManager.java +++ b/robots/puke/robot_pk_pdk/src/main/java/robot/mj/RobotConnectionManager.java @@ -184,12 +184,17 @@ public class RobotConnectionManager { //处理 seat //获取机器人的seat ITArray playerData = obj.getTArray("playerData"); + Integer robotSeat = null; for (int i = 0; i < playerData.size(); i++) { ITObject tms = playerData.getTObject(i); Integer tmuserid = tms.getInt("aid"); if (tmuserid == Integer.parseInt(robotUser.getRobotId())) { - Integer seat = tms.getInt("seat"); - robotUser.setSeat(seat); + robotSeat = tms.getInt("seat"); + robotUser.setSeat(robotSeat); + //同步到HuNanPaoDeKuai实例 + HuNanPaoDeKuai currentInstance = getHuNanPaoDeKuaiInstance(connecId); + currentInstance.seat = robotSeat; + System.out.println("断线重连:设置机器人座位号 seat=" + robotSeat); } } System.out.println("playerData:" + playerData); @@ -208,60 +213,86 @@ public class RobotConnectionManager { hcard.add(hand_card.getInt(i)); } } - ITArray outcard_list = new TArray(); - if (info_list != null) { - for (int i = 0; i < info_list.size(); i++) { - ITObject tms = info_list.getTObject(i); - Integer playerid = tms.getInt("playerid"); - if (playerid == Integer.parseInt(robotUser.getRobotId())) { - outcard_list = tms.getTArray("outcard_list"); - } - } - } - + System.out.println("hcard>0" + hcard); if (hcard.size() > 0) { //同步手牌 HuNanPaoDeKuai currentInstance = getHuNanPaoDeKuaiInstance(connecId); - //同步逻辑 - if (hcard.size() > 0) { - //直接同步服务器手牌数据 + //同步手牌逻辑 + if (hcard.size() > currentInstance.getPaoDekuaiCardInhand().size()) { + //服务器手牌更多 说明有新牌需要更新 currentInstance.updateHandCard(hcard); System.out.println("断线重连:同步手牌数据,服务器手牌:" + hcard); + } else { + System.out.println("断线重连:本地手牌数量充足,无需更新"); } - //同步最后出牌信息 - /*if(info_list!=null) { - for (int i = 0; i < info_list.size(); i++) { - ITObject playerInfo = info_list.getTObject(i); - Integer playerid = playerInfo.getInt("playerid"); + //同步最后出牌信息 获取上家出牌信息 + if(reloadInfo != null) { + //获取当前出牌的座位号 + Integer lastOutCardSeat = reloadInfo.getInt("last_outcard_seat"); + System.out.println("断线重连:上家座位号 = " + lastOutCardSeat); + + if(lastOutCardSeat != null && info_list != null) { + //找到上家的出牌记录 + for (int i = 0; i < info_list.size(); i++) { + ITObject playerInfo = info_list.getTObject(i); + Integer playerid = playerInfo.getInt("playerid"); + Integer seat = getPlayerSeat(obj, playerid); - //找到最后出牌的玩家信息 - if(playerid != null && playerid == Integer.parseInt(robotUser.getRobotId())) { - ITObject lastOutCard = playerInfo.getTObject("last_outcard"); - if(lastOutCard != null) { - //提取最后出牌的关键信息 - int minCard = lastOutCard.getInt("min_card"); - int len = lastOutCard.getInt("len"); - int type = lastOutCard.getInt("type"); - ITArray cardList = lastOutCard.getTArray("card_list"); + //找到上家的出牌信息 + if(seat != null && seat.equals(lastOutCardSeat)) { + //从last_outcard路径获取上家最新出牌信息 + ITObject lastOutCard = playerInfo.getTObject("last_outcard"); + if(lastOutCard != null) { + //提取最后出牌的关键信息 + int minCard = lastOutCard.getInt("min_card"); + int len = lastOutCard.getInt("len"); + int type = lastOutCard.getInt("type"); + ITArray cardList = lastOutCard.getTArray("card_list"); - ITObject syncedCardList = TObject.newInstance(); - syncedCardList.putTArray("card_list", cardList); - syncedCardList.putInt("min_card", minCard); - syncedCardList.putInt("len", len); - syncedCardList.putInt("type", type); + ITObject syncedCardList = TObject.newInstance(); + syncedCardList.putTArray("card_list", cardList); + syncedCardList.putInt("min_card", minCard); + syncedCardList.putInt("len", len); + syncedCardList.putInt("type", type); - currentInstance.setCard_list(syncedCardList); - System.out.println("断线重连:同步最后出牌数据 - min_card:" + minCard + ", len:" + len + ", type:" + type + ", cards:" + cardList); + currentInstance.setCard_list(syncedCardList); + System.out.println("断线重连:同步上家最后出牌数据 - seat:" + seat + ", min_card:" + minCard + ", len:" + len + ", type:" + type + ", cards:" + cardList); + } else { + System.out.println("断线重连:上家seat=" + seat + "没有last_outcard信息"); + //如果上家没有出牌信息 清空card_list + currentInstance.setCard_list(TObject.newInstance()); + } + break; + } } - break; + } else { + System.out.println("断线重连:未找到有效的last_outcard_seat或info_list为空"); + //清空出牌记录以避免使用过期数据 + currentInstance.setCard_list(TObject.newInstance()); } } - }*/ sleepTime(2000); + //重连完成后强制同步座位号状态 + if (robotSeat != null) { + currentInstance.seat = robotSeat; + System.out.println("重连后强制同步座位号: seat=" + robotSeat); + } else { + System.err.println("警告:未能获取到机器人座位号,使用默认值0"); + currentInstance.seat = 0; + } + + //验证seat和guangboseat不同 + if (currentInstance.seat == currentInstance.guangboseat && currentInstance.guangboseat != 0) { + System.err.println("严重警告:seat和guangboseat相同!seat=" + currentInstance.seat + ", guangboseat=" + currentInstance.guangboseat); + //强制修正 + currentInstance.seat = (currentInstance.guangboseat == 1) ? 2 : 1; + System.out.println("已强制修正seat为: " + currentInstance.seat); + } + currentInstance.outCard(client); } else { System.err.println("警告:重连时未获取到手牌数据"); @@ -308,15 +339,26 @@ public class RobotConnectionManager { Integer seat1 = param.getInt("seat"); System.out.println("跑的快seat1 77777777777777777777777777" + seat1); System.out.println("跑的快palyer6666666666666666666666666" + player); + + //增加调试信息 + System.out.println("=== 2004协议调试信息 ==="); + System.out.println("当前机器人seat: " + huNanPaoDeKuai.seat); + System.out.println("协议中的seat1: " + seat1); + System.out.println("协议中的player: " + player); + System.out.println("机器人ID: " + robotUser.getRobotId()); + if (seat1 != null) { huNanPaoDeKuai.seat = seat1; + System.out.println("更新seat为: " + seat1); } //出牌 + System.out.println("准备调用outCard方法"); huNanPaoDeKuai.outCard(client); //处理完协议后保存到Redis HuNanPaoDeKuai currentInstance = huNanPaoDeKuaiInstances.get(connecId); currentInstance.saveToRedis(connecId); + System.out.println("2004协议处理完成"); } //结算准备 else if ("2007".equalsIgnoreCase(command)) { @@ -524,4 +566,23 @@ public class RobotConnectionManager { } } + /** + * 根据玩家ID获取座位号 + */ + private Integer getPlayerSeat(ITObject tableInfo, Integer playerId) { + if (tableInfo == null || playerId == null) return null; + + ITArray playerData = tableInfo.getTArray("playerData"); + if (playerData != null) { + for (int i = 0; i < playerData.size(); i++) { + ITObject player = playerData.getTObject(i); + Integer aid = player.getInt("aid"); + if (aid != null && aid.equals(playerId)) { + return player.getInt("seat"); + } + } + } + return null; + } + } \ No newline at end of file diff --git a/robots/puke/robot_pk_pdk/src/main/java/robot/mj/handler/HuNanPaoDeKuai.java b/robots/puke/robot_pk_pdk/src/main/java/robot/mj/handler/HuNanPaoDeKuai.java index b7916bb..0c1ce6e 100644 --- a/robots/puke/robot_pk_pdk/src/main/java/robot/mj/handler/HuNanPaoDeKuai.java +++ b/robots/puke/robot_pk_pdk/src/main/java/robot/mj/handler/HuNanPaoDeKuai.java @@ -139,6 +139,8 @@ public class HuNanPaoDeKuai { } catch (NumberFormatException e) { seat = 0; } + } else { + seat = 0; } if (stateMap.containsKey("guangboseat")) { @@ -147,6 +149,8 @@ public class HuNanPaoDeKuai { } catch (NumberFormatException e) { guangboseat = 0; } + } else { + guangboseat = 0; } if (stateMap.containsKey("remain")) { @@ -250,9 +254,22 @@ public class HuNanPaoDeKuai { */ public String outCard(TaurusClient client) { try { + System.out.println("=== 开始执行outCard方法 ==="); + System.out.println("当前seat: " + seat); + System.out.println("当前guangboseat: " + guangboseat); + System.out.println("当前手牌数量: " + paoDekuaiCardInhand.size()); + System.out.println("当前card_list: " + card_list); + ITArray itArray = null; itArray = test.intelligentPaoDeKuaiOutCard(this, paoDekuaiCardInhand, card_list, seatRemainHistory); System.out.println("itArray-----" + itArray); + + //无法跟牌且不是下家只剩一张牌的情况 则pass + if (itArray == null && remain != 1) { + System.out.println("无法跟牌,选择pass"); + return null; + } + if (remain == 1 && itArray == null) { //如果玩家的下家只有一张牌,玩家出单张必须是最大的 CardObj maxSingleCard = CardUtil.findMaxSingleCard(paoDekuaiCardInhand); System.out.println("出的最大牌 " + maxSingleCard); @@ -277,8 +294,10 @@ public class HuNanPaoDeKuai { System.out.println("出牌成功: " + response.returnCode); }); }catch (Exception e) { - + System.err.println("出牌异常: " + e.getMessage()); + e.printStackTrace(); } + System.out.println("=== outCard方法执行结束 ==="); return null; } @@ -315,24 +334,29 @@ public class HuNanPaoDeKuai { System.out.println("seat字段: " + param.getInt("seat")); System.out.println("player字段: " + param.getInt("player")); - card_list = param.getTObject("card_obj"); - - // 修复:正确获取座位号 - Integer seat = param.getInt("seat"); // 从seat字段获取座位号 - if (seat != null) { - guangboseat = seat; + //正确获取座位号 + Integer broadcastSeat = param.getInt("seat");//从seat字段获取座位号 + if (broadcastSeat != null) { + guangboseat = broadcastSeat; System.out.println("使用seat字段设置guangboseat: " + guangboseat); } else { - // 兼容旧版本:尝试从player字段获取座位号 + //从player字段获取座位号 guangboseat = param.getInt("player"); System.out.println("使用player字段设置guangboseat: " + guangboseat); } + //更新出牌信息 + ITObject cardObj = param.getTObject("card_obj"); + if (cardObj != null) { + card_list = cardObj; + System.out.println("更新card_list为: " + card_list); + } + remain = param.getInt("remain"); //剩余牌数量 saveRemainHistory(guangboseat, remain); System.out.println("座位号和手牌数量记录 " + seatRemainHistory); System.out.println("广播手牌数量" + "座位号 " + guangboseat + "手牌数量 " + remain); - System.out.println("座位号:" + guangboseat + "的用户出牌广播:" + param.getTObject("card_obj")); + System.out.println("座位号:" + guangboseat + "的用户出牌广播:" + card_list); System.out.println("======================="); return null; } @@ -345,4 +369,4 @@ public class HuNanPaoDeKuai { } -} +} \ No newline at end of file diff --git a/robots/puke/robot_pk_pdk/src/main/java/taurus/util/test.java b/robots/puke/robot_pk_pdk/src/main/java/taurus/util/test.java index f058930..0da813f 100644 --- a/robots/puke/robot_pk_pdk/src/main/java/taurus/util/test.java +++ b/robots/puke/robot_pk_pdk/src/main/java/taurus/util/test.java @@ -38,6 +38,10 @@ public class test { if (huNanPaoDeKuai.seat == huNanPaoDeKuai.guangboseat) { + System.out.println("=== 座位号匹配错误检测 ==="); + System.out.println("seat=" + huNanPaoDeKuai.seat + ", guangboseat=" + huNanPaoDeKuai.guangboseat); + System.out.println("检测到seat与guangboseat相同,可能存在状态同步问题"); + Map otherSeatsLastRemain = getOtherSeatsLastRemain(seat, seatRemainHistory); boolean hasAnyEqualOne = otherSeatsLastRemain.values().stream().anyMatch(value -> value == 1); if (hasAnyEqualOne) { @@ -59,6 +63,7 @@ public class test { System.out.println("广播座位号 9999999999 " + huNanPaoDeKuai.guangboseat); //座位号出问题了 + System.out.println("座位号不匹配,切换到首轮出牌逻辑"); playCards = intelligentFirstPlayWithRemainConsideration(huNanPaoDeKuai, sortedHand, seatRemainHistory); return CardUtil.toTArray(playCards); } @@ -73,6 +78,11 @@ public class test { return CardUtil.toTArray1(maxSingleCard); } else { playCards = intelligentResponsePlayWithRemainConsideration(sortedHand, minCard, len, opponentCards, type); + //如果返回空列表 + if (playCards == null || playCards.isEmpty()) { + System.out.println("无法跟牌,执行pass操作"); + return null; + } return CardUtil.toTArray(playCards); } } @@ -2328,9 +2338,12 @@ public class test { */ private static PlayOptionWithRemain selectBestOptionWithRemain(List options, int handSize, boolean isFirstPlay) { + if (options == null || options.isEmpty()) { + return null; + } return options.stream() .max(Comparator.comparingInt(option -> option.totalScore)) - .orElse(options.isEmpty() ? null : options.get(0)); + .orElse(null); } /**