--[[ Descripttion:战斗房间 version: Author: Neo,Huang Date: 2023-11-16 23:24:42 LastEditors: Neo,Huang LastEditTime: 2023-11-16 23:25:34 --]] local lib_battle_redis = require("lib_battle_redis") local timeUtil = require("utils.timeUtil") local root = {} local function _get_global_room_key() return "global:room" end local function _get_room_key(id) return string.format("room:%s", tostring(id)) end local function _get_active_room_id_key() return "active:rooms" end -- 分配房间ID function root:gen_room_id() local key = _get_global_room_key() local subKey = "max:room:id" local id = lib_battle_redis:hincrby(key, subKey, 1) if id > 200000000 then id = 1 lib_battle_redis:hset(key, subKey, id) end return id end -- 获取当前活跃房间ID列表 function root:get_active_room_id_list() local key = _get_active_room_id_key() return lib_battle_redis:smembers(key) end -- 创建房间 function root:create_match(uid, playCount, boxIdList) if is_empty(uid) then return false end if is_empty(playCount) or playCount < 2 or playCount > 3 then return false end if is_empty(boxIdList) or #boxIdList < 1 then return false end local roomId = self:gen_room_id() -- 设置房间信息 local playerList = {{uid = uid, seat = 1, status = 1}} local key = _get_room_key(roomId) lib_battle_redis:hset(key, "createTime", timeUtil.now()) -- 创建时间 lib_battle_redis:hset(key, "hostUid", uid) -- 房主 lib_battle_redis:hset(key, "playCount", playCount) -- 战斗人数 lib_battle_redis:hset(key, "boxIdList", boxIdList) -- 战斗箱子ID列表 lib_battle_redis:hset(key, "playerList", playerList) -- 玩家列表 lib_battle_redis:hset(key, "status", 0) -- 战斗状态 -- 房间进入准备战斗集合 key = _get_active_room_id_key() lib_battle_redis:sadd(key, roomId) return true end -- 销毁房间 function root:destroy_room(roomId) if is_empty(roomId) then return false end local key = _get_room_key(roomId) -- 房间是否存在 local isExist = lib_battle_redis:exists(key) if not isExist then -- 从活跃房间集合删除 lib_battle_redis:sismerber(_get_active_room_id_key(), roomId) return false end local status = lib_battle_redis:hget(key, "status") if tonumber(status) >= 0 then -- 战斗已开始 return false end -- 删除房间信息 lib_battle_redis:del(key) -- 从活跃房间集合删除 lib_battle_redis:sismerber(_get_active_room_id_key(), roomId) return true end -- 是否等待开战结束 function root:is_wait_battle_end(roomId) if is_empty(roomId) then return false end local key = _get_room_key(roomId) -- 房间是否存在 local isExist = lib_battle_redis:exists(key) if not isExist then return true end local currTime = timeUtil.now() local createTime = lib_battle_redis:hget(key, "createTime") local waitSeconds = 10 if currTime < createTime + waitSeconds then return false end return true end return root