package com.group.service;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;

import com.data.bean.AccountBean;
import com.data.bean.GameBean;
import com.data.bean.GroupBean;
import com.data.bean.GroupMemberBean;
import com.data.bean.GroupPlayBean;
import com.data.cache.AccountCache;
import com.data.cache.BaseCache;
import com.data.cache.GameCache;
import com.data.cache.GroupCache;
import com.data.cache.GroupMemberCache;
import com.data.util.ErrorCode;
import com.data.util.EventType;
import com.data.util.Utility;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.plugin.redis.RedisLock;
import com.taurus.core.util.Logger;
import com.taurus.core.util.StringUtil;
import com.taurus.web.WebException;

import redis.clients.jedis.Jedis;

import static java.lang.Thread.sleep;

public class GroupRoomService {
	private static final Logger log = Logger.getLogger(GroupRoomService.class);

	/** 解散时间 */
	private static final String[] DISMISS_TIME = { "30", "60", "90", "180" };
	/** 踢出时间 */
	private static final String[] KICK_TIME = { "30", "60", "120", "180" };

	private static final String _getSvr(Jedis jedis0, GameBean gb) {
		String svr = null;
		Set<String> gamesvrs = gb.svr_list;
		Integer conns = null;
		for (String str : gamesvrs) {
			String currConns = jedis0.hget(str, "conns");
			if (conns == null && currConns != null
					|| (conns != null && currConns != null && Integer.parseInt(currConns) < conns)) {
				conns = Integer.parseInt(currConns);
				svr = str;
			}
		}
		return svr;
	}

	private final static String createGroupRoom(String session, int groupId, int pid) throws Exception {
		Jedis jedis0 = Redis.use("group1_db0").getJedis();
		try {
			GroupPlayBean gpb = GroupCache.getPlay(groupId, pid);
			if (gpb == null) {
				throw new WebException(ErrorCode.GROUP_PLAY_EXIST);
			}

			int gameId = gpb.gameId;
			GameBean gb = GameCache.getGame(gameId);
			String svr = _getSvr(jedis0, gb);
			if (svr == null) {
				log.error("createGroupRoom gameId:" + gameId + " svr:" + svr);
				throw new WebException(ErrorCode.NO_SERVICE);
			}

			String grooms_key = GroupCache.genRoomsKey(groupId);
			GroupBean group = GroupCache.getGroup(groupId);
			ITObject configData = TObject.newFromJsonData(gpb.config);
			int maxPlayers = gpb.maxPlayers;

			int opt = configData.getInt("opt");

			String owner_session = AccountCache.genKey(group.owner);
			Integer pay = gb.pay.get("pay" + opt + "_" + maxPlayers);
			if (pay == null) {
				log.warn("pay no set!");
				throw new WebException(ErrorCode._FAILED);
			}

			if (pay > 0) {
				ArrayList<Long> result_list = Utility.payDiamo(jedis0, owner_session, pay);
				if (result_list == null) {
					throw new WebException(ErrorCode._FAILED);
				}
				long result = result_list.get(0);
				if (result != 0) {
					log.error("createGroupRoom groupId:" + groupId + " pid:" + pid + " group.owner:" + group.owner
							+ " no diamo");
					throw new WebException(ErrorCode.NO_DIAMO);
				}
				long cur_diamo = result_list.get(1);
				Utility.payDiamo(EventType.REDIS_EVENT_GROUP_ROOM, group.owner, gameId, pay, (int) cur_diamo, groupId,
						pid);
			}

			int dissolve_opt = group.dissolve_opt;
			int kick_opt = group.kick_opt;

			String newRoomId = Redis.use("group1_db1").rpop("free_room");
			Redis.use("group1_db1").lpush("free_room", newRoomId);
			String room_key = "room:" + newRoomId;

			long time = System.currentTimeMillis() / 1000;
			Map<String, String> roomMap = new HashMap<String, String>();
			roomMap.put("id", newRoomId);
			roomMap.put("owner", owner_session);
			roomMap.put("svr", svr);
			roomMap.put("AA", "0");
			roomMap.put("pay", pay + "");
			roomMap.put("agent", "1");
			roomMap.put("group", groupId + "");
			roomMap.put("gpid", pid + "");
			roomMap.put("payer", group.owner + "");
			roomMap.put("maxPlayers", maxPlayers + "");
			roomMap.put("times", gb.opt.get(opt) + "");
			roomMap.put("opt", opt + "");
			roomMap.put("status", "0");
			roomMap.put("hpOnOff", gpb.hpOnOff + "");
			roomMap.put("rewardType", gpb.rewardType + "");
			roomMap.put("rewardValueType", gpb.rewardValueType + "");
			roomMap.put("xipai_rewardType", gpb.xipai_rewardType + "");
			roomMap.put("xipai_rewardValueType", gpb.xipai_rewardValueType + "");
			roomMap.put("dismiss_time", DISMISS_TIME[dissolve_opt - 1]);
			roomMap.put("kick_time", KICK_TIME[kick_opt - 1]);
			roomMap.put("hp_times", gpb.hp_times + "");
			if (gpb.hpOnOff == 1) {
				ITObject hpObj = TObject.newFromJsonData(gpb.hpConfig);
				if (hpObj.getInt("limitInRoom") != null) {
					roomMap.put("limitInRoom", hpObj.getInt("limitInRoom") + "");
				}
				configData.putTObject("hpData", hpObj);
			}

			configData.del("opt");
			configData.del("AA");
			roomMap.put("options", configData.toJson());
			roomMap.put("game", gameId + "");
			roomMap.put("open", "1");
			roomMap.put("round", "0");
			roomMap.put("create_time", time + "");
			roomMap.put("cache_ver", "1");
			jedis0.hmset(room_key, roomMap);

			/**
			 * pid open status 剩余空位 11 1 1 00
			 */

			String fake = jedis0.hget(room_key, "fake");
			if (StringUtil.isNotEmpty(fake)) {
				log.info("create room:" + newRoomId + " session:" + session + " fake:" + fake + " pay:" + pay
						+ " players:" + jedis0.hget(room_key, "players"));
				Redis.use().hset(room_key, "status", 3 + "");
				Redis.use().hincrBy(room_key, "cache_ver", 1);
				Redis.use().expire(room_key, 20);
			} else {
				Redis.use("group1_db11").zadd(grooms_key, pid * 10000 + 1101, room_key);
				GroupPublisherService.addRoomEvt(groupId, newRoomId);
			}
			return room_key;
		} finally {
			jedis0.close();
		}

	}

	/**
	 * 检测成员在不在房间里
	 * 
	 * @param groupId
	 * @param session
	 * @return
	 */
	public static final boolean checkRoom(int groupId, int uid) {
		Jedis jedis0 = Redis.use("group1_db0").getJedis();
		try {
			String session = AccountCache.genKey(uid);
			String oldRoom = Utility.getOldRoomV2(jedis0, 0, session, uid);
			if (StringUtil.isNotEmpty(oldRoom)) {
				return true;
			}
		} finally {
			jedis0.close();
		}
		return false;
	}

	public static final boolean checkRoom(String room_key, Jedis jedis0) {
		RedisLock lock = new RedisLock(room_key, jedis0);
		lock.lock();
		try {
			String status = jedis0.hget(room_key, "status");
			if (StringUtil.isEmpty(status) || status.equals("2")) {
				return false;
			}
			return true;
		} finally {
			lock.unlock(false);
		}
	}

	public static final boolean checkFakeRoom(String room_key, Jedis jedis0) {
		RedisLock lock = new RedisLock(room_key, jedis0);
		lock.lock();
		try {
			String fake = jedis0.hget(room_key, "fake");
			if (!StringUtil.isEmpty(fake)) {
				return true;
			}
			return false;
		} finally {
			lock.unlock(false);
		}
	}

	private static final ITObject publicJoinRoom(int groupId, String session, String room_key, String platform)
			throws Exception {

		List<String> x_room_list = Redis.use("group1_db0").hmget(room_key, "fake");
		String fake_json = x_room_list.get(0);
		if (fake_json != null) {
			log.error("session:" + session + " public join room:" + room_key + " fail, fake != null");
			throw new WebException(ErrorCode.ROOM_CLOSE);
		}

		Jedis jedis0 = Redis.use("group1_db0").getJedis();

		RedisLock lock = null;
		try {
			String finalRoom = room_key;

			boolean enter_old = false;
			int uid = AccountCache.getAccount(session).id;
			String oldRoom = Utility.getOldRoomV2(jedis0, 0, session, uid);
			if (StringUtil.isNotEmpty(oldRoom)) {
				String group = jedis0.hget(oldRoom, "group");
				if (StringUtil.isEmpty(group) || Integer.parseInt(group) != groupId) {
					throw new WebException(ErrorCode.GROUP_NOT_CURGROUP_ROOM);
				}
				enter_old = true;
				finalRoom = oldRoom;
			}

			lock = new RedisLock(session, jedis0);
			lock.lock();
			List<String> room_list = jedis0.hmget(finalRoom, "hpOnOff", "hp_times", "limitInRoom", "status", "open",
					"svr", "game", "gpid", "group", "maxPlayers", "AA", "opt", "players");
			String status = room_list.get(3);
			if (StringUtil.isEmpty(status)) {
				throw new WebException(ErrorCode.GROUP_ROOM_DEL);
			}
			int _status = Integer.parseInt(status);

			if (_status == 2 || _status == 3) {
				Utility.delRoomBySession(jedis0, session, finalRoom);
				throw new WebException(ErrorCode.GROUP_ROOM_DEL);
			}

			String gm_key = GroupMemberCache.genKey(groupId, uid);
			int hp_times = 1;
			String group = room_list.get(8);
			if (StringUtil.isNotEmpty(group)) {
				String hp_times_str = room_list.get(1);
				hp_times = Integer.parseInt(group) > 0 ? Integer.parseInt(hp_times_str) : 1;
			}
			int gameId = Integer.parseInt(room_list.get(6));
			int hpOnOff = Integer.parseInt(room_list.get(0));
			GameBean gb = GameCache.getGame(gameId);
			String svr = room_list.get(5);
			if (svr == null || !jedis0.exists(svr)) {
				svr = _getSvr(jedis0, gb);
			}
			if (svr == null) {
				log.error("publicJoinRoom room_key:" + room_key + " gm_key:" + gm_key + " svr:" + svr);
				throw new WebException(ErrorCode.NO_SERVICE);
			}
			jedis0.hset(finalRoom, "svr", svr);

			int gpid = Integer.parseInt(room_list.get(7));
			GroupBean group_bean = GroupCache.getGroup(groupId);

			if (!enter_old) {
				int _maxPlayers = Integer.parseInt(room_list.get(9));
				String _players = room_list.get(12);
				if (StringUtil.isNotEmpty(_players)) {
					ITArray arr = TArray.newFromJsonData(_players);
					if (arr.size() >= _maxPlayers) {
						boolean flag = true;
						for (int i = 0; i < arr.size(); i++) {
							int player_id = arr.getInt(i);
							if (player_id == uid) {
								flag = false;
							}
						}

						if (flag) {
							log.info("publicJoinRoom enter group fail, room full:" + gm_key + " room_key:" + room_key);
							throw new WebException(ErrorCode.ROOM_CLOSE);
						}
					}
				}

				String open = room_list.get(4);
				if ((StringUtil.isEmpty(open) || open.equals("0"))) {
					log.info("publicJoinRoom enter group fail, room open close:" + gm_key + " room_key:" + room_key);
					throw new WebException(ErrorCode.ROOM_CLOSE);
				}

				int AA = Integer.parseInt(room_list.get(10));
				if (AA == 1) {
					int maxPlayers = Integer.parseInt(room_list.get(9));
					int opt = Integer.parseInt(room_list.get(11));

					Integer pay = gb.pay.get("pay" + opt + "_" + maxPlayers);
					if (pay == null) {
						log.warn("pay no set!");
						throw new WebException(ErrorCode._FAILED);
					}
					pay = (int) Math.ceil((double) pay / maxPlayers);
					int result = Utility.checkRoomDiamo(jedis0, session, pay);
					if (result != 0) {
						log.error("publicJoinRoom groupId:" + groupId + " no diamo");
						throw new WebException(result);
					}
				}

				if (group_bean.ban == 1) {
					throw new WebException(ErrorCode.GROUP_BAN);
				}

				GroupPlayBean gpb = GroupCache.getPlay(groupId, gpid);
				if (gpb == null || gpb.ban == 1) {
					throw new WebException(ErrorCode.GROUP_BAN_PLAY);
				}

				Jedis jedis10 = Redis.use("group1_db10").getJedis();

				RedisLock lock2 = new RedisLock(gm_key, jedis10);
				try {
					lock2.lock();
					List<String> list = jedis10.hmget(gm_key, "ban", "hp");
					String ban = list.get(0);
					if (StringUtil.isNotEmpty(ban) && ban.equals("1")) {
						throw new WebException(ErrorCode.GROUP_MEMBER_BAN);
					}

					String ban_key = "ban{" + groupId + "}:" + uid;
					Set<String> ban_set = Redis.use("group1_db11").smembers(ban_key);
					if (ban_set.size() > 0) {
						String players = room_list.get(12);
						if (StringUtil.isNotEmpty(players)) {
							ITArray arr = TArray.newFromJsonData(players);
							for (int i = 0; i < arr.size(); ++i) {
								String tem = arr.getInt(i) + "";
								if (ban_set.contains(tem)) {
									throw new WebException(ErrorCode.GROUP_BAN_DESK);
								}
							}
						}
					}

					long uid_hp = Long.parseLong(jedis10.hget(gm_key, "hp"));
					/*
					 * if (uid_hp > Integer.MAX_VALUE) { throw new
					 * WebException(ErrorCode.GROUP_PARTNER_HP_THAN_LIMIET); }
					 */
					if (uid_hp <= 0) {
						throw new WebException(ErrorCode.GROUP_LIMIT_NO_HP);
					}

					if (hpOnOff == 1) {
						String hp = list.get(1);
						String limitInRoom = room_list.get(2);
						/*
						 * if (Long.parseLong(hp) > Integer.MAX_VALUE) { throw new
						 * WebException(ErrorCode.GROUP_PARTNER_HP_THAN_LIMIET); }
						 */

						boolean pass = Redis.use("group1_db1").sismember("pass_games", gameId + "");
						if (!pass && StringUtil.isNotEmpty(limitInRoom)) {
							if (Integer.parseInt(hp) < Integer.parseInt(limitInRoom)) {
								throw new WebException(ErrorCode.GROUP_LIMIT_NO_HP);
							}
						}
					}
					jedis0.hset(session, "room", finalRoom);

					GroupMemberBean gmb = GroupCache.getMember(groupId, uid);

					List<Integer> t_list = Utility.getMemberParents(jedis10, groupId, uid, true);
					if (t_list != null) {
						// 奖励还是佣金?
						ITArray list1 = TArray.newInstance();
						String p_reward_key = GroupCache.genRewardKey(groupId, gpid);
						String p_xipai_reward_key = GroupCache.genXiPaiRewardKey(groupId, gpid);
						for (Integer par : t_list) {
							Double val = jedis10.zscore(p_reward_key, par.toString());
							int r = 0;
							if (val != null) {
								r = val.intValue();
							}
							int x = 0;
							Double xipai_val = jedis10.zscore(p_xipai_reward_key, par.toString());

							if (xipai_val != null) {
								x = xipai_val.intValue();
							}
							if (r > 0 || x > 0) {
								ITObject obj = TObject.newInstance();
								obj.putInt("p", par);
								if (r > 0) {
									obj.putInt("r", r);
								}
								if (x > 0) {
									obj.putInt("x", x);
								}
								list1.addTObject(obj);
							}
						}

						jedis0.hset(finalRoom, "prs_" + uid, list1.toJson());
					}
				} finally {
					// jedis10.close();
					lock2.unlock();
				}
			}

			ITObject resData = TObject.newInstance();
			resData.putString("room_id", finalRoom.replace("room:", ""));
			resData.putInt("hp_times", hp_times);
			ITObject gameObj = gb.getTObject();
			resData.putTObject("game_info", gameObj);
			resData.putInt("status", _status);
			List<String> server_ip = jedis0.hmget(svr, "ip", "port", "intranet");
			// resData.putString("server_ip", server_ip.get(0));
			resData.putString("server_ip", server_ip.get(2));
			resData.putString("server_port", server_ip.get(1));
			resData.putInt("groupId", groupId);
			resData.putInt("pid", gpid);
			resData.putInt("hpOnOff", hpOnOff);
			resData.putBoolean("ban_chat1", group_bean.ban_chat1);
			resData.putBoolean("ban_chat2", group_bean.ban_chat2);
			String lev_str = Redis.use("group1_db10").hget(gm_key, "lev");
			int lev = 3;
			if (StringUtil.isNotEmpty(lev_str)) {
				lev = Integer.parseInt(lev_str);
			}
			resData.putInt("lev", lev);
			resData.putInt("gameId", gameId);

			return resData;
		} finally {
			if (lock != null)
				lock.unlock(false);
			jedis0.close();
		}
	}

	/**
	 * 加入房间
	 * 
	 * @param groupId
	 * @param roomid
	 * @param session
	 * @param platform
	 * @return
	 * @throws Exception
	 */
	public static ITObject joinRoom(int groupId, String roomid, String session, String platform) throws Exception {
		String grooms_key = GroupCache.genRoomsKey(groupId);
		String room_key = "room:" + roomid;
		Long tem = Redis.use("group1_db11").zrank(grooms_key, room_key);
		if (tem == null) {
			throw new WebException(ErrorCode.NO_ROOM_NUM);
		}

		List<String> room_list = Redis.use("group1_db0").hmget(room_key, "fake", "gpid");
		int pid = Integer.parseInt(room_list.get(1));
		String fake_json = room_list.get(0);
		if (fake_json != null) {
			throw new WebException(ErrorCode.ROOM_CLOSE);
		}
		AccountBean acc = AccountCache.getAccount(session);
		List<String> s_list = Redis.use("group1_db0").hmget(session, "regTime");
		String robotInfo = s_list.get(0);
		if (StringUtil.isNotEmpty(robotInfo) && robotInfo.equals("9999")) {
			log.error("robot:" + acc.id + " match room error");
			throw new WebException(ErrorCode.ROOM_CLOSE);
		}
		int uid = AccountCache.getAccount(session).id;
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		List<Integer> t_list = Utility.getMemberParents(jedis10, groupId, uid, true);

		jedis10.close();

		if (t_list != null) {
			// 判断阀值
			for (Integer par : t_list) {
				// 以前的
				String gm_par_key = GroupMemberCache.genKey(groupId, par);
				// log.error("par:" + par);
				String autoscore1 = Redis.use("group1_db10").hget(gm_par_key, "group_black_max_value");
				// log.error("autoscore1:" + autoscore1);
				Integer autoscore = 0;
				if (!StringUtil.isEmpty(autoscore1)) {
					autoscore = Integer.parseInt(autoscore1);
				}

				if (autoscore > 0) {
					ITObject totalHp = GroupService.getHpTotal(groupId, par, 30);
					Long hp = totalHp.getLong("hp");
					log.error("autoscore:" + autoscore);
					log.error("hp:" + hp);

					if (hp / 100 < autoscore) {
						throw new WebException(ErrorCode.GROUP_MEMBER_BAN);
					}
				}
			}
		}
		/*
		 * boolean xingyuhao = false; if (Redis.use("group1_db1").sismember("gods",
		 * Integer.toString(uid))) { xingyuhao = true; }
		 * 
		 * if (xingyuhao) { boolean isSpecialGods = false;
		 * 
		 * boolean superflag = false;
		 * 
		 * { String specail = Redis.use("group1_db1").hget("gods_special",
		 * Integer.toString(uid)); if (StringUtil.isNotEmpty(specail)) { isSpecialGods =
		 * true; } }
		 * 
		 * String superid = Redis.use("group1_db1").hget("gods_super",
		 * Integer.toString(uid)); if (StringUtil.isNotEmpty(superid)) { superflag =
		 * true; }
		 * 
		 * { List<String> player_list = Redis.use("group1_db0").hmget(room_key,
		 * "players");
		 * 
		 * String players_json = player_list.get(0); if
		 * (StringUtil.isNotEmpty(players_json)) { ITArray players =
		 * TArray.newFromJsonData(players_json); for (int i = 0; i < players.size();
		 * i++) { int player_id = players.getInt(i); if (player_id == uid) continue;
		 * 
		 * String supersid = Redis.use("group1_db1").hget("gods_super",
		 * Integer.toString(player_id)); if (StringUtil.isNotEmpty(supersid)) {
		 * superflag = true; }
		 * 
		 * if (Redis.use("group1_db1").sismember("gods", Integer.toString(player_id))) {
		 * boolean isPlayerSpecialGods = false; String specail =
		 * Redis.use("group1_db1").hget("gods_special", Integer.toString(player_id)); if
		 * (StringUtil.isNotEmpty(specail)) { isPlayerSpecialGods = true; }
		 * 
		 * if (!isSpecialGods && isPlayerSpecialGods) {
		 * 
		 * } else { if (isSpecialGods && isPlayerSpecialGods) {
		 * 
		 * } else { if (superflag) {
		 * 
		 * } else { log.error("xingyun:" + acc.id + " can't join rooms:" + room_key +
		 * " xingyun playerId:" + player_id); throw new
		 * WebException(ErrorCode.GROUP_BAN_DESK); }
		 * 
		 * } } } } } } }
		 */

		// 检测是否可娱乐
		for (int i = 0; i < t_list.size(); i++) {
			ITArray list = GroupService.getMemberPlayList(groupId, t_list.get(i));
			if (list.size() != 0) {

				for (int j = 0; j < list.size(); j++) {
					if (list.getInt(j) == pid) {
						throw new WebException(ErrorCode.GROUP_BAN_PLAY);
					}
				}

			}
		}
		try {
			ITObject resData = publicJoinRoom(groupId,session, room_key,platform);

			int gameId1 = resData.getInt("gameId");
			String robot_host = null;
			if(gameId1!=0) {
				/*if (gameId1 == 17) {
					robot_host = "8.138.120.132:8917";
				}*/
				if (gameId1 == 22) {
					robot_host = "8.138.120.132:8722";
				}
			}
			if(StringUtil.isNotEmpty(robot_host)) {
				sendRobotData(resData.getString("room_id"),groupId,robot_host,gameId1);
			}


			Jedis jedis11 = Redis.use("group1_db11").getJedis();
			jedis11.zremrangeByScore(grooms_key, 0, 0);
			jedis11.close();
			return resData;
		} catch (WebException e) {
			if (e.getCode() == ErrorCode.GROUP_ROOM_DEL || e.getCode() == ErrorCode.NO_ROOM_NUM) {
				Redis.use("group1_db11").zrem(grooms_key, room_key);
			}
			throw e;
		} finally {

		}
	}

	/**
	 * 匹配房间
	 * 
	 * @param groupId
	 * @param pid
	 * @param session
	 * @param platform
	 * @return
	 * @throws Exception
	 */
	public static ITObject matchRoom(int groupId, int pid, String session, String platform, boolean is_null)
			throws Exception {

		int min_value = pid * 10000 + 1101;
		int max_value = pid * 10000 + (is_null ? 1101 : 9999);
		String grooms_key = GroupCache.genRoomsKey(groupId);
		ITObject resData = null;
		Set<String> rooms = null;
		Jedis jedis = Redis.use("group1_db11").getJedis();
		try {

			rooms = jedis.zrevrangeByScore(grooms_key, max_value, min_value);

			Set<String> delRooms = new HashSet<String>();
			for (String room : rooms) {
				List<String> room_list = Redis.use("group1_db0").hmget(room, "fake");

				String fake_json = room_list.get(0);
				if (fake_json == null) {
					continue;
				}

				delRooms.add(room);
			}
			rooms.removeAll(delRooms);
		} finally {
			jedis.close();
		}

		boolean isWhite = false;
		boolean isBlack = false;
		boolean xingyuhao = false;
		AccountBean acc = AccountCache.getAccount(session);
		List<String> s_list = Redis.use("group1_db0").hmget(session, "regTime");
		String robotInfo = s_list.get(0);
		if (StringUtil.isNotEmpty(robotInfo) && robotInfo.equals("9999")) {
			log.error("robot:" + acc.id + " match room error");
			throw new WebException(ErrorCode.ROOM_CLOSE);
		}
		int uid = acc.id;
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		List<Integer> t_list = Utility.getMemberParents(jedis10, groupId, uid, true);
		String gm_keyx = GroupMemberCache.genKey(groupId, uid);
		String blackx = jedis10.hget(gm_keyx, "black");
		if (StringUtil.isNotEmpty(blackx) && blackx.equals("2")) {
			isWhite = true;
		}
		if (StringUtil.isNotEmpty(blackx) && blackx.equals("1")) {
			isBlack = true;
		}
		jedis10.close();

		if (t_list != null) {
			// 判断阀值
			for (Integer par : t_list) {
				// get aoto_score
				String gm_par_key = GroupMemberCache.genKey(groupId, par);
				String autoscore1 = Redis.use("group1_db10").hget(gm_par_key, "group_black_max_value");
				Integer autoscore = 0;
				if (!StringUtil.isEmpty(autoscore1)) {
					autoscore = Integer.parseInt(autoscore1);
				}

				if (autoscore > 0) {
					ITObject totalHp = GroupService.getHpTotal(groupId, par, 30);
					Long hp = totalHp.getLong("hp");
					if (hp / 100 < autoscore) {
						throw new WebException(ErrorCode.GROUP_MEMBER_BAN);
					}
				}
			}
		}

//		if (Redis.use("group1_db1").sismember("gods", Integer.toString(uid))) {
//			xingyuhao = true;
//		}
//
//		if (xingyuhao) {
//
//			List<String> blackRooms = new ArrayList<>();
//			List<String> whiteRooms = new ArrayList<>();
//			List<String> genralRooms = new ArrayList<>();
//			for (String room : rooms) {
//				List<String> room_list = Redis.use("group1_db0").hmget(room, "players");
//
//				String players_json = room_list.get(0);
//				if (players_json == null) {
//					continue;
//				}
//
//				ITArray players = TArray.newFromJsonData(players_json);
//
//				boolean hasblack = false;
//				boolean haswhite = false;
//				for (int i = 0; i < players.size(); i++) {
//					int player_id = players.getInt(i);
//					if (player_id == uid)
//						continue;
//					if (Redis.use("group1_db1").sismember("gods", Integer.toString(player_id))) {
//						haswhite = true;
//					}
//					String gm_key = GroupMemberCache.genKey(groupId, player_id);
//					String black = Redis.use("group1_db10").hget(gm_key, "black");
//					if (StringUtil.isNotEmpty(black) && black.equals("2")) {
//						haswhite = true;
//					}
//					if (StringUtil.isNotEmpty(black) && black.equals("1")) {
//						hasblack = true;
//					}
//				}
//
//				if (haswhite) {
//					whiteRooms.add(room);
//				}
//				if (hasblack) {
//					blackRooms.add(room);
//				}
//				if (haswhite == false && hasblack == false) {
//					genralRooms.add(room);
//				}
//			}
//
//			if (blackRooms.size() == 0) {
//				blackRooms.addAll(genralRooms);
//				// if (blackRooms.size() == 0)
//				// {
//				// blackRooms.addAll(whiteRooms);
//				// }
//			}
//
//			if (blackRooms.size() > 0) {
//				Collections.shuffle(blackRooms);
//
//				for (String room : blackRooms) {
//					try {
//						resData = publicJoinRoom(groupId, session, room, platform);
//						break;
//					} catch (WebException e) {
//						int code = e.getCode();
//						if (code == ErrorCode.GROUP_ROOM_DEL || code == ErrorCode.NO_ROOM_NUM) {
//							Redis.use("group1_db11").zrem(grooms_key, room);
//						} else if (code == ErrorCode.NO_SERVICE || code == ErrorCode.GROUP_BAN
//								|| code == ErrorCode.GROUP_LIMIT_NO_HP || code == ErrorCode.GROUP_MEMBER_BAN
//								|| code == ErrorCode.GROUP_BAN_PLAY) {
//							throw e;
//						}
//					}
//				}
//			}
//		} else if (isWhite) {
//
//
//
//
//			List<String> blackRooms = new ArrayList<>();
//			List<String> whiteRooms = new ArrayList<>();
//			List<String> genralRooms = new ArrayList<>();
//			for (String room : rooms) {
//				List<String> room_list = Redis.use("group1_db0").hmget(room, "players");
//
//				String players_json = room_list.get(0);
//				if (players_json == null) {
//					continue;
//				}
//
//				ITArray players = TArray.newFromJsonData(players_json);
//
//				boolean hasblack = false;
//				boolean haswhite = false;
//				for (int i = 0; i < players.size(); i++) {
//					int player_id = players.getInt(i);
//					if (Redis.use("group1_db1").sismember("gods", Integer.toString(player_id))) {
//						haswhite = true;
//					}
//					String gm_key = GroupMemberCache.genKey(groupId, player_id);
//					String black = Redis.use("group1_db10").hget(gm_key, "black");
//					if (StringUtil.isNotEmpty(black) && black.equals("2")) {
//						haswhite = true;
//					}
//					if (StringUtil.isNotEmpty(black) && black.equals("1")) {
//						hasblack = true;
//					}
//				}
//
//				if (haswhite) {
//					whiteRooms.add(room);
//				}
//				if (hasblack) {
//					blackRooms.add(room);
//				}
//				if (hasblack == false && haswhite == false) {
//					genralRooms.add(room);
//				}
//			}
//
//			if (blackRooms.size() == 0) {
//				blackRooms.addAll(genralRooms);
//				if (blackRooms.size() == 0) {
//					blackRooms.addAll(whiteRooms);
//				}
//			}
//
//			if (blackRooms.size() > 0) {
//				Collections.shuffle(blackRooms);
//
//				for (String room : blackRooms) {
//					try {
//						resData = publicJoinRoom(groupId, session, room, platform);
//						break;
//					} catch (WebException e) {
//						int code = e.getCode();
//						if (code == ErrorCode.GROUP_ROOM_DEL || code == ErrorCode.NO_ROOM_NUM) {
//							Redis.use("group1_db11").zrem(grooms_key, room);
//						} else if (code == ErrorCode.NO_SERVICE || code == ErrorCode.GROUP_BAN
//								|| code == ErrorCode.GROUP_LIMIT_NO_HP || code == ErrorCode.GROUP_MEMBER_BAN
//								|| code == ErrorCode.GROUP_BAN_PLAY) {
//							throw e;
//						}
//					}
//				}
//			} else {
//				for (String room : rooms) {
//					try {
//						resData = publicJoinRoom(groupId, session, room, platform);
//						break;
//					} catch (WebException e) {
//						int code = e.getCode();
//						if (code == ErrorCode.GROUP_ROOM_DEL || code == ErrorCode.NO_ROOM_NUM) {
//							Redis.use("group1_db11").zrem(grooms_key, room);
//						} else if (code == ErrorCode.NO_SERVICE || code == ErrorCode.GROUP_BAN
//								|| code == ErrorCode.GROUP_LIMIT_NO_HP || code == ErrorCode.GROUP_MEMBER_BAN
//								|| code == ErrorCode.GROUP_BAN_PLAY) {
//							throw e;
//						}
//					}
//				}
//			}
//		} else if (isBlack) {
//
//			List<String> blackRooms = new ArrayList<>();
//			List<String> whiteRooms = new ArrayList<>();
//			List<String> genralRooms = new ArrayList<>();
//			for (String room : rooms) {
//
//				List<String> room_list = Redis.use("group1_db0").hmget(room, "players");
//
//				String players_json = room_list.get(0);
//				if (players_json == null) {
//					continue;
//				}
//
//				ITArray players = TArray.newFromJsonData(players_json);
//
//
//				boolean hasblack = false;
//				boolean haswhite = false;
//				for (int i = 0; i < players.size(); i++) {
//					int player_id = players.getInt(i);
//					if (Redis.use("group1_db1").sismember("gods", Integer.toString(player_id))) {
//						haswhite = true;
//					}
//					String gm_key = GroupMemberCache.genKey(groupId, player_id);
//					String black = Redis.use("group1_db10").hget(gm_key, "black");
//					if (StringUtil.isNotEmpty(black) && black.equals("1")) {
//						hasblack = true;
//					}
//					if (StringUtil.isNotEmpty(black) && black.equals("2")) {
//						haswhite = true;
//					}
//				}
//
//				if (haswhite) {
//					whiteRooms.add(room);
//				}
//				if (hasblack) {
//					blackRooms.add(room);
//				}
//				if (haswhite == false && hasblack == false) {
//					genralRooms.add(room);
//				}
//			}
//
//			if (whiteRooms.size() == 0) {
//				whiteRooms.addAll(genralRooms);
//				if (whiteRooms.size() == 0) {
//					whiteRooms.addAll(blackRooms);
//				}
//			}
//
//			if (whiteRooms.size() > 0) {
//
//				Collections.shuffle(whiteRooms);
//
//				for (String room : whiteRooms) {
//					try {
//						resData = publicJoinRoom(groupId, session, room, platform);
//						break;
//					} catch (WebException e) {
//						int code = e.getCode();
//						if (code == ErrorCode.GROUP_ROOM_DEL || code == ErrorCode.NO_ROOM_NUM) {
//							Redis.use("group1_db11").zrem(grooms_key, room);
//						} else if (code == ErrorCode.NO_SERVICE || code == ErrorCode.GROUP_BAN
//								|| code == ErrorCode.GROUP_LIMIT_NO_HP || code == ErrorCode.GROUP_MEMBER_BAN
//								|| code == ErrorCode.GROUP_BAN_PLAY) {
//							throw e;
//						}
//					}
//				}
//			} else {
//
//				for (String room : rooms) {
//					try {
//						resData = publicJoinRoom(groupId, session, room, platform);
//						break;
//					} catch (WebException e) {
//						int code = e.getCode();
//						if (code == ErrorCode.GROUP_ROOM_DEL || code == ErrorCode.NO_ROOM_NUM) {
//							Redis.use("group1_db11").zrem(grooms_key, room);
//						} else if (code == ErrorCode.NO_SERVICE || code == ErrorCode.GROUP_BAN
//								|| code == ErrorCode.GROUP_LIMIT_NO_HP || code == ErrorCode.GROUP_MEMBER_BAN
//								|| code == ErrorCode.GROUP_BAN_PLAY) {
//							throw e;
//						}
//					}
//				}
//			}
//		} else {
//
//			List<String> blackRooms = new ArrayList<>();
//			List<String> whiteRooms = new ArrayList<>();
//			List<String> genralRooms = new ArrayList<>();
//			for (String room : rooms) {
//				List<String> room_list = Redis.use("group1_db0").hmget(room, "players");
//
//				String players_json = room_list.get(0);
//				if (players_json == null) {
//					continue;
//				}
//
//				ITArray players = TArray.newFromJsonData(players_json);
//
//				boolean hasblack = false;
//				boolean haswhite = false;
//				for (int i = 0; i < players.size(); i++) {
//					int player_id = players.getInt(i);
//					if (Redis.use("group1_db1").sismember("gods", Integer.toString(player_id))) {
//						haswhite = true;
//					}
//					String gm_key = GroupMemberCache.genKey(groupId, player_id);
//					String black = Redis.use("group1_db10").hget(gm_key, "black");
//					if (StringUtil.isNotEmpty(black) && black.equals("1")) {
//						hasblack = true;
//					}
//					if (StringUtil.isNotEmpty(black) && black.equals("2")) {
//						haswhite = true;
//					}
//				}
//
//				if (haswhite) {
//					whiteRooms.add(room);
//				}
//				if (hasblack) {
//					blackRooms.add(room);
//				}
//				if (hasblack == false && haswhite == false) {
//					genralRooms.add(room);
//				}
//			}
//
//			if (genralRooms.size() == 0) {
//				genralRooms.addAll(blackRooms);
//				if (genralRooms.size() == 0) {
//					genralRooms.addAll(whiteRooms);
//				}
//			}
//
//			if (genralRooms.size() > 0) {
//				Collections.shuffle(genralRooms);
//
//				for (String room : genralRooms) {
//					try {
//						resData = publicJoinRoom(groupId, session, room, platform);
//						break;
//					} catch (WebException e) {
//						int code = e.getCode();
//						if (code == ErrorCode.GROUP_ROOM_DEL || code == ErrorCode.NO_ROOM_NUM) {
//							Redis.use("group1_db11").zrem(grooms_key, room);
//						} else if (code == ErrorCode.NO_SERVICE || code == ErrorCode.GROUP_BAN
//								|| code == ErrorCode.GROUP_LIMIT_NO_HP || code == ErrorCode.GROUP_MEMBER_BAN
//								|| code == ErrorCode.GROUP_BAN_PLAY) {
//							throw e;
//						}
//					}
//				}
//			} else {
//				for (String room : rooms) {
//					try {
//						resData = publicJoinRoom(groupId, session, room, platform);
//						break;
//					} catch (WebException e) {
//						int code = e.getCode();
//						if (code == ErrorCode.GROUP_ROOM_DEL || code == ErrorCode.NO_ROOM_NUM) {
//							Redis.use("group1_db11").zrem(grooms_key, room);
//						} else if (code == ErrorCode.NO_SERVICE || code == ErrorCode.GROUP_BAN
//								|| code == ErrorCode.GROUP_LIMIT_NO_HP || code == ErrorCode.GROUP_MEMBER_BAN
//								|| code == ErrorCode.GROUP_BAN_PLAY) {
//							throw e;
//						}
//					}
//				}
//			}
//		}

		// 检测是否可娱乐
		if (t_list != null) {
			for (int i = 0; i < t_list.size(); i++) {
				ITArray list = GroupService.getMemberPlayList(groupId, t_list.get(i));
				if (list.size() != 0) {

					for (int j = 0; j < list.size(); j++) {
						if (list.getInt(j) == pid) {
							throw new WebException(ErrorCode.GROUP_BAN_PLAY);
						}
					}

				}
			}
		}

		for (String room : rooms) {
			try {
				resData = publicJoinRoom(groupId, session, room, platform);
				log.info(String.format("match_room,room:%s", room));
				break;
			} catch (WebException e) {
				int code = e.getCode();
				if (code == ErrorCode.GROUP_ROOM_DEL || code == ErrorCode.NO_ROOM_NUM) {
					Redis.use("group1_db11").zrem(grooms_key, room);
				} else if (code == ErrorCode.NO_SERVICE || code == ErrorCode.GROUP_BAN
						|| code == ErrorCode.GROUP_LIMIT_NO_HP || code == ErrorCode.GROUP_MEMBER_BAN
						|| code == ErrorCode.GROUP_BAN_PLAY) {
					throw e;
				}
			}
		}

		if (resData == null) {
			String key = createGroupRoom(session, groupId, pid);
			resData = publicJoinRoom(groupId, session, key, platform);
		}


		//检查玩家今天是否已经与机器人对打过
		if (!isPlayerRobotToday(acc.id)) {
			log.info("matchRoom() ==> 玩家今天和机器人对打过了isPlayerRobotToday = " + isPlayerRobotToday(acc.id));
			//匹配
			Jedis jedis11 = Redis.use("group1_db11").getJedis();
			Jedis jedis0 = Redis.use("group1_db0").getJedis();
			try {
				jedis11.zremrangeByScore(grooms_key, 0, 0);

				//通知 机器人
				String gameId1 = jedis11.hget("g{383709}:play:"+pid,"gameId");
				String robot_host = null;
				if(StringUtil.isNotEmpty(gameId1)) {
					/*if (Integer.parseInt(gameId1) == 17) {
						robot_host = "8.138.120.132:8917";
					}*/
					if (Integer.parseInt(gameId1) == 22) {
						robot_host = "8.138.120.132:8722";
					}

					log.info("StringUtil.isNotEmpty(gameId1) && StringUtil.isNotEmpty(robot_host)" + gameId1 + " " +robot_host);
					if (StringUtil.isNotEmpty(gameId1) && StringUtil.isNotEmpty(robot_host)) {
						String roomid = resData.getString("room_id");
						log.info("room_idroom_idroom_id" + roomid);
						sendRobotData(roomid, groupId,robot_host,Integer.parseInt(gameId1));
					}

				}

			} finally {
				jedis11.close();
				jedis0.close();
			}
		}
		return resData;
	}

	/**
	 * 检查玩家今天是否已经与机器人对打过
	 * @param playerId 玩家ID
	 */
	private static boolean isPlayerRobotToday(int playerId) {
		Jedis jedis = Redis.use("group1_db3").getJedis();
		try {
			String redisKey = "player_user:" + playerId;
			String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			String matchedDate = jedis.get(redisKey);

			if (matchedDate != null && matchedDate.equals(today)) {
				log.info("玩家{" + playerId + "}当天已与机器人对打");
				return true;
			}
		} catch (Exception e) {
			log.error("检查玩家对打记录时发生异常", e);
		} finally {
			jedis.close();
		}
		return false;
	}

	/**
	 * //发起机器人请求（HTTP 同步方式，替代原 TCP 异步多线程方式）
	 *
	 * 业务场景：房间里有 1 个真人玩家时，调用此方法让指定机器人加入房间陪打。
	 * 机器人ID 直接从房间的 players 列表中取（已经存在房间里的人是机器人）。
	 *
	 * @param roomid    房间ID
	 * @param groupid   群组ID
	 * @param robot_host 机器人服务 TCP 地址 "ip:tcpport"，HTTP 端口 = TCP端口 + 1000
	 * @param gameId
	 * @return
	 */
	public static ITObject sendRoboJointData(String roomid,int groupid, String robot_host,int gameId){
		System.out.println("sendRoboJointData");
		Jedis js0 = Redis.use("group1_db0").getJedis();
		Jedis js3 = Redis.use("group1_db3").getJedis();
		try {
			String robot = js0.hget("room:"+roomid,"robot");
			String plays = js0.hget("room:"+roomid,"players");
			if (StringUtil.isEmpty(plays)) {
				return null;
			}
			ITArray players = TArray.newFromJsonData(plays);
			if (players.size()!=1){
				return null;
			}
			int robotid = players.getInt(0);
			if(robotid==0){
				return null;
			}

			String wokelock = "wokelock";
			js3.set(wokelock+robotid, String.valueOf(1));
			js3.expire(wokelock+robotid, 35);
			sleep(1000);
			//通过 HTTP 同步调用机器人服务的 225 协议入口
			String url = buildRobotHttpUrl(robot_host);
			String body = buildJoinRoomBody(robotid, roomid, groupid);
			String resp = httpPostJson(url, body, 5000);
			log.info("sendRoboJointData roomid:"+roomid+" robotid:"+robotid+" resp:"+resp);
		} catch (Exception e){
			log.error("sendRoboJointData 异常 roomid:"+roomid, e);
		} finally {
			js0.close();
			js3.close();
		}
		return null;
	}

	/**
	 * //发起机器人请求
	 * @return
	 */
	public static ITObject sendRobotData(String roomid,int groupid,String robot_host,int gameId){

		//Jedis js1 = Redis.use("group1_db3").getJedis();


		log.info("sendRobotData :"+roomid+"groupid:"+groupid+"robot_host:"+robot_host);
		Jedis js3 = Redis.use("group1_db3").getJedis();
		try {

			String flag = js3.get("roomsend" + roomid);
			if (flag != null) {
				return null;
			}
			js3.set("roomsend" + roomid, 1 + "");
			js3.expire("roomsend" + roomid, 5);

			int robotid = 0;
			Set<String> jsRobot = js3.smembers("robots");
			log.info("jsRobot" + jsRobot);
			String wokelock = "wokelock";
			for (String key : jsRobot) {
				String tmp = js3.get(wokelock + key);
				if (StringUtil.isEmpty(tmp)) {
					robotid = Integer.parseInt(key);
				}
			}


//            int robotid = 0;
//            Map<String, String> jsRobot = js1.hgetAll("{robot426149}" + gameId);
//            String wokelock = "wokelock";
//            for (String key : jsRobot.keySet()) {
//                String tmp = js1.get(wokelock + key);
//                if (StringUtil.isEmpty(tmp)) {
//                    robotid = Integer.parseInt(key);
//                    break;
//                }
//            }

			if (robotid == 0) {
				log.info("sendRobotData 没有可用机器人 roomid:" + roomid);
				return null;
			}

			//设置机器人占用标记
			js3.set(wokelock + robotid, String.valueOf(1));
			js3.expire(wokelock + robotid, 35);

//            js1.expire(wokelock + robotid, 900);

			//通过 HTTP 同步调用机器人服务的 225 协议入口
			sleep(1000);
			String url = buildRobotHttpUrl(robot_host);
			String body = buildJoinRoomBody(robotid, roomid, groupid);
			String resp = httpPostJson(url, body, 5000);
			log.info("sendRobotData roomid:" + roomid + " robotid:" + robotid + " resp:" + resp);
		} catch (Exception e) {
			log.error("sendRobotData 异常 roomid:" + roomid, e);
		} finally {
			js3.close();
		}
		//client.killConnection();
		return null;
	}


	/**
	 * 根据机器人 TCP 地址推导 HTTP URL
	 * 规则：HTTP 端口 = TCP 端口 + 1000（与 robot_mj_hz 的 Config.HTTP_SERVER_PORT 一致）
	 * 例：robot_host="192.168.0.32:8722" → "http://192.168.0.32:9722/robot/joinRoom"
	 */
	private static String buildRobotHttpUrl(String robot_host) {
		String host;
		int tcpPort;
		int colonIdx = robot_host.indexOf(':');
		if (colonIdx > 0) {
			host = robot_host.substring(0, colonIdx);
			tcpPort = Integer.parseInt(robot_host.substring(colonIdx + 1));
		} else {
			host = robot_host;
			tcpPort = 8722;
		}
		int httpPort = tcpPort + 1000;
		return "http://" + host + ":" + httpPort + "/robot/joinRoom";
	}

	/**
	 * 构造 225 协议请求体 JSON
	 */
	private static String buildJoinRoomBody(int robotid, String roomid, int groupid) {
		StringBuilder sb = new StringBuilder(64);
		sb.append("{\"robotid\":").append(robotid)
				.append(",\"roomid\":\"").append(roomid).append("\"")
				.append(",\"groupid\":").append(groupid)
				.append("}");
		return sb.toString();
	}

	/**
	 * 通过 JDK HttpURLConnection 发送 POST JSON 请求（同步）
	 * 替代原 TaurusClient + CompletableFuture + eventThread 异步 TCP 方式
	 *
	 * @param urlStr    完整 URL
	 * @param jsonBody  请求体 JSON 字符串
	 * @param timeoutMs 连接与读取超时（毫秒）
	 * @return 响应体字符串；失败时返回错误描述
	 */
	private static String httpPostJson(String urlStr, String jsonBody, int timeoutMs) {
		HttpURLConnection conn = null;
		try {
			URL url = new URL(urlStr);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
			conn.setConnectTimeout(timeoutMs);
			conn.setReadTimeout(timeoutMs);
			conn.setDoOutput(true);
			conn.setDoInput(true);

			//写入请求体
			byte[] bodyBytes = jsonBody.getBytes(StandardCharsets.UTF_8);
			try (OutputStream os = conn.getOutputStream()) {
				os.write(bodyBytes);
				os.flush();
			}

			//读取响应
			int code = conn.getResponseCode();
			InputStream is = (code >= 200 && code < 300) ? conn.getInputStream() : conn.getErrorStream();
			String resp = readAll(is);
			if (code < 200 || code >= 300) {
				log.warn("httpPostJson 失败 url:" + urlStr + " code:" + code + " resp:" + resp);
				return "HTTP " + code + ": " + resp;
			}
			return resp;
		} catch (Exception e) {
			log.error("httpPostJson 异常 url:" + urlStr, e);
			return "ERROR:" + e.getMessage();
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
		}
	}

	/**
	 * 读取输入流为字符串
	 */
	private static String readAll(InputStream in) throws Exception {
		if (in == null) return "";
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buf = new byte[1024];
		int n;
		while ((n = in.read(buf)) > 0) {
			baos.write(buf, 0, n);
		}
		in.close();
		return new String(baos.toByteArray(), StandardCharsets.UTF_8);
	}


	/**
	 * 删除房间
	 * 
	 * @param groupId
	 * @param roomId
	 * @return
	 * @throws Exception
	 */
	public static final ITObject delRoom(int groupId, String roomId) throws Exception {
		String tag_key = "room:" + roomId;
		Jedis jedis0 = Redis.use("group1_db0").getJedis();
//		String svr = StringUtil.Empty;

		RedisLock lock = new RedisLock(tag_key, jedis0);
		try {
			lock.lock();
			List<String> list = jedis0.hmget(tag_key, "AA", "payer", "pay", "group", "game", "status", "gpid", "fake",
					"players");
			String status = list.get(5);
			if (StringUtil.isEmpty(status)) {
				throw new WebException(ErrorCode.NO_ROOM_NUM);
			}
			int _status = Integer.parseInt(status);
			if (_status == 2 || _status == 3) {
				throw new WebException(ErrorCode.GROUP_ROOM_DEL);
			}
			String group = list.get(3);
			int _gid = 0;
			if (StringUtil.isNotEmpty(group)) {
				_gid = Integer.parseInt(group);
			}

			String strGpid = list.get(6);
			int _gpid = 0;
			if (StringUtil.isNotEmpty(strGpid)) {
				_gpid = Integer.parseInt(strGpid);
			}
			if (_gid != groupId) {
				throw new WebException(ErrorCode.GROUP_NOT_CURGROUP_ROOM);
			}
			String strFake = list.get(7);
			if (_status == 0) {
				if (strFake == null) {
					int gameId = Integer.parseInt(list.get(4));
					boolean pay_AA = Integer.parseInt(list.get(0)) == 1;
					if (!pay_AA) {
						int payer = Integer.parseInt(list.get(1));
						int pay = Integer.parseInt(list.get(2));
						Utility.payDiamo(EventType.REDIS_EVENT_BACK_PAY, payer, gameId, pay, groupId, _gpid);
					}
				}
			}
			if (strFake != null) {
				try {
					ITArray players = TArray.newFromJsonData(list.get(8));
					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));

					}
				} catch (Exception e) {

				}
			}
			jedis0.hset(tag_key, "status", "2");
			BaseCache.updateCacheVer(jedis0, tag_key);
			GroupPublisherService.delRoomEvt(groupId, roomId);
//			svr = jedis0.hget(tag_key, "svr");
//		
//			List<String> server_ip = Redis.use("group1_db0").hmget(svr, "ip", "port");
//			String ip = server_ip.get(0);
//			if(StringUtil.isEmpty(ip)) {
//				throw new WebException(ErrorCode.NO_SERVICE);
//			}
//			String port =server_ip.get(1);
			ITObject obj = TObject.newInstance();
//			obj.putString("ip", ip);
//			obj.putInt("port", Integer.parseInt(port));
			return obj;
		} finally {
			lock.unlock();
		}

	}
}
