Compare commits

..

No commits in common. "e87d180063ef056e0206a4ce938b4788262897d6" and "8a2d6765e02c2a41b8c48112056bf7483da4cd20" have entirely different histories.

3 changed files with 47 additions and 145 deletions

View File

@ -562,65 +562,35 @@ public class UpdatePlayRoomJob implements Job{
public int getRobot(int maxPlayers)
{
Jedis jedis = Redis.use("group1_db1").getJedis();
String lockValue = null;
try {
//按房间类型区分
String lockKey = "robot_allocation_lock_" + maxPlayers;
lockValue = System.currentTimeMillis() + "_" + Thread.currentThread().getId();
boolean locked = false;
for (int i = 0; i < 50; i++) {
String result = jedis.set(lockKey, lockValue, "NX", "EX", 15);
if ("OK".equals(result)) {
locked = true;
break;
}
Thread.sleep(50);
}
if (!locked) {
logger.warn("获取机器人分配锁超时maxPlayers:" + maxPlayers + ", 返回0");
return 0;
}
//检查是否有足够的机器人
long freeRobotNum = jedis.scard("free_robot");
long usedRobotNum = jedis.scard("used_robot");
logger.info("机器人资源状态 - 可用:" + freeRobotNum + ", 已使用:" + usedRobotNum + ", 需要:" + maxPlayers);
if (freeRobotNum < maxPlayers)
long freeRobotNum = Redis.use("group1_db1").scard("free_robot");
if (freeRobotNum < maxPlayers)
{
long useRobotNum = Redis.use("group1_db1").scard("used_robot");
if (useRobotNum >= 100)
{
long useRobotNum = jedis.scard("used_robot");
logger.warn("机器人数量不足,当前可用:" + freeRobotNum + ", 已使用:" + useRobotNum + ", 需要:" + maxPlayers);
//如果机器人总数接近上限
if (useRobotNum + freeRobotNum >= 100)
logger.warn("robot not enough, cur used robot " + useRobotNum);
}
try {
createRobot();
freeRobotNum = Redis.use("group1_db1").scard("free_robot");
if (freeRobotNum < maxPlayers)
{
logger.warn("机器人总数接近上限:" + (useRobotNum + freeRobotNum) + ", 考虑增加机器人容量");
}
try {
createRobot();
freeRobotNum = jedis.scard("free_robot");
if (freeRobotNum < maxPlayers)
{
logger.error("创建机器人后仍然不足,可用:" + freeRobotNum + ", 需要:" + maxPlayers);
return 0;
}
logger.info("成功创建机器人,当前可用:" + freeRobotNum);
}
catch (Exception e)
{
logger.error("创建机器人时发生异常: " + e.getMessage(), e);
logger.error("create robot failed............");
return 0;
}
}
}
catch (Exception e)
{
logger.error(e);
}
}
try {
while(freeRobotNum > 0)
{
String strRobotId = jedis.spop("free_robot");
freeRobotNum = jedis.scard("free_robot");
String strRobotId = Redis.use("group1_db1").spop("free_robot");
freeRobotNum = Redis.use("group1_db1").scard("free_robot");
if (StringUtil.isEmpty(strRobotId))
{
createRobot();
@ -656,7 +626,7 @@ public class UpdatePlayRoomJob implements Job{
}
}
jedis.sadd("used_robot", strRobotId);
Redis.use("group1_db1").sadd("used_robot", strRobotId);
int robotId = Integer.parseInt(strRobotId);
String fakeHp = Redis.use("group1_db10").hget("fake_"+strRobotId, "fake_hp");
if (StringUtil.isEmpty(fakeHp))
@ -671,21 +641,6 @@ public class UpdatePlayRoomJob implements Job{
{
logger.error(e);
}
finally {
// 释放锁
try {
if (lockValue != null) {
String lockKey = "robot_allocation_lock_" + maxPlayers;
String currentValue = jedis.get(lockKey);
if (lockValue.equals(currentValue)) {
jedis.del(lockKey);
}
}
} catch (Exception e) {
logger.error("释放机器人分配锁时出错: " + e.getMessage());
}
jedis.close();
}
return 0;
}

View File

@ -507,14 +507,6 @@ public class UpdateRobotRoomJob implements Job{
String roomId = room_list.get(3);
int time = Integer.parseInt(roomtime);
if (time+20 < sttime) {
//回收机器人
for (int i = 0; i < players.size(); i++) {
int player_id = players.getInt(i);
Redis.use("group1_db1").srem("used_robot", Integer.toString(player_id));
Redis.use("group1_db1").sadd("free_robot", Integer.toString(player_id));
logger.info("回收机器人: " + player_id + " 到可用池");
}
js2.hset(allrobotkeys+gameid,players.get(0)+"",0+"");
//解散房间
GroupPublisherService.delRoomEvt(groupId, roomId);
@ -719,65 +711,35 @@ public class UpdateRobotRoomJob implements Job{
public int getRobot(int maxPlayers)
{
Jedis jedis = Redis.use("group1_db1").getJedis();
String lockValue = null;
try {
//按房间类型区分
String lockKey = "robot_allocation_lock_" + maxPlayers;
lockValue = System.currentTimeMillis() + "_" + Thread.currentThread().getId();
boolean locked = false;
for (int i = 0; i < 50; i++) {
String result = jedis.set(lockKey, lockValue, "NX", "EX", 15);
if ("OK".equals(result)) {
locked = true;
break;
}
Thread.sleep(50);
}
if (!locked) {
logger.warn("获取机器人分配锁超时maxPlayers:" + maxPlayers + ", 返回0");
return 0;
}
//检查是否有足够的机器人
long freeRobotNum = jedis.scard("free_robot");
long usedRobotNum = jedis.scard("used_robot");
logger.info("机器人资源状态 - 可用:" + freeRobotNum + ", 已使用:" + usedRobotNum + ", 需要:" + maxPlayers);
if (freeRobotNum < maxPlayers)
long freeRobotNum = Redis.use("group1_db1").scard("free_robot");
if (freeRobotNum < maxPlayers)
{
long useRobotNum = Redis.use("group1_db1").scard("used_robot");
if (useRobotNum >= 100)
{
long useRobotNum = jedis.scard("used_robot");
logger.warn("机器人数量不足,当前可用:" + freeRobotNum + ", 已使用:" + useRobotNum + ", 需要:" + maxPlayers);
//如果机器人总数接近上限
if (useRobotNum + freeRobotNum >= 100)
logger.warn("robot not enough, cur used robot " + useRobotNum);
}
try {
createRobot();
freeRobotNum = Redis.use("group1_db1").scard("free_robot");
if (freeRobotNum < maxPlayers)
{
logger.warn("机器人总数接近上限:" + (useRobotNum + freeRobotNum) + ", 考虑增加机器人容量");
}
try {
createRobot();
freeRobotNum = jedis.scard("free_robot");
if (freeRobotNum < maxPlayers)
{
logger.error("创建机器人后仍然不足,可用:" + freeRobotNum + ", 需要:" + maxPlayers);
return 0;
}
logger.info("成功创建机器人,当前可用:" + freeRobotNum);
}
catch (Exception e)
{
logger.error("创建机器人时发生异常: " + e.getMessage(), e);
logger.error("create robot failed............");
return 0;
}
}
}
catch (Exception e)
{
logger.error(e);
}
}
try {
while(freeRobotNum > 0)
{
String strRobotId = jedis.spop("free_robot");
freeRobotNum = jedis.scard("free_robot");
String strRobotId = Redis.use("group1_db1").spop("free_robot");
freeRobotNum = Redis.use("group1_db1").scard("free_robot");
if (StringUtil.isEmpty(strRobotId))
{
createRobot();
@ -813,7 +775,7 @@ public class UpdateRobotRoomJob implements Job{
}
}
jedis.sadd("used_robot", strRobotId);
Redis.use("group1_db1").sadd("used_robot", strRobotId);
int robotId = Integer.parseInt(strRobotId);
String fakeHp = Redis.use("group1_db10").hget("fake_"+strRobotId, "fake_hp");
if (StringUtil.isEmpty(fakeHp))
@ -828,21 +790,6 @@ public class UpdateRobotRoomJob implements Job{
{
logger.error(e);
}
finally {
//释放锁
try {
if (lockValue != null) {
String lockKey = "robot_allocation_lock_" + maxPlayers;
String currentValue = jedis.get(lockKey);
if (lockValue.equals(currentValue)) {
jedis.del(lockKey);
}
}
} catch (Exception e) {
logger.error("释放机器人分配锁时出错: " + e.getMessage());
}
jedis.close();
}
return 0;
}

View File

@ -1328,7 +1328,7 @@ public class GroupRoomService {
for (int i = 0; i < players.size(); i++) {
int player_id = players.getInt(i);
Redis.use("group1_db1").srem("used_robot", Integer.toString(player_id));
Redis.use("group1_db1").sadd("free_robot", Integer.toString(player_id));
//Redis.use("group1_db1").sadd("free_robot", Integer.toString(player_id));
}
}