修改已知问题

master
zhouwei 2026-02-05 19:46:57 +08:00
parent 1c5d453fb6
commit 5a3ab7efda
4 changed files with 200 additions and 353 deletions

View File

@ -16,6 +16,8 @@ import taurus.client.TaurusClient;
import taurus.client.business.GroupRoomBusiness; import taurus.client.business.GroupRoomBusiness;
import taurus.util.ROBOTEventType; import taurus.util.ROBOTEventType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -156,7 +158,7 @@ public class EXGameController extends GameController {
client.send(Config.JOIN_ROOM_CS, params, response -> { client.send(Config.JOIN_ROOM_CS, params, response -> {
System.out.println("joinRoomController: " + response); System.out.println("joinRoomController: " + response);
RobotUser roomInfo = new RobotUser(); RobotUser roomInfo = new RobotUser();
roomInfo.setRoomId(roomId); roomInfo.setCurrentRoomId(Integer.parseInt(roomId));
roomInfo.setConnecId(connecId); roomInfo.setConnecId(connecId);
roomInfo.setUserId(0); roomInfo.setUserId(0);
//机器人房间映射关系 //机器人房间映射关系
@ -214,69 +216,52 @@ public class EXGameController extends GameController {
} }
} }
/**
* web_group
*/
@ActionKey(value = "225", validate = GameInterceptor.NOT_PLAYER) @ActionKey(value = "225", validate = GameInterceptor.NOT_PLAYER)
public void webGroup(Session session, ITObject params, int gid) { public void webGroup(Session session, ITObject params, int gid) {
int robotId = params.getInt("robotid"); int robotId = params.getInt("robotid");
String roomId = params.getString("roomid"); String roomId = params.getString("roomid");
int groupId = params.getInt("groupid"); int groupId = params.getInt("groupid");
try (Jedis jedis0 = Redis.use("group1_db0").getJedis();Jedis jedis2 = Redis.use("group1_db2").getJedis()){
Set<String> robotTokens = jedis0.smembers("{user}:"+robotId+"_token");
String robotSession = null;
for (String token : robotTokens) { //防止玩家操作同一房间 导致其他机器人加入
if (jedis0.exists(token)) { List<RobotUser> robotUsers = getRobotUsersByRoomId(Integer.parseInt(roomId));
robotSession = token; if (!robotUsers.isEmpty()) {
break; synchronized (robotUsers) {
RobotUser robotUser = robotUsers.get(0);
if (robotId != Integer.parseInt(robotUser.getRobotId())) {
System.err.println("房间{"+ roomId +"}中已有机器人{"+robotUser.getRobotId()+"},当前机器人{"+robotId+"}不执行加入逻辑");
return;
} }
} }
System.out.println("进房间: "+robotId);
System.out.println("进房间: "+roomId);
System.out.println("进房间: "+"room:"+ roomId);
System.out.println("进房间: "+"{user}:"+robotId);
TaurusClient client = getCsMjGameServerConnection(roomId+"_"+robotId);
GroupRoomBusiness.joinRoom(groupId, "room:"+ roomId, "{user}:"+robotId, null);
//机器人房间映射关系
RobotUser robotUser = getRobotRoomInfo(String.valueOf(robotId));
if (robotUser.getCurrentRoomId() == 0) {
robotUser.setCurrentRoomId(Integer.parseInt(roomId));
robotUser.setClient(client);
robotUser.setConnecId(roomId+ "_"+ robotId);
}
robotRoomMapping.put(robotUser.getConnecId(), robotUser);
robotRoomMapping.remove(robotUser.getRobotId());
System.out.println(roomId);
System.out.println(Integer.parseInt(roomId));
System.out.println(robotUser.getCurrentRoomId());
Thread.sleep(2000);
params.putString("session","{user}:"+robotId+ "," + robotSession);
//发送加入房间请求到game_mj_cs
client.send(Config.JOIN_ROOM_CS, params, response -> {
System.out.println("joinRoomController: " + response);
robotConnectionManager.reconnectToGameServer(response, robotUser, client);
});
Thread.sleep(1000);
if(client.isConnected()){
client.send(Config.GAME_READY_CS, params, response -> {
System.out.println("1003:"+response);
});
jedis2.hset("gallrobot", String.valueOf(robotUser.getRobotId()), "1");
robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_READY);
robotConnectionManager.setSessionAndToken("{user}:"+robotId, robotSession, robotUser.getConnecId());
}
robotUser.setIntoRoomTime(robotConnectionManager.getTime());
} catch (Exception e) {
throw new RuntimeException(e);
} }
System.err.println("225开始进房间: "+"room:"+ roomId +"robot:"+robotId);
//加入房间
joinRoomCommon(robotId, roomId, groupId, params);
System.err.println("225已进入房间准备成功: "+"room:"+ roomId +"robot:"+robotId);
} }
/**
* web_group
*/
@ActionKey(value = "226", validate = GameInterceptor.NOT_PLAYER)
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);
//加入房间
joinRoomCommon(params.getInt("robotid"), params.getString("roomid"), params.getInt("groupid"), params);
System.err.println("226已进入房间准备成功: " + "room:" + roomId + "robot:" + robotId);
}
/**
* 线
* */
public void webGroupJoinRoom(RobotUser robotUser) { public void webGroupJoinRoom(RobotUser robotUser) {
String connecId = robotUser.getConnecId(); String connecId = robotUser.getConnecId();
TaurusClient client = getCsMjGameServerConnection(connecId);
//重启检查
try (Jedis jedis0 = Redis.use("group1_db0").getJedis();Jedis jedis2 = Redis.use("group1_db2").getJedis()){ try (Jedis jedis0 = Redis.use("group1_db0").getJedis();Jedis jedis2 = Redis.use("group1_db2").getJedis()){
Set<String> robotTokens = jedis0.smembers("{user}:"+robotUser.getRobotId()+"_token"); Set<String> robotTokens = jedis0.smembers("{user}:"+robotUser.getRobotId()+"_token");
String robotSession = null; String robotSession = null;
@ -292,40 +277,70 @@ public class EXGameController extends GameController {
robotRoomMapping.remove(connecId); robotRoomMapping.remove(connecId);
return; return;
} }
System.out.println("重连进房间: "+robotUser.getRobotId());
System.out.println("重连进房间: "+robotUser.getCurrentRoomId()); System.err.println("重启后开始进房间: " + "room:" + robotUser.getCurrentRoomId() + "robot:" + robotUser.getRobotId());
System.out.println("重连进房间: "+"room:"+ robotUser.getCurrentRoomId()); ITObject params = new TObject();
System.out.println("重连进房间: "+"{user}:"+robotUser.getRobotId()); params.putString("session", "{user}:" + robotUser.getRobotId() + "," + robotSession);
GroupRoomBusiness.joinRoom(Integer.parseInt(robotUser.getRobotGroupid()), "room:"+ robotUser.getCurrentRoomId(), "{user}:"+robotUser.getRobotId(), null); //加入房间
joinRoomCommon(Integer.parseInt(robotUser.getRobotId()), String.valueOf(robotUser.getCurrentRoomId()), Integer.parseInt(robotUser.getRobotGroupid()), params);
System.err.println("重启后已进入房间准备成功: " + "room:" + robotUser.getCurrentRoomId() + "robot:" + robotUser.getRobotId());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
*
*/
private void joinRoomCommon(int robotId, String roomId, int groupId, ITObject params) {
try (Jedis jedis0 = Redis.use("group1_db0").getJedis();Jedis jedis2 = Redis.use("group1_db2").getJedis()) {
Set<String> robotTokens = jedis0.smembers("{user}:" + robotId + "_token");
String robotSession = null;
for (String token : robotTokens) {
if (jedis0.exists(token)) {
robotSession = token;
break;
}
}
System.err.println("开始进房间: room:" + roomId);
System.err.println("开始进房间: {user}:" + robotId);
TaurusClient client = getCsMjGameServerConnection(roomId + "_" + robotId);
GroupRoomBusiness.joinRoom(groupId, "room:" + roomId, "{user}:" + robotId, null);
//机器人房间映射关系 //机器人房间映射关系
if (robotUser.getRoomId() == null) { RobotUser robotUser = getRobotRoomInfo(String.valueOf(robotId));
robotUser.setCurrentRoomId(robotUser.getCurrentRoomId()); String connecId = roomId + "_" + robotId;
if (robotUser.getCurrentRoomId() == 0) {
robotUser.setCurrentRoomId(Integer.parseInt(roomId));
robotUser.setClient(client); robotUser.setClient(client);
robotUser.setConnecId(connecId);
} }
robotRoomMapping.put(robotUser.getConnecId(), robotUser); robotRoomMapping.put(robotUser.getConnecId(), robotUser);
robotRoomMapping.remove(robotUser.getRobotId()); robotRoomMapping.remove(robotUser.getRobotId());
Thread.sleep(2000); Thread.sleep(2000);
ITObject params = new TObject(); params.putString("session", "{user}:" + robotId + "," + robotSession);
params.putString("session","{user}:"+robotUser.getRobotId()+ "," + robotSession);
//发送加入房间请求到game_mj_cs //发送加入房间请求到game_mj_cs
client.send(Config.JOIN_ROOM_CS, params, response -> { client.send(Config.JOIN_ROOM_CS, params, response -> {
System.out.println("joinRoomController: " + response);
robotConnectionManager.reconnectToGameServer(response, robotUser, client); robotConnectionManager.reconnectToGameServer(response, robotUser, client);
}); });
System.err.println("已进入房间成功: " + robotUser.getConnecId());
Thread.sleep(1000); Thread.sleep(1000);
if (client.isConnected()) { if (client.isConnected()) {
client.send(Config.GAME_READY_CS, params, response -> { client.send(Config.GAME_READY_CS, params, response -> {
System.out.println("1003:" + response); System.out.println("1003:" + response);
}); });
jedis2.hset("gallrobot", String.valueOf(robotUser.getRobotId()), "1"); jedis2.hset("gallrobot", String.valueOf(robotUser.getRobotId()), "1");
robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_READY); robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_READY);
robotConnectionManager.setSessionAndToken("{user}:" + robotId, robotSession, robotUser.getConnecId());
} }
robotUser.setIntoRoomTime(robotConnectionManager.getTime()); robotUser.setIntoRoomTime(robotConnectionManager.getTime());
System.err.println("已进入房间准备成功: " + robotUser.getConnecId());
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -349,6 +364,21 @@ public class EXGameController extends GameController {
return robotRoomMapping.get(robotId); return robotRoomMapping.get(robotId);
} }
/**
* IDRobotUser
*/
public List<RobotUser> getRobotUsersByRoomId(int roomId) {
String prefix = roomId + "_";
List<RobotUser> result = new ArrayList<>();
for (Map.Entry<String, RobotUser> entry : robotRoomMapping.entrySet()) {
if (entry.getKey().startsWith(prefix)) {
result.add(entry.getValue());
}
}
return result;
}
/** /**
* ID * ID
*/ */

View File

@ -59,10 +59,6 @@ public class EXMainServer extends MainServer{
robotRoomMapping.put(entry.getKey(), robotUser); robotRoomMapping.put(entry.getKey(), robotUser);
} }
TPServer.me().getTimerPool().scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
for(Map.Entry<String, RobotUser> entry : robotRoomMapping.entrySet()) { for(Map.Entry<String, RobotUser> entry : robotRoomMapping.entrySet()) {
RobotUser robotUser = entry.getValue(); RobotUser robotUser = entry.getValue();
//1、登录 //1、登录
@ -70,67 +66,54 @@ public class EXMainServer extends MainServer{
if(!robotUser.isLogin){ if(!robotUser.isLogin){
robotConnectionManager.login(robotUser); robotConnectionManager.login(robotUser);
} }
/*//2、链接
//判断是否链接
System.out.println("robotUser.isconnect"+robotUser.getIsconnect());
if(!robotUser.getIsconnect()){
robotConnectionManager.connectGame(robotUser);
}else{
robotConnectionManager.renconnect(robotUser);
} }
// TPServer.me().getTimerPool().scheduleAtFixedRate(new Runnable() {
//4、加入房间 // @Override
if(robotUser.getStatus()==0){ // public void run() {
//没状态时候进入加入房间 //
ITObject params = new TObject(); // for(Map.Entry<String, RobotUser> entry : robotRoomMapping.entrySet()) {
params.putString("connecId", robotUser.getRoomId()+"_"+robotUser.getRobotId()); // RobotUser robotUser = entry.getValue();
params.putString("roomId", robotUser.getRoomId()); // //1、登录
params.putString("groupId", robotUser.getRobotGroupid()); // //判断是否登录
params.putString("session", "{user}:"+robotUser.getRobotId()); // if(!robotUser.isLogin){
exGameController.joinRoom(null, params, robotUser.getClient().getId()); // robotConnectionManager.login(robotUser);
} // }
System.out.println("robotUser.getIntoRoomTime()"+robotUser.getIntoRoomTime()); // /*//2、链接
System.out.println("robGetTiem"+robotConnectionManager.getTime()); // //判断是否链接
if(robotUser.getIntoRoomTime()+6<=robotConnectionManager.getTime()){ // System.out.println("robotUser.isconnect"+robotUser.getIsconnect());
//5、退出房间 // if(!robotUser.getIsconnect()){
robotConnectionManager.outoRoom(robotUser); // robotConnectionManager.connectGame(robotUser);
}*/ // }else{
// robotConnectionManager.renconnect(robotUser);
} // }
} //
}, 0, 5 ,TimeUnit.SECONDS); //
// //4、加入房间
// if(robotUser.getStatus()==0){
// //没状态时候进入加入房间
// ITObject params = new TObject();
// params.putString("connecId", robotUser.getRoomId()+"_"+robotUser.getRobotId());
// params.putString("roomId", robotUser.getRoomId());
// params.putString("groupId", robotUser.getRobotGroupid());
// params.putString("session", "{user}:"+robotUser.getRobotId());
// exGameController.joinRoom(null, params, robotUser.getClient().getId());
// }
// System.out.println("robotUser.getIntoRoomTime()"+robotUser.getIntoRoomTime());
// System.out.println("robGetTiem"+robotConnectionManager.getTime());
// if(robotUser.getIntoRoomTime()+6<=robotConnectionManager.getTime()){
// //5、退出房间
// robotConnectionManager.outoRoom(robotUser);
// }*/
//
// }
// }
// }, 0, 5 ,TimeUnit.SECONDS);
//5、干活 //5、干活
log.info("长沙麻将机器人服务器已启动"); log.info("长沙麻将机器人服务器已启动");
log.info("服务器将监听端口 {} 用于接收robot_mgr管理协议", gameSetting.port); log.info("服务器将监听端口 {} 用于接收robot_mgr管理协议", gameSetting.port);
} }
/**
*
*/
private void startConnectionCheckScheduler() {
Thread connectionCheckThread = new Thread(() -> {
while (connectionCheckRunning) {
try {
for(Map.Entry<String, RobotUser> entry : robotRoomMapping.entrySet()) {
RobotUser robotUser = entry.getValue();
robotConnectionManager.checkIsConnect(robotUser);
}
Thread.sleep(8000);
} catch (InterruptedException e) {
break;
} catch (Exception e) {
log.error("连接检查线程异常", e);
}
}
}, "Connection-Check-Thread");
connectionCheckThread.setDaemon(true);
connectionCheckThread.start();
}
/** /**
* 线 * 线
*/ */

View File

@ -47,26 +47,19 @@ public class RobotConnectionManager {
private Map<Integer, List<Integer>> getPlayerOutcardsMap(String connecId) { private Map<Integer, List<Integer>> getPlayerOutcardsMap(String connecId) {
return playerOutcardsMapByConn.computeIfAbsent(connecId, k -> new ConcurrentHashMap<>()); return playerOutcardsMapByConn.computeIfAbsent(connecId, k -> new ConcurrentHashMap<>());
} }
private Map<Integer, List<Integer>> getPlayerchisMap(String connecId) { private Map<Integer, List<Integer>> getPlayerchisMap(String connecId) {
return playerchisMapByConn.computeIfAbsent(connecId, k -> new ConcurrentHashMap<>()); return playerchisMapByConn.computeIfAbsent(connecId, k -> new ConcurrentHashMap<>());
} }
private Map<Integer, List<Integer>> getPlayerpengsMap(String connecId) { private Map<Integer, List<Integer>> getPlayerpengsMap(String connecId) {
return playerpengsMapByConn.computeIfAbsent(connecId, k -> new ConcurrentHashMap<>()); return playerpengsMapByConn.computeIfAbsent(connecId, k -> new ConcurrentHashMap<>());
} }
private Map<Integer, List<Integer>> getPlayermingsMap(String connecId) { private Map<Integer, List<Integer>> getPlayermingsMap(String connecId) {
return playermingsMapByConn.computeIfAbsent(connecId, k -> new ConcurrentHashMap<>()); return playermingsMapByConn.computeIfAbsent(connecId, k -> new ConcurrentHashMap<>());
} }
private Map<Integer, List<Integer>> getPlayerzisMap(String connecId) { private Map<Integer, List<Integer>> getPlayerzisMap(String connecId) {
return playerzisMapByConn.computeIfAbsent(connecId, k -> new ConcurrentHashMap<>()); return playerzisMapByConn.computeIfAbsent(connecId, k -> new ConcurrentHashMap<>());
} }
private int groupId = 0;
private int pid = 0; private int pid = 0;
private int playerId = 0;
private String playKey = "";
private Map<Integer, Integer> count = new HashMap<Integer, Integer>(); private Map<Integer, Integer> count = new HashMap<Integer, Integer>();
/*长沙麻将游戏算法相关 end*/ /*长沙麻将游戏算法相关 end*/
@ -178,25 +171,10 @@ public class RobotConnectionManager {
} }
} }
/**
* leftover_robot 退
*/
private void updateLeftoverRobot(int robotId) {
try (Jedis jedis2 = Redis.use("group1_db2").getJedis()) {
jedis2.hset("gallrobot", String.valueOf(robotId), "0");
jedis2.hset("{grobot}:" + robotId, "start", "0");
System.out.println("机器人 {"+robotId+"} 退出房间修改gallrobot为0");
}
}
/** /**
* *
*/ */
public void setupEventListeners(TaurusClient client, String connecId) { public void setupEventListeners(TaurusClient client, String connecId) {
//添加消息事件监听器 //添加消息事件监听器
IEventListener messageListener = new IEventListener() { IEventListener messageListener = new IEventListener() {
@Override @Override
@ -287,11 +265,14 @@ public class RobotConnectionManager {
//同步手牌 //同步手牌
HuNanChangSha currentInstance = getHuNanChangShaInstance(connecId); HuNanChangSha currentInstance = getHuNanChangShaInstance(connecId);
//检查Redis恢复的数据 避免覆盖 //同步逻辑比较手牌数量
if (currentInstance.getChangShaCardInhand().isEmpty()) { List<Integer> currentHand = currentInstance.getChangShaCardInhand();
//手牌集合为空 需要同步数据 if (currentHand.isEmpty() || hcard.size() > currentHand.size()) {
//手牌集合为空 或者 玩家出牌了
currentInstance.updateHandCard(hcard); currentInstance.updateHandCard(hcard);
System.out.println("断线重连:同步手牌数据"); System.out.println("断线重连:同步手牌数据,服务器手牌:" + hcard);
} else {
System.out.println("断线重连使用Redis恢复的手牌数据数量:" + currentHand.size());
} }
if(outcard_list.size()>0){ if(outcard_list.size()>0){
@ -301,9 +282,12 @@ public class RobotConnectionManager {
} }
//检查出牌记录是否需要同步 //检查出牌记录是否需要同步
if (currentInstance.getChuGuoCardInhand().isEmpty()) { List<Integer> currentOutCards = currentInstance.getChuGuoCardInhand();
if (currentOutCards.isEmpty() || outcards.size() > currentOutCards.size()) {
currentInstance.updateOutCard(outcards); currentInstance.updateOutCard(outcards);
System.out.println("断线重连:同步出牌数据"); System.out.println("断线重连:同步出牌数据,服务器出牌:" + outcards);
} else {
System.out.println("断线重连使用Redis恢复的出牌数据数量:" + currentOutCards.size());
} }
} }
@ -336,14 +320,10 @@ public class RobotConnectionManager {
HuNanChangSha huNanChangSha = getHuNanChangShaInstance(connecId); HuNanChangSha huNanChangSha = getHuNanChangShaInstance(connecId);
try (Jedis jedis0 = Redis.use().getJedis();Jedis jedis2 = Redis.use("group1_db2").getJedis();){ try (Jedis jedis0 = Redis.use().getJedis();Jedis jedis2 = Redis.use("group1_db2").getJedis();){
//长沙麻将 机器人处理事件 //长沙麻将 机器人处理事件
//[TCP->822] data:{"tip_list":[{"type":8,"id":1,"opcard":[],"weight":8,"card":0}],"types":[{"type":21,"value":1}]}
//板胡Event [TCP->823] data:{"type":8,"seat":1,"data":[{"opcard":[204,204,204,108,108,108],"type":21,"value":1}]}
//初始化收手牌 //初始化收手牌
if ("811".equalsIgnoreCase(command)) { if ("811".equalsIgnoreCase(command)) {
robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_WORKING); robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_WORKING);
//初始化收手牌 //初始化收手牌
// System.out.println("初始化手牌" + session);
String key = robotId+""; String key = robotId+"";
log.info("key+++++++++++++++++++++++++++++++++++" + key); log.info("key+++++++++++++++++++++++++++++++++++" + key);
if (jedis2.hget("{robortInfo}:" + key, "circleId") != null && jedis2.hget("{robortInfo}:" + key, "pid") != null) { if (jedis2.hget("{robortInfo}:" + key, "circleId") != null && jedis2.hget("{robortInfo}:" + key, "pid") != null) {
@ -364,7 +344,6 @@ public class RobotConnectionManager {
} }
//出牌广播 //出牌广播
else if ("812".equalsIgnoreCase(command)) { else if ("812".equalsIgnoreCase(command)) {
//{"opzicards":[{"opzicards":[],"playerId":101555}],"opmingcards":[{"opmingcards":[],"playerId":101555}],"outcard_map":[{"outcards":[209,205],"playerId":101555},{"outcards":[],"playerId":112233}],"card":205,"opchicards":[{"opchicards":[105,103],"playerId":101555}],"oppengcards":[{"oppengcards":[],"playerId":101555}],"seat":1}
ITArray outcard_map = param.getTArray("outcard_map"); ITArray outcard_map = param.getTArray("outcard_map");
ITArray opchicards = param.getTArray("opchicards"); ITArray opchicards = param.getTArray("opchicards");
ITArray oppengcards = param.getTArray("oppengcards"); ITArray oppengcards = param.getTArray("oppengcards");
@ -477,7 +456,7 @@ public class RobotConnectionManager {
} }
//出牌提示 //出牌提示
else if ("813".equalsIgnoreCase(command)) { else if ("813".equalsIgnoreCase(command)) {
// 获取当前连接专用的Maps //获取当前连接的Maps
Map<Integer, List<Integer>> currentPlayerOutcardsMap = getPlayerOutcardsMap(connecId); Map<Integer, List<Integer>> currentPlayerOutcardsMap = getPlayerOutcardsMap(connecId);
Map<Integer, List<Integer>> currentPlayerchisMap = getPlayerchisMap(connecId); Map<Integer, List<Integer>> currentPlayerchisMap = getPlayerchisMap(connecId);
Map<Integer, List<Integer>> currentPlayerpengsMap = getPlayerpengsMap(connecId); Map<Integer, List<Integer>> currentPlayerpengsMap = getPlayerpengsMap(connecId);
@ -561,37 +540,16 @@ public class RobotConnectionManager {
} }
}); });
} }
//2026.02.03修改 玩家解散房间 //2026.02.05修改 玩家解散房间
else if ("2005".equalsIgnoreCase(command)) { else if ("2005".equalsIgnoreCase(command)) {
CompletableFuture.runAsync(() -> {
ITArray playsList = param.getTArray("list");
for (int i = 0; i < playsList.size(); i++) {
ITObject playerData = playsList.getTObject(i);
Integer userId = playerData.getInt("aid");
if (userId.equals(robotId)) {
String roomKey = robotUser.getRoomId();
String gpid = jedis0.hget(roomKey, "gpid");
String gpId = jedis0.hget(roomKey, "group");
sleepTime(25000);
//更新机器人剩余数量
if (count != null && count.containsKey(Integer.parseInt(gpid))) {
Integer currentValue = count.get(Integer.parseInt(gpid));
if (currentValue > 0) {
count.put(Integer.parseInt(gpid), currentValue - 1);
}
}
EXGameController.removeRobotRoomInfo(String.valueOf(robotId)); EXGameController.removeRobotRoomInfo(String.valueOf(robotId));
//更新机器人剩余数量 //更新机器人剩余数量
updateLeftoverRobot(robotId); updateLeftoverRobot(robotId);
disconnectFromGameServer(connecId); disconnectFromGameServer(connecId);
System.out.println("2005玩家发送解散房间协议robotId: {"+robotId+"}"); System.out.println("2005玩家发送解散房间协议robotId: {"+robotId+"}");
} }
}
});
}
//2026.02.03修改 解散房间时候恢复机器人账号可以使用 //2026.02.03修改 解散房间时候恢复机器人账号可以使用
else if ("2008".equalsIgnoreCase(command)) { else if ("2008".equalsIgnoreCase(command)) {
updateLeftoverRobot(Integer.parseInt(robotUser.getRobotId())); updateLeftoverRobot(Integer.parseInt(robotUser.getRobotId()));
disconnectFromGameServer(connecId); disconnectFromGameServer(connecId);
} }
@ -602,7 +560,7 @@ public class RobotConnectionManager {
sleepTime(6000); sleepTime(6000);
if (robotUser != null) { if (robotUser != null) {
String roomKey = robotUser.getRoomId(); String roomKey = String.valueOf(robotUser.getCurrentRoomId());
//查询该房间的玩家信息 //查询该房间的玩家信息
String playersStr = jedis0.hget(roomKey, "players"); String playersStr = jedis0.hget(roomKey, "players");
@ -654,11 +612,6 @@ public class RobotConnectionManager {
huNanChangSha.getChuGuoPainum().clear(); huNanChangSha.getChuGuoPainum().clear();
Integer type = param.getInt("type"); Integer type = param.getInt("type");
if (type == 1 || type == 2) { //为1为大结算 为2为解散 if (type == 1 || type == 2) { //为1为大结算 为2为解散
Jedis jedis11s = Redis.use("group1_db11").getJedis();
try {
String key = "g{" + groupId + "}:play:" + pid;
// jedis11s.hincrBy(key, "leftover_robot", 1);
//
if (count != null && count.containsKey(pid)) { if (count != null && count.containsKey(pid)) {
Integer currentValue = count.get(pid); Integer currentValue = count.get(pid);
if (currentValue > 0) { if (currentValue > 0) {
@ -666,60 +619,23 @@ public class RobotConnectionManager {
} }
} }
//todo 可能要修改机器人状态 //更新机器人剩余数量
/*String sql = String.format("UPDATE `account` SET start = %d WHERE id = %d", 0, playerId); updateLeftoverRobot(Integer.parseInt(robotUser.getRobotId()));
try {
DataBase.use().executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}*/
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis11s.close();
} }
if (count != null && count.containsKey(pid)) {
Integer value = count.get(pid);
// 如果找到了对应的 pid
Jedis jedis12 = Redis.use("group1_db11").getJedis();
String shangxianRobot = jedis12.hget(playKey, "shangxian_robot");
String leftoverRobot = jedis12.hget(playKey, "leftover_robot");
/*if (shangxianRobot != null && leftoverRobot != null) {
if (value == 0) {
jedis12.hset(playKey, "leftover_robot", shangxianRobot);
}
}*/
jedis12.close();
}
}
// playerState.pongGroups.clear();;
// playerState.handCards.clear();
// playerState.chiGroups.clear();
// playerState.gangGroups.clear();;
sleepTime(1000); sleepTime(1000);
ITObject params = TObject.newInstance(); ITObject params = TObject.newInstance();
params.putString("session", client.getSession()); params.putString("session", client.getSession());
//todo session + "," + token
client.send("1003", params, new ICallback<MessageResponse>() { client.send("1003", params, new ICallback<MessageResponse>() {
@Override @Override
public void action(MessageResponse messageResponse) { public void action(MessageResponse messageResponse) {
} }
}); });
} }
//服务器通知客户端有玩家执行了操作 //服务器通知客户端有玩家执行了操作
else if ("815".equalsIgnoreCase(command)) { else if ("815".equalsIgnoreCase(command)) {
//[TCP->815] data:{"playerid":101555,"card":104,"opcard":[105,103],"from_seat":2,"type":1,"opengang":false}
huNanChangSha.shanchuchuguopai(param); huNanChangSha.shanchuchuguopai(param);
//处理完协议后保存到Redis //处理完协议后保存到Redis
HuNanChangSha currentInstance = huNanChangShaInstances.get(connecId); HuNanChangSha currentInstance = huNanChangShaInstances.get(connecId);
@ -742,11 +658,7 @@ public class RobotConnectionManager {
} }
else if ("822".equalsIgnoreCase(command)) { else if ("822".equalsIgnoreCase(command)) {
ITObject params = TObject.newInstance(); ITObject params = TObject.newInstance();
//params.putInt("qi", 0);
params.putInt("id", 1); params.putInt("id", 1);
//[TCP->822] data:{"tip_list":[{"type":8,"id":1,"opcard":[],"weight":8,"card":0}],"types":[{"type":21,"value":1}]}
//板胡Event [TCP->823] data:{"type":8,"seat":1,"data":[{"opcard":[204,204,204,108,108,108],"type":21,"value":1}]}
client.send("612", params, response -> { client.send("612", params, response -> {
}); });
@ -766,12 +678,17 @@ public class RobotConnectionManager {
} }
} }
public static void sleepTime(int time) { /**
try { * leftover_robot 退
// 添加延迟 */
Thread.sleep(time); private void updateLeftoverRobot(int robotId) {
} catch (InterruptedException e) { try (Jedis jedis2 = Redis.use("group1_db2").getJedis()) {
e.printStackTrace();
jedis2.hset("gallrobot", String.valueOf(robotId), "0");
jedis2.hset("{grobot}:" + robotId, "start", "0");
System.out.println("机器人 {"+robotId+"} 退出房间修改gallrobot为0");
} }
} }
@ -808,6 +725,7 @@ public class RobotConnectionManager {
connectGame(robotUser); connectGame(robotUser);
robotUser.setConnecId(robotUser.getCurrentRoomId()+"_"+robotUser.getRobotId()); robotUser.setConnecId(robotUser.getCurrentRoomId()+"_"+robotUser.getRobotId());
System.err.println("重启获取的机器人还有当前房间,准备加入: "+robotUser.getConnecId());
exGameController.webGroupJoinRoom(robotUser); exGameController.webGroupJoinRoom(robotUser);
} }
} }
@ -856,72 +774,6 @@ public class RobotConnectionManager {
} }
} }
public void checkIsConnect(RobotUser robotUser){
System.out.println("checkIsConnect:"+robotUser.getRobotId()+"==="+robotUser.getStatus());
System.out.println("checkIsConnectgetId:"+robotUser.getRobotId()+"==="+robotUser.getClient().getId());
System.out.println("checkIsConnectgetGameID:"+robotUser.getRobotId()+"==="+robotUser.getClient().getGameID());
if(robotUser.getIsLogin()&&robotUser.getClient()!=null){
System.out.println("connectid:"+robotUser.getClient().getId());
robotUser.setIsconnect(robotUser.getClient().isConnected());
}
}
/**
* 退
*/
public void outoRoom(RobotUser robotUser) {
//判断当前状态是否在工作
System.out.println("outoRoom:"+ROBOTEventType.ROBOT_INTOROOM_READY);
if (robotUser.getStatus() == ROBOTEventType.ROBOT_INTOROOM_READY) {
//判断房间是否有人
//room:197276
Jedis jedis0 = Redis.use("group1_db0").getJedis();
try {
System.out.println("getCurrentRoomId:"+robotUser.getCurrentRoomId());
if(robotUser.getCurrentRoomId()!=0){
System.out.println("getCurrentRoomId:"+ROBOTEventType.ROBOT_INTOROOM_READY);
String roomkey="room:"+robotUser.getCurrentRoomId();
List<String> room_list = jedis0.hmget(roomkey, "fake", "status", "round", "id", "times", "players", "create_time", "fake_existTime");
System.out.println("room_list:"+room_list);
String roomId = room_list.get(3);
if(room_list.get(5)==null){
ITObject params = TObject.newInstance();
params.putString("session", robotUser.getLoginsession() + "," + robotUser.getToken());
System.out.println("logoutroom:"+params);
if(robotUser.getClient().isConnected()){
robotUser.getClient().send("1005", params,response -> {
});
robotUser.setStatus(ROBOTEventType.ROBOT_UNUSE);
}
}
ITArray players = TArray.newFromJsonData(room_list.get(5));
System.out.println("players:"+players);
if(players.size()<2){
ITObject params = TObject.newInstance();
params.putString("session", robotUser.getLoginsession() + "," + robotUser.getToken());
System.out.println("logoutroom:"+params);
if(robotUser.getClient().isConnected()){
robotUser.getClient().send("1005", params,response -> {
});
robotUser.setStatus(ROBOTEventType.ROBOT_UNUSE);
}
}
}
}catch (Exception e){
}finally {
jedis0.close();
}
}
}
public int getTime(){
return Integer.parseInt((System.currentTimeMillis() + "").substring(0, 10));
}
/** /**
* *
*/ */
@ -935,7 +787,6 @@ public class RobotConnectionManager {
} }
} }
/** /**
* connecId * connecId
*/ */
@ -943,4 +794,18 @@ public class RobotConnectionManager {
return robotRoomMapping.get(connecId) != null ? robotRoomMapping.get(connecId).getClient() : null; return robotRoomMapping.get(connecId) != null ? robotRoomMapping.get(connecId).getClient() : null;
} }
public int getTime(){
return Integer.parseInt((System.currentTimeMillis() + "").substring(0, 10));
}
public static void sleepTime(int time) {
try {
//添加延迟
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }

View File

@ -6,11 +6,8 @@ import taurus.client.TaurusClient;
* *
*/ */
public class RobotUser { public class RobotUser {
private String roomId;
private String connecId; private String connecId;
private int userId; private int userId;
private String groupId;
private String wanfaId;
private String robotId; private String robotId;
private int seat; private int seat;
@ -140,14 +137,6 @@ public class RobotUser {
this.status = status; this.status = status;
} }
public String getRoomId() {
return roomId;
}
public void setRoomId(String roomId) {
this.roomId = roomId;
}
public String getConnecId() { public String getConnecId() {
return connecId; return connecId;
} }
@ -164,22 +153,6 @@ public class RobotUser {
this.userId = userId; this.userId = userId;
} }
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getWanfaId() {
return wanfaId;
}
public void setWanfaId(String wanfaId) {
this.wanfaId = wanfaId;
}
public String getRobotId() { public String getRobotId() {
return robotId; return robotId;
} }
@ -188,8 +161,4 @@ public class RobotUser {
this.robotId = robotId; this.robotId = robotId;
} }
@Override
public String toString() {
return "RobotRoomInfo{roomId='" + roomId + "', connecId='" + connecId + "'}";
}
} }