Blame view

src/services/globald.lua 10.2 KB
314bc5df   zhengshouren   提交服务器初始代码
1
2
3
4
  local skynet = require "skynet"
  local harbor = require "skynet.harbor"
  local json = require("shared.json")
  local redisproxy = require("shared.redisproxy")
1279810c   liuzujun   海港贸易任务新增表
5
  local mysqlproxy = require("shared.mysqlproxy")
314bc5df   zhengshouren   提交服务器初始代码
6
7
8
9
10
  
  require "shared.init"
  require "utils.init"
  require "RedisKeys"
  require "skynet.manager"
66fe093a   liuzujun   元旦关卡活动
11
  require "GlobalVar"
913e070e   liuzujun   添加订单表,全局id定时回写数据库
12
  require "utils.MysqlUtil"
314bc5df   zhengshouren   提交服务器初始代码
13
  
875e5071   zhouhaihai   服务名称修改
14
  
314bc5df   zhengshouren   提交服务器初始代码
15
16
17
18
19
  local ipairs = ipairs
  local table_insert = table.insert
  local tarr2tab = table.array2Table
  local string_format = string.format
  
314bc5df   zhengshouren   提交服务器初始代码
20
  
875e5071   zhouhaihai   服务名称修改
21
  local CHECK_MAIL_STATUS_INTERVAL 	= 100 * 60
66fe093a   liuzujun   元旦关卡活动
22
  local CHECK_BATTLE_ACT_CLOSE_INTERVAL = 100 * 1
913e070e   liuzujun   添加订单表,全局id定时回写数据库
23
  local SAVE_AUTOINCREMENT_SET_INTERVAL = 100 * 2
314bc5df   zhengshouren   提交服务器初始代码
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
67
  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
68
  				"createtime", email.startTime,
314bc5df   zhengshouren   提交服务器初始代码
69
  				"title", email.title,
eb4b0152   zhouhaihai   邮件增加 stitle
70
  				"stitle", email.stitle,
314bc5df   zhengshouren   提交服务器初始代码
71
72
73
74
  				"content", email.content,
  				"attachments", email.attachments,
  				"endtime", email.endTime,
  				"mid", email.mid,
017184f4   zhouhaihai   global
75
76
  				"timestamp", now,
  				"delayType", email.delayType
314bc5df   zhengshouren   提交服务器初始代码
77
78
79
80
  			)
  		else
  			redisproxy:hmset(string_format("globalEmail:%s", gid), 
  				"id", gid,
ca128b32   zhouhaihai   邮件时间bug
81
  				"createtime", email.startTime,
d9cb65a9   zhouhaihai   bug
82
  				"title", email.title,
eb4b0152   zhouhaihai   邮件增加 stitle
83
  				"stitle", email.stitle,
314bc5df   zhengshouren   提交服务器初始代码
84
85
86
  				"content", email.content,
  				"attachments", email.attachments,
  				"endtime", email.endTime,
017184f4   zhouhaihai   global
87
88
  				"timestamp", now,
  				"delayType", email.delayType
314bc5df   zhengshouren   提交服务器初始代码
89
90
91
92
93
94
95
96
97
98
99
100
  			)
  		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   liuzujun   元旦关卡活动
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
138
  -- @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   liuzujun   添加邮件表
139
  							mysqlproxy:insertEmail({
66fe093a   liuzujun   元旦关卡活动
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  								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   liuzujun   添加订单表,全局id定时回写数据库
159
  local function save_autoincrement_timer()
1279810c   liuzujun   海港贸易任务新增表
160
  	local function saveUidToMysql(tbName, keyName, fieldName, ignore)
913e070e   liuzujun   添加订单表,全局id定时回写数据库
161
162
163
  		if not fieldName then fieldName = "value" end
  		local mysqlVal = getDbCfgVal(tbName, keyName, fieldName) or 0
  		local redisVal = tonum(redisproxy:hget(tbName, keyName))
1279810c   liuzujun   海港贸易任务新增表
164
  		if (not ignore) and redisVal > mysqlVal then
913e070e   liuzujun   添加订单表,全局id定时回写数据库
165
166
167
  			setDbCfgVal(tbName, keyName, fieldName, redisVal)
  		end
  	end
c384626d   zhouhaihai   好友
168
  
913e070e   liuzujun   添加订单表,全局id定时回写数据库
169
170
171
172
173
174
  	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")
1279810c   liuzujun   海港贸易任务新增表
175
176
177
178
179
  	saveUidToMysql("autoincrement_set", "stopcreate", true)
  	saveUidToMysql("autoincrement_set", "maintain", true)
  	saveUidToMysql("autoincrement_set", "seaportTime0", true)
  	saveUidToMysql("autoincrement_set", "seaportTime1", true)
  	saveUidToMysql("autoincrement_set", "seaportTime2", true)
913e070e   liuzujun   添加订单表,全局id定时回写数据库
180
181
182
  
  	skynet.timeout(SAVE_AUTOINCREMENT_SET_INTERVAL, save_autoincrement_timer)
  end
c384626d   zhouhaihai   好友
183
  
1279810c   liuzujun   海港贸易任务新增表
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  local function save_seaport_task()
  	local keys = {[SEAPORT_TRADE_TASK_1] = "seaport_task_1", [SEAPORT_TRADE_TASK_2] = "seaport_task_2"}
  	local sql = "INSERT INTO `%s` VALUES (%d, %d) ON DUPLICATE KEY UPDATE `value` = %d;"
  	for k, tb_name in pairs(keys) do
  		local data = redisproxy:hgetall(k)
  		if not next(data) then
  			mysqlproxy:query(string_format("DELETE FROM %s;", tb_name))
  		end
  		local infos = tarr2tab(data)
  		for id, value in pairs(infos) do
  			mysqlproxy:query(string_format(sql, tb_name, id, value, value))
  		end
  	end
  
  	skynet.timeout(SAVE_AUTOINCREMENT_SET_INTERVAL, save_seaport_task)
  end
  
65b551a7   chenyueqi   海港贸易季初版
201
202
  -- @desc: 检查海港贸易开启、关闭
  local function check_trade_seaport_status()
e820b3f3   chenyueqi   海港贸易季bug修正
203
  	local nowTime = skynet.timex()
65b551a7   chenyueqi   海港贸易季初版
204
  	local nextTime = dayLater(nowTime)
e820b3f3   chenyueqi   海港贸易季bug修正
205
  	local interval = 100 * (nextTime - nowTime + 1)
65b551a7   chenyueqi   海港贸易季初版
206
207
  
  	local curTm = os.date("*t", nowTime)
e820b3f3   chenyueqi   海港贸易季bug修正
208
209
210
211
212
213
214
215
216
  	if curTm.wday == 7 and curTm.hour >= 4 then -- 周六
  		local oldTime = tonumber(redisproxy:hget("autoincrement_set", "seaportTime0")) or 0
  		local cur4Time = specTime({hour = 4},nowTime)
  		if cur4Time ~= oldTime then
  			redisproxy:hset("autoincrement_set", "seaportTime0", cur4Time)
  			redisproxy:hset("autoincrement_set", "seaportTime1", 1)
  			redisproxy:del(SEAPORT_TRADE_TASK_1)
  			redisproxy:del(SEAPORT_TRADE_TASK_2)
  		end
65b551a7   chenyueqi   海港贸易季初版
217
  	end
e820b3f3   chenyueqi   海港贸易季bug修正
218
219
220
221
  	if curTm.wday == 1 and curTm.hour >= 4 then -- 周日
  		if (tonumber(redisproxy:hget("autoincrement_set", "seaportTime1")) or 0) == 1 then
  			redisproxy:hset("autoincrement_set", "seaportTime2", 1)
  		end
65b551a7   chenyueqi   海港贸易季初版
222
  	end
e820b3f3   chenyueqi   海港贸易季bug修正
223
  	if curTm.wday == 2 and curTm.hour >= 4 then -- 周一
65b551a7   chenyueqi   海港贸易季初版
224
225
226
227
  		-- redisproxy:hset("autoincrement_set", "seaportTime0", 0)
  		redisproxy:hset("autoincrement_set", "seaportTime1", 0)
  		redisproxy:hset("autoincrement_set", "seaportTime2", 0)
  	end
c384626d   zhouhaihai   好友
228
  
65b551a7   chenyueqi   海港贸易季初版
229
230
  	skynet.timeout(interval, check_trade_seaport_status)
  end
c384626d   zhouhaihai   好友
231
  
d9d63fa0   chenyueqi   海港贸易季自动填充,活动结束后的奖...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
  -- @desc: 海港贸易捐赠作弊
  local seaportTimes = {}
  local function check_trade_seaport_donate()
  	local time1 = tonumber(redisproxy:hget("autoincrement_set", "seaportTime1")) or 0
  	local time2 = tonumber(redisproxy:hget("autoincrement_set", "seaportTime2")) or 0
  	if time1 == 0 and time2 == 0 then
  		seaportTimes = {}
  		skynet.timeout(360000, check_trade_seaport_donate)
  		return
  	end
  
  	local csvdb = require "shared.csvdata"
  	local donateCsv = csvdb["seaport_purchaseCsv"]
  
  	local times = {time1, time2}
  	for phase, open in ipairs(times) do
  		if open == 1 and not seaportTimes[phase] then
  			seaportTimes[phase] = {}
  			for _, _ in ipairs(donateCsv[phase]) do
  				table.insert(seaportTimes[phase],math.randomInt(1800, 3600))
  			end
  		end
  	end
  
  	local interval = 3600
  	local phase, id = 0, 0
  
  	for key1, data in ipairs(seaportTimes) do
  		for key2, temp in pairs(data) do
  			if times[key1] == 1 and temp < interval then
  				interval = temp
  				phase = key1
  				id = key2
  			end
  		end
  	end
  
  	for _, data in ipairs(seaportTimes) do
  		for id, temp in pairs(data) do
  			data[id] = temp - interval
  			if data[id] == 0 then
  				data[id] = math.randomInt(1800, 3600)
  			end
  		end
  	end
  
  	if phase ~= 0 and id ~= 0 then
  		local redisKeys = {SEAPORT_TRADE_TASK_1,SEAPORT_TRADE_TASK_2}
  		local all = donateCsv[phase][id].need_num
  		local old = tonumber(redisproxy:hget(redisKeys[phase],id)) or 0
  		local add = math.min(math.randomInt(math.floor(all/20),math.floor(all/12)),all - old)
  		if add > 0 then
  			redisproxy:hincrby(redisKeys[phase],id,add)
  		else
  			seaportTimes[phase][id] = nil
  		end
  	end
  
  	skynet.timeout(interval * 100, check_trade_seaport_donate)
  end
  
4cd97819   zhouhaihai   工作日玩法增加机器人
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  local function check_work_battle()
  	local csvdb = require "shared.csvdata"
  	local now = skynet.timex()
  	local day = weekday(now)
  	local workMainCsv = csvdb["work_mainCsv"][day]
  	if workMainCsv and isSpecTime(now - RESET_TIME * 3600, csvdb["time_resetCsv"][TimeReset["WorkBattle1"]].start / 3600, 24) then
  		local round = math.floor((now - START_RESET_TIME) / 604800)
  		local field = round * 10 + day
  		local count = tonum(redisproxy:hget(WORK_BATTLE_COUNT, field))
  		if count < workMainCsv.target_num then
  			local add = math.floor(workMainCsv.target_num * math.randomInt(3125, 5000) / 100000)
  			redisproxy:hincrby(WORK_BATTLE_COUNT, field, add)
  		end
  	end
28b0c285   zhouhaihai   时间随机精确到秒
307
  	skynet.timeout(math.randomInt(10 * 60, 15 * 60) * 100, check_work_battle)
4cd97819   zhouhaihai   工作日玩法增加机器人
308
309
  end
  
314bc5df   zhengshouren   提交服务器初始代码
310
  local CMD = {}
c384626d   zhouhaihai   好友
311
  
875e5071   zhouhaihai   服务名称修改
312
313
  
  -- 服务器缓存50条消息
c384626d   zhouhaihai   好友
314
315
316
317
318
319
320
321
322
323
  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   好友
324
325
326
327
328
  function CMD.getWorldMsg(channel)
  	local msgs = cacheWorldMsg[channel] or {}
  	return msgs
  end
  
875e5071   zhouhaihai   服务名称修改
329
  
314bc5df   zhengshouren   提交服务器初始代码
330
  function CMD.start()
4cd97819   zhouhaihai   工作日玩法增加机器人
331
  	math.randomInit(skynet.timex())
314bc5df   zhengshouren   提交服务器初始代码
332
  	check_mail_queue()
14f57eed   liuzujun   去除globald中发放活动排行奖励
333
  	--check_battle_act_close()
913e070e   liuzujun   添加订单表,全局id定时回写数据库
334
  	save_autoincrement_timer()
65b551a7   chenyueqi   海港贸易季初版
335
  	check_trade_seaport_status()
1279810c   liuzujun   海港贸易任务新增表
336
  	save_seaport_task()
d9d63fa0   chenyueqi   海港贸易季自动填充,活动结束后的奖...
337
  	check_trade_seaport_donate()
4cd97819   zhouhaihai   工作日玩法增加机器人
338
  	check_work_battle()
314bc5df   zhengshouren   提交服务器初始代码
339
340
341
342
  end
  
  local function __init__()
  	skynet.dispatch("lua", function(_, _, command, ...)
c384626d   zhouhaihai   好友
343
344
345
346
347
348
349
350
  		local f = CMD[command]
  		if f then
  			if command == "sendWorldMsg" then
  				skynet.ignoreret()
  				f(...)
  			else
  				skynet.ret(skynet.pack(f(...)))
  			end
314bc5df   zhengshouren   提交服务器初始代码
351
352
  		end
  	end)
a5486ede   zhouhaihai   csvdata 修改为 share...
353
  	skynet.register(".globald")
875e5071   zhouhaihai   服务名称修改
354
  
314bc5df   zhengshouren   提交服务器初始代码
355
356
357
  end
  
  skynet.start(__init__)