修改红中打包问题
parent
4ea9fea6f0
commit
f22e96c4d4
|
|
@ -25,6 +25,25 @@
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
<!-- -->
|
||||||
|
<!-- <build>-->
|
||||||
|
<!-- <finalName>robot</finalName>-->
|
||||||
|
<!-- <plugins>-->
|
||||||
|
<!-- <plugin>-->
|
||||||
|
<!-- <groupId>org.apache.maven.plugins</groupId>-->
|
||||||
|
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
|
||||||
|
<!-- <version>3.6.1</version>-->
|
||||||
|
<!-- <configuration>-->
|
||||||
|
<!-- <!– <compilerArgument>-parameters</compilerArgument> –>-->
|
||||||
|
<!-- <source>1.8</source>-->
|
||||||
|
<!-- <target>1.8</target>-->
|
||||||
|
<!-- <encoding>UTF-8</encoding>-->
|
||||||
|
<!-- </configuration>-->
|
||||||
|
<!-- </plugin>-->
|
||||||
|
|
||||||
|
<!-- </plugins>-->
|
||||||
|
<!-- </build>-->
|
||||||
|
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>robot</finalName>
|
<finalName>robot</finalName>
|
||||||
|
|
@ -41,7 +60,29 @@
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.4</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<transformers>
|
||||||
|
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<mainClass>robot.mj.EXMainServer</mainClass>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
||||||
|
|
@ -36,11 +36,11 @@ public class Config {
|
||||||
/** 游戏服务器主机地址 */
|
/** 游戏服务器主机地址 */
|
||||||
/*public static final String GAME_SERVER_HOST = "8.134.76.43";
|
/*public static final String GAME_SERVER_HOST = "8.134.76.43";
|
||||||
public static final String DEFAULT_GROUP_ID = "762479";*/
|
public static final String DEFAULT_GROUP_ID = "762479";*/
|
||||||
public static final String DEFAULT_GROUP_ID = "426149";
|
public static final String DEFAULT_GROUP_ID = "383709";
|
||||||
public static final String GAME_SERVER_HOST = "127.0.0.1";
|
public static final String GAME_SERVER_HOST = "8.163.97.101";
|
||||||
|
|
||||||
/** 游戏服务器端口 */
|
/** 游戏服务器端口 */
|
||||||
public static final String GAME_SERVER_PORT = "6421";
|
public static final String GAME_SERVER_PORT = "26421";
|
||||||
|
|
||||||
/** 默认密码 */
|
/** 默认密码 */
|
||||||
public static final String DEFAULT_PASSWORD = "123456";
|
public static final String DEFAULT_PASSWORD = "123456";
|
||||||
|
|
|
||||||
|
|
@ -416,6 +416,8 @@ public class EXGameController extends GameController {
|
||||||
//成功响应后才建立映射关系
|
//成功响应后才建立映射关系
|
||||||
synchronized (robotRoomMapping) {
|
synchronized (robotRoomMapping) {
|
||||||
robotRoomMapping.put(robotUser.getConnecId(), robotUser);
|
robotRoomMapping.put(robotUser.getConnecId(), robotUser);
|
||||||
|
robotConnectionManager.reconnectToGameServer(response, robotUser, client);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("机器人{"+robotId+"}已成功加入房间{"+roomId+"},建立映射关系");
|
log.info("机器人{"+robotId+"}已成功加入房间{"+roomId+"},建立映射关系");
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ import com.taurus.core.events.Event;
|
||||||
import com.taurus.core.events.IEventListener;
|
import com.taurus.core.events.IEventListener;
|
||||||
import com.taurus.core.plugin.redis.Redis;
|
import com.taurus.core.plugin.redis.Redis;
|
||||||
import com.taurus.core.util.ICallback;
|
import com.taurus.core.util.ICallback;
|
||||||
|
import com.taurus.core.util.Logger;
|
||||||
import com.taurus.core.util.StringUtil;
|
import com.taurus.core.util.StringUtil;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import robot.mj.business.AccountBusiness;
|
import robot.mj.business.AccountBusiness;
|
||||||
import robot.mj.handler.HuNanHongZhong;
|
import robot.mj.handler.HuNanHongZhong;
|
||||||
|
|
@ -20,6 +20,7 @@ import taurus.client.TaurusClient;
|
||||||
import taurus.client.SocketCode;
|
import taurus.client.SocketCode;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
import taurus.util.ROBOTEventType;
|
import taurus.util.ROBOTEventType;
|
||||||
|
import com.taurus.core.util.Logger;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
|
@ -31,8 +32,9 @@ import static robot.mj.thread.ThreadPoolConfig.scheduleDelay;
|
||||||
* 机器人连接管理器 - 管理与游戏服务器的连接
|
* 机器人连接管理器 - 管理与游戏服务器的连接
|
||||||
*/
|
*/
|
||||||
public class RobotConnectionManager {
|
public class RobotConnectionManager {
|
||||||
|
private static final Logger log = Logger.getLogger(RobotConnectionManager.class);
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(RobotConnectionManager.class);
|
// private static final Logger log = LoggerFactory.getLogger(RobotConnectionManager.class);
|
||||||
private static final Map<String, HuNanHongZhong> huNanHongZhongInstances = new ConcurrentHashMap<>();
|
private static final Map<String, HuNanHongZhong> huNanHongZhongInstances = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
//记录活跃连接 用于资源清理判断
|
//记录活跃连接 用于资源清理判断
|
||||||
|
|
@ -45,8 +47,8 @@ public class RobotConnectionManager {
|
||||||
private static final long MAX_CONNECTION_LIFETIME = 5 * 60 * 1000;
|
private static final long MAX_CONNECTION_LIFETIME = 5 * 60 * 1000;
|
||||||
private final EXGameController exGameController;
|
private final EXGameController exGameController;
|
||||||
|
|
||||||
private final String host= Config.GAME_SERVER_HOST;
|
private final String host = Config.GAME_SERVER_HOST;
|
||||||
private final int port= Integer.parseInt(Config.GAME_SERVER_PORT);
|
private final int port = Integer.parseInt(Config.GAME_SERVER_PORT);
|
||||||
|
|
||||||
public RobotConnectionManager() {
|
public RobotConnectionManager() {
|
||||||
exGameController = new EXGameController();
|
exGameController = new EXGameController();
|
||||||
|
|
@ -208,14 +210,14 @@ public class RobotConnectionManager {
|
||||||
ITObject param = message.param;
|
ITObject param = message.param;
|
||||||
//回调协议号
|
//回调协议号
|
||||||
String command = message.command;
|
String command = message.command;
|
||||||
|
log.info("收到游戏协议 " + command);
|
||||||
//根据玩法 ID 处理不同的回调
|
//根据玩法 ID 处理不同的回调
|
||||||
if (StringUtil.isNotEmpty(command)) {
|
if (StringUtil.isNotEmpty(command)) {
|
||||||
//直接处理协议
|
//直接处理协议
|
||||||
handleProtocol(command, message, client, connecId);
|
handleProtocol(command, message, client, connecId);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("处理游戏协议时发生异常:connecId: {}, command: {}", connecId, ((Message)event.getParameter("msg")).command, e);
|
log.error("处理游戏协议时发生异常:connecId: {}, command: {}", connecId, ((Message) event.getParameter("msg")).command, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -243,14 +245,14 @@ public class RobotConnectionManager {
|
||||||
* 机器人断线重连
|
* 机器人断线重连
|
||||||
*/
|
*/
|
||||||
public void reconnectToGameServer(MessageResponse response, RobotUser robotUser, TaurusClient client) {
|
public void reconnectToGameServer(MessageResponse response, RobotUser robotUser, TaurusClient client) {
|
||||||
String connecId = robotUser.getCurrentRoomId()+"_"+robotUser.getRobotId();
|
String connecId = robotUser.getCurrentRoomId() + "_" + robotUser.getRobotId();
|
||||||
//先设置机器人状态为准备状态
|
//先设置机器人状态为准备状态
|
||||||
robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_READY);
|
robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_READY);
|
||||||
|
|
||||||
if(client.isConnected()){
|
if (client.isConnected()) {
|
||||||
try {
|
try {
|
||||||
log.info(String.valueOf(response.messageData.param));
|
log.info(String.valueOf(response.messageData.param));
|
||||||
if (response.messageData.param==null) {
|
if (response.messageData.param == null) {
|
||||||
log.info("警告:reconnectToGameServer 重连时未获取到参数");
|
log.info("警告:reconnectToGameServer 重连时未获取到参数");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -363,10 +365,10 @@ public class RobotConnectionManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}finally {
|
} finally {
|
||||||
|
|
||||||
}
|
}
|
||||||
}else {
|
} else {
|
||||||
renconnect(robotUser);
|
renconnect(robotUser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -376,326 +378,321 @@ public class RobotConnectionManager {
|
||||||
*/
|
*/
|
||||||
private void handleProtocol(String command, Message message, TaurusClient client, String connecId) {
|
private void handleProtocol(String command, Message message, TaurusClient client, String connecId) {
|
||||||
RobotUser robotUser = robotRoomMapping.get(connecId);
|
RobotUser robotUser = robotRoomMapping.get(connecId);
|
||||||
|
log.info("connecId " + connecId);
|
||||||
|
|
||||||
//更新连接的最后访问时间
|
|
||||||
EXGameController.updateLastAccessTime(connecId);
|
|
||||||
|
|
||||||
if (robotUser == null) {
|
for (Map.Entry<String, RobotUser> entry : robotRoomMapping.entrySet()) {
|
||||||
log.info("未找到机器人用户信息,连接ID: " + connecId);
|
String key = entry.getKey();
|
||||||
return;
|
RobotUser user = entry.getValue();
|
||||||
|
log.info("Key" + key);
|
||||||
|
log.info("user seat" + user.getSeat());
|
||||||
|
log.info("user connecId" + user.getConnecId());
|
||||||
|
log.info("user robotId" + user.getRobotId());
|
||||||
|
|
||||||
|
log.info("robotRoomMapping " + robotRoomMapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
int robotId = Integer.parseInt(robotUser.getRobotId());
|
//更新连接的最后访问时间
|
||||||
ITObject param = message.param;
|
EXGameController.updateLastAccessTime(connecId);
|
||||||
HuNanHongZhong huNanHongZhong = getHuNanHongZhongInstance(connecId);
|
|
||||||
Jedis jedis0 = Redis.use().getJedis();
|
|
||||||
Jedis jedis2 = Redis.use("group1_db2").getJedis();
|
|
||||||
try {
|
|
||||||
//红中麻将 机器人处理事件
|
|
||||||
//初始化手牌
|
|
||||||
if ("811".equalsIgnoreCase(command)) {
|
|
||||||
robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_WORKING);
|
|
||||||
huNanHongZhong.cardInHead(command, message, client);
|
|
||||||
//处理完协议后保存到Redis
|
|
||||||
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
|
||||||
currentInstance.saveToRedis(connecId);
|
|
||||||
}
|
|
||||||
//出牌广播
|
|
||||||
else if ("812".equalsIgnoreCase(command)) {
|
|
||||||
huNanHongZhong.drawCard(command, message);
|
|
||||||
//处理完协议后保存到Redis
|
|
||||||
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
|
||||||
currentInstance.saveToRedis(connecId);
|
|
||||||
}
|
|
||||||
//摸牌
|
|
||||||
else if ("819".equalsIgnoreCase(command)) {
|
|
||||||
huNanHongZhong.getCard(command, message);
|
|
||||||
//处理完协议后保存到Redis
|
|
||||||
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
|
||||||
currentInstance.saveToRedis(connecId);
|
|
||||||
}
|
|
||||||
//出牌,牌权
|
|
||||||
else if ("813".equalsIgnoreCase(command)) {
|
|
||||||
huNanHongZhong.outCard(client);
|
|
||||||
//处理完协议后保存到Redis
|
|
||||||
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
|
||||||
currentInstance.saveToRedis(connecId);
|
|
||||||
}
|
|
||||||
//结算
|
|
||||||
else if ("817".equalsIgnoreCase(command)) {
|
|
||||||
huNanHongZhong.getHongZhongCardInhand().clear();
|
|
||||||
huNanHongZhong.getChuGuoCardInhand().clear();
|
|
||||||
log.info("红中结算");
|
|
||||||
Integer type = param.getInt("type");
|
|
||||||
if (type == 1 || type == 2) { //为1为大结算 为2为解散
|
|
||||||
//更新机器人剩余数量
|
|
||||||
updateLeftoverRobot(Integer.parseInt(robotUser.getRobotId()));
|
|
||||||
|
|
||||||
//游戏结束后主动断开连接
|
if (robotUser == null) {
|
||||||
disconnectFromGameServer(connecId);
|
log.info("未找到机器人用户信息,连接ID: " + connecId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int robotId = Integer.parseInt(robotUser.getRobotId());
|
||||||
|
ITObject param = message.param;
|
||||||
|
HuNanHongZhong huNanHongZhong = getHuNanHongZhongInstance(connecId);
|
||||||
|
Jedis jedis0 = Redis.use().getJedis();
|
||||||
|
Jedis jedis2 = Redis.use("group1_db2").getJedis();
|
||||||
|
try {
|
||||||
|
//红中麻将 机器人处理事件
|
||||||
|
//初始化手牌
|
||||||
|
if ("811".equalsIgnoreCase(command)) {
|
||||||
|
robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_WORKING);
|
||||||
|
huNanHongZhong.cardInHead(command, message, client);
|
||||||
|
//处理完协议后保存到Redis
|
||||||
|
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
||||||
|
currentInstance.saveToRedis(connecId);
|
||||||
}
|
}
|
||||||
ITObject params = TObject.newInstance();
|
//出牌广播
|
||||||
params.putString("session", client.getSession());
|
else if ("812".equalsIgnoreCase(command)) {
|
||||||
client.send("1003", params, new ICallback<MessageResponse>() {
|
huNanHongZhong.drawCard(command, message);
|
||||||
@Override
|
//处理完协议后保存到Redis
|
||||||
public void action(MessageResponse messageResponse) {
|
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
||||||
|
currentInstance.saveToRedis(connecId);
|
||||||
|
}
|
||||||
|
//摸牌
|
||||||
|
else if ("819".equalsIgnoreCase(command)) {
|
||||||
|
huNanHongZhong.getCard(command, message, robotUser);
|
||||||
|
//处理完协议后保存到Redis
|
||||||
|
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
||||||
|
currentInstance.saveToRedis(connecId);
|
||||||
|
}
|
||||||
|
//出牌,牌权
|
||||||
|
else if ("813".equalsIgnoreCase(command)) {
|
||||||
|
huNanHongZhong.outCard(client);
|
||||||
|
//处理完协议后保存到Redis
|
||||||
|
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
||||||
|
currentInstance.saveToRedis(connecId);
|
||||||
|
}
|
||||||
|
//结算
|
||||||
|
else if ("817".equalsIgnoreCase(command)) {
|
||||||
|
huNanHongZhong.getHongZhongCardInhand().clear();
|
||||||
|
huNanHongZhong.getChuGuoCardInhand().clear();
|
||||||
|
log.info("红中结算");
|
||||||
|
Integer type = param.getInt("type");
|
||||||
|
if (type == 1 || type == 2) { //为1为大结算 为2为解散
|
||||||
|
//更新机器人剩余数量
|
||||||
|
updateLeftoverRobot(Integer.parseInt(robotUser.getRobotId()));
|
||||||
|
|
||||||
|
//游戏结束后主动断开连接
|
||||||
|
disconnectFromGameServer(connecId);
|
||||||
}
|
}
|
||||||
});
|
ITObject params = TObject.newInstance();
|
||||||
}
|
params.putString("session", client.getSession());
|
||||||
//杠碰胡通知协议
|
client.send("1003", params, new ICallback<MessageResponse>() {
|
||||||
else if ("814".equalsIgnoreCase(command)) {
|
@Override
|
||||||
huNanHongZhong.actionCard(param, client);
|
public void action(MessageResponse messageResponse) {
|
||||||
//处理完协议后保存到Redis
|
|
||||||
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
|
||||||
currentInstance.saveToRedis(connecId);
|
|
||||||
} else if ("820".equalsIgnoreCase(command)) {
|
|
||||||
HuNanHongZhong.changePlayer(command, message);
|
|
||||||
//处理完协议后保存到Redis
|
|
||||||
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
|
||||||
currentInstance.saveToRedis(connecId);
|
|
||||||
}
|
|
||||||
//服务器通知客户端有玩家执行了操作
|
|
||||||
else if ("815".equalsIgnoreCase(command)) {
|
|
||||||
huNanHongZhong.shanchuchuguopai(param);
|
|
||||||
//处理完协议后保存到Redis
|
|
||||||
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
|
||||||
currentInstance.saveToRedis(connecId);
|
|
||||||
}
|
|
||||||
//玩家加入房间
|
|
||||||
else if ("2001".equalsIgnoreCase(command)) {
|
|
||||||
scheduleDelay(() -> {
|
|
||||||
Jedis jedis = Redis.use().getJedis();
|
|
||||||
try {
|
|
||||||
String roomKey = String.valueOf(robotUser.getCurrentRoomId());
|
|
||||||
|
|
||||||
//查询该房间的玩家信息
|
|
||||||
String playersStr = jedis.hget("room:"+roomKey, "players");
|
|
||||||
if (!playersStr.equals("[]")) {
|
|
||||||
String players = playersStr.substring(1, playersStr.length() - 1);
|
|
||||||
String[] playerIds = players.split(",");
|
|
||||||
|
|
||||||
//判断只有当前机器人一个玩家
|
|
||||||
if (playerIds.length == 1) {
|
|
||||||
int playerId = Integer.parseInt(playerIds[0].trim());
|
|
||||||
if (playerId == robotId) {
|
|
||||||
|
|
||||||
//发送退出房间协议
|
|
||||||
ITObject params = TObject.newInstance();
|
|
||||||
client.send("1005", params, response -> {
|
|
||||||
EXGameController.removeRobotRoomInfo(String.valueOf(robotId));
|
|
||||||
//更新机器人剩余数量
|
|
||||||
updateLeftoverRobot(robotId);
|
|
||||||
disconnectFromGameServer(connecId);
|
|
||||||
log.info("2002发送退出房间协议1005,robotId: {"+robotId+"}");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
});
|
||||||
log.error("处理玩家加入房间检查时发生异常", e);
|
}
|
||||||
} finally {
|
//杠碰胡通知协议
|
||||||
//确保Jedis连接关闭
|
else if ("814".equalsIgnoreCase(command)) {
|
||||||
if (jedis != null) {
|
huNanHongZhong.actionCard(param, client);
|
||||||
jedis.close();
|
//处理完协议后保存到Redis
|
||||||
}
|
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
||||||
}
|
currentInstance.saveToRedis(connecId);
|
||||||
}, 6, TimeUnit.SECONDS);
|
} else if ("820".equalsIgnoreCase(command)) {
|
||||||
log.info("玩家{"+ robotUser.getCurrentRoomId()+"}加入房间:"+ param);
|
HuNanHongZhong.changePlayer(command, message);
|
||||||
}
|
//处理完协议后保存到Redis
|
||||||
//玩家退出房间也要检查
|
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
||||||
else if ("2002".equalsIgnoreCase(command)) {
|
currentInstance.saveToRedis(connecId);
|
||||||
//直接使用定时任务替代Thread.sleep,避免嵌套异步调用
|
}
|
||||||
scheduleDelay(() -> {
|
//服务器通知客户端有玩家执行了操作
|
||||||
Jedis jedis = Redis.use().getJedis();
|
else if ("815".equalsIgnoreCase(command)) {
|
||||||
try {
|
huNanHongZhong.shanchuchuguopai(param);
|
||||||
String roomKey = String.valueOf(robotUser.getCurrentRoomId());
|
//处理完协议后保存到Redis
|
||||||
|
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
|
||||||
//查询该房间的玩家信息
|
currentInstance.saveToRedis(connecId);
|
||||||
String playersStr = jedis.hget("room:"+roomKey, "players");
|
}
|
||||||
if (!playersStr.equals("[]")) {
|
//玩家加入房间
|
||||||
String players = playersStr.substring(1, playersStr.length() - 1);
|
else if ("2001".equalsIgnoreCase(command)) {
|
||||||
String[] playerIds = players.split(",");
|
scheduleDelay(() -> {
|
||||||
|
Jedis jedis = Redis.use().getJedis();
|
||||||
//判断只有当前机器人一个玩家
|
try {
|
||||||
if (playerIds.length == 1) {
|
|
||||||
int playerId = Integer.parseInt(playerIds[0].trim());
|
|
||||||
if (playerId == robotId) {
|
|
||||||
|
|
||||||
//发送退出房间协议
|
|
||||||
ITObject params = TObject.newInstance();
|
|
||||||
client.send("1005", params, response -> {
|
|
||||||
EXGameController.removeRobotRoomInfo(String.valueOf(robotId));
|
|
||||||
//更新机器人剩余数量
|
|
||||||
updateLeftoverRobot(robotId);
|
|
||||||
disconnectFromGameServer(connecId);
|
|
||||||
log.info("2002发送退出房间协议1005,robotId: {"+robotId+"}");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("处理玩家退出房间检查时发生异常");
|
|
||||||
} finally {
|
|
||||||
if (jedis != null) {
|
|
||||||
jedis.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 6, TimeUnit.SECONDS);
|
|
||||||
}
|
|
||||||
//玩家解散房间
|
|
||||||
else if ("2005".equalsIgnoreCase(command)) {
|
|
||||||
EXGameController.removeRobotRoomInfo(String.valueOf(robotId));
|
|
||||||
//更新机器人剩余数量
|
|
||||||
updateLeftoverRobot(robotId);
|
|
||||||
disconnectFromGameServer(connecId);
|
|
||||||
log.info("2005玩家发送解散房间协议,robotId: {"+robotId+"}");
|
|
||||||
}
|
|
||||||
//解散房间时候恢复机器人账号可以使用
|
|
||||||
else if ("2008".equalsIgnoreCase(command)) {
|
|
||||||
updateLeftoverRobot(Integer.parseInt(robotUser.getRobotId()));
|
|
||||||
disconnectFromGameServer(connecId);
|
|
||||||
}
|
|
||||||
else if ("2009".equalsIgnoreCase(command)) {
|
|
||||||
scheduleDelay(() -> {
|
|
||||||
Jedis jedis = null;
|
|
||||||
try {
|
|
||||||
jedis = Redis.use().getJedis();
|
|
||||||
Integer paramRobotId = param.getInt("aid");
|
|
||||||
if (robotUser != null && paramRobotId != null) {
|
|
||||||
String roomKey = String.valueOf(robotUser.getCurrentRoomId());
|
String roomKey = String.valueOf(robotUser.getCurrentRoomId());
|
||||||
|
|
||||||
//查询该房间的玩家信息
|
//查询该房间的玩家信息
|
||||||
String playersStr = jedis.hget(roomKey, "players");
|
String playersStr = jedis.hget("room:" + roomKey, "players");
|
||||||
if (playersStr != null && !playersStr.equals("[]")) {
|
if (!playersStr.equals("[]")) {
|
||||||
String players = playersStr.substring(1, playersStr.length() - 1);
|
String players = playersStr.substring(1, playersStr.length() - 1);
|
||||||
String[] playerIds = players.split(",");
|
String[] playerIds = players.split(",");
|
||||||
|
|
||||||
//判断只有当前机器人一个玩家
|
//判断只有当前机器人一个玩家
|
||||||
if (playerIds.length == 1) {
|
if (playerIds.length == 1) {
|
||||||
int playerId = Integer.parseInt(playerIds[0].trim());
|
int playerId = Integer.parseInt(playerIds[0].trim());
|
||||||
if (playerId == paramRobotId) {
|
if (playerId == robotId) {
|
||||||
|
|
||||||
//发送退出房间协议
|
//发送退出房间协议
|
||||||
ITObject params = TObject.newInstance();
|
ITObject params = TObject.newInstance();
|
||||||
client.send("1005", params, response -> {
|
client.send("1005", params, response -> {
|
||||||
EXGameController.removeRobotRoomInfo(String.valueOf(paramRobotId));
|
EXGameController.removeRobotRoomInfo(String.valueOf(robotId));
|
||||||
//断开连接
|
|
||||||
disconnectFromGameServer(connecId);
|
|
||||||
//更新机器人剩余数量
|
//更新机器人剩余数量
|
||||||
updateLeftoverRobot(paramRobotId);
|
updateLeftoverRobot(robotId);
|
||||||
log.info("2009发送退出房间协议1005,robotId: {"+paramRobotId+"}");
|
disconnectFromGameServer(connecId);
|
||||||
|
log.info("2002发送退出房间协议1005,robotId: {" + robotId + "}");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("处理玩家加入房间检查时发生异常", e);
|
||||||
|
} finally {
|
||||||
|
//确保Jedis连接关闭
|
||||||
|
if (jedis != null) {
|
||||||
|
jedis.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException e) {
|
}, 6, TimeUnit.SECONDS);
|
||||||
log.error("2009协议数字格式异常,robotId: " + param.get("aid") + ", connecId: " + connecId);
|
log.info("玩家{" + robotUser.getCurrentRoomId() + "}加入房间:" + param);
|
||||||
} catch (NullPointerException e) {
|
}
|
||||||
log.error("2009协议空指针异常,connecId: " + connecId);
|
//玩家退出房间也要检查
|
||||||
} catch (Exception e) {
|
else if ("2002".equalsIgnoreCase(command)) {
|
||||||
log.error("2009协议处理异常: " + e.getMessage() + ", connecId: " + connecId);
|
//直接使用定时任务替代Thread.sleep,避免嵌套异步调用
|
||||||
} finally {
|
scheduleDelay(() -> {
|
||||||
if (jedis != null) {
|
Jedis jedis = Redis.use().getJedis();
|
||||||
jedis.close();
|
try {
|
||||||
|
String roomKey = String.valueOf(robotUser.getCurrentRoomId());
|
||||||
|
|
||||||
|
//查询该房间的玩家信息
|
||||||
|
String playersStr = jedis.hget("room:" + roomKey, "players");
|
||||||
|
if (!playersStr.equals("[]")) {
|
||||||
|
String players = playersStr.substring(1, playersStr.length() - 1);
|
||||||
|
String[] playerIds = players.split(",");
|
||||||
|
|
||||||
|
//判断只有当前机器人一个玩家
|
||||||
|
if (playerIds.length == 1) {
|
||||||
|
int playerId = Integer.parseInt(playerIds[0].trim());
|
||||||
|
if (playerId == robotId) {
|
||||||
|
|
||||||
|
//发送退出房间协议
|
||||||
|
ITObject params = TObject.newInstance();
|
||||||
|
client.send("1005", params, response -> {
|
||||||
|
EXGameController.removeRobotRoomInfo(String.valueOf(robotId));
|
||||||
|
//更新机器人剩余数量
|
||||||
|
updateLeftoverRobot(robotId);
|
||||||
|
disconnectFromGameServer(connecId);
|
||||||
|
log.info("2002发送退出房间协议1005,robotId: {" + robotId + "}");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("处理玩家退出房间检查时发生异常");
|
||||||
|
} finally {
|
||||||
|
if (jedis != null) {
|
||||||
|
jedis.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 6, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
//玩家解散房间
|
||||||
|
else if ("2005".equalsIgnoreCase(command)) {
|
||||||
|
EXGameController.removeRobotRoomInfo(String.valueOf(robotId));
|
||||||
|
//更新机器人剩余数量
|
||||||
|
updateLeftoverRobot(robotId);
|
||||||
|
disconnectFromGameServer(connecId);
|
||||||
|
log.info("2005玩家发送解散房间协议,robotId: {" + robotId + "}");
|
||||||
|
}
|
||||||
|
//解散房间时候恢复机器人账号可以使用
|
||||||
|
else if ("2008".equalsIgnoreCase(command)) {
|
||||||
|
updateLeftoverRobot(Integer.parseInt(robotUser.getRobotId()));
|
||||||
|
disconnectFromGameServer(connecId);
|
||||||
|
} else if ("2009".equalsIgnoreCase(command)) {
|
||||||
|
scheduleDelay(() -> {
|
||||||
|
Jedis jedis = null;
|
||||||
|
try {
|
||||||
|
jedis = Redis.use().getJedis();
|
||||||
|
Integer paramRobotId = param.getInt("aid");
|
||||||
|
if (robotUser != null && paramRobotId != null) {
|
||||||
|
String roomKey = String.valueOf(robotUser.getCurrentRoomId());
|
||||||
|
|
||||||
|
//查询该房间的玩家信息
|
||||||
|
String playersStr = jedis.hget(roomKey, "players");
|
||||||
|
if (playersStr != null && !playersStr.equals("[]")) {
|
||||||
|
String players = playersStr.substring(1, playersStr.length() - 1);
|
||||||
|
String[] playerIds = players.split(",");
|
||||||
|
|
||||||
|
//判断只有当前机器人一个玩家
|
||||||
|
if (playerIds.length == 1) {
|
||||||
|
int playerId = Integer.parseInt(playerIds[0].trim());
|
||||||
|
if (playerId == paramRobotId) {
|
||||||
|
//发送退出房间协议
|
||||||
|
ITObject params = TObject.newInstance();
|
||||||
|
client.send("1005", params, response -> {
|
||||||
|
EXGameController.removeRobotRoomInfo(String.valueOf(paramRobotId));
|
||||||
|
//断开连接
|
||||||
|
disconnectFromGameServer(connecId);
|
||||||
|
//更新机器人剩余数量
|
||||||
|
updateLeftoverRobot(paramRobotId);
|
||||||
|
log.info("2009发送退出房间协议1005,robotId: {" + paramRobotId + "}");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
log.error("2009协议数字格式异常,robotId: " + param.get("aid") + ", connecId: " + connecId);
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
log.error("2009协议空指针异常,connecId: " + connecId);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("2009协议处理异常: " + e.getMessage() + ", connecId: " + connecId);
|
||||||
|
} finally {
|
||||||
|
if (jedis != null) {
|
||||||
|
jedis.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 6, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("处理接收到的游戏协议异常");
|
||||||
|
} finally {
|
||||||
|
jedis0.close();
|
||||||
|
jedis2.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加leftover_robot数量 机器人退出房间
|
||||||
|
*/
|
||||||
|
private void updateLeftoverRobot ( int robotId){
|
||||||
|
Jedis jedis2 = Redis.use("group1_db2").getJedis();
|
||||||
|
try {
|
||||||
|
|
||||||
|
jedis2.hset("gallrobot", String.valueOf(robotId), "0");
|
||||||
|
|
||||||
|
jedis2.hset("{grobot}:" + robotId, "start", "0");
|
||||||
|
|
||||||
|
log.info("机器人 {" + robotId + "} 退出房间,修改gallrobot为0");
|
||||||
|
} finally {
|
||||||
|
jedis2.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 机器人登录
|
||||||
|
*/
|
||||||
|
public void login (RobotUser robotUser){
|
||||||
|
log.info("开始机器人登录,robotId: {" + robotUser.getRobotId() + "}");
|
||||||
|
ITObject object = null;
|
||||||
|
AccountBusiness accountBusiness = null;
|
||||||
|
accountBusiness = new AccountBusiness();
|
||||||
|
try {
|
||||||
|
//先快速登录
|
||||||
|
object = accountBusiness.fastLogin(Integer.parseInt(robotUser.getRobotId()));
|
||||||
|
log.info("机器人登录成功,robotId: {" + robotUser.getRobotId() + "}");
|
||||||
|
if (object == null) {
|
||||||
|
object = accountBusiness.idPasswordLogin(Integer.parseInt(robotUser.getRobotId()), robotUser.getPassword());
|
||||||
|
}
|
||||||
|
ITObject finalObject = object;
|
||||||
|
CompletableFuture.runAsync(() -> {
|
||||||
|
if (finalObject != null) {
|
||||||
|
//判断是否有房间
|
||||||
|
if (finalObject.getTObject("account") != null) {
|
||||||
|
ITObject validate = TObject.newInstance();
|
||||||
|
validate.putString("token", finalObject.getString("token"));
|
||||||
|
robotUser.setToken(finalObject.getString("token"));
|
||||||
|
;
|
||||||
|
robotUser.setLoginsession("{user}:" + robotUser.getRobotId());
|
||||||
|
if (robotUser.getLoginsession() != null) {
|
||||||
|
robotUser.setIsLogin(true);
|
||||||
|
}
|
||||||
|
if (finalObject.getTObject("account").get("roomid") != null) {
|
||||||
|
String roomid = finalObject.getTObject("account").get("roomid").toString();
|
||||||
|
robotUser.setCurrentRoomId(Integer.parseInt(roomid));
|
||||||
|
connectGame(robotUser);
|
||||||
|
|
||||||
|
robotUser.setConnecId(robotUser.getCurrentRoomId() + "_" + robotUser.getRobotId());
|
||||||
|
log.info("重启获取的机器人还有当前房间,准备加入: " + robotUser.getConnecId());
|
||||||
|
exGameController.webGroupJoinRoom(robotUser);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 6, TimeUnit.SECONDS);
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("机器人登录异常");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("处理接收到的游戏协议异常");
|
|
||||||
} finally {
|
|
||||||
jedis0.close();
|
|
||||||
jedis2.close();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public void connectGame (RobotUser robotUser){
|
||||||
* 增加leftover_robot数量 机器人退出房间
|
if (robotUser.isLogin) {
|
||||||
*/
|
if (robotUser.getClient() == null) {
|
||||||
private void updateLeftoverRobot(int robotId) {
|
TaurusClient client = new TaurusClient(robotUser.getGameHost() + ":" + robotUser.getGamePort(), "hz" + robotUser.getRobotId(), TaurusClient.ConnectionProtocol.Tcp);
|
||||||
Jedis jedis2 = Redis.use("group1_db2").getJedis();
|
|
||||||
try {
|
|
||||||
|
|
||||||
jedis2.hset("gallrobot", String.valueOf(robotId), "0");
|
|
||||||
|
|
||||||
jedis2.hset("{grobot}:" + robotId, "start", "0");
|
|
||||||
|
|
||||||
log.info("机器人 {"+robotId+"} 退出房间,修改gallrobot为0");
|
|
||||||
} finally {
|
|
||||||
jedis2.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 机器人登录
|
|
||||||
*/
|
|
||||||
public void login(RobotUser robotUser){
|
|
||||||
log.info("开始机器人登录,robotId: {"+robotUser.getRobotId()+"}");
|
|
||||||
ITObject object = null;
|
|
||||||
AccountBusiness accountBusiness = null;
|
|
||||||
accountBusiness = new AccountBusiness();
|
|
||||||
try {
|
|
||||||
//先快速登录
|
|
||||||
object = accountBusiness.fastLogin(Integer.parseInt(robotUser.getRobotId()));
|
|
||||||
log.info("机器人登录成功,robotId: {"+robotUser.getRobotId()+"}");
|
|
||||||
if(object==null){
|
|
||||||
object = accountBusiness.idPasswordLogin(Integer.parseInt(robotUser.getRobotId()), robotUser.getPassword());
|
|
||||||
}
|
|
||||||
ITObject finalObject = object;
|
|
||||||
CompletableFuture.runAsync(() -> {
|
|
||||||
if (finalObject != null) {
|
|
||||||
//判断是否有房间
|
|
||||||
if(finalObject.getTObject("account")!=null){
|
|
||||||
ITObject validate = TObject.newInstance();
|
|
||||||
validate.putString("token", finalObject.getString("token"));
|
|
||||||
robotUser.setToken(finalObject.getString("token"));;
|
|
||||||
robotUser.setLoginsession("{user}:"+robotUser.getRobotId());
|
|
||||||
if (robotUser.getLoginsession() != null) {
|
|
||||||
robotUser.setIsLogin(true);
|
|
||||||
}
|
|
||||||
if(finalObject.getTObject("account").get("roomid")!=null){
|
|
||||||
String roomid = finalObject.getTObject("account").get("roomid").toString();
|
|
||||||
robotUser.setCurrentRoomId(Integer.parseInt(roomid));
|
|
||||||
connectGame(robotUser);
|
|
||||||
|
|
||||||
robotUser.setConnecId(robotUser.getCurrentRoomId()+"_"+robotUser.getRobotId());
|
|
||||||
log.info("重启获取的机器人还有当前房间,准备加入: "+robotUser.getConnecId());
|
|
||||||
exGameController.webGroupJoinRoom(robotUser);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("机器人登录异常");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void connectGame(RobotUser robotUser){
|
|
||||||
if(robotUser.isLogin){
|
|
||||||
if(robotUser.getClient()==null){
|
|
||||||
TaurusClient client = new TaurusClient(robotUser.getGameHost()+":"+robotUser.getGamePort(), "hz"+robotUser.getRobotId(), TaurusClient.ConnectionProtocol.Tcp);
|
|
||||||
client.setSession(robotUser.getLoginsession());
|
|
||||||
client.connect();
|
|
||||||
setupEventListeners(client, robotUser.getCurrentRoomId()+"_"+robotUser.getRobotId());
|
|
||||||
robotUser.setIsconnect(client.isConnected());
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
log.error("连接超时异常");
|
|
||||||
}
|
|
||||||
robotUser.setClient(client);
|
|
||||||
EXGameController.robotRoomMapping.put(robotUser.getCurrentRoomId()+"_"+robotUser.getRobotId(), robotUser);
|
|
||||||
}else{
|
|
||||||
log.info("机器人已连接,准备加入房间");
|
|
||||||
log.info("client.isConnected()"+robotUser.getClient().isConnected());
|
|
||||||
if(robotUser.getClient().isConnected()){
|
|
||||||
robotUser.setIsconnect(true);
|
|
||||||
}else{
|
|
||||||
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.setSession(robotUser.getLoginsession());
|
||||||
client.connect();
|
client.connect();
|
||||||
|
setupEventListeners(client, robotUser.getCurrentRoomId() + "_" + robotUser.getRobotId());
|
||||||
robotUser.setIsconnect(client.isConnected());
|
robotUser.setIsconnect(client.isConnected());
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
|
@ -703,44 +700,62 @@ public class RobotConnectionManager {
|
||||||
log.error("连接超时异常");
|
log.error("连接超时异常");
|
||||||
}
|
}
|
||||||
robotUser.setClient(client);
|
robotUser.setClient(client);
|
||||||
EXGameController.robotRoomMapping.put(robotUser.getCurrentRoomId()+"_"+robotUser.getRobotId(), robotUser);
|
EXGameController.robotRoomMapping.put(robotUser.getCurrentRoomId() + "_" + robotUser.getRobotId(), robotUser);
|
||||||
|
} else {
|
||||||
|
log.info("机器人已连接,准备加入房间");
|
||||||
|
log.info("client.isConnected()" + robotUser.getClient().isConnected());
|
||||||
|
if (robotUser.getClient().isConnected()) {
|
||||||
|
robotUser.setIsconnect(true);
|
||||||
|
} else {
|
||||||
|
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();
|
||||||
|
robotUser.setIsconnect(client.isConnected());
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
log.error("连接超时异常");
|
||||||
|
}
|
||||||
|
robotUser.setClient(client);
|
||||||
|
EXGameController.robotRoomMapping.put(robotUser.getCurrentRoomId() + "_" + robotUser.getRobotId(), robotUser);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重连
|
* 重连
|
||||||
*/
|
*/
|
||||||
public void renconnect(RobotUser robotUser){
|
public void renconnect (RobotUser robotUser){
|
||||||
TaurusClient client = robotUser.getClient();
|
TaurusClient client = robotUser.getClient();
|
||||||
if(client!=null){
|
if (client != null) {
|
||||||
if(client.isConnected()){
|
if (client.isConnected()) {
|
||||||
client.connect();
|
client.connect();
|
||||||
robotUser.setIsconnect(client.isConnected());
|
robotUser.setIsconnect(client.isConnected());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据connecId获取游戏服务器连接
|
* 根据connecId获取游戏服务器连接
|
||||||
*/
|
*/
|
||||||
public TaurusClient getGameClient(String connecId) {
|
public TaurusClient getGameClient (String connecId){
|
||||||
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) {
|
|
||||||
log.error("连接超时异常");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
public int getTime () {
|
||||||
|
return Integer.parseInt((System.currentTimeMillis() + "").substring(0, 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void sleepTime ( int time){
|
||||||
|
try {
|
||||||
|
//添加延迟
|
||||||
|
Thread.sleep(time);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
log.error("连接超时异常");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -6,22 +6,26 @@ import com.taurus.core.entity.ITArray;
|
||||||
import com.taurus.core.entity.ITObject;
|
import com.taurus.core.entity.ITObject;
|
||||||
import com.taurus.core.entity.TObject;
|
import com.taurus.core.entity.TObject;
|
||||||
import com.taurus.core.plugin.redis.Redis;
|
import com.taurus.core.plugin.redis.Redis;
|
||||||
|
import com.taurus.core.util.Logger;
|
||||||
import com.taurus.core.util.StringUtil;
|
import com.taurus.core.util.StringUtil;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
|
import robot.mj.info.RobotUser;
|
||||||
import taurus.client.Message;
|
import taurus.client.Message;
|
||||||
import taurus.client.TaurusClient;
|
import taurus.client.TaurusClient;
|
||||||
import taurus.util.CardUtil;
|
import taurus.util.CardUtil;
|
||||||
import taurus.util.HongZhongSuanFaTest;
|
import taurus.util.HongZhongSuanFaTest;
|
||||||
import taurus.util.Util;
|
import taurus.util.Util;
|
||||||
|
import com.taurus.core.util.Logger;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static robot.mj.thread.ThreadPoolConfig.getBusinessThreadPool;
|
import static robot.mj.thread.ThreadPoolConfig.getBusinessThreadPool;
|
||||||
|
|
||||||
public class HuNanHongZhong {
|
public class HuNanHongZhong {
|
||||||
private static final Logger log = LoggerFactory.getLogger(HuNanHongZhong.class);
|
// private static final Logger log = LoggerFactory.getLogger(HuNanHongZhong.class);
|
||||||
|
private static final Logger log = Logger.getLogger(HuNanHongZhong.class);
|
||||||
|
|
||||||
|
|
||||||
//红中麻将手牌
|
//红中麻将手牌
|
||||||
private final List<Integer> hongZhongCardInhand = new ArrayList<>();
|
private final List<Integer> hongZhongCardInhand = new ArrayList<>();
|
||||||
|
|
@ -92,9 +96,9 @@ public class HuNanHongZhong {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将当前实例状态序列化为JSON字符串并保存到Redis
|
* 将当前实例状态序列化为JSON字符串并保存到Redis
|
||||||
|
*
|
||||||
* @param connecId 连接ID
|
* @param connecId 连接ID
|
||||||
*/
|
*/
|
||||||
public void saveToRedis(String connecId) {
|
public void saveToRedis(String connecId) {
|
||||||
|
|
@ -122,6 +126,7 @@ public class HuNanHongZhong {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从Redis恢复实例状态
|
* 从Redis恢复实例状态
|
||||||
|
*
|
||||||
* @param connecId 连接ID
|
* @param connecId 连接ID
|
||||||
* @return 是否成功恢复
|
* @return 是否成功恢复
|
||||||
*/
|
*/
|
||||||
|
|
@ -140,14 +145,16 @@ public class HuNanHongZhong {
|
||||||
if (stateMap.containsKey("hongZhongCardInhand")) {
|
if (stateMap.containsKey("hongZhongCardInhand")) {
|
||||||
hongZhongCardInhand.clear();
|
hongZhongCardInhand.clear();
|
||||||
List<Integer> handCards = gson.fromJson(stateMap.get("hongZhongCardInhand"),
|
List<Integer> handCards = gson.fromJson(stateMap.get("hongZhongCardInhand"),
|
||||||
new TypeToken<List<Integer>>(){}.getType());
|
new TypeToken<List<Integer>>() {
|
||||||
|
}.getType());
|
||||||
if (handCards != null) hongZhongCardInhand.addAll(handCards);
|
if (handCards != null) hongZhongCardInhand.addAll(handCards);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stateMap.containsKey("hongZhongchuguopai")) {
|
if (stateMap.containsKey("hongZhongchuguopai")) {
|
||||||
hongZhongchuguopai.clear();
|
hongZhongchuguopai.clear();
|
||||||
List<Integer> handCards = gson.fromJson(stateMap.get("hongZhongchuguopai"),
|
List<Integer> handCards = gson.fromJson(stateMap.get("hongZhongchuguopai"),
|
||||||
new TypeToken<List<Integer>>(){}.getType());
|
new TypeToken<List<Integer>>() {
|
||||||
|
}.getType());
|
||||||
if (handCards != null) hongZhongchuguopai.addAll(handCards);
|
if (handCards != null) hongZhongchuguopai.addAll(handCards);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -167,6 +174,7 @@ public class HuNanHongZhong {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从Redis清除实例状态
|
* 从Redis清除实例状态
|
||||||
|
*
|
||||||
* @param connecId 连接ID
|
* @param connecId 连接ID
|
||||||
*/
|
*/
|
||||||
public static void removeFromRedis(String connecId) {
|
public static void removeFromRedis(String connecId) {
|
||||||
|
|
@ -184,6 +192,7 @@ public class HuNanHongZhong {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同步手牌
|
* 同步手牌
|
||||||
|
*
|
||||||
* @param handCard
|
* @param handCard
|
||||||
*/
|
*/
|
||||||
public void updateHandCard(List<Integer> handCard) {
|
public void updateHandCard(List<Integer> handCard) {
|
||||||
|
|
@ -227,18 +236,19 @@ public class HuNanHongZhong {
|
||||||
* @param message 消息对象
|
* @param message 消息对象
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String getCard(String command, Message message) {
|
public String getCard(String command, Message message, RobotUser robotUser) {
|
||||||
log.info("摸牌协议-----{} message---{}", command, message);
|
|
||||||
if (command.equalsIgnoreCase("819")) {
|
if (command.equalsIgnoreCase("819")) {
|
||||||
ITObject param = message.param;
|
ITObject param = message.param;
|
||||||
if (param == null) {
|
if (param == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// {seat=2, Ishupai=0, isBaoTing=-1, tingcard=0, isgang=0, card=101, left_count=106}
|
log.info("819机器人摸牌所有参数" + param);
|
||||||
log.info("轮到用户:{}的用户摸牌, 牌为:{}", param.getInt("player"), param.getInt("card"));
|
Integer seat1 = param.getInt("seat");
|
||||||
log.debug("用户id: {}", playerId);
|
Integer seat2 = robotUser.getSeat();
|
||||||
log.debug("座位号: {}", param.getInt("seat"));
|
log.info("819摸牌 后台发的座位号" + seat1);
|
||||||
if (param.getInt("player") != null) {
|
log.info("819摸牌 机器人座位号" + seat2);
|
||||||
|
|
||||||
|
if (Objects.equals(seat1, seat2)) {
|
||||||
int drawnCard = param.getInt("card");
|
int drawnCard = param.getInt("card");
|
||||||
|
|
||||||
hongZhongSuanFaTest.drawnCards = drawnCard;
|
hongZhongSuanFaTest.drawnCards = drawnCard;
|
||||||
|
|
@ -434,56 +444,55 @@ public class HuNanHongZhong {
|
||||||
public String outCard(TaurusClient client) {
|
public String outCard(TaurusClient client) {
|
||||||
// 调用分离分析方法,将刻子、顺子、红中单独拎出后分析剩余牌
|
// 调用分离分析方法,将刻子、顺子、红中单独拎出后分析剩余牌
|
||||||
try {
|
try {
|
||||||
System.out.println("当前机器人手牌" + hongZhongCardInhand);
|
System.out.println("当前机器人手牌" + hongZhongCardInhand);
|
||||||
log.info("[HuNanHongZhong] 出牌前分离分析手牌结构...");
|
log.info("[HuNanHongZhong] 出牌前分离分析手牌结构...");
|
||||||
hongZhongSuanFaTest.separateAndAnalyzeHand(hongZhongCardInhand);
|
hongZhongSuanFaTest.separateAndAnalyzeHand(hongZhongCardInhand);
|
||||||
|
|
||||||
// 红中麻将出牌
|
// 红中麻将出牌
|
||||||
String hongzhongOutCard = hongZhongSuanFaTest.outCardSuanFa(hongZhongCardInhand, hongZhongCard);
|
String hongzhongOutCard = hongZhongSuanFaTest.outCardSuanFa(hongZhongCardInhand, hongZhongCard);
|
||||||
// String hongzhongOutCard = hongZhongSuanFaTest.outCardSuanFa(list, hongZhongCard);
|
// String hongzhongOutCard = hongZhongSuanFaTest.outCardSuanFa(list, hongZhongCard);
|
||||||
ITObject params = TObject.newInstance();
|
ITObject params = TObject.newInstance();
|
||||||
int cardToOut;
|
int cardToOut;
|
||||||
if (StringUtil.isNotEmpty(hongzhongOutCard)) {
|
if (StringUtil.isNotEmpty(hongzhongOutCard)) {
|
||||||
cardToOut = Integer.parseInt(hongzhongOutCard);
|
cardToOut = Integer.parseInt(hongzhongOutCard);
|
||||||
} else {
|
} else {
|
||||||
cardToOut = hongZhongCardInhand.get(0);
|
cardToOut = hongZhongCardInhand.get(0);
|
||||||
}
|
|
||||||
params.putInt("card", cardToOut);
|
|
||||||
|
|
||||||
int outCountBefore = hongZhongchuguopai.size(); // 当前历史出牌数量
|
|
||||||
|
|
||||||
// 第n次出牌时,发送前n-1张出牌
|
|
||||||
if (outCountBefore >= 1) {
|
|
||||||
// 发送前n-1张(所有历史出牌)
|
|
||||||
List<Integer> cardsToSend = hongZhongchuguopai.subList(0, outCountBefore);
|
|
||||||
params.putTArray("outcard_list", CardUtil.maJiangToTArray(cardsToSend));
|
|
||||||
}
|
|
||||||
params.putTArray("card_list", CardUtil.maJiangToTArray(hongZhongCardInhand));
|
|
||||||
log.info("机器人牌============{}", params);
|
|
||||||
// 将当前出的牌添加到历史出牌列表
|
|
||||||
hongZhongchuguopai.add(cardToOut);
|
|
||||||
// 从手牌中移除
|
|
||||||
hongZhongCardInhand.remove(Integer.valueOf(cardToOut));
|
|
||||||
log.info("出牌: {}", cardToOut);
|
|
||||||
log.info("目前机器人剩余手牌:{}", hongZhongCardInhand.toString());
|
|
||||||
params.putString("session", session + "," + token);
|
|
||||||
|
|
||||||
//使用线程池替代CompletableFuture.runAsync + Thread.sleep
|
|
||||||
getBusinessThreadPool().execute(() -> {
|
|
||||||
try {
|
|
||||||
int ot = new Random().nextInt(2);
|
|
||||||
Thread.sleep(ot * 1000+1000);
|
|
||||||
client.send("611", params, response -> {
|
|
||||||
|
|
||||||
});
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("线程执行错误");
|
|
||||||
}
|
}
|
||||||
});
|
params.putInt("card", cardToOut);
|
||||||
|
|
||||||
|
int outCountBefore = hongZhongchuguopai.size(); // 当前历史出牌数量
|
||||||
|
|
||||||
|
// 第n次出牌时,发送前n-1张出牌
|
||||||
|
if (outCountBefore >= 1) {
|
||||||
|
// 发送前n-1张(所有历史出牌)
|
||||||
|
List<Integer> cardsToSend = hongZhongchuguopai.subList(0, outCountBefore);
|
||||||
|
params.putTArray("outcard_list", CardUtil.maJiangToTArray(cardsToSend));
|
||||||
|
}
|
||||||
|
params.putTArray("card_list", CardUtil.maJiangToTArray(hongZhongCardInhand));
|
||||||
|
// 将当前出的牌添加到历史出牌列表
|
||||||
|
hongZhongchuguopai.add(cardToOut);
|
||||||
|
// 从手牌中移除
|
||||||
|
hongZhongCardInhand.remove(Integer.valueOf(cardToOut));
|
||||||
|
log.info("机器人 出牌"+ cardToOut);
|
||||||
|
log.info("目前机器人剩余手牌"+ hongZhongCardInhand);
|
||||||
|
params.putString("session", session + "," + token);
|
||||||
|
log.info("机器人params" + params);
|
||||||
|
//使用线程池替代CompletableFuture.runAsync + Thread.sleep
|
||||||
|
getBusinessThreadPool().execute(() -> {
|
||||||
|
try {
|
||||||
|
int ot = new Random().nextInt(2);
|
||||||
|
Thread.sleep(ot * 1000 + 1000);
|
||||||
|
client.send("611", params, response -> {
|
||||||
|
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("线程执行错误");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
}catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue