HomeServer/Server/AllServer/GameServer/Birthday.lua

173 lines
6.2 KiB
Lua
Raw Normal View History

2024-11-20 15:41:09 +08:00
local skynet = require "skynet"
local oo = require "Class"
local log = require "Log"
local pb = require "pb"
local dataType = require "DataType"
local errorInfo = require "ErrorInfo"
local json = require "json"
local Birthday = oo.class()
--触发对话类型
Birthday.TriggerDialogType_0 = 0 --0 未触发或者已经触发
Birthday.TriggerDialogType_1 = 1 --1 生日当天触发
Birthday.TriggerDialogType_2 = 2 --2 生日未来15天内触发
function Birthday:Init()
end
--生日日期显示
function Birthday:Show( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SBirthdayShow", c2sData.data ))
local data = {}
local birthday=player.gameData.personal.birthday
if birthday.lastExecutionTime ~= "" then --第二年修改日期方法
local yearStr = string.match(birthday.lastExecutionTime, "%d+") -- 从字符串中提取出数字部分
local yearNumber = tonumber(yearStr)
local nowTime = skynet.GetTime()
local year1 = tonumber(os.date("%Y", nowTime))
if year1> yearNumber then
--新的一年,重置相关数据
birthday.lastSetNum=1
birthday.lastExecutionTime = ""
end
end
data.lastSetNum=birthday.lastSetNum
data.birthdayDate = birthday.birthdayDate
data.show = birthday.show
s2cData.cmd = pb.enum("MsgType","CMD_S2C_BirthdayShow")
s2cData.data = assert(pb.encode("S2CBirthdayShow", data))
end
--生日日期保存
function Birthday:BirthdayDateSet( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SPersonalSetBirthdayData", c2sData.data ))
local birthday=player.gameData.personal.birthday
local data = {}
local partnerId = player.gameData.partner.id
local birthdayDate = c2sData.data.birthdayDate
local isShow = c2sData.data.show
if birthday.birthdayDate == "" then
birthday.birthdayDate = birthdayDate
else
if birthday.lastSetNum == 1 and birthday.birthdayDate ~= birthdayDate then
--有剩余修改次数 并且日期不同 ,就重置修改次数 ,并且更新日期
local nowTime =skynet.GetTime()
local execDate = os.date("%Y/%m/%d", nowTime)
birthday.lastExecutionTime = execDate
birthday.birthdayDate = birthdayDate
birthday.lastSetNum=0
end
end
--是否展示自己生日
if isShow then
birthday.show= true
else
birthday.show= false
end
data.birthdayDate = birthday.birthdayDate
data.show = birthday.show
data.lastSetNum = birthday.lastSetNum
local isDialogShow,isTriggerBirthDay= self:DialogShow(player)
data.isDialogShow = isDialogShow
skynet.server.personal:SetDetail( partnerId , "birthdayDate", birthdayDate , "birthdayShow" , isShow )
if isTriggerBirthDay then
skynet.server.friend:CheckNewMsg( player )
end
s2cData.cmd = pb.enum("MsgType","CMD_S2C_PersonalSetBirthdayData")
s2cData.data = assert(pb.encode("S2CPersonalSetBirthdayData", data))
end
--生日前置对话隐藏标记
function Birthday:DialogShow(player)
local isTriggerBirthDay = false
local now = skynet.GetTime()
local birthday = player.gameData.personal.birthday
local pattern = "(%d%d%d%d)/(%d%d)/(%d%d)"
if birthday.birthdayDate == "" then
birthday.isDialogShow=self.TriggerDialogType_0
return birthday.isDialogShow
end
local year, month, day = birthday.birthdayDate:match(pattern)
local year1, month1, day1 = tonumber(os.date("%Y", now)), tonumber(os.date("%m", now)), tonumber(os.date("%d", now)) --当前时间
local year2, month2, day2 = tonumber(year), tonumber(month), tonumber(day) --我的生日时间
if birthday.lastUpdateTime~="" then --第二年领取奖励时间重置
local formattedDate = os.date("%Y/%m/%d", birthday.lastUpdateTime)
local yearStr = string.match(formattedDate, "%d+") -- 从字符串中提取出数字部分
local yearNumber = tonumber(yearStr)
if year1> yearNumber and now - birthday.lastUpdateTime > 15*(3600*24) then
birthday.lastUpdateTime=""
end
end
if birthday.lastUpdateTime~="" then
birthday.isDialogShow=self.TriggerDialogType_0
else
if month1 == month2 then
local diffDay = day1 - day2
--触发奖励状态0未触发或者已经触发 1生日当天触发 2:生日未来15天内触发
if diffDay == 0 then
birthday.isDialogShow=self.TriggerDialogType_1
birthday.lastUpdateTime=now
isTriggerBirthDay = true
elseif diffDay <=15 and diffDay > 0 then
birthday.isDialogShow=self.TriggerDialogType_2
birthday.lastUpdateTime=now
isTriggerBirthDay = true
else
birthday.isDialogShow=self.TriggerDialogType_0
end
else
local num1 = os.time({ year = year1, month=month1, day=day1 })
local num2
if month2 == 12 and month1 == 1 then
num2 = os.time({ year = year1-1, month=month2, day=day2 })
else
num2 = os.time({ year = year1, month=month2, day=day2 })
end
if ( num1 - num2 ) / (3600*24) >= 0 and ( num1 - num2 ) / (3600*24) <= 15 then
birthday.isDialogShow=self.TriggerDialogType_2
birthday.lastUpdateTime=now
isTriggerBirthDay = true
else
birthday.isDialogShow=self.TriggerDialogType_0
end
end
end
return birthday.isDialogShow,isTriggerBirthDay
end
--是否解锁NPC对话
function Birthday:IsUnlockNpcDialog( player )
local birthday = player.gameData.personal.birthday
if self.TriggerDialogType_1 == birthday.isDialogShow or self.TriggerDialogType_2 == birthday.isDialogShow then
return true
end
return false
end
--检测解决NPC对话
function Birthday:CheckUnlockNpcDialog( player )
local isDialogShow,isTriggerBirthDay=skynet.server.birthday:DialogShow(player)
if isTriggerBirthDay then
skynet.server.friend:CheckNewMsg( player )
end
return isDialogShow
end
skynet.server.birthday = Birthday
return Birthday