Explorar el Código

新增战斗房间

neo hace 2 años
padre
commit
039a418412

+ 1 - 1
common/core/nodeMgr.lua

@@ -118,7 +118,7 @@ function root.broadcast_game_agent(mdl, cmd, ...)
118 118
 end
119 119
 
120 120
 -- 广播
121
-function root:broadcast_proto_notify(protoName, msg)
121
+function root.broadcast_proto_notify(protoName, msg)
122 122
 	if protoName == nil then
123 123
 		log.error("广播消息推送失败 protoName[%s]", tostring(protoName))
124 124
 		return false

+ 102 - 0
common/db/redisBattleUtil.lua

@@ -0,0 +1,102 @@
1
+--[[
2
+Descripttion:
3
+version:
4
+Author: Neo,Huang
5
+Date: 2022-01-06 16:50:11
6
+LastEditors: Neo,Huang
7
+LastEditTime: 2022-01-25 16:30:38
8
+--]]
9
+local lib_battle_redis = require("lib_battle_redis")
10
+
11
+-- redis基础操作
12
+-- 本文件所有处理不会对module对应的数据库进行操作,玩家个人数据请使用 data.module
13
+local root = {}
14
+
15
+----------------------------------------
16
+-- main key
17
+----------------------------------------
18
+function root.get_int(key)
19
+    local val = lib_battle_redis:get(key)
20
+    return val and tonumber(val) or 0
21
+end
22
+function root.get_json(key)
23
+    local val = lib_battle_redis:get(key)
24
+    val = val and cjson_decode(val) or {}
25
+    -- 主要针对C++中的NULL
26
+    if is_empty(val) then
27
+        val = {}
28
+    end
29
+    return val
30
+end
31
+----------------------------------------
32
+-- subKey
33
+----------------------------------------
34
+function root.hset(tb, key, val)
35
+    if is_empty(tb) or is_empty(key) then
36
+        return
37
+    end
38
+    if is_nil(val) then
39
+        return root.hdel(tb, key)
40
+    end
41
+    local info = val
42
+    if type(val) == "table" then
43
+        info = cjson_encode(val)
44
+    end
45
+    return lib_battle_redis:hset(tb, key, info)
46
+end
47
+
48
+function root.hget_int(tb, key)
49
+    local val = lib_battle_redis:hget(tb, key)
50
+    return val and tonumber(val) or 0
51
+end
52
+
53
+function root.hget_json(tb, key)
54
+    local val = lib_battle_redis:hget(tb, key)
55
+    val = val and cjson_decode(val) or {}
56
+    -- 主要针对C++中的NULL
57
+    if is_empty(val) then
58
+        val = {}
59
+    end
60
+    return val
61
+end
62
+
63
+function root.hmset(key, mapData)
64
+    if is_empty(key) or is_empty(mapData) then
65
+        return false
66
+    end
67
+    local res = {}
68
+    for k, v in pairs(mapData) do
69
+        table.insert(res, k)
70
+        table.insert(res, v)
71
+    end
72
+
73
+    return lib_battle_redis:hmset(key, table.unpack(res))
74
+end
75
+-- 获取模块所有数据
76
+function root.hgetall(key)
77
+    local data = lib_battle_redis:hgetall(key)
78
+    if not is_empty(data) then
79
+        local ret = {}
80
+        for i = 1, #data, 2 do
81
+            ret[data[i]] = data[i + 1]
82
+        end
83
+
84
+        return ret
85
+    end
86
+end
87
+
88
+function root.do_eval(evalkey, keyCount, ...)
89
+    local data = lib_battle_redis:eval(root.evalStr[evalkey], keyCount, ...)
90
+    return data
91
+end
92
+
93
+-- redis 执行 lua 脚本.
94
+root.evalStr = {
95
+    ["aaa"] = [[redis.call('lpush', KEYS[1], ARGV[1]); redis.call('ltrim', KEYS[1], 0, ARGV[2]);]],
96
+    ["bbb"] = [["redis.call('zincrby',KEYS[1], ARGV[1], ARGV[2]);
97
+        local scores    = redis.call('zscore', KEYS[1], ARGV[2]);
98
+        local rank      = redis.call('zrevrank', KEYS[1], ARGV[2]); 
99
+        return {rank, scores};]]
100
+}
101
+
102
+return root

+ 47 - 0
dev/adapt/boxAdapt.lua

@@ -32,4 +32,51 @@ function root:blind_get_box_key_info(boxId, key)
32 32
     end
33 33
 end
34 34
 
35
+----------------------------------------
36
+-- 战斗
37
+----------------------------------------
38
+-- 获取箱子配置
39
+function root:battle_get_box_key_info(boxId, key)
40
+    if is_empty(boxId) then
41
+        return
42
+    end
43
+    local conf = baseAdapt:getConfig("BattleBoxConfig")
44
+    for _, v in ipairs(conf) do
45
+        if v.boxId == boxId then
46
+            if is_empty(key) then
47
+                return v
48
+            else
49
+                return v[key]
50
+            end
51
+        end
52
+    end
53
+end
54
+-- 参加战斗消耗
55
+function root:battle_get_box_price(boxIdList)
56
+    if is_empty(boxIdList) then
57
+        return 0
58
+    end
59
+    local price = 0
60
+    for _, v in ipairs(boxIdList) do
61
+        local boxPrice = self:battle_get_box_key_info(v, "price")
62
+        if not is_empty(boxPrice) then
63
+            price = price + boxPrice
64
+        end
65
+    end
66
+    return price
67
+end
68
+-- 检查战斗箱子
69
+function root:battle_is_box_list_valid(boxIdList)
70
+    if is_empty(boxIdList) then
71
+        return false
72
+    end
73
+    for _, v in ipairs(boxIdList) do
74
+        local conf = self:battle_get_box_key_info(v)
75
+        if is_empty(conf) or is_empty(conf.price) then
76
+            return false
77
+        end
78
+    end
79
+    return true
80
+end
81
+
35 82
 return root

+ 40 - 0
dev/data/battle.lua

@@ -0,0 +1,40 @@
1
+--[[
2
+Descripttion:战斗
3
+version:
4
+Author: Neo,Huang
5
+Date: 2021-09-15 19:47:54
6
+LastEditors: Neo,Huang
7
+LastEditTime: 2021-09-15 19:47:55
8
+--]]
9
+local moduleData = require("data.module")
10
+
11
+local MODULE_NAME = "battle"
12
+
13
+local root = {}
14
+
15
+-- 房间 - 更新
16
+function root:band_room(uid, roomId, costItems)
17
+    if is_empty(uid) then
18
+        return false
19
+    end
20
+    moduleData:hset(uid, MODULE_NAME, "roomId", roomId)
21
+    moduleData:hset(uid, MODULE_NAME, "costItems", costItems)
22
+    return true
23
+end
24
+-- 房间 - 获取
25
+function root:get_room_id(uid)
26
+    if is_empty(uid) then
27
+        return
28
+    end
29
+    return moduleData:hget_int(uid, MODULE_NAME, "roomId")
30
+end
31
+
32
+-- 获取进入房间消耗
33
+function root:get_room_cost_items(uid)
34
+    if is_empty(uid) then
35
+        return
36
+    end
37
+    return moduleData:hget_json(uid, MODULE_NAME, "costItems")
38
+end
39
+
40
+return root

+ 56 - 0
dev/modules/battle.lua

@@ -0,0 +1,56 @@
1
+local bagData = require("data.bag")
2
+
3
+local root = class("battle", require("base.baseModule"))
4
+
5
+function root:ctor(uid)
6
+    root.super.ctor(self, uid, "battle", "uid", true)
7
+    self.uid = uid
8
+end
9
+
10
+function root:mysql_get_init_columns()
11
+    return {
12
+        uid = "int(11) unsigned NOT NULL",
13
+        roomId = "int(11) DEFAULT 0 COMMENT '当前房间ID'",
14
+        costItems = "json COMMENT '消耗物品列表'"
15
+    }
16
+end
17
+
18
+-- 登录
19
+function root:do_login()
20
+end
21
+
22
+-- 绑定房间
23
+function root:band_room(roomId, costItems)
24
+    self:redis_update_key_info("roomId", roomId)
25
+    self:redis_update_key_info("costItems", costItems)
26
+end
27
+-- 解绑房间
28
+-- ty - 解绑类型: leave:离开 dismiss:解散 seat:更换座位 settle:结算
29
+function root:unband_room(ty)
30
+    self:redis_update_key_info("roomId")
31
+    local costItems = self:redis_get_key_info("costItems")
32
+    if (ty == "leave" or ty == "dismiss" or ty == "seat") and not is_empty(costItems) then
33
+        -- 返回物品
34
+        local keyEvent = string.format("room-%s", ty)
35
+        bagData:add_items(self.uid, costItems, keyEvent)
36
+    end
37
+    self:redis_update_key_info("costItems")
38
+    return true
39
+end
40
+-- 是否绑定房间
41
+function root:is_band_room()
42
+    local roomId = self:redis_get_key_info("roomId")
43
+    local costItems = self:redis_get_key_info("costItems")
44
+    return not is_empty(roomId) and not is_empty(costItems)
45
+end
46
+
47
+-- 获取当前房间ID
48
+function root:get_room_id()
49
+    return self:redis_get_key_info("roomId")
50
+end
51
+-- 获取绑定房间消耗物品
52
+function root:get_room_cost_items()
53
+    return self:redis_get_key_info("costItems")
54
+end
55
+
56
+return root

+ 286 - 11
dev/utils/util_match.lua

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

+ 2 - 2
dev/utils/util_player.lua

@@ -11,7 +11,7 @@ local moduleData = require("data.module")
11 11
 local root = {}
12 12
 
13 13
 -- 玩家信息
14
-function root:get_player_info(uid)
14
+function root:get_base_info(uid)
15 15
     local info = {
16 16
         uid = uid,
17 17
         nickname = moduleData:hget(uid, "user", "nickname"),
@@ -25,7 +25,7 @@ function root:get_player_info(uid)
25 25
 end
26 26
 
27 27
 -- 玩家基础信息
28
-function root:get_base_info(uid)
28
+function root:get_player_info(uid)
29 29
     local info = {
30 30
         uid = uid,
31 31
         nickname = moduleData:hget(uid, "user", "nickname"),

+ 176 - 0
nodes/game/interface/room.lua

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

+ 3 - 1
nodes/match/service/srvMatch.lua

@@ -9,6 +9,7 @@ LastEditTime: 2023-11-16 23:19:50
9 9
 local timer = require("timer")
10 10
 local baseService = require("baseService")
11 11
 local util_match = require("utils.util_match")
12
+local nodeMgr = require("nodeMgr")
12 13
 
13 14
 local timerDestroyRoom = nil
14 15
 
@@ -24,7 +25,8 @@ local function _destroy_room()
24 25
         local roomId = tonumber(v)
25 26
         -- 是否等待开始战斗结束
26 27
         if util_match:is_wait_battle_end(roomId) then
27
-            -- TODO:通知在线玩家 - 销毁房间
28
+            -- 通知在线玩家 - 销毁房间
29
+            nodeMgr.broadcast_proto_notify("on_room_destroy", {roomId = roomId})
28 30
             -- 删除房间信息
29 31
             util_match:destroy_room(roomId)
30 32
         end

+ 127 - 0
nodes/match/service/srvRoom.lua

@@ -0,0 +1,127 @@
1
+--[[
2
+Descripttion:房间管理
3
+version:
4
+Author: Neo,Huang
5
+Date: 2023-11-16 22:19:05
6
+LastEditors: Neo,Huang
7
+LastEditTime: 2023-11-16 23:19:50
8
+--]]
9
+local code = require("code")
10
+local baseService = require("baseService")
11
+local util_match = require("utils.util_match")
12
+local util_user = require("utils.util_user")
13
+
14
+local battleData = require("data.battle")
15
+local bagData = require("data.bag")
16
+
17
+local root = {}
18
+
19
+-- 坐下
20
+function root.seat_down(uid, roomId, seatId)
21
+    if is_empty(roomId) or is_empty(seatId) then
22
+        return code.PARAMTER_ERROR
23
+    end
24
+    -- 房间是否存在
25
+    if not util_match:is_room_exist(roomId) then
26
+        -- 房间不存在
27
+        util_user:user_proto_notify("on_room_seat_down", {code = code.UNKNOWN})
28
+        return
29
+    end
30
+    local seatUid = util_match:get_seat_player(roomId, seatId)
31
+    if not is_empty(seatUid) then
32
+        -- 座位已占用
33
+        util_user:user_proto_notify("on_room_seat_down", {code = code.UNKNOWN})
34
+        return
35
+    end
36
+    local status = util_match:get_room_status(roomId)
37
+    if status ~= 0 then
38
+        -- 房间状态错误
39
+        util_user:user_proto_notify("on_room_seat_down", {code = code.UNKNOWN})
40
+        return
41
+    end
42
+    -- 进入房间
43
+    local _roomId = battleData:get_room_id()
44
+    if not is_empty(_roomId) then
45
+        local roomPlayerInfo = util_match:get_room_player_info(_roomId, uid)
46
+        -- 同一个房间
47
+        if roomId == _roomId then
48
+            -- 相同位置
49
+            if roomPlayerInfo.seatId == seatId then
50
+                util_user:user_proto_notify("on_room_seat_down", {code = code.UNKNOWN})
51
+                return
52
+            end
53
+            if not util_match:seat_down(uid, roomId, seatId) then
54
+                -- 房间/座位不存在
55
+                util_user:user_proto_notify("on_room_seat_down", {code = code.UNKNOWN})
56
+                return
57
+            end
58
+            -- 未坐下
59
+            if is_empty(roomPlayerInfo.seatId) then
60
+                -- 消耗道具
61
+                local price = util_match:get_room_price(_roomId)
62
+                local ok, costItems = bagData:is_enough(uid, {{id = gameConst.ITEM_ID.DIAMOND, count = price}})
63
+                if not ok then
64
+                    util_user:user_proto_notify("on_room_seat_down", {code = code.BAG.ITEM_NOT_ENOUGH})
65
+                    return
66
+                end
67
+                -- 玩家绑定房间信息 - 且消耗道具
68
+                util_match:band_room(uid, roomId, costItems)
69
+            end
70
+            util_user:user_proto_notify("on_room_seat_down", {code = code.OK})
71
+            return
72
+        end
73
+        -- 不同房间 - 离开原来房间
74
+
75
+        -- 房间状态
76
+        local status = util_match:get_room_status(_roomId)
77
+        if status > 0 then
78
+            util_user:user_proto_notify("on_room_seat_down", {code = code.UNKNOWN})
79
+            return
80
+        end
81
+
82
+        if not is_empty(roomPlayerInfo.seatId) then
83
+            -- 玩家解绑房间信息 - 且返还房费
84
+            util_match:unband_room(uid, "seat")
85
+        end
86
+        -- 通知在线玩家 - 离开房间
87
+        local pack = {
88
+            type = 101,
89
+            roomId = _roomId,
90
+            changeRoomPlayer = {util_match:pack_room_player_info(_roomId, uid)}
91
+        }
92
+        nodeMgr.broadcast_proto_notify("on_room_player_change", pack)
93
+
94
+        util_match:leave(uid, _roomId)
95
+    end
96
+
97
+    -- 消耗道具
98
+    local price = util_match:get_room_price(roomId)
99
+    local ok, costItems = bagData:is_enough(uid, {{id = gameConst.ITEM_ID.DIAMOND, count = price}})
100
+    if not ok then
101
+        util_user:user_proto_notify("on_room_seat_down", {code = code.BAG.ITEM_NOT_ENOUGH})
102
+        return
103
+    end
104
+    if not util_match:seat_down(uid, roomId, seatId) then
105
+        -- 房间/座位不存在
106
+        util_user:user_proto_notify("on_room_seat_down", {code = code.UNKNOWN})
107
+        return
108
+    end
109
+    -- 玩家绑定房间信息 - 且消耗道具
110
+    util_match:band_room(uid, roomId, costItems)
111
+
112
+    -- 通知在线玩家 - 坐下
113
+    local pack = {
114
+        type = 102,
115
+        roomId = roomId,
116
+        changeRoomPlayer = {util_match:pack_room_player_info(roomId, uid)}
117
+    }
118
+    nodeMgr.broadcast_proto_notify("on_room_player_change", pack)
119
+end
120
+
121
+function root.onStart()
122
+end
123
+
124
+function root.onStop()
125
+end
126
+
127
+baseService.start(root, ".srvRoom", true)