room.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. --[[
  2. Descripttion:房间
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-05 17:32:01
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-05 20:29:29
  8. --]]
  9. local code = require("code")
  10. local util_match = require("utils.util_match")
  11. local gameConst = require("const.gameConst")
  12. local nodeMgr = require("nodeMgr")
  13. local nodeUtil = require("utils.nodeUtil")
  14. local util_battle = require("utils.util_battle")
  15. local boxAdapt = require("adapt.boxAdapt")
  16. local bagData = require("data.bag")
  17. local battleData = require("data.battle")
  18. local root = {}
  19. -- 获取房间列表
  20. function root:room_get_info(role, msg)
  21. local ret = {
  22. roomList = util_match:pack_room_info_list(),
  23. myRoom = util_match:pack_player_room_info(role.uid)
  24. }
  25. return code.OK, ret
  26. end
  27. -- 创建房间
  28. function root:room_create_room(role, msg)
  29. local playCount = msg.playCount
  30. local battleBoxList = msg.battleBoxList
  31. local uid = role.uid
  32. -- 检查箱子
  33. if not boxAdapt:battle_is_box_list_valid(battleBoxList) then
  34. return code.BATTLE.CREATE_BOX_VALID
  35. end
  36. -- 对战人数限制
  37. local minPlayCount = boxAdapt:battle_const("min_play_count") or 2
  38. local maxPlayCount = boxAdapt:battle_const("max_play_count") or 3
  39. if playCount < minPlayCount or playCount > maxPlayCount then
  40. return code.BATTLE.CREATE_PLAY_COUNT_ERR
  41. end
  42. -- 消耗
  43. local price = boxAdapt:battle_get_box_price(battleBoxList)
  44. local ok, costItems = bagData:is_enough(uid, {{id = gameConst.ITEM_ID.DIAMOND, count = price}})
  45. if not ok then
  46. return code.BAG.ITEM_NOT_ENOUGH
  47. end
  48. -- 创建房间
  49. local ok, roomId = util_match:create_room(uid, playCount, battleBoxList)
  50. if not ok then
  51. return code.PARAMTER_ERROR
  52. end
  53. -- 玩家绑定房间信息 - 且消耗道具
  54. util_match:band_room(uid, roomId, costItems)
  55. -- 全服广播
  56. local pack = {
  57. room = util_match:pack_room_info(roomId)
  58. }
  59. nodeMgr.broadcast_proto_notify("on_room_new", pack)
  60. local ret = {
  61. room = util_match:pack_room_info(roomId)
  62. }
  63. return code.OK, ret
  64. end
  65. -- 进入房间
  66. function root:room_player_enter(role, msg)
  67. local uid = role.uid
  68. local roomId = msg.roomId
  69. if is_empty(roomId) then
  70. return code.PARAMTER_ERROR
  71. end
  72. -- 房间是否存在
  73. local ok = util_match:is_room_exist(roomId)
  74. if not ok then
  75. -- 房间不存在
  76. return code.BATTLE.ENTER_ROOM_NOT_EXIST
  77. end
  78. ok = util_match:enter_room(uid, roomId)
  79. if not ok then
  80. -- 进入房间失败
  81. return code.BATTLE.ENTER_PLAYER_EXIST
  82. end
  83. -- 通知在线玩家
  84. local pack = {
  85. type = 100,
  86. roomId = roomId,
  87. changeRoomPlayer = {util_match:pack_room_player_info(roomId, uid)}
  88. }
  89. nodeMgr.broadcast_proto_notify("on_room_player_change", pack)
  90. local ret = {
  91. room = util_match:pack_room_info(roomId)
  92. }
  93. return code.OK, ret
  94. end
  95. -- 坐下
  96. function root:room_player_seat(role, msg)
  97. local uid = role.uid
  98. local roomId = msg.roomId
  99. local seatId = msg.seatId
  100. if is_empty(roomId) or is_empty(seatId) then
  101. return code.PARAMTER_ERROR
  102. end
  103. -- 避免数据竞争
  104. nodeUtil:send_to_node("match", ".srvRoom", "seat_down", uid, roomId, seatId)
  105. return code.OK
  106. end
  107. -- 站起
  108. function root:room_stand_up(role, msg)
  109. local uid = role.uid
  110. local roomId = battleData:get_room_id()
  111. -- 玩家未进入房间
  112. if is_empty(roomId) then
  113. return code.BATTLE.STAND_NOT_ENTER_ROOM
  114. end
  115. -- 房间状态
  116. local status = util_match:get_room_status(roomId)
  117. if status ~= 0 then
  118. return code.BATTLE.STAND_BATTLE_START
  119. end
  120. -- 房间是否存在
  121. local ok = util_match:stand_up(uid, roomId)
  122. if not ok then
  123. -- 玩家未坐下
  124. return code.BATTLE.STAND_NOT_SEAT_DOWN
  125. end
  126. -- 玩家解绑房间信息 - 且返还房费
  127. util_match:unband_room(uid, "seat")
  128. -- 通知在线玩家 - 站起
  129. local pack = {
  130. type = 103,
  131. roomId = roomId,
  132. changeRoomPlayer = {util_match:pack_room_player_info(roomId, uid)}
  133. }
  134. nodeMgr.broadcast_proto_notify("on_room_player_change", pack)
  135. return code.OK
  136. end
  137. -- 离开房间
  138. function root:room_player_leave(role, msg)
  139. local uid = role.uid
  140. local roomId = battleData:get_room_id(uid)
  141. -- 玩家未进入房间
  142. if is_empty(roomId) then
  143. return code.BATTLE.LEAVE_NOT_ENTER_ROOM
  144. end
  145. -- 房间状态
  146. local status = util_match:get_room_status(roomId)
  147. if status > 0 then
  148. return code.BATTLE.LEAVE_BATTLE_START
  149. end
  150. -- 通知在线玩家 - 站起
  151. local pack = {
  152. type = 101,
  153. roomId = roomId,
  154. changeRoomPlayer = {util_match:pack_room_player_info(roomId, uid)}
  155. }
  156. nodeMgr.broadcast_proto_notify("on_room_player_change", pack)
  157. util_match:leave(uid, roomId)
  158. -- 玩家解绑房间信息 - 且返还房费
  159. util_match:unband_room(uid, "leave")
  160. return code.OK
  161. end
  162. -- 获取房间对战记录列表 - 每次请求返回20条
  163. function root:room_get_record_list(role, msg)
  164. local lastTime = msg.lastTime
  165. local list = util_battle:get_record_list(lastTime)
  166. return code.OK, {list = list}
  167. end
  168. -- 获取玩家个人对战记录 - 每次请求返回20条
  169. function root:room_get_player_record_list(role, msg)
  170. local lastTime = msg.lastTime
  171. local list = util_battle:get_player_record_list(role.uid, lastTime)
  172. return code.OK, {list = list}
  173. end
  174. -- 获取精彩对战记录列表 - 仅返回当前最近20(配置)条记录
  175. function root:room_get_brilliant_record_list(role, msg)
  176. local list = util_battle:get_brilliant_record_list()
  177. return code.OK, {list = list}
  178. end
  179. return root