跑得快重连后座位号不同无法出牌问题

master
zhouwei 2026-02-07 15:52:46 +08:00
parent e8cdeef66c
commit a03b28196d
3 changed files with 148 additions and 50 deletions

View File

@ -184,12 +184,17 @@ public class RobotConnectionManager {
//处理 seat //处理 seat
//获取机器人的seat //获取机器人的seat
ITArray playerData = obj.getTArray("playerData"); ITArray playerData = obj.getTArray("playerData");
Integer robotSeat = null;
for (int i = 0; i < playerData.size(); i++) { for (int i = 0; i < playerData.size(); i++) {
ITObject tms = playerData.getTObject(i); ITObject tms = playerData.getTObject(i);
Integer tmuserid = tms.getInt("aid"); Integer tmuserid = tms.getInt("aid");
if (tmuserid == Integer.parseInt(robotUser.getRobotId())) { if (tmuserid == Integer.parseInt(robotUser.getRobotId())) {
Integer seat = tms.getInt("seat"); robotSeat = tms.getInt("seat");
robotUser.setSeat(seat); robotUser.setSeat(robotSeat);
//同步到HuNanPaoDeKuai实例
HuNanPaoDeKuai currentInstance = getHuNanPaoDeKuaiInstance(connecId);
currentInstance.seat = robotSeat;
System.out.println("断线重连:设置机器人座位号 seat=" + robotSeat);
} }
} }
System.out.println("playerData:" + playerData); System.out.println("playerData:" + playerData);
@ -208,60 +213,86 @@ public class RobotConnectionManager {
hcard.add(hand_card.getInt(i)); 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); System.out.println("hcard>0" + hcard);
if (hcard.size() > 0) { if (hcard.size() > 0) {
//同步手牌 //同步手牌
HuNanPaoDeKuai currentInstance = getHuNanPaoDeKuaiInstance(connecId); HuNanPaoDeKuai currentInstance = getHuNanPaoDeKuaiInstance(connecId);
//同步逻辑 //同步手牌逻辑
if (hcard.size() > 0) { if (hcard.size() > currentInstance.getPaoDekuaiCardInhand().size()) {
//直接同步服务器手牌数据 //服务器手牌更多 说明有新牌需要更新
currentInstance.updateHandCard(hcard); currentInstance.updateHandCard(hcard);
System.out.println("断线重连:同步手牌数据,服务器手牌:" + hcard); System.out.println("断线重连:同步手牌数据,服务器手牌:" + hcard);
} else {
System.out.println("断线重连:本地手牌数量充足,无需更新");
} }
//同步最后出牌信息 //同步最后出牌信息 获取上家出牌信息
/*if(info_list!=null) { if(reloadInfo != null) {
for (int i = 0; i < info_list.size(); i++) { //获取当前出牌的座位号
ITObject playerInfo = info_list.getTObject(i); Integer lastOutCardSeat = reloadInfo.getInt("last_outcard_seat");
Integer playerid = playerInfo.getInt("playerid"); System.out.println("断线重连:上家座位号 = " + lastOutCardSeat);
//找到最后出牌的玩家信息 if(lastOutCardSeat != null && info_list != null) {
if(playerid != null && playerid == Integer.parseInt(robotUser.getRobotId())) { //找到上家的出牌记录
ITObject lastOutCard = playerInfo.getTObject("last_outcard"); for (int i = 0; i < info_list.size(); i++) {
if(lastOutCard != null) { ITObject playerInfo = info_list.getTObject(i);
//提取最后出牌的关键信息 Integer playerid = playerInfo.getInt("playerid");
int minCard = lastOutCard.getInt("min_card"); Integer seat = getPlayerSeat(obj, playerid);
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); if(seat != null && seat.equals(lastOutCardSeat)) {
syncedCardList.putInt("min_card", minCard); //从last_outcard路径获取上家最新出牌信息
syncedCardList.putInt("len", len); ITObject lastOutCard = playerInfo.getTObject("last_outcard");
syncedCardList.putInt("type", type); 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");
currentInstance.setCard_list(syncedCardList); ITObject syncedCardList = TObject.newInstance();
System.out.println("断线重连:同步最后出牌数据 - min_card:" + minCard + ", len:" + len + ", type:" + type + ", cards:" + cardList); 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("断线重连:同步上家最后出牌数据 - 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); 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); currentInstance.outCard(client);
} else { } else {
System.err.println("警告:重连时未获取到手牌数据"); System.err.println("警告:重连时未获取到手牌数据");
@ -308,15 +339,26 @@ public class RobotConnectionManager {
Integer seat1 = param.getInt("seat"); Integer seat1 = param.getInt("seat");
System.out.println("跑的快seat1 77777777777777777777777777" + seat1); System.out.println("跑的快seat1 77777777777777777777777777" + seat1);
System.out.println("跑的快palyer6666666666666666666666666" + player); 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) { if (seat1 != null) {
huNanPaoDeKuai.seat = seat1; huNanPaoDeKuai.seat = seat1;
System.out.println("更新seat为: " + seat1);
} }
//出牌 //出牌
System.out.println("准备调用outCard方法");
huNanPaoDeKuai.outCard(client); huNanPaoDeKuai.outCard(client);
//处理完协议后保存到Redis //处理完协议后保存到Redis
HuNanPaoDeKuai currentInstance = huNanPaoDeKuaiInstances.get(connecId); HuNanPaoDeKuai currentInstance = huNanPaoDeKuaiInstances.get(connecId);
currentInstance.saveToRedis(connecId); currentInstance.saveToRedis(connecId);
System.out.println("2004协议处理完成");
} }
//结算准备 //结算准备
else if ("2007".equalsIgnoreCase(command)) { 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;
}
} }

View File

@ -139,6 +139,8 @@ public class HuNanPaoDeKuai {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
seat = 0; seat = 0;
} }
} else {
seat = 0;
} }
if (stateMap.containsKey("guangboseat")) { if (stateMap.containsKey("guangboseat")) {
@ -147,6 +149,8 @@ public class HuNanPaoDeKuai {
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
guangboseat = 0; guangboseat = 0;
} }
} else {
guangboseat = 0;
} }
if (stateMap.containsKey("remain")) { if (stateMap.containsKey("remain")) {
@ -250,9 +254,22 @@ public class HuNanPaoDeKuai {
*/ */
public String outCard(TaurusClient client) { public String outCard(TaurusClient client) {
try { 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 itArray = null;
itArray = test.intelligentPaoDeKuaiOutCard(this, paoDekuaiCardInhand, card_list, seatRemainHistory); itArray = test.intelligentPaoDeKuaiOutCard(this, paoDekuaiCardInhand, card_list, seatRemainHistory);
System.out.println("itArray-----" + itArray); System.out.println("itArray-----" + itArray);
//无法跟牌且不是下家只剩一张牌的情况 则pass
if (itArray == null && remain != 1) {
System.out.println("无法跟牌选择pass");
return null;
}
if (remain == 1 && itArray == null) { //如果玩家的下家只有一张牌,玩家出单张必须是最大的 if (remain == 1 && itArray == null) { //如果玩家的下家只有一张牌,玩家出单张必须是最大的
CardObj maxSingleCard = CardUtil.findMaxSingleCard(paoDekuaiCardInhand); CardObj maxSingleCard = CardUtil.findMaxSingleCard(paoDekuaiCardInhand);
System.out.println("出的最大牌 " + maxSingleCard); System.out.println("出的最大牌 " + maxSingleCard);
@ -277,8 +294,10 @@ public class HuNanPaoDeKuai {
System.out.println("出牌成功: " + response.returnCode); System.out.println("出牌成功: " + response.returnCode);
}); });
}catch (Exception e) { }catch (Exception e) {
System.err.println("出牌异常: " + e.getMessage());
e.printStackTrace();
} }
System.out.println("=== outCard方法执行结束 ===");
return null; return null;
} }
@ -315,24 +334,29 @@ public class HuNanPaoDeKuai {
System.out.println("seat字段: " + param.getInt("seat")); System.out.println("seat字段: " + param.getInt("seat"));
System.out.println("player字段: " + param.getInt("player")); System.out.println("player字段: " + param.getInt("player"));
card_list = param.getTObject("card_obj"); //正确获取座位号
Integer broadcastSeat = param.getInt("seat");//从seat字段获取座位号
// 修复:正确获取座位号 if (broadcastSeat != null) {
Integer seat = param.getInt("seat"); // 从seat字段获取座位号 guangboseat = broadcastSeat;
if (seat != null) {
guangboseat = seat;
System.out.println("使用seat字段设置guangboseat: " + guangboseat); System.out.println("使用seat字段设置guangboseat: " + guangboseat);
} else { } else {
// 兼容旧版本:尝试从player字段获取座位号 //从player字段获取座位号
guangboseat = param.getInt("player"); guangboseat = param.getInt("player");
System.out.println("使用player字段设置guangboseat: " + guangboseat); 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"); //剩余牌数量 remain = param.getInt("remain"); //剩余牌数量
saveRemainHistory(guangboseat, remain); saveRemainHistory(guangboseat, remain);
System.out.println("座位号和手牌数量记录 " + seatRemainHistory); System.out.println("座位号和手牌数量记录 " + seatRemainHistory);
System.out.println("广播手牌数量" + "座位号 " + guangboseat + "手牌数量 " + remain); System.out.println("广播手牌数量" + "座位号 " + guangboseat + "手牌数量 " + remain);
System.out.println("座位号:" + guangboseat + "的用户出牌广播:" + param.getTObject("card_obj")); System.out.println("座位号:" + guangboseat + "的用户出牌广播:" + card_list);
System.out.println("======================="); System.out.println("=======================");
return null; return null;
} }

View File

@ -38,6 +38,10 @@ public class test {
if (huNanPaoDeKuai.seat == huNanPaoDeKuai.guangboseat) { if (huNanPaoDeKuai.seat == huNanPaoDeKuai.guangboseat) {
System.out.println("=== 座位号匹配错误检测 ===");
System.out.println("seat=" + huNanPaoDeKuai.seat + ", guangboseat=" + huNanPaoDeKuai.guangboseat);
System.out.println("检测到seat与guangboseat相同可能存在状态同步问题");
Map<Integer, Integer> otherSeatsLastRemain = getOtherSeatsLastRemain(seat, seatRemainHistory); Map<Integer, Integer> otherSeatsLastRemain = getOtherSeatsLastRemain(seat, seatRemainHistory);
boolean hasAnyEqualOne = otherSeatsLastRemain.values().stream().anyMatch(value -> value == 1); boolean hasAnyEqualOne = otherSeatsLastRemain.values().stream().anyMatch(value -> value == 1);
if (hasAnyEqualOne) { if (hasAnyEqualOne) {
@ -59,6 +63,7 @@ public class test {
System.out.println("广播座位号 9999999999 " + huNanPaoDeKuai.guangboseat); System.out.println("广播座位号 9999999999 " + huNanPaoDeKuai.guangboseat);
//座位号出问题了 //座位号出问题了
System.out.println("座位号不匹配,切换到首轮出牌逻辑");
playCards = intelligentFirstPlayWithRemainConsideration(huNanPaoDeKuai, sortedHand, seatRemainHistory); playCards = intelligentFirstPlayWithRemainConsideration(huNanPaoDeKuai, sortedHand, seatRemainHistory);
return CardUtil.toTArray(playCards); return CardUtil.toTArray(playCards);
} }
@ -73,6 +78,11 @@ public class test {
return CardUtil.toTArray1(maxSingleCard); return CardUtil.toTArray1(maxSingleCard);
} else { } else {
playCards = intelligentResponsePlayWithRemainConsideration(sortedHand, minCard, len, opponentCards, type); playCards = intelligentResponsePlayWithRemainConsideration(sortedHand, minCard, len, opponentCards, type);
//如果返回空列表
if (playCards == null || playCards.isEmpty()) {
System.out.println("无法跟牌执行pass操作");
return null;
}
return CardUtil.toTArray(playCards); return CardUtil.toTArray(playCards);
} }
} }
@ -2328,9 +2338,12 @@ public class test {
*/ */
private static PlayOptionWithRemain selectBestOptionWithRemain(List<PlayOptionWithRemain> options, private static PlayOptionWithRemain selectBestOptionWithRemain(List<PlayOptionWithRemain> options,
int handSize, boolean isFirstPlay) { int handSize, boolean isFirstPlay) {
if (options == null || options.isEmpty()) {
return null;
}
return options.stream() return options.stream()
.max(Comparator.comparingInt(option -> option.totalScore)) .max(Comparator.comparingInt(option -> option.totalScore))
.orElse(options.isEmpty() ? null : options.get(0)); .orElse(null);
} }
/** /**