util_match.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. --[[
  2. Descripttion:战斗房间
  3. version:
  4. Author: Neo,Huang
  5. Date: 2023-11-16 23:24:42
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2023-11-16 23:25:34
  8. --]]
  9. local lib_battle_redis = require("lib_battle_redis")
  10. local timeUtil = require("utils.timeUtil")
  11. local root = {}
  12. local function _get_global_room_key()
  13. return "global:room"
  14. end
  15. local function _get_room_key(id)
  16. return string.format("room:%s", tostring(id))
  17. end
  18. local function _get_active_room_id_key()
  19. return "active:rooms"
  20. end
  21. -- 分配房间ID
  22. function root:gen_room_id()
  23. local key = _get_global_room_key()
  24. local subKey = "max:room:id"
  25. local id = lib_battle_redis:hincrby(key, subKey, 1)
  26. if id > 200000000 then
  27. id = 1
  28. lib_battle_redis:hset(key, subKey, id)
  29. end
  30. return id
  31. end
  32. -- 获取当前活跃房间ID列表
  33. function root:get_active_room_id_list()
  34. local key = _get_active_room_id_key()
  35. return lib_battle_redis:smembers(key)
  36. end
  37. -- 创建房间
  38. function root:create_match(uid, playCount, boxIdList)
  39. if is_empty(uid) then
  40. return false
  41. end
  42. if is_empty(playCount) or playCount < 2 or playCount > 3 then
  43. return false
  44. end
  45. if is_empty(boxIdList) or #boxIdList < 1 then
  46. return false
  47. end
  48. local roomId = self:gen_room_id()
  49. -- 设置房间信息
  50. local playerList = {{uid = uid, seat = 1, status = 1}}
  51. local key = _get_room_key(roomId)
  52. lib_battle_redis:hset(key, "createTime", timeUtil.now()) -- 创建时间
  53. lib_battle_redis:hset(key, "hostUid", uid) -- 房主
  54. lib_battle_redis:hset(key, "playCount", playCount) -- 战斗人数
  55. lib_battle_redis:hset(key, "boxIdList", boxIdList) -- 战斗箱子ID列表
  56. lib_battle_redis:hset(key, "playerList", playerList) -- 玩家列表
  57. lib_battle_redis:hset(key, "status", 0) -- 战斗状态
  58. -- 房间进入准备战斗集合
  59. key = _get_active_room_id_key()
  60. lib_battle_redis:sadd(key, roomId)
  61. return true
  62. end
  63. -- 销毁房间
  64. function root:destroy_room(roomId)
  65. if is_empty(roomId) then
  66. return false
  67. end
  68. local key = _get_room_key(roomId)
  69. -- 房间是否存在
  70. local isExist = lib_battle_redis:exists(key)
  71. if not isExist then
  72. -- 从活跃房间集合删除
  73. lib_battle_redis:sismerber(_get_active_room_id_key(), roomId)
  74. return false
  75. end
  76. local status = lib_battle_redis:hget(key, "status")
  77. if tonumber(status) >= 0 then
  78. -- 战斗已开始
  79. return false
  80. end
  81. -- 删除房间信息
  82. lib_battle_redis:del(key)
  83. -- 从活跃房间集合删除
  84. lib_battle_redis:sismerber(_get_active_room_id_key(), roomId)
  85. return true
  86. end
  87. -- 是否等待开战结束
  88. function root:is_wait_battle_end(roomId)
  89. if is_empty(roomId) then
  90. return false
  91. end
  92. local key = _get_room_key(roomId)
  93. -- 房间是否存在
  94. local isExist = lib_battle_redis:exists(key)
  95. if not isExist then
  96. return true
  97. end
  98. local currTime = timeUtil.now()
  99. local createTime = lib_battle_redis:hget(key, "createTime")
  100. local waitSeconds = 10
  101. if currTime < createTime + waitSeconds then
  102. return false
  103. end
  104. return true
  105. end
  106. return root