Administrator 5 bulan lalu
induk
melakukan
a15ae2a87f

+ 34 - 0
incubator-game/src/main/java/com/incubator/game/constant/RoomStatus.java

@@ -0,0 +1,34 @@
+package com.incubator.game.constant;
+
+public class RoomStatus {
+    // 服务器IP或者域名地址
+    private String name;
+    // 在线用户数量
+    private Long roomId;
+    //房间比赛类型
+    private Long roomType;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Long getRoomId() {
+        return roomId;
+    }
+
+    public void setRoomId(Long roomId) {
+        this.roomId = roomId;
+    }
+
+    public Long getRoomType() {
+        return roomType;
+    }
+
+    public void setRoomType(Long roomType) {
+        this.roomType = roomType;
+    }
+}

+ 153 - 44
incubator-game/src/main/java/com/incubator/game/data/jedis/RedisUtil.java

@@ -6,9 +6,10 @@ import com.dyuproject.protostuff.LinkedBuffer;
 import com.dyuproject.protostuff.ProtostuffIOUtil;
 import com.dyuproject.protostuff.ProtostuffIOUtil;
 import com.dyuproject.protostuff.runtime.RuntimeSchema;
 import com.dyuproject.protostuff.runtime.RuntimeSchema;
 import com.google.gson.Gson;
 import com.google.gson.Gson;
+import com.incubator.game.constant.RoomStatus;
 import com.incubator.game.constant.WkServerStatus;
 import com.incubator.game.constant.WkServerStatus;
-import com.incubator.game.data.po.RoomPO;
 import com.incubator.game.room.Room;
 import com.incubator.game.room.Room;
+import org.apache.poi.ss.formula.functions.T;
 import redis.clients.jedis.Jedis;
 import redis.clients.jedis.Jedis;
 import redis.clients.jedis.exceptions.JedisDataException;
 import redis.clients.jedis.exceptions.JedisDataException;
 
 
@@ -733,12 +734,90 @@ public final class RedisUtil {
 		}
 		}
 	}
 	}
 
 
+
+	/**
+	 * 缓存一个对象到 Redis 的哈希表中
+	 * @param redisKey Redis 键
+	 * @param key 哈希表中的字段名
+	 * @param o 要缓存的对象
+	 */
+	public static <T> void saveHashToRedis(String redisKey, String key, T o) {
+		Jedis jedis = null;
+		try {
+			jedis = JedisPoolUtil.getJedis();
+			jedis.hset(redisKey, key, new Gson().toJson(o));
+		} catch (Exception e) {
+			e.printStackTrace();
+		} finally {
+			if (jedis != null) {
+				jedis.close();
+			}
+		}
+	}
+
+	/**
+	 * 从 Redis 中查询所有对象
+	 * @param redisKey Redis 键
+	 * @param clazz 对象的类型
+	 * @return 包含所有对象的 HashMap
+	 */
+	public static <T> HashMap<String, T> getHashToRedis(String redisKey, Class<T> clazz) {
+		Jedis jedis = null;
+		Gson gson = new Gson();
+		try {
+			jedis = JedisPoolUtil.getJedis();
+			Map<String, String> redisMap = jedis.hgetAll(redisKey);
+			HashMap<String, T> resultMap = new HashMap<>();
+
+			for (Map.Entry<String, String> entry : redisMap.entrySet()) {
+				T obj = gson.fromJson(entry.getValue(), clazz);
+				resultMap.put(entry.getKey(), obj);
+			}
+			return resultMap;
+		} catch (Exception e) {
+			e.printStackTrace();
+			return null;
+		} finally {
+			if (jedis != null) {
+				jedis.close();
+			}
+		}
+	}
+
+	/**
+	 * 从 Redis 中查询单个对象
+	 * @param redisKey Redis 键
+	 * @param key 哈希表中的字段名
+	 * @param clazz 对象的类型
+	 * @return 对应的对象,如果不存在则返回 null
+	 */
+	public static <T> T getHashToRedis(String redisKey, String key, Class<T> clazz) {
+		Jedis jedis = null;
+		Gson gson = new Gson();
+		try {
+			jedis = JedisPoolUtil.getJedis();
+			String json = jedis.hget(redisKey, key);
+			if (json != null) {
+				return gson.fromJson(json, clazz);
+			}
+			return null;
+		} catch (Exception e) {
+			e.printStackTrace();
+			return null;
+		} finally {
+			if (jedis != null) {
+				jedis.close();
+			}
+		}
+	}
+
+
 	/**
 	/**
 	 * 缓存一个WkServerStatus到redis,成功返回1,失败返回0
 	 * 缓存一个WkServerStatus到redis,成功返回1,失败返回0
 	 * @param redisKey
 	 * @param redisKey
 	 * @param wkServerStatus
 	 * @param wkServerStatus
 	 */
 	 */
-	public static void saveWkToRedis(String redisKey, WkServerStatus wkServerStatus) {
+	public static void saveRoomToRedis(String redisKey, RoomStatus wkServerStatus) {
 		Jedis jedis = null;
 		Jedis jedis = null;
 		try {
 		try {
 			jedis = JedisPoolUtil.getJedis();
 			jedis = JedisPoolUtil.getJedis();
@@ -752,23 +831,25 @@ public final class RedisUtil {
 		}
 		}
 
 
 	}
 	}
+
+
 	/**
 	/**
 	 * 从 Redis 中查询所有 WkServerStatus 对象
 	 * 从 Redis 中查询所有 WkServerStatus 对象
 	 * @param redisKey Redis 键
 	 * @param redisKey Redis 键
 	 * @return 包含所有 WkServerStatus 对象的 HashMap
 	 * @return 包含所有 WkServerStatus 对象的 HashMap
 	 */
 	 */
-	public static HashMap<String, WkServerStatus> getWkToRedis(String redisKey) {
+	public static HashMap<String, RoomStatus> getRoomToRedis(String redisKey) {
 		Jedis jedis = null;
 		Jedis jedis = null;
 		Gson gson = new Gson();
 		Gson gson = new Gson();
 		try {
 		try {
 			jedis = JedisPoolUtil.getJedis();
 			jedis = JedisPoolUtil.getJedis();
 			// 从 Redis 中获取哈希表的所有字段和值
 			// 从 Redis 中获取哈希表的所有字段和值
 			Map<String, String> redisMap = jedis.hgetAll(redisKey);
 			Map<String, String> redisMap = jedis.hgetAll(redisKey);
-			HashMap<String, WkServerStatus> resultMap = new HashMap<>();
+			HashMap<String, RoomStatus> resultMap = new HashMap<>();
 
 
 			// 将 Redis 中的 JSON 字符串反序列化为 WkServerStatus 对象
 			// 将 Redis 中的 JSON 字符串反序列化为 WkServerStatus 对象
 			for (Map.Entry<String, String> entry : redisMap.entrySet()) {
 			for (Map.Entry<String, String> entry : redisMap.entrySet()) {
-				WkServerStatus wk = gson.fromJson(entry.getValue(), WkServerStatus.class);
+				RoomStatus wk = gson.fromJson(entry.getValue(), RoomStatus.class);
 				resultMap.put(entry.getKey(), wk);
 				resultMap.put(entry.getKey(), wk);
 			}
 			}
 			return resultMap;
 			return resultMap;
@@ -781,13 +862,14 @@ public final class RedisUtil {
 			}
 			}
 		}
 		}
 	}
 	}
+
 	/**
 	/**
 	 * 从 Redis 中查询单个 WkServerStatus 对象
 	 * 从 Redis 中查询单个 WkServerStatus 对象
 	 * @param redisKey Redis 键
 	 * @param redisKey Redis 键
 	 * @param name 字段名
 	 * @param name 字段名
 	 * @return 对应的 WkServerStatus 对象,如果不存在则返回 null
 	 * @return 对应的 WkServerStatus 对象,如果不存在则返回 null
 	 */
 	 */
-	public static WkServerStatus getWkToRedis(String redisKey, String name) {
+	public static RoomStatus getRoomToRedis(String redisKey, String name) {
 		Jedis jedis = null;
 		Jedis jedis = null;
 		Gson gson = new Gson();
 		Gson gson = new Gson();
 		try {
 		try {
@@ -796,7 +878,7 @@ public final class RedisUtil {
 			String json = jedis.hget(redisKey, name);
 			String json = jedis.hget(redisKey, name);
 			if (json != null) {
 			if (json != null) {
 				// 将 JSON 字符串反序列化为 WkServerStatus 对象
 				// 将 JSON 字符串反序列化为 WkServerStatus 对象
-				return gson.fromJson(json, WkServerStatus.class);
+				return gson.fromJson(json, RoomStatus.class);
 			}
 			}
 			return null;
 			return null;
 		} catch (Exception e) {
 		} catch (Exception e) {
@@ -810,56 +892,83 @@ public final class RedisUtil {
 	}
 	}
 
 
 	/**
 	/**
-	 * 缓存一个roomMap到redis,成功返回1,失败返回0
+	 * 缓存一个WkServerStatus到redis,成功返回1,失败返回0
 	 * @param redisKey
 	 * @param redisKey
-	 * @param roomMap
+	 * @param wkServerStatus
 	 */
 	 */
-	public static void saveRoomMapToRedis(String redisKey, Map<Long, Room> roomMap) {
-		Jedis jedis = JedisPoolUtil.getJedis();
-		for (Map.Entry<Long, Room> entry : roomMap.entrySet()) {
-			jedis.hset(redisKey, entry.getKey().toString(), serializeRoom(entry.getValue()));
+	public static void saveWkToRedis(String redisKey, WkServerStatus wkServerStatus) {
+		Jedis jedis = null;
+		try {
+			jedis = JedisPoolUtil.getJedis();
+			jedis.hset(redisKey, wkServerStatus.getName(), new Gson().toJson(wkServerStatus));
+		} catch (Exception e) {
+			e.printStackTrace();
+		} finally {
+			if (jedis != null) {
+				jedis.close();
+			}
 		}
 		}
-	}
 
 
+	}
 	/**
 	/**
-	 * 从redis中获取roomMap
-	 * @param redisKey
-	 * @return
+	 * 从 Redis 中查询所有 WkServerStatus 对象
+	 * @param redisKey Redis 键
+	 * @return 包含所有 WkServerStatus 对象的 HashMap
 	 */
 	 */
-	public static Map<Long, Room> getRoomMapFromRedis(String redisKey) {
-		Jedis jedis = JedisPoolUtil.getJedis();
-		Map<String, String> redisMap = jedis.hgetAll(redisKey);
-		Map<Long, Room> roomMap = new ConcurrentHashMap<>();
-		for (Map.Entry<String, String> entry : redisMap.entrySet()) {
-			roomMap.put(Long.parseLong(entry.getKey()), deserializeRoom(entry.getValue()));
-		}
-		return roomMap;
-	}
-
-
+	public static HashMap<String, WkServerStatus> getWkToRedis(String redisKey) {
+		Jedis jedis = null;
+		Gson gson = new Gson();
+		try {
+			jedis = JedisPoolUtil.getJedis();
+			// 从 Redis 中获取哈希表的所有字段和值
+			Map<String, String> redisMap = jedis.hgetAll(redisKey);
+			HashMap<String, WkServerStatus> resultMap = new HashMap<>();
 
 
+			// 将 Redis 中的 JSON 字符串反序列化为 WkServerStatus 对象
+			for (Map.Entry<String, String> entry : redisMap.entrySet()) {
+				WkServerStatus wk = gson.fromJson(entry.getValue(), WkServerStatus.class);
+				resultMap.put(entry.getKey(), wk);
+			}
+			return resultMap;
+		} catch (Exception e) {
+			e.printStackTrace();
+			return null;
+		} finally {
+			if (jedis != null) {
+				jedis.close();
+			}
+		}
+	}
 	/**
 	/**
-	 * 从 Redis Hash roomMap 中移除指定的键
-	 *
-	 * @param redisKey Redis Hash 的键
-	 * @param roomId   要移除的房间 ID
+	 * 从 Redis 中查询单个 WkServerStatus 对象
+	 * @param redisKey Redis 键
+	 * @param name 字段名
+	 * @return 对应的 WkServerStatus 对象,如果不存在则返回 null
 	 */
 	 */
-	public static void removeKeyFromRoomMap(String redisKey, Long roomId) {
-		Jedis jedis = JedisPoolUtil.getJedis();
-		jedis.hdel(redisKey, roomId.toString());
+	public static WkServerStatus getWkToRedis(String redisKey, String name) {
+		Jedis jedis = null;
+		Gson gson = new Gson();
+		try {
+			jedis = JedisPoolUtil.getJedis();
+			// 从 Redis 中获取指定字段的值
+			String json = jedis.hget(redisKey, name);
+			if (json != null) {
+				// 将 JSON 字符串反序列化为 WkServerStatus 对象
+				return gson.fromJson(json, WkServerStatus.class);
+			}
+			return null;
+		} catch (Exception e) {
+			e.printStackTrace();
+			return null;
+		} finally {
+			if (jedis != null) {
+				jedis.close();
+			}
+		}
 	}
 	}
 
 
 
 
 
 
-	private static String serializeRoom(Room room) {
-		// 使用 JSON 序列化 Room 对象
-		return JSONUtil.toJsonStr(room);
-	}
-
-	private static Room deserializeRoom(String json) {
-		// 使用 JSON 反序列化为 Room 对象
-		return  JSONUtil.toBean(json, Room.class);
-	}