room.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 boxAdapt = require("adapt.boxAdapt")
  14. local bagData = require("data.bag")
  15. local battleData = require("data.battle")
  16. local root = {}
  17. -- 获取房间列表
  18. function root:room_get_info(role, msg)
  19. local ret = {
  20. roomList = util_match:pack_room_info_list(),
  21. myRoom = util_match:pack_player_room_info(role.uid)
  22. }
  23. return code.OK, ret
  24. end
  25. -- 创建房间
  26. function root:room_create_room(role, msg)
  27. local msg = msg.playCount
  28. local battleBoxList = msg.battleBoxList
  29. local uid = role.uid
  30. -- 检查箱子
  31. if not boxAdapt:battle_is_box_list_valid(battleBoxList) then
  32. return code.UNKNOWN
  33. end
  34. -- 消耗
  35. local price = boxAdapt:battle_get_box_price(battleBoxList)
  36. local ok, costItems = bagData:is_enough(uid, {{id = gameConst.ITEM_ID.DIAMOND, count = price}})
  37. if not ok then
  38. return code.BAG.ITEM_NOT_ENOUGH
  39. end
  40. -- 创建房间
  41. local ok, roomId = util_match:create_room(uid, playCount, battleBoxList)
  42. if not ok then
  43. return code.PARAMTER_ERROR
  44. end
  45. -- 玩家绑定房间信息 - 且消耗道具
  46. util_match:band_room(uid, roomId, costItems)
  47. -- 全服广播
  48. local pack = {
  49. room = util_match:pack_room_info(roomId)
  50. }
  51. nodeMgr.broadcast_proto_notify("on_room_new", pack)
  52. local ret = {
  53. room = util_match:pack_room_info(roomId)
  54. }
  55. return code.OK, ret
  56. end
  57. -- 进入房间
  58. function root:room_player_enter(role, msg)
  59. local uid = role.uid
  60. local roomId = msg.roomId
  61. if is_empty(roomId) then
  62. return code.PARAMTER_ERROR
  63. end
  64. -- 房间是否存在
  65. local ok = util_match:is_room_exist(roomId)
  66. if not ok then
  67. -- 房间不存在
  68. return code.UNKNOWN
  69. end
  70. ok = util_match:enter_room(uid, roomId)
  71. if not ok then
  72. -- 进入房间失败
  73. return code.UNKNOWN
  74. end
  75. -- 通知在线玩家
  76. local pack = {
  77. type = 100,
  78. roomId = roomId,
  79. changeRoomPlayer = {util_match:pack_room_player_info(roomId, uid)}
  80. }
  81. nodeMgr.broadcast_proto_notify("on_room_player_change", pack)
  82. local ret = {
  83. room = util_match:pack_room_info(roomId)
  84. }
  85. return code.OK, ret
  86. end
  87. -- 坐下
  88. function root:room_player_seat(role, msg)
  89. local uid = role.uid
  90. local roomId = msg.roomId
  91. local seatId = msg.seatId
  92. if is_empty(roomId) or is_empty(seatId) then
  93. return code.PARAMTER_ERROR
  94. end
  95. -- 避免数据竞争
  96. nodeUtil:send_to_node("match", ".srvRoom", "seat_down", uid, roomId, seatId)
  97. return code.OK
  98. end
  99. -- 站起
  100. function root:room_stand_up(role, msg)
  101. local uid = role.uid
  102. local roomId = battleData:get_room_id()
  103. -- 玩家未进入房间
  104. if is_empty(roomId) then
  105. return code.UNKNOWN
  106. end
  107. -- 房间状态
  108. local status = util_match:get_room_status(roomId)
  109. if status ~= 0 then
  110. return code.UNKNOWN
  111. end
  112. -- 房间是否存在
  113. local ok = util_match:stand_up(uid, roomId)
  114. if not ok then
  115. -- 玩家未坐下
  116. return code.UNKNOWN
  117. end
  118. -- 玩家解绑房间信息 - 且返还房费
  119. util_match:unband_room(uid, "seat")
  120. -- 通知在线玩家 - 站起
  121. local pack = {
  122. type = 103,
  123. roomId = roomId,
  124. changeRoomPlayer = {util_match:pack_room_player_info(roomId, uid)}
  125. }
  126. nodeMgr.broadcast_proto_notify("on_room_player_change", pack)
  127. return code.OK
  128. end
  129. -- 离开房间
  130. function root:room_player_leave(role, msg)
  131. local uid = role.uid
  132. local roomId = battleData:get_room_id(uid)
  133. -- 玩家未进入房间
  134. if is_empty(roomId) then
  135. return code.UNKNOWN
  136. end
  137. -- 房间状态
  138. local status = util_match:get_room_status(roomId)
  139. if status > 0 then
  140. return code.UNKNOWN
  141. end
  142. -- 通知在线玩家 - 站起
  143. local pack = {
  144. type = 101,
  145. roomId = roomId,
  146. changeRoomPlayer = {util_match:pack_room_player_info(roomId, uid)}
  147. }
  148. nodeMgr.broadcast_proto_notify("on_room_player_change", pack)
  149. util_match:leave(uid, roomId)
  150. -- 玩家解绑房间信息 - 且返还房费
  151. util_match:unband_room(uid, "leave")
  152. return code.OK
  153. end
  154. return root