Browse Source

修改模块

neo 1 year ago
parent
commit
5c51932673

+ 518 - 0
common/utils/timeUtil.lua

@@ -0,0 +1,518 @@
1
+local skynet = require "skynet"
2
+local moduleData = require("data.module")
3
+
4
+local root = {
5
+	SEC_PER_HOUR = 3600,
6
+	SEC_PER_DAY = 86400,
7
+	SEC_PER_WEEK = 7 * 86400,
8
+	REFRESH_HOUR = 5
9
+}
10
+
11
+-- 获取当前时间 单位秒
12
+function root.currentTime()
13
+	return math.floor(skynet.time())
14
+end
15
+-- 获取玩家当前时间
16
+function root.now(uid)
17
+	local currTime = skynet_time()
18
+	if not IS_TEST then
19
+		return currTime
20
+	end
21
+	local deltaTime = moduleData:hget_int(uid, "user", "time:sys")
22
+	if not is_empty(deltaTime) then
23
+		return currTime + deltaTime
24
+	end
25
+	return currTime
26
+end
27
+
28
+function root.toString(t)
29
+	return os.date("%Y-%m-%d %H:%M:%S", t)
30
+end
31
+
32
+function root.toDate(t)
33
+	return os.date("%Y-%m-%d", t or root.currentTime())
34
+end
35
+
36
+function root.toTab(t)
37
+	return os.date("*t", t)
38
+end
39
+
40
+function root.toInt(tab)
41
+	return os.time(tab)
42
+end
43
+
44
+function root.getYday(t)
45
+	return os.date("%j", t)
46
+end
47
+
48
+-- 获取时间的0点时间戳
49
+function root.getCday(ctime)
50
+	local a = root.toTab(ctime)
51
+	a.hour, a.min, a.sec = 0, 0, 0
52
+	return root.toInt(a)
53
+end
54
+
55
+-- 将时间格式化秒 %Y-%m-%d %H:%M:%S
56
+function root.getSecond(t)
57
+	local function split(str, pat)
58
+		local t = {}
59
+		local fpat = "(.-)" .. pat
60
+		local last_end = 1
61
+		local s, e, cap = str:find(fpat, 1)
62
+		while s do
63
+			if s ~= 1 or cap ~= "" then
64
+				table.insert(t, cap)
65
+			end
66
+			last_end = e + 1
67
+			s, e, cap = str:find(fpat, last_end)
68
+		end
69
+
70
+		if last_end <= #str then
71
+			cap = str:sub(last_end)
72
+			table.insert(t, cap)
73
+		end
74
+
75
+		return t
76
+	end
77
+
78
+	local a = split(t, " ")
79
+	local b = split(a[1], "-")
80
+	local c = split(a[2], ":")
81
+
82
+	return root.toInt({year = b[1], month = b[2], day = b[3], hour = c[1], min = c[2], sec = c[3]})
83
+end
84
+
85
+function root.getDayCount(year, month)
86
+	local year = tonumber(year)
87
+	local month = tonumber(month)
88
+	if not year or not month then
89
+		return 0
90
+	end
91
+
92
+	if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 then
93
+		return 31
94
+	end
95
+
96
+	if month == 2 then
97
+		if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then
98
+			return 29
99
+		else
100
+			return 28
101
+		end
102
+	end
103
+
104
+	return 30
105
+end
106
+
107
+function root.getTime(year, month, day, hour, min, sec)
108
+	local timeStruct = {}
109
+	timeStruct.sec = tonumber(sec) or 0
110
+	timeStruct.min = tonumber(min) or 0
111
+	timeStruct.hour = tonumber(hour) or 0
112
+	timeStruct.day = tonumber(day) or 0
113
+	timeStruct.month = tonumber(month) or 0
114
+	timeStruct.year = tonumber(year) or 0
115
+
116
+	return root.toInt(timeStruct)
117
+end
118
+
119
+-- 能否日刷新
120
+function root.dailyRefresh(atime, btime, hour, min, sec)
121
+	local atime = tonumber(atime) or 0
122
+	local btime = tonumber(btime) or root.currentTime()
123
+	local hour, min, sec = tonumber(hour) or 0, tonumber(min) or 0, tonumber(sec) or 0
124
+
125
+	local aTimeTab, bTimeTab = root.toTab(atime), root.toTab(btime)
126
+	bTimeTab.hour, bTimeTab.min, bTimeTab.sec = hour, min, sec
127
+	aTimeTab.hour, aTimeTab.min, aTimeTab.sec = hour, min, sec
128
+	-- 超过1天
129
+	if btime - atime >= 24 * 60 * 60 then
130
+		return true
131
+	end
132
+	-- 1天之内
133
+	if btime >= root.toInt(aTimeTab) and root.toInt(aTimeTab) >= atime then
134
+		return true
135
+	end
136
+	if btime >= root.toInt(bTimeTab) and root.toInt(bTimeTab) >= atime then
137
+		return true
138
+	end
139
+
140
+	return false
141
+end
142
+
143
+-- 能否周刷新 [wday=2 是周一]
144
+function root.WeekRefresh(atime, btime, wday, hour, min, sec)
145
+	atime = tonumber(atime) or 0
146
+	btime = tonumber(btime) or root.currentTime()
147
+	wday, hour, min, sec = tonumber(wday) or 2, tonumber(hour) or 0, tonumber(min) or 0, tonumber(sec) or 0
148
+
149
+	local aTimeTab, bTimeTab = root.toTab(atime), root.toTab(btime)
150
+	bTimeTab.day = bTimeTab.day + (wday - bTimeTab.wday)
151
+	aTimeTab.day = aTimeTab.day + (wday - aTimeTab.wday)
152
+	bTimeTab.hour, bTimeTab.min, bTimeTab.sec = hour, min, sec
153
+	aTimeTab.hour, aTimeTab.min, aTimeTab.sec = hour, min, sec
154
+	-- 超过七天
155
+	if btime - atime >= 7 * 24 * 60 * 60 then
156
+		return true
157
+	end
158
+	-- 七天之内
159
+	if btime >= root.toInt(aTimeTab) and root.toInt(aTimeTab) >= atime then
160
+		return true
161
+	end
162
+	if btime >= root.toInt(bTimeTab) and root.toInt(bTimeTab) >= atime then
163
+		return true
164
+	end
165
+
166
+	return false
167
+end
168
+
169
+-- 能否月刷新
170
+function root.can_month_refresh(atime, btime, in_day, hour, min, sec)
171
+	local atime = tonumber(atime) or 0
172
+	local btime = tonumber(btime) or root.currentTime()
173
+	local bTimeTab, aTimeTab = root.toTab(btime), root.toTab(atime)
174
+	local day, hour, min, sec = tonumber(in_day) or 1, tonumber(hour) or 0, tonumber(min) or 0, tonumber(sec) or 0
175
+	aTimeTab.day, aTimeTab.hour, aTimeTab.min, aTimeTab.sec = day, hour, min, sec
176
+	bTimeTab.day, bTimeTab.hour, bTimeTab.min, bTimeTab.sec = day, hour, min, sec
177
+	-- 超过一月
178
+	local month = (bTimeTab.year - aTimeTab.year) * 12
179
+	if bTimeTab.month + month > aTimeTab.month + 1 then
180
+		return true
181
+	end
182
+	-- 一月之内
183
+	if btime >= root.toInt(aTimeTab) and root.toInt(aTimeTab) >= atime then
184
+		return true
185
+	end
186
+	if btime >= root.toInt(bTimeTab) and root.toInt(bTimeTab) >= atime then
187
+		return true
188
+	end
189
+
190
+	return false
191
+end
192
+
193
+-- 两个时间相距天数
194
+function root.getDisDays(t1, t2)
195
+	if t1 == nil or t2 == nil then
196
+		return 0
197
+	end
198
+
199
+	local d1 = root.getYday(t1)
200
+	local d2 = root.getYday(t2)
201
+	return math.abs(d1 - d2)
202
+end
203
+
204
+function root.isSameDay(t1, t2)
205
+	if t1 == nil or t2 == nil then
206
+		return false
207
+	end
208
+	local d1 = root.getYday(t1)
209
+	local d2 = root.getYday(t2)
210
+	if d1 == d2 then
211
+		return true
212
+	end
213
+	return false
214
+end
215
+-- 是否同一天(凌晨5点)
216
+function root.is_same_c5_day(t1, t2)
217
+	if t1 == nil or t2 == nil then
218
+		return false
219
+	end
220
+	local d1 = root.getYday(t1 - 5 * 3600)
221
+	local d2 = root.getYday(t2 - 5 * 3600)
222
+	if d1 == d2 then
223
+		return true
224
+	end
225
+	return false
226
+end
227
+
228
+-- 获取当前天数(自1970年)
229
+function root.getDaysOfTimestampExt(timestamp, diffTime)
230
+	timestamp = (timestamp or root.currentTime()) - (diffTime or 0)
231
+	return math.floor((timestamp + (8 * 3600)) / (3600 * 24))
232
+end
233
+
234
+-- 获取获取相差的天数
235
+function root.getDisDaysC5(t1, t2)
236
+	if t1 == nil or t2 == nil then
237
+		return 0
238
+	end
239
+
240
+	local d1 = root.getDaysOfTimestampExt(t1, 5 * 3600)
241
+	local d2 = root.getDaysOfTimestampExt(t2, 5 * 3600)
242
+	return math.abs(d1 - d2)
243
+end
244
+
245
+-- 获取获取相差的天数
246
+function root.getDisDaysExt(t1, t2)
247
+	if t1 == nil or t2 == nil then
248
+		return 0
249
+	end
250
+
251
+	local d1 = root.getDaysOfTimestampExt(t1)
252
+	local d2 = root.getDaysOfTimestampExt(t2)
253
+	return math.abs(d1 - d2)
254
+end
255
+
256
+-- 获取下一个凌晨5点时间戳
257
+function root.get_next_c5_time(currTime)
258
+	currTime = currTime or skynet_time()
259
+	local startTime = root.getDayStartTime(currTime) + 5 * 3600
260
+	if currTime >= startTime then
261
+		return startTime + 24 * 3600
262
+	end
263
+	return startTime
264
+end
265
+
266
+-- 获取下周一开始时间 5点
267
+function root.c5_get_next_week_start_time(currTime)
268
+	local date = os.date("*t", currTime)
269
+	-- log.info("获取下周一5点时间 date[%s]", tostring(date))
270
+	if date.wday == 1 and date.hour < 5 then
271
+		return root.getTime(date.year, date.month, date.day, 5, 0, 0)
272
+	end
273
+	local diffDays
274
+	local wday = date.wday - 1
275
+	if wday == 0 then
276
+		wday = 7
277
+	end
278
+	diffDays = 8 - wday
279
+	-- log.info("diffDays = %s", diffDays)
280
+	return root.getDayStartTime(currTime, diffDays) + 5 * 3600
281
+end
282
+
283
+-- 获取下个月1号开始时间 5点
284
+function root.c5_get_next_month_start_time(currTime)
285
+	local date = os.date("*t", currTime)
286
+	-- log.info("获取下周一5点时间 date[%s]", tostring(date))
287
+	if date.day == 1 and date.hour < 5 then
288
+		return root.getTime(date.year, date.month, date.day, 5, 0, 0)
289
+	end
290
+
291
+	date.month = date.month + 1
292
+	return root.getTime(date.year, date.month, date.day, 5, 0, 0)
293
+end
294
+
295
+-- 两个时间间隔(凌晨5点)
296
+function root.get_c5_dis_days(t1, t2)
297
+	if t1 == nil or t2 == nil then
298
+		return 0
299
+	end
300
+
301
+	local d1 = root.getYday(t1 - 5 * 3600)
302
+	local d2 = root.getYday(t2 - 5 * 3600)
303
+	return math.abs(d1 - d2)
304
+end
305
+
306
+function root.isSameWeek(t1, t2)
307
+	local dateA = os.date("*t", t1)
308
+	local dateB = os.date("*t", t2)
309
+	local diff = math.abs(dateA.yday - dateB.yday)
310
+	if diff == 0 then
311
+		return true
312
+	end
313
+
314
+	local beforeDt = 0
315
+	if dateA.yday < dateB.yday then
316
+		beforeDt = dateA
317
+	else
318
+		beforeDt = dateB
319
+	end
320
+	if diff > 7 then
321
+		return false
322
+	end
323
+
324
+	local wday = beforeDt.wday - 1
325
+	if wday == 0 then
326
+		wday = 7
327
+	end
328
+
329
+	if wday + diff > 7 then
330
+		return false
331
+	end
332
+
333
+	return true
334
+end
335
+
336
+function root.isSameMonth(t1, t2)
337
+	if t1 == nil or t2 == nil then
338
+		return false
339
+	end
340
+	local n1 = os.date("*t", t1)
341
+	local n2 = os.date("*t", t2)
342
+	if n1.year == n2.year and n1.month == n2.month then
343
+		return true
344
+	end
345
+	return false
346
+end
347
+
348
+-- 获取当天结束时间
349
+function root.getDayEndTime(currTime, days)
350
+	local daySec = 24 * 3600
351
+	local addDay = days or 1
352
+	local timeout = math.floor((currTime + 8 * 3600) / daySec) * daySec + daySec * addDay - 8 * 3600 - 1
353
+
354
+	return timeout
355
+end
356
+
357
+-- 获取当天开始时间
358
+function root.getDayStartTime(currTime, days)
359
+	local daySec = 24 * 3600
360
+	local addDay = days or 0
361
+	local timeout = math.floor((currTime + 8 * 3600) / daySec) * daySec + daySec * addDay - 8 * 3600
362
+
363
+	return timeout
364
+end
365
+
366
+-- 获取当前周开始时间[周日开始]
367
+function root.getWeekStartTime(currTime)
368
+	local t = root.toTab(currTime)
369
+	local startTime = root.getDayStartTime(currTime)
370
+	return startTime - (t.wday - 1) * root.SEC_PER_DAY
371
+end
372
+
373
+-- 获取当天周结束时间[周六结束]
374
+function root.getWeekEndTime(currTime)
375
+	local startTime = root.getWeekStartTime(currTime)
376
+	return startTime + root.SEC_PER_WEEK - 1
377
+end
378
+
379
+-- 获取当前周的指定时间的时间戳[从周日起]
380
+-- day周日为0,周六为6
381
+function root.getTimeByWeek(currTime, day, h, m, s)
382
+	day = day or 0
383
+	h = h or 0
384
+	m = m or 0
385
+	s = s or 0
386
+	local startTime = root.getWeekStartTime(currTime)
387
+	local addTime = day * root.SEC_PER_DAY + h * root.SEC_PER_HOUR + m * 60 + s
388
+	return startTime + addTime
389
+end
390
+
391
+-- 获取现在时间是几星期几.day周日为0,周六为6
392
+function root.getWeekDay(currTime)
393
+	local tab = os.date("*t", currTime or os.time())
394
+	return tab.wday - 1
395
+end
396
+
397
+-- 获取当天0点时间戳
398
+function root.getUnixtimeToday(t1)
399
+	local nt = os.date("*t", t1)
400
+	return os.time({year = nt.year, month = nt.month, day = nt.day, hour = 0, min = 0, sec = 0})
401
+end
402
+
403
+-- 获取明天0点时间戳
404
+function root.getUnixtimeTomorrow(t1)
405
+	return root.getUnixtimeToday(t1) + 86400
406
+end
407
+
408
+-- 明天零点倒计时
409
+function root.countdownToTomorrow(t1)
410
+	return root.getUnixtimeTomorrow(t1) - (t1 or os.time())
411
+end
412
+
413
+-- 跨分钟数
414
+function root.get_across_minutes(startTime, endTime)
415
+	if startTime == nil or endTime == nil then
416
+		return 0
417
+	end
418
+	local s = math.floor(startTime / 60)
419
+	local e = math.floor(endTime / 60)
420
+	if endTime > e * 60 then
421
+		e = e + 1
422
+	end
423
+	log.info(
424
+		"get_across_minutes startTime[%s] s[%s] endTime[%s] e[%s]",
425
+		tostring(startTime),
426
+		tostring(s),
427
+		tostring(endTime),
428
+		tostring(e)
429
+	)
430
+	return math.abs(e - s)
431
+end
432
+-- 跨小时数(rate每多少整小时,可以是每整1小时,也可以是每整2小时)
433
+function root.get_across_hours(startTime, endTime, rate)
434
+	if startTime == nil or endTime == nil then
435
+		return 0
436
+	end
437
+
438
+	if not rate then
439
+		rate = 3600
440
+	end
441
+
442
+	local s = math.floor(startTime / rate)
443
+	local e = math.floor(endTime / rate)
444
+	log.info(
445
+		"get_across_hours startTime[%s] s[%s] endTime[%s] e[%s]",
446
+		tostring(startTime),
447
+		tostring(s),
448
+		tostring(endTime),
449
+		tostring(e)
450
+	)
451
+	return math.abs(e - s)
452
+end
453
+-- 跨天数
454
+function root.c5_get_cross_days(startTime, endTime)
455
+	if startTime == nil or endTime == nil then
456
+		return 0
457
+	end
458
+	local days = 0
459
+	local nextTime = root.get_next_c5_time(startTime)
460
+	while nextTime < endTime do
461
+		days = days + 1
462
+		nextTime = nextTime + 24 * 3600
463
+	end
464
+	return days
465
+end
466
+
467
+--#region ----------------------------------------- 时间的扩展:功能代码块
468
+-- 为什么重新实现?
469
+-- 部分功能实现得过于复杂,没必要过于遇合,需要考虑到lua的性能消耗.
470
+
471
+-- 获取当天0点时间戳
472
+function root.getTodayHourTimestamp(hour)
473
+	local t_date = os.date("*t")
474
+	return os.time(
475
+		{
476
+			year = t_date.year,
477
+			month = t_date.month,
478
+			day = t_date.day,
479
+			hour = hour or root.REFRESH_HOUR,
480
+			min = 0,
481
+			sec = 0
482
+		}
483
+	)
484
+end
485
+
486
+-- 跨天数
487
+-- 获取某个时间戳跨越了多少天(5点为分界线)
488
+function root.getDaysOfTimestamp(timestamp)
489
+	-- 错误的时间参数返回1天
490
+	if not timestamp or timestamp == 0 then
491
+		return 1
492
+	end
493
+
494
+	local dt = root.getTodayHourTimestamp()
495
+	if timestamp > dt then
496
+		-- 以一天来计算,可能
497
+		-- return 888
498
+		return math.ceil((timestamp - dt) / root.SEC_PER_DAY)
499
+	else
500
+		-- 逻辑刷新前面要加1.
501
+		return math.ceil((dt - timestamp) / root.SEC_PER_DAY) + 1
502
+	end
503
+end
504
+
505
+-- 是否已经跨天了
506
+function root.isOtherDay(uid, actData)
507
+	-- 刷新时间
508
+	local currTime = root.now(uid or 0)
509
+	if actData.nextUT == nil or currTime >= actData.nextUT then
510
+		actData.nextUT = root.get_next_c5_time(currTime)
511
+		return true
512
+	end
513
+	return false
514
+end
515
+
516
+--#endregion
517
+
518
+return root

+ 95 - 1
dev/base/baseModule.lua

@@ -11,10 +11,11 @@ local lib_game_mysql = require("lib_game_mysql")
11 11
 
12 12
 local root = class("base")
13 13
 
14
-function root:ctor(uid, mdlName, keyName)
14
+function root:ctor(uid, mdlName, keyName, isSaveDB)
15 15
     self.uid = uid
16 16
     self.mdlName = mdlName
17 17
     self.keyName = keyName
18
+    self.isSaveDB = isSaveDB
18 19
 end
19 20
 
20 21
 -- 模块sql属性
@@ -27,10 +28,44 @@ function root:mysql_get_table_info()
27 28
     return {
28 29
         mdlName = self.mdlName,
29 30
         keyName = self.keyName,
31
+        isSaveDB = self.isSaveDB,
30 32
         columnNameOptions = self:mysql_get_init_columns()
31 33
     }
32 34
 end
33 35
 
36
+function root:getKeyValue()
37
+    return self[self.keyName]
38
+end
39
+-- 获取模块key
40
+function root:get_module_redis_key()
41
+    local moduleKey = string.format("%s:%s", self.mdlName, tostring(self:getKeyValue()))
42
+    return moduleKey
43
+end
44
+
45
+-- 获取玩家模块数据
46
+function root:get_data_from_db()
47
+    local moduleKey = self:get_module_redis_key()
48
+    local res = redisUtil.hgetall(moduleKey)
49
+    if not is_empty(res) then
50
+        return res
51
+    end
52
+
53
+    if self.isSaveDB then
54
+        local sql = sqlUtil.select_table_by_key(self.mdlName, self.keyName, self:getKeyValue())
55
+        res = lib_game_mysql:query(sql)
56
+        if res and res[1] then
57
+            -- add to redis
58
+            redisUtil.hmset(moduleKey, res[1])
59
+            -- get again
60
+            res = redisUtil.hgetall(moduleKey)
61
+            if not is_empty(res) then
62
+                return res
63
+            end
64
+        end
65
+        return res
66
+    end
67
+end
68
+
34 69
 -- 字段类型 - json
35 70
 function root:is_json(column)
36 71
     return string.match(string.upper(column), "JSON")
@@ -76,4 +111,63 @@ function root:backup_to_db()
76 111
     lib_game_mysql:query(sql)
77 112
 end
78 113
 
114
+----------------------------------------
115
+-- 实时数据
116
+----------------------------------------
117
+local moduleData = require("data.module")
118
+-- 获取数据类型
119
+function root:get_sub_key_type(subKey)
120
+    if subKey == nil then
121
+        return
122
+    end
123
+
124
+    local mpOptions = self:mysql_get_init_columns()
125
+    for k, v in pairs(mpOptions) do
126
+        if tostring(k) == tostring(subKey) then
127
+            if self:is_number(v) then
128
+                return "int"
129
+            end
130
+            if self:is_json(v) then
131
+                return "json"
132
+            end
133
+            return "string"
134
+        end
135
+    end
136
+end
137
+-- 获取数据
138
+function root:redis_get_key_info(subKey, uid)
139
+    if subKey == nil then
140
+        return
141
+    end
142
+    uid = uid or self.uid
143
+    local info = moduleData:hget(uid, self.mdlName, subKey)
144
+    local tyKey = self:get_sub_key_type(subKey)
145
+    if tyKey then
146
+        if tyKey == "json" then
147
+            if info then
148
+                info = cjson_decode(info)
149
+            end
150
+            -- 主要针对C++中的NULL
151
+            if is_empty(info) then
152
+                info = {}
153
+            end
154
+        end
155
+        if tyKey == "int" then
156
+            info = tonumber(info or 0)
157
+        end
158
+    end
159
+    return info
160
+end
161
+-- 设置数据
162
+function root:redis_update_key_info(subKey, info, uid)
163
+    uid = uid or self.uid
164
+    return moduleData:hset(uid, self.mdlName, subKey, info)
165
+end
166
+
167
+-- +1
168
+function root:redis_hincrby(subKey, uid)
169
+    uid = uid or self.uid
170
+    return moduleData:hincrby(uid, self.mdlName, subKey)
171
+end
172
+
79 173
 return root

+ 7 - 0
dev/data/module.lua

@@ -107,6 +107,13 @@ function root:hget_json(uid, mdl, key)
107 107
     return info
108 108
 end
109 109
 
110
+function root:hincrby(uid, mdl, key, val)
111
+    if is_nil(uid) or is_nil(mdl) or is_nil(key) then
112
+        return
113
+    end
114
+    return lib_game_redis:hincrby(get_player_key(uid, mdl), key, val or 1)
115
+end
116
+
110 117
 function root:model_func(func, mdl, key, defaultValue)
111 118
     local resFunc = function(root, uid)
112 119
         if func then

+ 35 - 0
dev/data/session.lua

@@ -0,0 +1,35 @@
1
+--[[
2
+Descripttion:节点信息
3
+version:
4
+Author: Neo,Huang
5
+Date: 2021-09-17 10:03:39
6
+LastEditors: Neo,Huang
7
+LastEditTime: 2021-09-17 10:06:57
8
+--]]
9
+local moduleData = require("data.module")
10
+
11
+local MODULE_NAME = "tb_session"
12
+
13
+local root = {}
14
+
15
+-- 更新玩家节点信息
16
+function root:user_update_cluster_info(uid, clusterName, nodeInfo)
17
+    if uid == nil or is_empty(clusterName) then
18
+        return false
19
+    end
20
+    moduleData:hset(uid, MODULE_NAME, clusterName, nodeInfo)
21
+    return true
22
+end
23
+-- 获取节点信息
24
+function root:user_get_cluster_info(uid, clusterName)
25
+    if uid == nil or is_empty(clusterName) then
26
+        return
27
+    end
28
+    local nodeInfo = moduleData:hget_json(uid, MODULE_NAME, clusterName)
29
+    if is_empty(nodeInfo) then
30
+        nodeInfo = nil
31
+    end
32
+    return nodeInfo
33
+end
34
+
35
+return root

+ 8 - 2
dev/data/user.lua

@@ -19,8 +19,6 @@ function root:user_init_register_info(uid, info)
19 19
     end
20 20
     moduleData:hset(uid, MODULE_NAME, "uid", uid)
21 21
     moduleData:hset(uid, MODULE_NAME, "registerTime", skynet_time())
22
-    moduleData:hset(uid, MODULE_NAME, "registerRegion", info.region or "")
23
-    moduleData:hset(uid, MODULE_NAME, "city", info.city or "")
24 22
     moduleData:hset(uid, MODULE_NAME, "version", info.version or "")
25 23
     moduleData:hset(uid, MODULE_NAME, "registerVersion", info.version or "")
26 24
     moduleData:hset(uid, MODULE_NAME, "appVerison", info.version or "")
@@ -52,4 +50,12 @@ function root:user_is_match_password(uid, password)
52 50
     return password == moduleData:hget(uid, MODULE_NAME, "password")
53 51
 end
54 52
 
53
+-- 账号状态
54
+function root:get_status(uid)
55
+    if uid == nil then
56
+        return 1
57
+    end
58
+    return moduleData:hget(uid, MODULE_NAME, "status")
59
+end
60
+
55 61
 return root

+ 26 - 0
dev/modules/bag.lua

@@ -0,0 +1,26 @@
1
+local root = class("moduleBag", require("base.baseModule"))
2
+
3
+function root:ctor(uid)
4
+    root.super.ctor(self, uid, "bag", "uid", true)
5
+    self.uid = uid
6
+end
7
+
8
+function root:mysql_get_init_columns()
9
+    return {
10
+        uid = "int(11) unsigned NOT NULL",
11
+        itemList = "JSON COMMENT '道具信息'",
12
+        lastBid = "int(11) DEFAULT 0 COMMENT '最后使用的bid'",
13
+        lastInitTime = "int(11) DEFAULT 0 COMMENT '最后初始化背包的时间戳'"
14
+    }
15
+end
16
+
17
+----------------------------------------
18
+-- 接口
19
+----------------------------------------
20
+-- 请求获取背包信息
21
+function root:itf_get_info(role, msg)
22
+    local items = self:redis_get_key_info("itemList")
23
+    return code.OK, {items = items}
24
+end
25
+
26
+return root

+ 12 - 7
dev/modules/user.lua

@@ -1,7 +1,8 @@
1 1
 local root = class("moduleUser", require("base.baseModule"))
2 2
 
3 3
 function root:ctor(uid)
4
-    root.super.ctor(self, uid, "user", "uid")
4
+    root.super.ctor(self, uid, "user", "uid", true)
5
+    self.uid = uid
5 6
 end
6 7
 
7 8
 function root:mysql_get_init_columns()
@@ -17,8 +18,6 @@ function root:mysql_get_init_columns()
17 18
         sysVer = "varchar(1024) DEFAULT NULL",
18 19
         version = "varchar(45) DEFAULT NULL",
19 20
         appVersion = "varchar(45) DEFAULT NULL",
20
-        city = "varchar(45) DEFAULT NULL",
21
-        region = "varchar(45) DEFAULT NULL",
22 21
         deviceId = "varchar(200) DEFAULT NULL",
23 22
         headUrl = "varchar(500) DEFAULT NULL",
24 23
         sex = "int(11) DEFAULT NULL",
@@ -26,13 +25,19 @@ function root:mysql_get_init_columns()
26 25
         lastLoginTime = "int(11) DEFAULT 0",
27 26
         logoutTime = "int(11) DEFAULT 0",
28 27
         registerTime = "int(11) DEFAULT 0 COMMENT '注册时间'",
29
-        registerRegion = "varchar(45) DEFAULT 0 COMMENT '注册区域'",
30
-        registerCity = "varchar(45) DEFAULT 0 COMMENT '注册城市'",
31 28
         registerVersion = "varchar(45) DEFAULT NULL COMMENT '注册版本'",
32 29
         ip = "varchar(45) DEFAULT NULL COMMENT 'IP'",
33
-        status = "int(11) DEFAULT 0 COMMENT '账号状态 0:正常 1:封号 2:注销'",
34
-        openId = "varchar(45) DEFAULT NULL COMMENT '用户唯一标识符'"
30
+        status = "int(11) DEFAULT 0 COMMENT '账号状态 0:正常 1:封号 2:注销'"
35 31
     }
36 32
 end
37 33
 
34
+----------------------------------------
35
+-- 接口
36
+----------------------------------------
37
+-- 获取自己的信息
38
+function root:itf_get_info(role, msg)
39
+    local items = self:redis_get_key_info("itemList")
40
+    return code.OK, {items = items}
41
+end
42
+
38 43
 return root

+ 18 - 0
nodes/game/interface/bag.lua

@@ -0,0 +1,18 @@
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
+
11
+local root = {}
12
+
13
+-- 获取背包信息
14
+function root:bag_get_info(role, msg)
15
+    return role.bag:itf_get_info(role, msg)
16
+end
17
+
18
+return root

+ 8 - 7
nodes/game/interface/user.lua

@@ -7,20 +7,19 @@ LastEditors: Neo,Huang
7 7
 LastEditTime: 2022-07-05 20:29:29
8 8
 --]]
9 9
 local code = require "code"
10
+local timeUtil = require("utils.timeUtil")
10 11
 
11
-local root = {}
12
+local moduleUser = require("modules.user")
12 13
 
13
-function root:self_info(uid, msg)
14
-    return code.OK
15
-end
14
+local root = {}
16 15
 
17
-function root:after_self_info(uid)
16
+function root:user_keepalive(role)
17
+    return code.OK, {systemTime = timeUtil.now(role.uid)}
18 18
 end
19 19
 
20
-function root:keep_alive(uid)
21
-    return code.OK
20
+-- 请求个人信息
21
+function root:user_self_info(role, msg)
22
+    return moduleUser:itf_get_info(role, msg)
22 23
 end
23 24
 
24 25
 return root

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

@@ -0,0 +1,96 @@
1
+local dataMode = require "dataMode"
2
+
3
+local sessionData = require("data.session")
4
+
5
+local root = class("role")
6
+
7
+-- 角色构造
8
+function root:ctor(uid, gSession)
9
+    local mapClass = dataMode.get_module_class_map()
10
+
11
+    self.uid = uid
12
+    self.gateNode = nil
13
+    self.gateAgent = nil
14
+
15
+    self.moduleList = {}
16
+    -- self.createTime = timeUtil.currentTime()
17
+    for _, moduleClass in pairs(mapClass) do
18
+        local cname = moduleClass.__cname
19
+        assert(self[cname] == nil)
20
+
21
+        local moduleObj = moduleClass.new(uid)
22
+        if not moduleObj.noPersonal then
23
+            self[cname] = moduleObj
24
+            table.insert(self.moduleList, cname)
25
+        end
26
+    end
27
+end
28
+
29
+-- 登陆各个模块
30
+function root:login(session)
31
+    -- 模块载入热数据
32
+    self:load_modules()
33
+
34
+    -- 初始化session 信息
35
+    local nodeInfo = {nodeName = session.gateNode, agent = session.gateAgent}
36
+    sessionData:user_update_cluster_info(self.uid, "gate", nodeInfo)
37
+    nodeInfo.nodeName = session.gameNode
38
+    nodeInfo.agent = session.gameAgent
39
+    sessionData:user_update_cluster_info(self.uid, "game", nodeInfo)
40
+    -- 记录网关节点信息
41
+    self.gateNode = session.gateNode
42
+    self.gateAgent = session.gateAgent
43
+
44
+    for _, cname in pairs(self.moduleList) do
45
+        self[cname]:do_login()
46
+    end
47
+end
48
+
49
+-- 加载模块
50
+function root:load_modules()
51
+    for _, cname in ipairs(self.moduleList) do
52
+        self[cname]:get_data_from_db()
53
+    end
54
+end
55
+
56
+-- 登出各个模块
57
+function root:logout_modules()
58
+    for _, cname in pairs(self.moduleList) do
59
+        self[cname]:do_logout()
60
+    end
61
+    -- 删除玩家session信息
62
+    sessionData:user_update_cluster_info(self.uid, "gate")
63
+    sessionData:user_update_cluster_info(self.uid, "game")
64
+end
65
+
66
+-- 初始化游戏事件
67
+function root:init_game_event()
68
+    self.eventIdMap = nil
69
+    for _, cname in pairs(self.moduleList) do
70
+        if self[cname].get_listen_event_list then
71
+            local eventIdList = self[cname]:get_listen_event_list()
72
+            for _, eventId in ipairs(eventIdList or {}) do
73
+                self.eventIdMap = self.eventIdMap or {}
74
+                self.eventIdMap[eventId] = self.eventIdMap[eventId] or {}
75
+                table.insert(self.eventIdMap[eventId], cname)
76
+            end
77
+        end
78
+    end
79
+end
80
+
81
+-- 分派玩家事件
82
+function root:dispath_game_event(id, params)
83
+    if not self.eventIdMap then
84
+        self:init_game_event()
85
+    end
86
+
87
+    if self.eventIdMap == nil or not self.eventIdMap[id] then
88
+        return
89
+    end
90
+
91
+    for _, cname in pairs(self.eventIdMap[id]) do
92
+        self[cname]:user_game_event(id, params)
93
+    end
94
+end
95
+
96
+return root

+ 56 - 0
nodes/game/lib/roleMgr.lua

@@ -0,0 +1,56 @@
1
+--[[
2
+Descripttion:角色管理
3
+version:
4
+Author: Neo,Huang
5
+Date: 2021-08-31 20:45:03
6
+LastEditors: Neo,Huang
7
+LastEditTime: 2021-09-24 09:55:30
8
+--]]
9
+local role = require "role"
10
+
11
+local root = class("roleMgr")
12
+
13
+function root:ctor()
14
+    self.roleList = {}
15
+    self.amount = 0
16
+end
17
+
18
+function root:login_role(uid, gSession)
19
+    local roleObj = role.new(uid, gSession)
20
+    roleObj:login(gSession)
21
+    log.print("login_role %s", uid)
22
+    self.roleList[uid] = roleObj
23
+    self.amount = self.amount + 1
24
+end
25
+
26
+function root:logout_role(uid)
27
+    local roleObj = self:get_role_obj(uid)
28
+    if not roleObj then
29
+        return
30
+    end
31
+
32
+    log.print("logout_role %s", uid)
33
+    roleObj:logout_modules()
34
+    self.roleList[uid] = nil
35
+    self.amount = self.amount - 1
36
+end
37
+
38
+function root:get_role_obj(uid)
39
+    return self.roleList[uid]
40
+end
41
+
42
+function root:get_role_obj_list()
43
+    return self.roleList
44
+end
45
+
46
+-- 派发游戏事件
47
+function root:dispath_game_event(uid, eventId, eventParams)
48
+    local roleObj = self:get_role_obj(uid)
49
+    if not roleObj then
50
+        return
51
+    end
52
+
53
+    roleObj:dispath_game_event(eventId, eventParams)
54
+end
55
+
56
+return root

+ 24 - 15
nodes/game/service/srvAgent.lua

@@ -7,6 +7,7 @@ local jmService = require "jmService"
7 7
 local nodeUtil = require("utils.nodeUtil")
8 8
 local lib_queue = require("lib_queue")
9 9
 local util_event = require("utils.util_event")
10
+local roleMgr = (require "roleMgr").new()
10 11
 
11 12
 local mapItf2Module = {}
12 13
 
@@ -16,7 +17,7 @@ local root = {
16 17
 
17 18
 -- 登录
18 19
 function root.login(uid, session)
19
-	root.mapUid2Agent[uid] = session
20
+	cs(uid)(roleMgr.login_role, roleMgr, uid, gSession)
20 21
 	-- 通知 agentmgr
21 22
 	skynet.call(".srvAgentMgr", "lua", "login", uid, session)
22 23
 
@@ -26,7 +27,7 @@ function root.login(uid, session)
26 27
 end
27 28
 -- 登出
28 29
 function root.logout(uid, session)
29
-	root.mapUid2Agent[uid] = nil
30
+	cs(uid)(roleMgr.logout_role, roleMgr, uid)
30 31
 
31 32
 	skynet.call(".srvAgentMgr", "lua", "logout", uid, session)
32 33
 	nodeUtil:user_master_unband_node(uid)
@@ -39,12 +40,12 @@ local function l_c2s_response(uid, protoName, errCode, res, ...)
39 40
 	res = res or {}
40 41
 	res.code = errCode
41 42
 
42
-	local session = root.mapUid2Agent[uid]
43
-	if session == nil or session.gateNode == nil or session.gateAgent == nil then
43
+	local role = roleMgr:get_role_obj(uid)
44
+	if not role or role.gateNode == nil or role.gateAgent == nil then
44 45
 		return
45 46
 	end
46 47
 
47
-	nodeUtil:send_to_node(session.gateNode, session.gateAgent, "c2s_response", uid, protoName, res, ...)
48
+	nodeUtil:send_to_node(role.gateNode, role.gateAgent, "c2s_response", uid, protoName, res, ...)
48 49
 end
49 50
 
50 51
 -- 格式化协议
@@ -52,10 +53,9 @@ local function l_get_interface_and_func(protoName)
52 53
 	-- return protoName:match "([^_]*)_(.*)"
53 54
 
54 55
 	local data = string.split(protoName, "_")
55
-	local itfName = table.remove(data, 1)
56
+	local itfName = data[1]
56 57
 
57
-	local funcName = table.concat(data, "_")
58
-	return itfName, funcName
58
+	return itfName, protoName
59 59
 end
60 60
 
61 61
 -- 获取接口对象
@@ -67,8 +67,8 @@ end
67 67
 
68 68
 -- 处理客户端协议数据
69 69
 function root.c2s_forward(uid, protoName, args, ...)
70
-	local session = root.mapUid2Agent[uid]
71
-	if session == nil then
70
+	local role = roleMgr:get_role_obj(uid)
71
+	if role == nil then
72 72
 		log.warning("c2s_forward not find role! uid:%s", uid)
73 73
 		return l_c2s_response(uid, protoName, code.INNER_SERVER_ERROR)
74 74
 	end
@@ -93,7 +93,7 @@ function root.c2s_forward(uid, protoName, args, ...)
93 93
 			skynet.error(debug.traceback())
94 94
 		end,
95 95
 		itfModule,
96
-		uid,
96
+		role,
97 97
 		args
98 98
 	)
99 99
 	if not ok then
@@ -117,7 +117,7 @@ function root.c2s_forward(uid, protoName, args, ...)
117 117
 			skynet.error(debug.traceback())
118 118
 		end,
119 119
 		itfModule,
120
-		uid,
120
+		role,
121 121
 		args
122 122
 	)
123 123
 	if not ok then
@@ -127,6 +127,11 @@ function root.c2s_forward(uid, protoName, args, ...)
127 127
 end
128 128
 
129 129
 function root.doCmd(uid, itfName, cmd, args)
130
+	local role = roleMgr:get_role_obj(uid)
131
+	if not role then
132
+		log.error("doCmd uid[%s] role nil", tostring(uid))
133
+		return
134
+	end
130 135
 	local itfModule = l_get_interface_module(itfName)
131 136
 	if itfModule == nil then
132 137
 		log.error("doCmd dont find module ! itfName:%s, cmd:%s", itfName, cmd)
@@ -145,7 +150,7 @@ function root.doCmd(uid, itfName, cmd, args)
145 150
 		function()
146 151
 			skynet.error(debug.traceback())
147 152
 		end,
148
-		uid,
153
+		role,
149 154
 		args
150 155
 	)
151 156
 
@@ -193,12 +198,16 @@ end
193 198
 
194 199
 -- 在线玩家数
195 200
 function root.get_player_count()
196
-	return #root.mapUid2Agent
201
+	local count = 0
202
+	for _, v in pairs(roleMgr.get_role_obj_list()) do
203
+		count = count + 1
204
+	end
205
+	return count
197 206
 end
198 207
 -- 打印 - 玩家列表
199 208
 function root.print_uid_list()
200 209
 	local uidList = {}
201
-	for uid, _ in pairs(root.mapUid2Agent) do
210
+	for uid, _ in pairs(roleMgr.get_role_obj_list()) do
202 211
 		table.insert(uidList, uid)
203 212
 	end
204 213
 	log.warning("print_uid_list uidList:%s", tostring(uidList))