ソースを参照

新增在线人数日志

neo 1 年間 前
コミット
04d7e7faed
共有5 個のファイルを変更した134 個の追加0 個の削除を含む
  1. 44 0
      common/utils/serverLogUtil.lua
  2. 53 0
      dev/utils/util_online.lua
  3. 5 0
      nodes/game/lib/role.lua
  4. 1 0
      nodes/global/main.lua
  5. 31 0
      nodes/global/service/onlineSrv.lua

+ 44 - 0
common/utils/serverLogUtil.lua

@@ -2,6 +2,7 @@
2 2
 local skynet = require "skynet"
3 3
 local timeUtil = require "utils.timeUtil"
4 4
 local lib_game_redis = require("lib_game_redis")
5
+local redisUtil = require("utils.redisUtil")
5 6
 
6 7
 local root = {}
7 8
 
@@ -196,4 +197,47 @@ function root.logBandShareCode(uid, bandShareCode, channel, version)
196 197
     writeServerLog("banding", date, dataStr)
197 198
 end
198 199
 
200
+-- 在线人数
201
+function root.logOnlineCount()
202
+    local mapCount = redisUtil.hgetall("online:count")
203
+    log.info("logOnlineCount mapCount[%s]", tostring(mapCount))
204
+    if is_empty(mapCount) then
205
+        return
206
+    end
207
+
208
+    local now = timeUtil.currentTime()
209
+    local date = timeUtil.toDate(now)
210
+    local dateStr = timeUtil.toString(now)
211
+
212
+    -- 全服
213
+    if mapCount["all"] then
214
+        local data = {
215
+            date,
216
+            dateStr,
217
+            "",
218
+            "",
219
+            mapCount["all"]
220
+        }
221
+        local dataStr = root.formatData(data)
222
+
223
+        writeServerLog("onlinecount", date, dataStr)
224
+    end
225
+    -- 主播
226
+    for k, v in pairs(mapCount) do
227
+        if string.match(k, "bandShareCode") and tonumber(v) > 0 then
228
+            local arr = string.split(k, ":")
229
+            local data = {
230
+                date,
231
+                dateStr,
232
+                arr[2],
233
+                "",
234
+                v
235
+            }
236
+            local dataStr = root.formatData(data)
237
+
238
+            writeServerLog("onlinecount", date, dataStr)
239
+        end
240
+    end
241
+end
242
+
199 243
 return root

+ 53 - 0
dev/utils/util_online.lua

@@ -0,0 +1,53 @@
1
+--[[
2
+Descripttion:玩家信息
3
+version:
4
+Author: Neo,Huang
5
+Date: 2022-05-26 19:45:58
6
+LastEditors: Neo,Huang
7
+LastEditTime: 2022-05-26 19:46:41
8
+--]]
9
+local moduleData = require("data.module")
10
+local lib_game_redis = require("lib_game_redis")
11
+
12
+local root = {}
13
+
14
+-- 新增在线人数
15
+function root:add_online_count(uid)
16
+    if is_empty(uid) or is_robot(uid) then
17
+        return false
18
+    end
19
+    local channel = moduleData:get_channel(uid)
20
+    local bandShareCode = moduleData:hget(uid, "user", "bandShareCode")
21
+    local key = "online:count"
22
+    -- 全服人数
23
+    lib_game_redis:hincrby(key, "all", 1)
24
+    -- 渠道
25
+    if not is_empty(channel) then
26
+        lib_game_redis:hincrby(key, string.format("channel:%s", tostring(channel)), 1)
27
+    end
28
+    -- 主播
29
+    if not is_empty(bandShareCode) then
30
+        lib_game_redis:hincrby(key, string.format("bandShareCode:%s", tostring(bandShareCode)), 1)
31
+    end
32
+end
33
+-- 减少在线人数
34
+function root:sub_online_count(uid)
35
+    if is_empty(uid) or is_robot(uid) then
36
+        return false
37
+    end
38
+    local channel = moduleData:get_channel(uid)
39
+    local bandShareCode = moduleData:hget(uid, "user", "bandShareCode")
40
+    local key = "online:count"
41
+    -- 全服人数
42
+    lib_game_redis:hincrby(key, "all", -1)
43
+    -- 渠道
44
+    if not is_empty(channel) then
45
+        lib_game_redis:hincrby(key, string.format("channel:%s", tostring(channel)), -1)
46
+    end
47
+    -- 主播
48
+    if not is_empty(bandShareCode) then
49
+        lib_game_redis:hincrby(key, string.format("bandShareCode:%s", tostring(bandShareCode)), -1)
50
+    end
51
+end
52
+
53
+return root

+ 5 - 0
nodes/game/lib/role.lua

@@ -1,6 +1,7 @@
1 1
 local dataMode = require "dataMode"
2 2
 local timeUtil = require("utils.timeUtil")
3 3
 local serverLogUtil = require("utils.serverLogUtil")
4
+local util_online = require("utils.util_online")
4 5
 
5 6
 local sessionData = require("data.session")
6 7
 local moduleData = require("data.module")
@@ -34,6 +35,8 @@ function root:login(session)
34 35
     -- 模块载入热数据
35 36
     self:load_modules()
36 37
 
38
+    -- 在线人数
39
+    util_online:add_online_count(self.uid)
37 40
     -- 初始化session 信息
38 41
     local nodeInfo = {nodeName = session.gateNode, agent = session.gateAgent}
39 42
     sessionData:user_update_cluster_info(self.uid, "gate", nodeInfo)
@@ -74,6 +77,8 @@ function root:logout_modules()
74 77
     -- 删除玩家session信息
75 78
     sessionData:user_update_cluster_info(self.uid, "gate")
76 79
     sessionData:user_update_cluster_info(self.uid, "game")
80
+    -- 在线人数
81
+    util_online:sub_online_count(self.uid)
77 82
 end
78 83
 
79 84
 -- 初始化游戏事件

+ 1 - 0
nodes/global/main.lua

@@ -30,6 +30,7 @@ skynet.start(
30 30
         skynet.uniqueservice("battlerecordSrv")
31 31
         skynet.uniqueservice("rollSrv")
32 32
         skynet.uniqueservice("itemrecordSrv")
33
+        skynet.uniqueservice("onlineSrv")
33 34
 
34 35
         -- 集群
35 36
         init_nodes:init_cluster()

+ 31 - 0
nodes/global/service/onlineSrv.lua

@@ -0,0 +1,31 @@
1
+--[[
2
+Descripttion:追梦
3
+version:
4
+Author: Neo,Huang
5
+Date: 2023-11-16 21:45:09
6
+LastEditors: Neo,Huang
7
+LastEditTime: 2023-11-16 23:20:12
8
+--]]
9
+local timer = require("timer")
10
+local baseService = require("baseService")
11
+local serverLogUtil = require "utils.serverLogUtil"
12
+
13
+local timerOnlineLog = nil
14
+
15
+local root = {}
16
+
17
+-- 在线人数
18
+local function _online_log()
19
+	serverLogUtil.logOnlineCount()
20
+end
21
+
22
+function root.onStart()
23
+	timerOnlineLog = timer.timeOut(60, _online_log)
24
+end
25
+
26
+function root.onStop()
27
+	-- 取消定时器
28
+	timerOnlineLog.func = nil
29
+end
30
+
31
+baseService.start(root, ".onlineSrv", true)