util_match.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 redisBattleUtil = require("redisBattleUtil")
  12. local util_player = require("utils.util_player")
  13. local battleData = require("data.battle")
  14. local boxAdapt = require("adapt.boxAdapt")
  15. local root = {}
  16. local function _get_global_room_key()
  17. return "global:room"
  18. end
  19. local function _get_room_key(id)
  20. return string.format("room:%s", tostring(id))
  21. end
  22. local function _get_active_room_id_key()
  23. return "active:rooms"
  24. end
  25. -- 分配房间ID
  26. function root:gen_room_id()
  27. local key = _get_global_room_key()
  28. local subKey = "max:room:id"
  29. local id = lib_battle_redis:hincrby(key, subKey, 1)
  30. if id > 200000000 then
  31. id = 1
  32. lib_battle_redis:hset(key, subKey, id)
  33. end
  34. return id
  35. end
  36. -- 获取当前活跃房间ID列表
  37. function root:get_active_room_id_list()
  38. local key = _get_active_room_id_key()
  39. return lib_battle_redis:smembers(key)
  40. end
  41. -- 打包房间玩家信息
  42. function root:pack_room_player_info(roomId, uid)
  43. local info = self:get_room_player_info(roomId, uid)
  44. if is_empty(info) then
  45. return
  46. end
  47. info.playerInfo = util_player:get_base_info(uid)
  48. return info
  49. end
  50. -- 打包房间玩家信息列表
  51. function root:pack_room_player_info_list(playerList)
  52. if is_empty(playerList) then
  53. return
  54. end
  55. local infoList = {}
  56. for _, v in ipairs(playerList) do
  57. local info = {
  58. playerInfo = util_player:get_base_info(v.uid),
  59. seatId = v.seatId,
  60. status = v.status
  61. }
  62. table.insert(infoList, info)
  63. end
  64. return infoList
  65. end
  66. -- 打包房间信息
  67. function root:pack_room_info(roomId)
  68. if is_empty(roomId) then
  69. return
  70. end
  71. local key = _get_room_key(roomId)
  72. -- 房间是否存在
  73. local isExist = lib_battle_redis:exists(key)
  74. if not isExist then
  75. return
  76. end
  77. local info = {
  78. roomId = roomId,
  79. playCount = redisBattleUtil.hget_int(key, "playCount"),
  80. battleBoxList = redisBattleUtil.hget_json(key, "boxIdList"),
  81. status = redisBattleUtil.hget_int(key, "status"),
  82. createTime = redisBattleUtil.hget_int(key, "createTime")
  83. }
  84. -- 玩家信息列表
  85. local playerList = redisBattleUtil.hget_json(key, "playerList")
  86. info.playerList = self:pack_room_player_info_list(playerList)
  87. return info
  88. end
  89. -- 打包所有房间信息
  90. function root:pack_room_info_list()
  91. local roomIdList = self:get_active_room_id_list()
  92. if is_empty(roomIdList) then
  93. return
  94. end
  95. local roomInfoList = {}
  96. for _, v in ipairs(roomIdList) do
  97. local roomId = tonumber(v)
  98. local info = self:pack_room_info(roomId)
  99. if not is_empty(info) then
  100. table.insert(roomInfoList, info)
  101. end
  102. end
  103. return roomInfoList
  104. end
  105. -- 打包玩家房间信息
  106. function root:pack_player_room_info(uid)
  107. if is_empty(uid) then
  108. return
  109. end
  110. local roomId = battleData:get_room_id(uid)
  111. if is_empty(roomId) then
  112. return
  113. end
  114. return self:pack_room_info(roomId)
  115. end
  116. -- 创建房间
  117. function root:create_room(uid, playCount, boxIdList)
  118. if is_empty(uid) then
  119. return false
  120. end
  121. if is_empty(playCount) or playCount < 2 or playCount > 3 then
  122. return false
  123. end
  124. if is_empty(boxIdList) or #boxIdList < 1 then
  125. return false
  126. end
  127. local roomId = self:gen_room_id()
  128. -- 设置房间信息
  129. local playerList = {{uid = uid, seatId = 1, status = 1}}
  130. local key = _get_room_key(roomId)
  131. redisBattleUtil.hset(key, "createTime", timeUtil.now()) -- 创建时间
  132. redisBattleUtil.hset(key, "hostUid", uid) -- 房主
  133. redisBattleUtil.hset(key, "playCount", playCount) -- 战斗人数
  134. redisBattleUtil.hset(key, "boxIdList", boxIdList) -- 战斗箱子ID列表
  135. redisBattleUtil.hset(key, "playerList", playerList) -- 玩家列表
  136. redisBattleUtil.hset(key, "status", 0) -- 战斗状态
  137. -- 房间进入准备战斗集合
  138. key = _get_active_room_id_key()
  139. lib_battle_redis:sadd(key, roomId)
  140. return true, roomId
  141. end
  142. -- 房间是否存在
  143. function root:is_room_exist(roomId)
  144. local key = _get_room_key(roomId)
  145. -- 房间是否存在
  146. return lib_battle_redis:exists(key)
  147. end
  148. -- 座位是否已被占
  149. function root:get_seat_player(roomId, seatId)
  150. if is_empty(roomId) or is_empty(seatId) then
  151. return
  152. end
  153. local key = _get_room_key(roomId)
  154. -- 房间是否存在
  155. local isExist = lib_battle_redis:exists(key)
  156. if not isExist then
  157. return
  158. end
  159. local playerList = redisBattleUtil.hget_json(key, "playerList")
  160. for _, v in ipairs(playerList) do
  161. if v.seatId and v.seatId == seatId then
  162. return v.uid
  163. end
  164. end
  165. end
  166. -- 进入房间
  167. function root:enter_room(uid, roomId)
  168. if is_empty(uid) or is_empty(roomId) then
  169. return false
  170. end
  171. local key = _get_room_key(roomId)
  172. -- 房间是否存在
  173. local isExist = lib_battle_redis:exists(key)
  174. if not isExist then
  175. return false
  176. end
  177. local playerList = redisBattleUtil.hget_json(key, "playerList")
  178. for _, v in ipairs(playerList) do
  179. if v.uid == uid then
  180. -- 玩家已进入房间
  181. return false
  182. end
  183. end
  184. table.insert(playerList, {uid = uid, status = 0})
  185. redisBattleUtil.hset(key, "playerList", playerList)
  186. return true
  187. end
  188. -- 坐下
  189. function root:seat_down(uid, roomId, seatId)
  190. if is_empty(uid) or is_empty(roomId) or is_empty(seatId) then
  191. return false
  192. end
  193. local key = _get_room_key(roomId)
  194. -- 房间是否存在
  195. local isExist = lib_battle_redis:exists(key)
  196. if not isExist then
  197. return false
  198. end
  199. local isMatch = false
  200. local playerList = redisBattleUtil.hget_json(key, "playerList")
  201. for _, v in ipairs(playerList) do
  202. if v.uid == uid then
  203. v.seatId = seatId
  204. v.status = 1
  205. isMatch = true
  206. break
  207. end
  208. end
  209. if not isMatch then
  210. table.insert(playerList, {uid = uid, seatId = seatId, status = 1})
  211. end
  212. redisBattleUtil.hset(key, "playerList", playerList)
  213. return true
  214. end
  215. -- 站起
  216. function root:stand_up(uid, roomId)
  217. if is_empty(uid) or is_empty(roomId) then
  218. return false
  219. end
  220. local key = _get_room_key(roomId)
  221. -- 房间是否存在
  222. local isExist = lib_battle_redis:exists(key)
  223. if not isExist then
  224. return false
  225. end
  226. local isMatch = false
  227. local playerList = redisBattleUtil.hget_json(key, "playerList")
  228. for _, v in ipairs(playerList) do
  229. if v.uid == uid then
  230. if v.seatId == nil or v.seatId == 0 then
  231. return false
  232. end
  233. v.seatId = nil
  234. v.status = 0
  235. isMatch = true
  236. break
  237. end
  238. end
  239. if not isMatch then
  240. return false
  241. end
  242. redisBattleUtil.hset(key, "playerList", playerList)
  243. return true
  244. end
  245. -- 离开房间
  246. function root:leave(uid, roomId)
  247. if is_empty(uid) or is_empty(roomId) then
  248. return false
  249. end
  250. local key = _get_room_key(roomId)
  251. -- 房间是否存在
  252. local isExist = lib_battle_redis:exists(key)
  253. if not isExist then
  254. return false
  255. end
  256. local playerList = redisBattleUtil.hget_json(key, "playerList")
  257. for k, v in ipairs(playerList) do
  258. if v.uid == uid then
  259. table.remove(playerList, k)
  260. break
  261. end
  262. end
  263. redisBattleUtil.hset(key, "playerList", playerList)
  264. return true
  265. end
  266. -- 获取房间玩家信息
  267. function root:get_room_player_info(roomId, uid)
  268. if is_empty(roomId) or is_empty(uid) then
  269. return
  270. end
  271. local key = _get_room_key(roomId)
  272. -- 房间是否存在
  273. local isExist = lib_battle_redis:exists(key)
  274. if not isExist then
  275. return
  276. end
  277. local playerList = redisBattleUtil.hget_json(key, "playerList")
  278. for _, v in ipairs(playerList) do
  279. if v.uid == uid then
  280. return v
  281. end
  282. end
  283. end
  284. -- 获取房间消耗价格
  285. function root:get_room_price(roomId)
  286. local key = _get_room_key(roomId)
  287. local boxIdList = redisBattleUtil.hget_json(key, "boxIdList")
  288. if is_empty(boxIdList) then
  289. return
  290. end
  291. return boxAdapt:battle_get_box_price(boxIdList)
  292. end
  293. -- 获取房间状态
  294. -- 0:等待 1:进行中 2:结算 3:销毁
  295. function root:get_room_status(roomId)
  296. if is_empty(roomId) then
  297. return 3
  298. end
  299. local key = _get_room_key(roomId)
  300. -- 房间是否存在
  301. local isExist = lib_battle_redis:exists(key)
  302. if not isExist then
  303. return 3
  304. end
  305. local status = redisBattleUtil.hget_int(key, "status")
  306. return status
  307. end
  308. -- 销毁房间
  309. function root:destroy_room(roomId)
  310. if is_empty(roomId) then
  311. return false
  312. end
  313. local key = _get_room_key(roomId)
  314. -- 房间是否存在
  315. local isExist = lib_battle_redis:exists(key)
  316. if not isExist then
  317. -- 从活跃房间集合删除
  318. lib_battle_redis:sismerber(_get_active_room_id_key(), roomId)
  319. return false
  320. end
  321. local status = redisBattleUtil.hget_int(key, "status")
  322. if status >= 0 then
  323. -- 战斗已开始
  324. return false
  325. end
  326. -- 删除房间信息
  327. lib_battle_redis:del(key)
  328. -- 从活跃房间集合删除
  329. lib_battle_redis:sismerber(_get_active_room_id_key(), roomId)
  330. return true
  331. end
  332. -- 是否等待开战结束
  333. function root:is_wait_battle_end(roomId)
  334. if is_empty(roomId) then
  335. return false
  336. end
  337. local key = _get_room_key(roomId)
  338. -- 房间是否存在
  339. local isExist = lib_battle_redis:exists(key)
  340. if not isExist then
  341. return true
  342. end
  343. local currTime = timeUtil.now()
  344. local createTime = redisBattleUtil.hget_int(key, "createTime")
  345. local waitSeconds = 10
  346. if currTime < createTime + waitSeconds then
  347. return false
  348. end
  349. return true
  350. end
  351. -- 房间是否开始战斗
  352. function root:is_room_start_battle(roomId)
  353. local status = self:get_room_status(roomId)
  354. if status > 0 then
  355. return false
  356. end
  357. local seatCount = 0
  358. local key = _get_room_key(roomId)
  359. local playerList = redisBattleUtil.hget_json(key, "playerList")
  360. for _, v in ipairs(playerList) do
  361. if v.seatId and v.seatId > 0 then
  362. seatCount = seatCount + 1
  363. end
  364. end
  365. local playCount = redisBattleUtil.hget_int(key, "playCount")
  366. return seatCount >= playCount
  367. end
  368. ----------------------------------------
  369. -- 接口
  370. ----------------------------------------
  371. local bagData = require("data.bag")
  372. -- 绑定房间
  373. function root:band_room(uid, roomId, costItems)
  374. if is_empty(uid) or is_empty(costItems) then
  375. return
  376. end
  377. battleData:band_room(uid, roomId, costItems)
  378. -- 消耗道具
  379. local keyEvent = string.format("room-seat-down-%s", tostring(roomId))
  380. bagData:consume_items(uid, costItems, keyEvent)
  381. end
  382. -- 解绑房间
  383. -- ty - 解绑类型: leave:离开 dismiss:解散 seat:更换座位 settle:结算
  384. function root:unband_room(uid, ty)
  385. if is_empty(uid) then
  386. return
  387. end
  388. local costItems = battleData:get_room_cost_items(uid)
  389. if (ty == "leave" or ty == "dismiss" or ty == "seat") and not is_empty(costItems) then
  390. -- 返回物品
  391. local keyEvent = string.format("room-%s", ty)
  392. bagData:add_items(uid, costItems, keyEvent)
  393. end
  394. battleData:band_room(uid)
  395. return true
  396. end
  397. return root