|
|
@@ -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
|