Blame view

src/services/globald.lua 5.75 KB
314bc5df   zhengshouren   提交服务器初始代码
1
2
3
4
5
6
7
8
9
  local skynet = require "skynet"
  local harbor = require "skynet.harbor"
  local json = require("shared.json")
  local redisproxy = require("shared.redisproxy")
  
  require "shared.init"
  require "utils.init"
  require "RedisKeys"
  require "skynet.manager"
66fe093a   测试   元旦关卡活动
10
  require "GlobalVar"
913e070e   测试   添加订单表,全局id定时回写数据库
11
  require "utils.MysqlUtil"
314bc5df   zhengshouren   提交服务器初始代码
12
  
875e5071   zhouhaihai   服务名称修改
13
  
314bc5df   zhengshouren   提交服务器初始代码
14
15
16
17
18
  local ipairs = ipairs
  local table_insert = table.insert
  local tarr2tab = table.array2Table
  local string_format = string.format
  
314bc5df   zhengshouren   提交服务器初始代码
19
  
875e5071   zhouhaihai   服务名称修改
20
  local CHECK_MAIL_STATUS_INTERVAL 	= 100 * 60
66fe093a   测试   元旦关卡活动
21
  local CHECK_BATTLE_ACT_CLOSE_INTERVAL = 100 * 1
913e070e   测试   添加订单表,全局id定时回写数据库
22
  local SAVE_AUTOINCREMENT_SET_INTERVAL = 100 * 2
314bc5df   zhengshouren   提交服务器初始代码
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  local function mailQuene()
  	local delayEmail = tonum(redisproxy:hget("autoincrement_set", "delay_email"))
  	if delayEmail == 0 then
  		return
  	end
  	local begin = math.max(delayEmail - 100, 1)
  	local mails = redisproxy:pipelining(function (red)
  		for id = begin, delayEmail do
  			red:hgetall(string.format("delayEmail:%s", id))
  		end
  	end)
  	if not mails then
  		return
  	end
  	local now = skynet.timex()
  	local mailList = {}
  	for index, data in ipairs(mails) do
  		if next(data) then
  			local email = tarr2tab(data)
  			if tonum(email.startTime) <= now then
  				table_insert(mailList, email)
  
  				if #mailList > 100 then
  					break
  				end
  			end
  		end
  	end
  	if #mailList == 0 then
  		return
  	end
  	redisproxy:pipelining(function (red)
  		for _, email in ipairs(mailList) do
  			red:del(string_format("delayEmail:%s", email.id))
  		end
  	end)
  	table.sort(mailList, function(a, b)
  		return tonum(a.id) < tonum(b.id)
  	end)
  	for _, email in ipairs(mailList) do
  		local gid = redisproxy:hincrby("autoincrement_set", "email", 1)
  		if email.mid then
  			redisproxy:hmset(string_format("globalEmail:%s", gid), 
  				"id", gid,
ca128b32   zhouhaihai   邮件时间bug
67
  				"createtime", email.startTime,
314bc5df   zhengshouren   提交服务器初始代码
68
  				"title", email.title,
eb4b0152   zhouhaihai   邮件增加 stitle
69
  				"stitle", email.stitle,
314bc5df   zhengshouren   提交服务器初始代码
70
71
72
73
  				"content", email.content,
  				"attachments", email.attachments,
  				"endtime", email.endTime,
  				"mid", email.mid,
017184f4   zhouhaihai   global
74
75
  				"timestamp", now,
  				"delayType", email.delayType
314bc5df   zhengshouren   提交服务器初始代码
76
77
78
79
  			)
  		else
  			redisproxy:hmset(string_format("globalEmail:%s", gid), 
  				"id", gid,
ca128b32   zhouhaihai   邮件时间bug
80
  				"createtime", email.startTime,
d9cb65a9   zhouhaihai   bug
81
  				"title", email.title,
eb4b0152   zhouhaihai   邮件增加 stitle
82
  				"stitle", email.stitle,
314bc5df   zhengshouren   提交服务器初始代码
83
84
85
  				"content", email.content,
  				"attachments", email.attachments,
  				"endtime", email.endTime,
017184f4   zhouhaihai   global
86
87
  				"timestamp", now,
  				"delayType", email.delayType
314bc5df   zhengshouren   提交服务器初始代码
88
89
90
91
92
93
94
95
96
97
98
99
  			)
  		end
  	end
  	redisproxy:hset("autoincrement_set", "emailTimestamp", now)
  end
  
  -- @desc: 定时邮件队列检测
  local function check_mail_queue()
  	pcall(mailQuene)
  	skynet.timeout(CHECK_MAIL_STATUS_INTERVAL, check_mail_queue)
  end
  
66fe093a   测试   元旦关卡活动
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  -- @desc: 检测关卡活动结束 排行榜奖励发放
  local lastCheckBattleActTs = 0
  local function check_battle_act_close()
  	local csvdb = require "shared.csvdata"
  	local act = require "models.Activity"
  	local st = 0
  	local et = 0
  	local actId = 0
  	local timeNow = skynet.timex()
  	for k, v in pairs(csvdb["activity_ctrlCsv"]) do
  		if v.showType == act.ActivityType.ChallengeLevel then
  			local openTimes = v.time:toArray(false, "=")
  			if openTimes[1] ~= "0" then
  				st = toUnixtime(openTimes[1]..string_format("%02x", RESET_TIME))
  			end
  			if openTimes[2] ~= "0" then
  				et = toUnixtime(openTimes[2]..string_format("%02x", RESET_TIME))
  			end
  			actId = k
  			break
  		end
  	end
  	if st ~= 0 and et ~= 0 and actId ~= 0 then
  		-- close
  		if lastCheckBattleActTs < et and timeNow >= et then
  			-- check rank key exist
  			local rankKey = RANK_COMMON..RANK_TYPE.ActBattleBoss
  			local flag = redisproxy:exists(rankKey)
  			if flag then
  				local actData = csvdb["activity_groupCsv"][actId]
  				local lastRank = actData[#actData].rank
  
  				local ids = redisproxy:zrevrange(rankKey, 0 , lastRank - 1)
  				local rankIndex = 0
  				for roleId in pairs(ids) do
  					rankIndex = rankIndex + 1
  					for _, cfg in pairs(actData) do
  						if rankIndex <= cfg.rank then
2ca93972   测试   添加邮件表
138
  							mysqlproxy:insertEmail({
66fe093a   测试   元旦关卡活动
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  								roleId = roleId,
  								emailId = cfg.email_1,
  								attachments = cfg.reward_1,
  								contentPms = {rankIndex},
  							})
  							break
  						end
  					end
  				end
  
  				redisproxy:del(rankKey..":bak")
  				redisproxy:rename(rankKey, rankKey..":bak")
  			end
  		end
  	end
  	lastCheckBattleActTs = skynet.timex()
  	skynet.timeout(CHECK_BATTLE_ACT_CLOSE_INTERVAL, check_battle_act_close)
  end
  
913e070e   测试   添加订单表,全局id定时回写数据库
158
159
160
161
162
163
164
165
166
  local function save_autoincrement_timer()
  	local function saveUidToMysql(tbName, keyName, fieldName)
  		if not fieldName then fieldName = "value" end
  		local mysqlVal = getDbCfgVal(tbName, keyName, fieldName) or 0
  		local redisVal = tonum(redisproxy:hget(tbName, keyName))
  		if redisVal > mysqlVal then
  			setDbCfgVal(tbName, keyName, fieldName, redisVal)
  		end
  	end
c384626d   zhouhaihai   好友
167
  
913e070e   测试   添加订单表,全局id定时回写数据库
168
169
170
171
172
173
  	saveUidToMysql("autoincrement_set", "role")
  	saveUidToMysql("autoincrement_set", "union")
  	saveUidToMysql("autoincrement_set", "order")
  	saveUidToMysql("autoincrement_set", "email")
  	saveUidToMysql("autoincrement_set", "emailTimestamp")
  	saveUidToMysql("autoincrement_set", "delay_email")
6136eaca   测试   添加好友表
174
175
  	saveUidToMysql("autoincrement_set", "stopcreate")
  	saveUidToMysql("autoincrement_set", "maintain")
913e070e   测试   添加订单表,全局id定时回写数据库
176
177
178
  
  	skynet.timeout(SAVE_AUTOINCREMENT_SET_INTERVAL, save_autoincrement_timer)
  end
c384626d   zhouhaihai   好友
179
  
314bc5df   zhengshouren   提交服务器初始代码
180
  local CMD = {}
c384626d   zhouhaihai   好友
181
  
875e5071   zhouhaihai   服务名称修改
182
183
  
  -- 服务器缓存50条消息
c384626d   zhouhaihai   好友
184
185
186
187
188
189
190
191
192
193
  local cacheWorldMsg = {}
  local CACHE_WORLD_MSG_COUNT = 50
  function CMD.sendWorldMsg(channel, msg)
  	cacheWorldMsg[channel] = cacheWorldMsg[channel] or {}
  	table.insert(cacheWorldMsg[channel], msg)
  	for i = #cacheWorldMsg[channel] - CACHE_WORLD_MSG_COUNT, 1, -1 do
  		table.remove(cacheWorldMsg[channel], i)
  	end
  end
  
c384626d   zhouhaihai   好友
194
195
196
197
198
  function CMD.getWorldMsg(channel)
  	local msgs = cacheWorldMsg[channel] or {}
  	return msgs
  end
  
875e5071   zhouhaihai   服务名称修改
199
  
314bc5df   zhengshouren   提交服务器初始代码
200
201
  function CMD.start()
  	check_mail_queue()
14f57eed   测试   去除globald中发放活动排行奖励
202
  	--check_battle_act_close()
913e070e   测试   添加订单表,全局id定时回写数据库
203
  	save_autoincrement_timer()
314bc5df   zhengshouren   提交服务器初始代码
204
205
206
207
  end
  
  local function __init__()
  	skynet.dispatch("lua", function(_, _, command, ...)
c384626d   zhouhaihai   好友
208
209
210
211
212
213
214
215
  		local f = CMD[command]
  		if f then
  			if command == "sendWorldMsg" then
  				skynet.ignoreret()
  				f(...)
  			else
  				skynet.ret(skynet.pack(f(...)))
  			end
314bc5df   zhengshouren   提交服务器初始代码
216
217
  		end
  	end)
a5486ede   zhouhaihai   csvdata 修改为 share...
218
  	skynet.register(".globald")
875e5071   zhouhaihai   服务名称修改
219
  
314bc5df   zhengshouren   提交服务器初始代码
220
221
222
  end
  
  skynet.start(__init__)