修复已打过玩家进入没打过玩家房间BUG问题
parent
d62392d553
commit
aa005007d6
|
|
@ -39,9 +39,8 @@ 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 = "383709";
|
|
||||||
public static final String GAME_SERVER_HOST = "8.163.97.101";*/
|
|
||||||
public static final String DEFAULT_GROUP_ID = "383709";
|
public static final String DEFAULT_GROUP_ID = "383709";
|
||||||
|
/*public static final String GAME_SERVER_HOST = "8.163.97.101";*/
|
||||||
public static final String GAME_SERVER_HOST = "8.138.6.129";
|
public static final String GAME_SERVER_HOST = "8.138.6.129";
|
||||||
|
|
||||||
/** 游戏服务器端口 */
|
/** 游戏服务器端口 */
|
||||||
|
|
|
||||||
|
|
@ -314,6 +314,14 @@ public class EXGameController extends GameController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//检查房间内的真人玩家是否今天已经和机器人对打过
|
||||||
|
if (hasPlayedToday(roomId)) {
|
||||||
|
log.warn("房间{"+roomId+"}中的真人玩家今天已和机器人对打过,拒绝机器人{"+robotId+"}加入");
|
||||||
|
result.putInt("code", 1);
|
||||||
|
result.putString("message", "房间内有玩家今天已对打");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
log.info("225 开始进房间:room:{"+roomId+"} robot:{"+robotId+"}");
|
log.info("225 开始进房间:room:{"+roomId+"} robot:{"+robotId+"}");
|
||||||
//加入房间
|
//加入房间
|
||||||
joinRoomCommon(robotId, roomId, groupId, params);
|
joinRoomCommon(robotId, roomId, groupId, params);
|
||||||
|
|
@ -760,6 +768,64 @@ public class EXGameController extends GameController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查房间内的真人玩家是否今天已经和机器人对打过
|
||||||
|
* @param roomId 房间ID
|
||||||
|
* @return true=有玩家今天打过(应阻止), false=都没打过(允许)
|
||||||
|
*/
|
||||||
|
private boolean hasPlayedToday(String roomId) {
|
||||||
|
Jedis jedis = Redis.use().getJedis();
|
||||||
|
Jedis jedis3 = Redis.use("group1_db3").getJedis();
|
||||||
|
try {
|
||||||
|
//查询该房间的玩家信息
|
||||||
|
String playersStr = jedis.hget("room:" + roomId, "players");
|
||||||
|
if (playersStr == null || playersStr.equals("[]")) {
|
||||||
|
log.info("房间{"+roomId+"}为空,无需检查");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String players = playersStr.substring(1, playersStr.length() - 1);
|
||||||
|
String[] playerIds = players.split(",");
|
||||||
|
|
||||||
|
//获取DB3中的机器人集合
|
||||||
|
Set<String> robotSet = jedis3.smembers("robots");
|
||||||
|
log.info("DB3 robots集合大小: " + robotSet.size());
|
||||||
|
|
||||||
|
for (String playerIdStr : playerIds) {
|
||||||
|
try {
|
||||||
|
int playerId = Integer.parseInt(playerIdStr.trim());
|
||||||
|
|
||||||
|
//判断是否是机器人
|
||||||
|
if (robotSet.contains(String.valueOf(playerId))) {
|
||||||
|
log.debug("玩家{"+playerId+"}是机器人,跳过");
|
||||||
|
continue; //是机器人跳过
|
||||||
|
}
|
||||||
|
|
||||||
|
//检查该真人玩家今天是否已和机器人对打过
|
||||||
|
String matchedDate = jedis3.get("player_user:" + playerId);
|
||||||
|
if (matchedDate != null) {
|
||||||
|
String today = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
|
||||||
|
if (matchedDate.equals(today)) {
|
||||||
|
log.warn("检测到玩家{"+playerId+"}今天已和机器人对打过,日期:{"+matchedDate+"}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
log.error("解析玩家 ID 失败: "+playerIdStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("房间{"+roomId+"}中的真人玩家今天都未和机器人对打过");
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("检查玩家对打记录时发生异常,roomId: {"+roomId+"}" + e);
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
jedis.close();
|
||||||
|
jedis3.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查机器人 ID 是否与房间内已有玩家冲突
|
* 检查机器人 ID 是否与房间内已有玩家冲突
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -490,6 +490,33 @@ public class RobotConnectionManager {
|
||||||
}
|
}
|
||||||
//玩家加入房间
|
//玩家加入房间
|
||||||
else if ("2001".equalsIgnoreCase(command)) {
|
else if ("2001".equalsIgnoreCase(command)) {
|
||||||
|
try {
|
||||||
|
Integer newPlayerId = param.getInt("aid");
|
||||||
|
if (newPlayerId != null && newPlayerId != robotId) {
|
||||||
|
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
||||||
|
//判断是否是机器人
|
||||||
|
Set<String> robotSet = jedis3.smembers("robots");
|
||||||
|
if (!robotSet.contains(String.valueOf(newPlayerId))) {
|
||||||
|
//是真人检查是否打过
|
||||||
|
String matchedDate = jedis3.get("player_user:" + newPlayerId);
|
||||||
|
if (matchedDate != null && matchedDate.equals(today)) {
|
||||||
|
log.warn("玩家{"+newPlayerId+"}今天已和机器人对打过,机器人立即退出房间");
|
||||||
|
//立即退出
|
||||||
|
ITObject exitParams = TObject.newInstance();
|
||||||
|
client.send("1005", exitParams, response -> {
|
||||||
|
EXGameController.removeRobotRoomInfo(String.valueOf(robotId));
|
||||||
|
updateLeftoverRobot(robotId);
|
||||||
|
disconnectFromGameServer(connecId);
|
||||||
|
log.info("发送退出房间协议1005,robotId: {" + robotId + "}");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("2001即时检查时发生异常", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
//【延迟检查】6秒后检查完整状态(用于只剩机器人的场景)
|
||||||
scheduleDelay(() -> {
|
scheduleDelay(() -> {
|
||||||
Jedis jedis = Redis.use().getJedis();
|
Jedis jedis = Redis.use().getJedis();
|
||||||
try {
|
try {
|
||||||
|
|
@ -516,15 +543,52 @@ public class RobotConnectionManager {
|
||||||
log.info("2002发送退出房间协议1005,robotId: {" + robotId + "}");
|
log.info("2002发送退出房间协议1005,robotId: {" + robotId + "}");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
//多人情况:检查新加入的真人玩家是否今天已和机器人对打过
|
||||||
|
boolean shouldExit = false;
|
||||||
|
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
||||||
|
|
||||||
|
Set<String> robotSet = jedis3.smembers("robots");
|
||||||
|
|
||||||
|
for (String playerIdStr : playerIds) {
|
||||||
|
try {
|
||||||
|
int playerId = Integer.parseInt(playerIdStr.trim());
|
||||||
|
|
||||||
|
//跳过机器人自己
|
||||||
|
if (playerId == robotId) continue;
|
||||||
|
|
||||||
|
//判断是否是机器人
|
||||||
|
if (robotSet.contains(String.valueOf(playerId))) {
|
||||||
|
log.debug("玩家{"+playerId+"}是机器人,跳过");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//检查该真人玩家今天是否已和机器人对打过
|
||||||
|
String matchedDate = jedis3.get("player_user:" + playerId);
|
||||||
|
if (matchedDate != null && matchedDate.equals(today)) {
|
||||||
|
log.warn("检测到玩家{"+playerId+"}今天已和机器人对打过,机器人将退出房间,日期:{"+matchedDate+"}");
|
||||||
|
shouldExit = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
log.error("解析玩家ID失败: "+playerIdStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldExit) {
|
||||||
|
//有玩家今天已打过 机器人退出房间
|
||||||
|
ITObject exitParams = TObject.newInstance();
|
||||||
|
client.send("1005", exitParams, response -> {
|
||||||
|
EXGameController.removeRobotRoomInfo(String.valueOf(robotId));
|
||||||
|
updateLeftoverRobot(robotId);
|
||||||
|
disconnectFromGameServer(connecId);
|
||||||
|
log.info("玩家已打过-2001发送退出房间协议1005,robotId: {" + robotId + "}");
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("处理玩家加入房间检查时发生异常", e);
|
log.error("处理玩家加入房间检查时发生异常", e);
|
||||||
} finally {
|
|
||||||
//确保Jedis连接关闭
|
|
||||||
if (jedis != null) {
|
|
||||||
jedis.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, 6, TimeUnit.SECONDS);
|
}, 6, TimeUnit.SECONDS);
|
||||||
log.info("玩家{" + robotUser.getCurrentRoomId() + "}加入房间:" + param);
|
log.info("玩家{" + robotUser.getCurrentRoomId() + "}加入房间:" + param);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue