CommonFunc.lua
9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
local skynet = require "skynet"
local cjson = require "shared.json"
local md5 = require("md5")
local httpc = require("http.httpc")
local servId = tonumber(skynet.getenv("servId"))
function getRandomName()
local nameCsv = csvdb["name_combCsv"]
local part1 = nameCsv[1][math.random(1, #nameCsv[1])].name
local part2 = nameCsv[2][math.random(1, #nameCsv[2])].name
local part3 = nameCsv[3][math.random(1, #nameCsv[3])].name
return table.concat({part1, part2, part3})
end
function getActionCode(role)
if not role.uniqueCount then
role.uniqueCount = 0
end
local action = {role:getProperty("id"), skynet.timex(), role.uniqueCount}
role.uniqueCount = role.uniqueCount + 1
return table.concat(action, "_")
end
-- begin 数据库自增字段
function getNextRoleId()
local roleId = redisproxy:hget("autoincrement_set", "role")
if roleId - servId * MAX_ROLE_NUM >= MAX_ROLE_NUM - 1 then
return
end
return redisproxy:hincrby("autoincrement_set", "role", 1)
end
function getNextTradeId()
return redisproxy:hincrby("autoincrement_set", "trade", 1)
end
-- end 数据库自增字段
-- 查找上限
-- [10, 20, 30, 40] 查找15, 返回指向10的元素
function lowerBoundSeach(data, searchKey)
-- 先排序
local lastKey = nil
local keys = table.keys(data)
table.sort(keys)
for _, key in ipairs(keys) do
if key > searchKey then
break
end
lastKey = key
end
return lastKey and data[lastKey] or nil
end
-- 将201402021800或者20140202的格式转化成unixtime
function toUnixtime(timeStr)
local strLength = string.len(timeStr)
if strLength ~= 8 and strLength ~= 10 then return end
local year = string.sub(timeStr, 1, 4)
local month = string.sub(timeStr, 5, 6)
local day = string.sub(timeStr, 7, 8)
local hour, minute = 0, 0
if strLength == 10 then
hour = string.sub(timeStr, 9, 10)
--minute = string.sub(timeStr, 11, 12)
end
return os.time{year=year, month=month, day=day, hour=hour, min=minute, sec=0}
end
-- 判断时间点是不是当天
function isToday(curTimestamp)
local curTm = os.date("*t", curTimestamp)
local nowTm = os.date("*t", skynet.timex())
return curTm.year == nowTm.year and curTm.month == nowTm.month and curTm.day == nowTm.day
end
function withTimeHead(value, now)
now = now or skynet.timex()
return (2000000000-now) .. "." .. value
end
function cutTimeHead(value)
return tonumber(value:match("%d+%.(%d+)"))
end
-- 判断是不是同一个星期
function isCrossWeek(target, now)
now = now or skynet.timex()
return specMonday(target) ~= specMonday(now - RESET_TIME * 3600)
end
-- 两个时间相隔多少星期
function crossWeek(target, now)
now = now or skynet.timex()
return math.floor((specMonday(target)-specMonday(now))/604800)
end
-- 判断是不是同一个月
function isCrossMonth(target, now)
now = now or skynet.timex()
local tarTm = os.date("*t", target - RESET_TIME * 3600)
local nowTm = os.date("*t", now - RESET_TIME * 3600)
if tarTm.year == nowTm.year and tarTm.month == nowTm.month then
return false
else
return true
end
end
function isCrossDay(lastTime, now, resetTime)
resetTime = resetTime or RESET_TIME
if lastTime == 0 then return true end
now = now or skynet.timex()
local todayResetH = specTime({hour = resetTime}, now - resetTime * 3600)
return lastTime < todayResetH and now >= todayResetH
end
function crossDay(target, now)
now = now or skynet.timex()
local tarTime = specTime({hour = RESET_TIME}, target)
--local nowTime = specTime({hour = RESET_TIME}, now) --当前时间有没有过一天而不是当前天四点
return math.floor((now - tarTime)/ 86400)
end
function crossWeekFromOpen(now)
now = now or skynet.timex()
local openTime = os.time{
year = tonum(SERV_OPEN:sub(1,4)),
month = tonum(SERV_OPEN:sub(5,6)),
day = tonum(SERV_OPEN:sub(7,8)),
}
return crossWeek(openTime, now)
end
function crossDayFromOpen(now)
now = now or skynet.timex()
local openTime = os.time{
year = tonum(SERV_OPEN:sub(1,4)),
month = tonum(SERV_OPEN:sub(5,6)),
day = tonum(SERV_OPEN:sub(7,8)),
hour = RESET_TIME,
}
return crossDay(openTime, now)
end
function diffFromOpen()
now = now or skynet.timex()
local openTime = os.time{
year = tonum(SERV_OPEN:sub(1,4)),
month = tonum(SERV_OPEN:sub(5,6)),
day = tonum(SERV_OPEN:sub(7,8)),
hour = RESET_TIME,
}
if now < openTime then
return -1
end
return math.floor((now - openTime) / DAY_SEC)
end
function diffFromTs(ts)
local now = skynet.timex()
local tm = os.date("*t", ts)
local time = os.time({year = tm.year, month = tm.month, day = tm.day, hour = RESET_TIME})
if now < time then
return 0
end
return math.floor((now - time) / DAY_SEC)
end
function getServerOpenTs()
local openTime = os.time{
year = tonum(SERV_OPEN:sub(1,4)),
month = tonum(SERV_OPEN:sub(5,6)),
day = tonum(SERV_OPEN:sub(7,8)),
hour = RESET_TIME,
}
return openTime
end
-- 30*86400 = 2592000
function monthLater(now)
now = now or skynet.timex()
now = now - RESET_TIME * 3600 -- 排除0-4点钟 影响
return specTime({hour = RESET_TIME}, now) + 2592000
end
-- 一天以后
function dayLater(now)
now = now or skynet.timex()
now = now - RESET_TIME * 3600 -- 排除0-4点钟 影响
return specTime({hour = RESET_TIME}, now) + 86400
end
-- 到下一个时间点的秒数差和下一个时间点的unixtime
function diffTime(params)
params = params or {}
local currentTime = skynet.timex()
local curTm = os.date("*t", currentTime)
local nextYear = params.year or curTm.year
local nextMonth = params.month or curTm.month
local nextDay = params.day or curTm.day + 1
local nextHour = params.hour or 0
local nextMinute = params.min or 0
local nextSecond = params.sec or 0
local nextUnixTime = os.time({ year = nextYear, month = nextMonth, day = nextDay, hour = nextHour, min = nextMinute, sec = nextSecond})
return os.difftime(nextUnixTime, currentTime), nextUnixTime
end
-- 取今天特殊时刻时间戳
function specTime(pms, now)
now = now or skynet.timex()
local tm = os.date("*t", now)
local year = pms.year or tm.year
local month = pms.month or tm.month
local day = pms.day or tm.day
local hour = pms.hour or 0
local min = pms.min or 0
local sec = pms.sec or 0
return os.time({year = year, month = month, day = day, hour = hour, min = min, sec = sec})
end
function specMonday(now)
now = now or skynet.timex()
local tm = os.date("*t", now)
local wday = (tm.wday + 6) % 7
if wday == 0 then wday = 7 end
local time = os.time({year = tm.year, month = tm.month, day = tm.day, hour = RESET_TIME})
return time - (wday - 1) * 86400
end
function isSpecTime(ctime, startHour, endHour, specday)
ctime = ctime or skynet.timex()
local tm = os.date("*t", ctime)
if specday then
local day = (tm.wday+6)%7
if day == 0 then day = 7 end
return specday == day and tm.hour >= startHour and tm.hour < endHour
end
return tm.hour >= startHour and tm.hour < endHour
end
function getSecond(timeStr)
timeStr = timeStr or "0000"
local hour = tonumber(string.sub(timeStr, 1, 2))
local min = tonumber(string.sub(timeStr, 3, 4))
return hour * 3600 + min * 60
end
function urlencode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
function isnan(value)
return value ~= value
end
-- 推送通知
local serverid = skynet.getenv "serverid"
local secretKey = "467C2221D3A20FE69D23A33E8940C2C5"
-- 推送该服务器的所有用户
-- tag都是交集处理
function notifyClients(msg, otherTags)
local tags = { serverid }
for _, tag in ipairs(otherTags or {}) do
table.insert(tags, tag)
end
local content = {
["appid"] = "1000013239",
["audience"] = {
[otherTags and "tag_and" or "tag"] = tags,
},
-- ["audience"] = "all",
["notification"] = {
["alert"] = msg,
},
["options"] = {
["ttl"] = 60 * 120
}
}
local contentJson = cjson.encode(content)
local header = {
["content-type"] = "application/x-www-form-urlencoded",
["X-MJPUSH-SIGNATURE"] = md5.sumhexa(urlencode(contentJson .. "&" .. secretKey))
}
local status, body = httpc.request("POST", "push.mjyun.com", "/api/push", {}, header, contentJson)
if tonumber(status) ~= 200 then
skynet.error(status, body)
end
end
-- { uid, msg, scheduleTime}
function notifyClient(params)
params = params or {}
params.key = "zhaolugame20170831"
local status, body = httpc.get(skynet.getenv("codeurl"),
"/push/notify_user?" .. httpGetFormatData(params), {}, {})
if tonumber(status) ~= 200 then
skynet.error(status, body)
return
end
return body
end
-- { uid, msgId}
function deleteNotify(params)
params = params or {}
params.key = "zhaolugame20170831"
local status, body = httpc.get(skynet.getenv("codeurl"),
"/push/delete_notify?" .. httpGetFormatData(params), {}, {})
if tonumber(status) ~= 200 then
skynet.error(status, body)
return
end
return body
end
--http get数据
function httpGetFormatData(params)
local function escape(s)
return (string.gsub(s, "([^A-Za-z0-9_])", function(c)
return string.format("%%%02X", string.byte(c))
end))
end
local body = {}
for k, v in pairs(params) do
table.insert(body, string.format("%s=%s", escape(k), escape(v)))
end
return table.concat(body, "&")
end