436 lines
12 KiB
Lua
436 lines
12 KiB
Lua
|
|
local skynet = require "skynet"
|
|||
|
|
local oo = require "Class"
|
|||
|
|
local log = require "Log"
|
|||
|
|
local Common = oo.class()
|
|||
|
|
local pb = require "pb"
|
|||
|
|
Common.word ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|||
|
|
Common.number = "0123456789"
|
|||
|
|
Common.numberExceptZero = "123456789"
|
|||
|
|
Common.HttpMsg_Login = 100 --登陆
|
|||
|
|
Common.web = "192.168.50.215:8080"
|
|||
|
|
Common.redeemUrl = "/web/api/redeemCode/use"
|
|||
|
|
Common.getMailUrl = "/web/api/mail/getMail"
|
|||
|
|
Common.useMailUrl = "/web/api/mail/useMail"
|
|||
|
|
Common.getAnnouncementUrl = "/web/api/announcement/getAnnouncement"
|
|||
|
|
|
|||
|
|
--打印表
|
|||
|
|
function Common:TableToString(t)
|
|||
|
|
local sp = " ";
|
|||
|
|
local list = {};
|
|||
|
|
local function addline(str)
|
|||
|
|
table.insert(list, str);
|
|||
|
|
end
|
|||
|
|
local function do_tostring(tt, l, ln)
|
|||
|
|
local tp = type(tt);
|
|||
|
|
if (tp == "table") then
|
|||
|
|
l = l + 1;
|
|||
|
|
if (l - 1 == 0) then
|
|||
|
|
addline("{");
|
|||
|
|
end
|
|||
|
|
for k, v in pairs(tt) do
|
|||
|
|
local pp = type(v);
|
|||
|
|
if (pp == "table") then
|
|||
|
|
addline(string.format("%"..l.."s[%s]={",sp,k));
|
|||
|
|
do_tostring(v, l + 1);
|
|||
|
|
addline(string.format("%"..l.."s},",sp));
|
|||
|
|
else
|
|||
|
|
addline(string.format("%"..l.."s[%s]=%s,",sp,k,tostring(v)));
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
if (l - 1 == 0) then
|
|||
|
|
addline("}");
|
|||
|
|
end
|
|||
|
|
else
|
|||
|
|
addline(string.format("%"..l.."s=%s,",sp,k,tostring(tt)));
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
do_tostring(t, 0);
|
|||
|
|
return table.concat(list, "\n");
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
function Common:Split(input, delimiter)
|
|||
|
|
input = tostring(input)
|
|||
|
|
delimiter = tostring(delimiter)
|
|||
|
|
if (delimiter=='') then
|
|||
|
|
return false
|
|||
|
|
end
|
|||
|
|
local pos,arr = 0, {}
|
|||
|
|
for st,sp in function() return string.find(input, delimiter, pos, true) end do
|
|||
|
|
table.insert(arr, string.sub(input, pos, st - 1))
|
|||
|
|
pos = sp + 1
|
|||
|
|
end
|
|||
|
|
table.insert(arr, string.sub(input, pos))
|
|||
|
|
return arr
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--是否为同一天
|
|||
|
|
function Common:IsSameDay(time1 , time2)
|
|||
|
|
if os.date("*t", time1).year == os.date("*t", time2).year and
|
|||
|
|
os.date("*t", time1).month == os.date("*t", time2).month and
|
|||
|
|
os.date("*t", time1).day == os.date("*t", time2).day then
|
|||
|
|
return true
|
|||
|
|
end
|
|||
|
|
return false
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--是否为连续的一天
|
|||
|
|
function Common:IsSeriesDay(time1 , time2)
|
|||
|
|
local t1 = os.date("*t",time1)
|
|||
|
|
local t2 = os.date("*t",time2)
|
|||
|
|
if t1.yday == t2.yday + 1 then
|
|||
|
|
return true
|
|||
|
|
elseif t2.yday == t1.yday + 1 then
|
|||
|
|
return true
|
|||
|
|
end
|
|||
|
|
return false
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--随机数字ID
|
|||
|
|
function Common:RandNumberID( maxCount )
|
|||
|
|
local rand = 0
|
|||
|
|
local name = ""
|
|||
|
|
for i = 1, maxCount do
|
|||
|
|
local temp = nil
|
|||
|
|
if 1 == i then
|
|||
|
|
rand = math.random(1,#self.numberExceptZero)
|
|||
|
|
temp = string.sub(self.numberExceptZero,rand,rand)
|
|||
|
|
name = name .. temp
|
|||
|
|
else
|
|||
|
|
rand = math.random(1,#self.number)
|
|||
|
|
temp = string.sub(self.number,rand,rand)
|
|||
|
|
name = name .. temp
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
return name
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--随机ID
|
|||
|
|
function Common:RandID( maxCount )
|
|||
|
|
local rand = 0
|
|||
|
|
local name = ""
|
|||
|
|
for i = 1, maxCount do
|
|||
|
|
rand = math.random(1,52)
|
|||
|
|
local temp = string.sub(self.word,rand,rand)
|
|||
|
|
name = name .. temp
|
|||
|
|
end
|
|||
|
|
return name
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--随机name
|
|||
|
|
function Common:RandName()
|
|||
|
|
local rand = 0
|
|||
|
|
local name = ""
|
|||
|
|
for i = 1, 10 do
|
|||
|
|
rand = math.random(1,52)
|
|||
|
|
local temp = string.sub(self.word,rand,rand)
|
|||
|
|
name = name .. temp
|
|||
|
|
end
|
|||
|
|
return name
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
|
|||
|
|
--随机token
|
|||
|
|
function Common:RandToken()
|
|||
|
|
local rand = 0
|
|||
|
|
local token = ""
|
|||
|
|
for i = 1, 12 do
|
|||
|
|
rand = math.random(1,52)
|
|||
|
|
local temp = string.sub(self.word,rand,rand)
|
|||
|
|
token = token .. temp
|
|||
|
|
end
|
|||
|
|
--token = "1234567890"
|
|||
|
|
return token
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--随机兑换码
|
|||
|
|
function Common:RandRedeemCode()
|
|||
|
|
local rand = 0
|
|||
|
|
local name = ""
|
|||
|
|
for i = 1, 20 do
|
|||
|
|
rand = math.random(1,52)
|
|||
|
|
local temp = string.sub(self.word,rand,rand)
|
|||
|
|
name = name .. temp
|
|||
|
|
end
|
|||
|
|
return name
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取主服ID
|
|||
|
|
function Common:GetMainServerID(serverId)
|
|||
|
|
local mainServerId = nil
|
|||
|
|
mainServerId = string.sub(serverId,1,1)
|
|||
|
|
if 0 == #mainServerId then
|
|||
|
|
mainServerId = nil
|
|||
|
|
end
|
|||
|
|
return mainServerId
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取主服ID
|
|||
|
|
function Common:GetClusterConfig(serverId)
|
|||
|
|
serverId = tonumber(serverId)
|
|||
|
|
for k, v in pairs(skynet.server.gameConfig.ClusterServerConfig) do
|
|||
|
|
if serverId == v.serverId then
|
|||
|
|
return v
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
return nil
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
function Common:Utf8FormString(t)
|
|||
|
|
local bytearr = {}
|
|||
|
|
for _, v in ipairs(t) do
|
|||
|
|
local utf8byte = v < 0 and (0xff + v + 1) or v
|
|||
|
|
table.insert(bytearr, string.char(utf8byte))
|
|||
|
|
end
|
|||
|
|
return table.concat(bytearr)
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--解析参数
|
|||
|
|
function Common:ParseUrlParam( url )
|
|||
|
|
local newUrl = nil
|
|||
|
|
local param = nil
|
|||
|
|
local post = string.find(url,"?")
|
|||
|
|
newUrl = string.sub(url,1, post -1)
|
|||
|
|
param = string.sub(url,post + 1, #url)
|
|||
|
|
return newUrl,param
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--URL编码
|
|||
|
|
function Common:UrlEncode(s)
|
|||
|
|
s = string.gsub(s, "([^%w%.%- ])", function(c)
|
|||
|
|
return string.format("%%%02X", string.byte(c)) end)
|
|||
|
|
return string.gsub(s, " ", "+")
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--URL解码
|
|||
|
|
function Common:UrlDecode(s)
|
|||
|
|
s = string.gsub(s, '%%(%x%x)', function(h)
|
|||
|
|
return string.char(tonumber(h, 16)) end)
|
|||
|
|
return s
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取字符串时间 2022-02-22 22:22:22
|
|||
|
|
function Common:GetStrTime( time )
|
|||
|
|
return os.date("%Y-%m-%d %H:%M:%S",time)
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--将字符串时间 2022-02-22 22:22:22 转为时间戳
|
|||
|
|
function Common:GetTime( date )
|
|||
|
|
local _, _, y, m, d, _hour, _min, _sec = string.find(date, "(%d+)-(%d+)-(%d+)%s*(%d+):(%d+):(%d+)");
|
|||
|
|
return os.time({year=y, month = m, day = d, hour = _hour, min = _min, sec = _sec});
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取一年几月有多少天
|
|||
|
|
function Common:GetDayCount( year , month )
|
|||
|
|
local temp ={31,28,31,30,31,30,31,31,30,31,30,31}
|
|||
|
|
--判断是否是闰年,如果为闰年,改变2月是29,否则改变2月是28
|
|||
|
|
if (( year%4==0 and year%100 ~= 0 ) or year%400==0 ) then
|
|||
|
|
temp[2] = 29
|
|||
|
|
else
|
|||
|
|
temp[2] = 28
|
|||
|
|
end
|
|||
|
|
return temp[ month ];
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取之前的某一天的起始和结束时间戳
|
|||
|
|
function Common:GetPreSomeDay( count )
|
|||
|
|
local now = skynet.GetTime()
|
|||
|
|
local curr_time = {}
|
|||
|
|
curr_time.year = os.date("%Y", now)
|
|||
|
|
curr_time.month = os.date("%m", now)
|
|||
|
|
curr_time.day = os.date("%d", now)
|
|||
|
|
|
|||
|
|
local startTime = os.time({year=curr_time.year, month=curr_time.month, day=curr_time.day - count, hour=0,min=0,sec=0})
|
|||
|
|
local startTime = os.date("%Y-%m-%d %H:%M:%S",startTime)
|
|||
|
|
return startTime.year , startTime.month , startTime.day
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取之后的某一天的起始和结束时间戳
|
|||
|
|
function Common:GetAfterSomeDay( count )
|
|||
|
|
local now = skynet.GetTime()
|
|||
|
|
local curr_time = {}
|
|||
|
|
curr_time.year = os.date("%Y", now)
|
|||
|
|
curr_time.month = os.date("%m", now)
|
|||
|
|
curr_time.day = os.date("%d", now)
|
|||
|
|
|
|||
|
|
local startTime = os.time({year=curr_time.year, month=curr_time.month, day=curr_time.day + count, hour=0,min=0,sec=0})
|
|||
|
|
--local startTime = os.date("%Y-%m-%d %H:%M:%S",startTime)
|
|||
|
|
return startTime
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取之后的某个小时的时间戳
|
|||
|
|
function Common:GetAfterSomeHour( count )
|
|||
|
|
local now = skynet.GetTime()
|
|||
|
|
local curr_time = {}
|
|||
|
|
curr_time.year = os.date("%Y", now)
|
|||
|
|
curr_time.month = os.date("%m", now)
|
|||
|
|
curr_time.day = os.date("%d", now)
|
|||
|
|
curr_time.hour = os.date("%H", now)
|
|||
|
|
curr_time.min = os.date("%M", now)
|
|||
|
|
curr_time.sec = os.date("%S", now)
|
|||
|
|
--向下取整 获取应该增加的天数
|
|||
|
|
local day = math.floor(count/24)
|
|||
|
|
--获取应该增加的小时
|
|||
|
|
local hour = count - 24*day
|
|||
|
|
local time = os.time({year=curr_time.year, month=curr_time.month, day=curr_time.day + day, hour=curr_time.hour+hour ,min=curr_time.min ,sec=curr_time.sec})
|
|||
|
|
--local startTime = os.date("%Y-%m-%d %H:%M:%S",startTime)
|
|||
|
|
return time
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--首字母小写
|
|||
|
|
function Common:FirstAlphabetToLower(str)
|
|||
|
|
return (str:gsub("^%u",string.lower))
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--随机不重复的项目
|
|||
|
|
function Common:RandNoRepeatItem( needCount , cfg )
|
|||
|
|
if not cfg then
|
|||
|
|
return nil
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local maxCircCount = 0 --最大循环数据
|
|||
|
|
local data = {}
|
|||
|
|
data = {}
|
|||
|
|
local cfgCount = #cfg
|
|||
|
|
while true do
|
|||
|
|
local index = math.random(1,cfgCount)
|
|||
|
|
local id = cfg[ index ].id
|
|||
|
|
local isExist = false
|
|||
|
|
--检查商品列表是否有重复
|
|||
|
|
for k, v in pairs( data ) do
|
|||
|
|
if v == id then
|
|||
|
|
isExist = true
|
|||
|
|
break
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
if not isExist then
|
|||
|
|
--不存在就插入数据
|
|||
|
|
table.insert( data , id )
|
|||
|
|
--最大6个商品,超过就退出
|
|||
|
|
if #data >= needCount then
|
|||
|
|
break
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
maxCircCount = maxCircCount + 1
|
|||
|
|
if maxCircCount >= 5000 then
|
|||
|
|
--防止死循环,计算5000次就强制退出
|
|||
|
|
break
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
return data
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取两点的距离
|
|||
|
|
function Common:Distance(x1,y1,x2,y2)
|
|||
|
|
return math.sqrt((y2-y1) * (y2-y1)+(x2-x1) * (x2-x1))
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--转换到染色家具ID
|
|||
|
|
function Common:TransferColorFurniture( id )
|
|||
|
|
local curFurniture = skynet.server.gameConfig.Furniture[ id ]
|
|||
|
|
if not curFurniture then
|
|||
|
|
log.info("获取家具配置错误 ID ",id)
|
|||
|
|
else
|
|||
|
|
if 2 == #curFurniture.dyeFurTrans and 0 == math.floor( curFurniture.id /1000000 ) then
|
|||
|
|
return curFurniture.dyeFurTrans[2] * 100000 + curFurniture.dyeFurTrans[1];
|
|||
|
|
else
|
|||
|
|
return id
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取星期几
|
|||
|
|
function Common:GetDayInWeek()
|
|||
|
|
local t = os.date("*t", skynet.GetTime())
|
|||
|
|
if 1 == t.wday then
|
|||
|
|
return 7
|
|||
|
|
else
|
|||
|
|
t.wday = t.wday - 1
|
|||
|
|
return t.wday
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--是否为新的一周
|
|||
|
|
function Common:IsNewWeek( time1 )
|
|||
|
|
local t1 = os.date("*t",time1)
|
|||
|
|
local t2 = os.date("*t",skynet.GetTime())
|
|||
|
|
if t1.yday + 7== t2.yday then
|
|||
|
|
return true
|
|||
|
|
end
|
|||
|
|
return false
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--奖励类型 str 转为 int
|
|||
|
|
function Common:AwardTypeStrToInt( type)
|
|||
|
|
local goodsType = nil
|
|||
|
|
if "NumericalValue" == type then
|
|||
|
|
goodsType = 0
|
|||
|
|
elseif "Furniture" == type then
|
|||
|
|
goodsType = pb.enum("EnumGoodsType" , "Furniture")
|
|||
|
|
elseif "Decorate" == type or "Decoration" == type then
|
|||
|
|
goodsType = pb.enum("EnumGoodsType" , "Decorate")
|
|||
|
|
elseif "Flowerpot" == type then
|
|||
|
|
goodsType = pb.enum("EnumGoodsType" , "Flowerpot")
|
|||
|
|
elseif "Seed" == type then
|
|||
|
|
goodsType = pb.enum("EnumGoodsType" , "Seed")
|
|||
|
|
elseif "Plant" == type then
|
|||
|
|
goodsType = pb.enum("EnumGoodsType" , "Plant")
|
|||
|
|
elseif "Clothes" == type then
|
|||
|
|
goodsType = pb.enum("EnumGoodsType" , "Clothes")
|
|||
|
|
elseif "PetClothes" == type then
|
|||
|
|
goodsType = pb.enum("EnumGoodsType" , "PetClothes")
|
|||
|
|
elseif "Fertilizer" == type then
|
|||
|
|
goodsType = pb.enum("EnumGoodsType" , "Fertilizer")
|
|||
|
|
elseif "Prop" == type then
|
|||
|
|
goodsType = pb.enum("EnumGoodsType" , "Prop")
|
|||
|
|
end
|
|||
|
|
return goodsType
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
|
|||
|
|
--十六进制转字符串
|
|||
|
|
function Common:HexToStr(hex)
|
|||
|
|
--判断输入类型
|
|||
|
|
if (type(hex)~="string") then
|
|||
|
|
return nil,"hex2str invalid input type"
|
|||
|
|
end
|
|||
|
|
--拼接字符串
|
|||
|
|
local index=1
|
|||
|
|
local ret=""
|
|||
|
|
for index=1,hex:len() do
|
|||
|
|
ret=ret..string.format("%02X",hex:sub(index):byte())
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
return ret
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--将字符串按格式转为16进制串
|
|||
|
|
function Common:StrToHex(str)
|
|||
|
|
--判断输入类型
|
|||
|
|
if (type(str)~="string") then
|
|||
|
|
return nil,"str2hex invalid input type"
|
|||
|
|
end
|
|||
|
|
--滤掉分隔符
|
|||
|
|
str=str:gsub("[%s%p]",""):upper()
|
|||
|
|
--检查内容是否合法
|
|||
|
|
if(str:find("[^0-9A-Fa-f]")~=nil) then
|
|||
|
|
return nil,"str2hex invalid input content"
|
|||
|
|
end
|
|||
|
|
--检查字符串长度
|
|||
|
|
if(str:len()%2~=0) then
|
|||
|
|
return nil,"str2hex invalid input lenth"
|
|||
|
|
end
|
|||
|
|
--拼接字符串
|
|||
|
|
local index=1
|
|||
|
|
local ret=""
|
|||
|
|
for index=1,str:len(),2 do
|
|||
|
|
ret=ret..string.char(tonumber(str:sub(index,index+1),16))
|
|||
|
|
end
|
|||
|
|
return ret
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
skynet.server.common = Common
|
|||
|
|
return Common
|