Browse Source

战场开房,初步逻辑

huangyuhao 1 year ago
parent
commit
d22496e77e

+ 18 - 1
common/core/nodeMgr.lua

@@ -113,10 +113,27 @@ function root.broadcast_game_agent(mdl, cmd, ...)
113 113
 		return
114 114
 	end
115 115
 	for _, v in ipairs(nodeInfoList) do
116
-		root.send(v, ".srvAgentMgr", "broadcast_agents", mdl, cmd, ...)
116
+		root.send(v.nodeName, ".srvAgentMgr", "broadcast_agents", mdl, cmd, ...)
117 117
 	end
118 118
 end
119 119
 
120
+-- 广播
121
+function root:broadcast_proto_notify(protoName, msg)
122
+	if protoName == nil then
123
+		log.error("广播消息推送失败 protoName[%s]", tostring(protoName))
124
+		return false
125
+	end
126
+	local nodeInfoList = root.get_type_node_list("gate")
127
+	if is_empty(nodeInfoList) then
128
+		return false
129
+	end
130
+	for _, v in ipairs(nodeInfoList) do
131
+		root.send(v.nodeName, ".wsSprotoWatchdog", "s2c_broadcast", protoName, msg)
132
+	end
133
+
134
+	return true
135
+end
136
+
120 137
 -- 获取某种类型的所有节点
121 138
 function root.get_type_node_list(nodeType)
122 139
 	local ok, nodes = root.call("master", ".srvNodeMgr", "get_type_node_list", tostring(nodeType))

+ 11 - 0
common/db/lib_battle_redis.lua

@@ -0,0 +1,11 @@
1
+--[[
2
+Descripttion:战斗
3
+version:
4
+Author: Neo,Huang
5
+Date: 2023-11-16 23:08:53
6
+LastEditors: Neo,Huang
7
+LastEditTime: 2023-11-16 23:10:13
8
+--]]
9
+local rpcRedis = require "rpcRedis"
10
+
11
+return rpcRedis.get_redis("game")

+ 5 - 4
dev/utils/util_global.lua

@@ -2,9 +2,9 @@
2 2
 Descripttion:全局
3 3
 version:
4 4
 Author: Neo,Huang
5
-Date: 2022-07-05 19:59:16
5
+Date: 2023-11-15 22:34:35
6 6
 LastEditors: Neo,Huang
7
-LastEditTime: 2022-07-05 20:08:31
7
+LastEditTime: 2023-11-16 23:30:37
8 8
 --]]
9 9
 local lib_game_redis = require("lib_game_redis")
10 10
 local lib_game_mysql = require("lib_game_mysql")
@@ -17,10 +17,11 @@ local root = {}
17 17
 
18 18
 -- 创建玩家id
19 19
 function root:gen_user_id()
20
-    local uid = lib_game_redis:hincrby(MODULE_NAME, "user:max:id", 1)
20
+    local subKey = "user:max:id"
21
+    local uid = lib_game_redis:hincrby(MODULE_NAME, subKey, 1)
21 22
     if uid < 1000000 then
22 23
         uid = uid + 1000000
23
-        lib_game_redis:set("user:max:id", uid)
24
+        lib_game_redis:hset(MODULE_NAME, subKey, uid)
24 25
     end
25 26
 
26 27
     return uid

+ 113 - 0
dev/utils/util_match.lua

@@ -0,0 +1,113 @@
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
+
12
+local root = {}
13
+
14
+local function _get_global_room_key()
15
+    return "global:room"
16
+end
17
+local function _get_room_key(id)
18
+    return string.format("room:%s", tostring(id))
19
+end
20
+local function _get_active_room_id_key()
21
+    return "active:rooms"
22
+end
23
+
24
+-- 分配房间ID
25
+function root:gen_room_id()
26
+    local key = _get_global_room_key()
27
+    local subKey = "max:room:id"
28
+
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
+
35
+    return id
36
+end
37
+
38
+-- 获取当前活跃房间ID列表
39
+function root:get_active_room_id_list()
40
+    local key = _get_active_room_id_key()
41
+    return lib_battle_redis:smembers(key)
42
+end
43
+-- 创建房间
44
+function root:create_match(uid, playCount, boxIdList)
45
+    if is_empty(uid) then
46
+        return false
47
+    end
48
+    if is_empty(playCount) or playCount < 2 or playCount > 3 then
49
+        return false
50
+    end
51
+    if is_empty(boxIdList) or #boxIdList < 1 then
52
+        return false
53
+    end
54
+    local roomId = self:gen_room_id()
55
+    -- 设置房间信息
56
+    local playerList = {{uid = uid, seat = 1, status = 1}}
57
+    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) -- 战斗状态
64
+    -- 房间进入准备战斗集合
65
+    key = _get_active_room_id_key()
66
+    lib_battle_redis:sadd(key, roomId)
67
+    return true
68
+end
69
+-- 销毁房间
70
+function root:destroy_room(roomId)
71
+    if is_empty(roomId) then
72
+        return false
73
+    end
74
+    local key = _get_room_key(roomId)
75
+    -- 房间是否存在
76
+    local isExist = lib_battle_redis:exists(key)
77
+    if not isExist then
78
+        -- 从活跃房间集合删除
79
+        lib_battle_redis:sismerber(_get_active_room_id_key(), roomId)
80
+        return false
81
+    end
82
+    local status = lib_battle_redis:hget(key, "status")
83
+    if tonumber(status) >= 0 then
84
+        -- 战斗已开始
85
+        return false
86
+    end
87
+    -- 删除房间信息
88
+    lib_battle_redis:del(key)
89
+    -- 从活跃房间集合删除
90
+    lib_battle_redis:sismerber(_get_active_room_id_key(), roomId)
91
+    return true
92
+end
93
+-- 是否等待开战结束
94
+function root:is_wait_battle_end(roomId)
95
+    if is_empty(roomId) then
96
+        return false
97
+    end
98
+    local key = _get_room_key(roomId)
99
+    -- 房间是否存在
100
+    local isExist = lib_battle_redis:exists(key)
101
+    if not isExist then
102
+        return true
103
+    end
104
+    local currTime = timeUtil.now()
105
+    local createTime = lib_battle_redis:hget(key, "createTime")
106
+    local waitSeconds = 10
107
+    if currTime < createTime + waitSeconds then
108
+        return false
109
+    end
110
+    return true
111
+end
112
+
113
+return root

+ 1 - 1
dev/utils/util_user.lua

@@ -48,7 +48,7 @@ function root:user_proto_notify(uid, protoName, msg)
48 48
     end
49 49
     local nodeInfo = self:user_get_cluster_info(uid, "gate")
50 50
     if nodeMgr.is_node_info_valid(nodeInfo) then
51
-        nodeMgr.send_with_node_info(nodeInfo, "pushS2C", uid, protoName, msg)
51
+        nodeMgr.send_with_node_info(nodeInfo, "s2c", uid, protoName, msg)
52 52
         return true
53 53
     else
54 54
         log.error("玩家消息推送失败 uid[%s] nodeInfo[%s] protoName[%s]", tostring(uid), tostring(nodeInfo), tostring(protoName))

+ 7 - 0
nodes/gate/service/srvSprotoAgent.lua

@@ -160,6 +160,13 @@ function CMD.s2c(uid, name, msg)
160 160
     local rs = protoUtil:encode_s2c_req(name, msg)
161 161
     skynet.send(GATE, "lua", "send_to_client", fd, rs)
162 162
 end
163
+-- 向所有玩家发消息
164
+function CMD.s2c_broadcast(name, msg)
165
+    local rs = protoUtil:encode_s2c_req(name, msg)
166
+    for fd, _ in pairs(mapFd) do
167
+        skynet.send(GATE, "lua", "send_to_client", fd, rs)
168
+    end
169
+end
163 170
 
164 171
 -- 回应客户端请求
165 172
 function CMD.c2s_response(uid, name, msg, session, ud)

+ 8 - 0
nodes/gate/service/wsSprotoWatchdog.lua

@@ -236,6 +236,14 @@ end
236 236
 function CMD.update_logic()
237 237
 end
238 238
 
239
+-- 广播协议
240
+function CMD.s2c_broadcast(protoName, msg)
241
+	local addrList = pool:get_agent_srv_addr_list()
242
+	for _, v in pairs(addrList) do
243
+		skynet.send(v, "lua", "s2c_broadcast", protoName, msg)
244
+	end
245
+end
246
+
239 247
 skynet.start(
240 248
 	function()
241 249
 		skynet.dispatch(

+ 3 - 3
nodes/global/service/dreamSrv.lua

@@ -2,9 +2,9 @@
2 2
 Descripttion:追梦
3 3
 version:
4 4
 Author: Neo,Huang
5
-Date: 2022-08-17 16:04:20
5
+Date: 2023-11-16 21:45:09
6 6
 LastEditors: Neo,Huang
7
-LastEditTime: 2022-08-20 15:18:25
7
+LastEditTime: 2023-11-16 23:20:12
8 8
 --]]
9 9
 local timer = require("timer")
10 10
 local baseService = require("baseService")
@@ -27,7 +27,7 @@ local function l_del_expire_record()
27 27
 end
28 28
 
29 29
 function CMD.onStart()
30
-	timerDelExpireMail = timer.timeOut(60, l_del_expire_mail)
30
+	timerDelExpireMail = timer.timeOut(60, l_del_expire_record)
31 31
 end
32 32
 
33 33
 function CMD.onStop()

+ 1 - 1
nodes/master/service/srvNodeMgr.lua

@@ -94,7 +94,7 @@ function root.user_dispatch_cluster_node(uid, clusterName)
94 94
 end
95 95
 
96 96
 -- 获取集群节点列表
97
-function root.get_cluster_node_info_list(clusterName)
97
+function root.get_type_node_list(clusterName)
98 98
 	return root.mapCluster[clusterName]
99 99
 end
100 100
 

+ 36 - 0
nodes/match/main.lua

@@ -0,0 +1,36 @@
1
+--[[
2
+Author: zkj
3
+Date: 2021-05-28 15:20:17
4
+LastEditTime: 2021-06-01 10:46:01
5
+LastEditors: Please set LastEditors
6
+Description: 游戏匹配服
7
+FilePath: /ux4-server/servers/match/main.lua
8
+--]]
9
+local skynet = require "skynet"
10
+require "skynet.manager"
11
+local init_nodes = require("init_nodes")
12
+
13
+skynet.start(
14
+    function()
15
+        -- 初始化控制台
16
+        init_nodes:init_console()
17
+
18
+        -- 数据库
19
+        init_nodes:init_mysql()
20
+        -- redis
21
+        init_nodes:init_redis()
22
+        -- 日志
23
+        init_nodes:init_logger()
24
+        -- 配置
25
+        init_nodes:init_config()
26
+
27
+        skynet.uniqueservice("srvMatchMgr")
28
+
29
+        -- 集群
30
+        init_nodes:init_cluster()
31
+
32
+        skynet.send(".steward", "lua", "pingMaster", "battle_master")
33
+
34
+        skynet.exit()
35
+    end
36
+)

+ 43 - 0
nodes/match/service/srvMatch.lua

@@ -0,0 +1,43 @@
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 timer = require("timer")
10
+local baseService = require("baseService")
11
+local util_match = require("utils.util_match")
12
+
13
+local timerDestroyRoom = nil
14
+
15
+local CMD = {}
16
+
17
+-- 关闭超时未开始战斗房间
18
+local function _destroy_room()
19
+    local activeRoomIdList = util_match:get_active_room_id_list()
20
+    if is_empty(activeRoomIdList) then
21
+        return
22
+    end
23
+    for _, v in ipairs(activeRoomIdList) do
24
+        local roomId = tonumber(v)
25
+        -- 是否等待开始战斗结束
26
+        if util_match:is_wait_battle_end(roomId) then
27
+            -- TODO:通知在线玩家 - 销毁房间
28
+            -- 删除房间信息
29
+            util_match:destroy_room(roomId)
30
+        end
31
+    end
32
+end
33
+
34
+function CMD.onStart()
35
+    timerDestroyRoom = timer.timeOut(1, _destroy_room)
36
+end
37
+
38
+function CMD.onStop()
39
+    -- 取消定时器
40
+    timerDestroyRoom.func = nil
41
+end
42
+
43
+baseService.start(CMD, ".srvMatch", true)

+ 18 - 2
run/ali-test/db.lua

@@ -2,9 +2,9 @@
2 2
 Descripttion:
3 3
 version:
4 4
 Author: Neo,Huang
5
-Date: 2022-07-05 10:54:51
5
+Date: 2023-11-08 22:38:51
6 6
 LastEditors: Neo,Huang
7
-LastEditTime: 2022-07-05 10:56:51
7
+LastEditTime: 2023-11-16 23:08:30
8 8
 --]]
9 9
 local root = {
10 10
     mysql = {},
@@ -38,4 +38,20 @@ root["redis"]["game"] = {
38 38
     }
39 39
 }
40 40
 
41
+-- 战斗redis
42
+root["redis"]["battle"] = {
43
+    {
44
+        host = "127.0.0.1",
45
+        port = 6381,
46
+        db = 1,
47
+        auth = "aGMtbmVvLWRldi0yMDIzMDQwNw=="
48
+    },
49
+    {
50
+        host = "127.0.0.1",
51
+        port = 6381,
52
+        db = 1,
53
+        auth = "aGMtbmVvLWRldi0yMDIzMDQwNw=="
54
+    }
55
+}
56
+
41 57
 return root

+ 18 - 2
run/neo-test1/db.lua

@@ -2,9 +2,9 @@
2 2
 Descripttion:
3 3
 version:
4 4
 Author: Neo,Huang
5
-Date: 2022-07-05 10:54:51
5
+Date: 2023-11-08 22:38:51
6 6
 LastEditors: Neo,Huang
7
-LastEditTime: 2022-07-05 10:56:51
7
+LastEditTime: 2023-11-16 23:08:03
8 8
 --]]
9 9
 local root = {
10 10
     mysql = {},
@@ -38,4 +38,20 @@ root["redis"]["game"] = {
38 38
     }
39 39
 }
40 40
 
41
+-- 战斗redis
42
+root["redis"]["battle"] = {
43
+    {
44
+        host = "127.0.0.1",
45
+        port = 6381,
46
+        db = 1,
47
+        auth = "aGMtbmVvLWRldi0yMDIzMDQwNw=="
48
+    },
49
+    {
50
+        host = "127.0.0.1",
51
+        port = 6381,
52
+        db = 1,
53
+        auth = "aGMtbmVvLWRldi0yMDIzMDQwNw=="
54
+    }
55
+}
56
+
41 57
 return root