You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
223 lines
5.8 KiB
223 lines
5.8 KiB
package serviceGame |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
"github.com/gogf/gf/v2/os/gtime" |
|
"time" |
|
"tyj_admin/api/v1/game" |
|
"tyj_admin/internal/consts" |
|
"tyj_admin/internal/dao" |
|
"tyj_admin/internal/model/do" |
|
"tyj_admin/internal/model/entity" |
|
"tyj_admin/internal/serviceGame/internal" |
|
) |
|
|
|
var ( |
|
notices []entity.GameNoticeLog |
|
mails []entity.Mail |
|
|
|
advertiseTime time.Time |
|
) |
|
|
|
func init() { |
|
//加载游戏相关配置 |
|
ticker := time.NewTicker(1 * time.Second) |
|
notices = []entity.GameNoticeLog{} |
|
mails = []entity.Mail{} |
|
//count := 1 |
|
ctx := context.TODO() |
|
model := dao.GameNoticeLog.Ctx(ctx) |
|
model = model.Where("send_time > ", gtime.TimestampMilli()) |
|
_ = model.Scan(¬ices) |
|
|
|
mails, _ = internal.GetCornMails(ctx, consts.Mail_Send_Time) |
|
|
|
go func() { |
|
for { |
|
//从定时器中获取数据 |
|
t := <-ticker.C |
|
gameCronService.Cron(t.UnixMilli()) |
|
gameCronService.MailCron(t.UnixMilli()) |
|
gameCronService.CronAdvertisement(ctx, t.Unix()) |
|
} |
|
}() |
|
|
|
internal.LogInit() |
|
} |
|
|
|
type IGameCron interface { |
|
AddCron(entity.GameNoticeLog) |
|
AddMailCorn(entity.Mail) |
|
DelCron(int64) |
|
Cron(int64) |
|
DelMailCron(string) |
|
MailCron(int64) |
|
GetCron(*game.GetCronReq) (*game.GetCronRes, error) |
|
GetMailCron(*game.GetMailCronReq) (*game.GetMailCronRes, error) |
|
} |
|
|
|
type gameCronImpl struct { |
|
} |
|
|
|
var gameCronService = gameCronImpl{} |
|
|
|
func GameCron() IGameCron { |
|
return &gameCronService |
|
} |
|
func (c *gameCronImpl) GetCron(req *game.GetCronReq) (res *game.GetCronRes, err error) { |
|
res = new(game.GetCronRes) |
|
res.NoticeLogs = notices |
|
return |
|
} |
|
func (c *gameCronImpl) GetMailCron(req *game.GetMailCronReq) (res *game.GetMailCronRes, err error) { |
|
res = new(game.GetMailCronRes) |
|
res.MailLogs = mails |
|
return |
|
} |
|
|
|
func (c *gameCronImpl) AddCron(req entity.GameNoticeLog) { |
|
for i := len(notices) - 1; i >= 0; i-- { |
|
notice := notices[i] |
|
if notice.Id == req.Id { |
|
notice.SendTime = req.SendTime |
|
notice.Status = req.Status |
|
notice.Content = req.Content |
|
notice.NoticeType = req.NoticeType |
|
notice.NoticeId = req.NoticeId |
|
notice.Channel = req.Channel |
|
//fmt.Println("notice.NoticeId: ", notice.NoticeId) |
|
return |
|
} |
|
} |
|
notices = append(notices, req) |
|
return |
|
} |
|
|
|
func (c *gameCronImpl) DelCron(id int64) { |
|
for i := len(notices) - 1; i >= 0; i-- { |
|
notice := notices[i] |
|
if notice.Id == id { |
|
notices = append(notices[:i], notices[i+1:]...) |
|
return |
|
} |
|
} |
|
return |
|
} |
|
|
|
func (c *gameCronImpl) Cron(sTime int64) { |
|
for i := len(notices) - 1; i >= 0; i-- { |
|
notice := notices[i] |
|
if notice.SendTime <= sTime { |
|
notices = append(notices[:i], notices[i+1:]...) |
|
ctx := context.TODO() |
|
noticeModel := dao.GameNotice.Ctx(ctx) |
|
noticeList := []entity.GameNotice{} |
|
noticeModel.Where("notice_type=?", notice.NoticeType).Where("channel=?", notice.Channel).Scan(¬iceList) |
|
if len(noticeList) > 0 { |
|
_, _ = dao.GameNotice.Ctx(ctx).Where("id=", noticeList[0].Id).Data(&do.GameNotice{ |
|
Content: notice.Content, |
|
CDate: time.Now().Unix(), |
|
}).Update() |
|
} else { |
|
_, _ = dao.GameNotice.Ctx(ctx).Insert(&do.GameNotice{ |
|
Status: notice.Status, |
|
Content: notice.Content, |
|
NoticeType: notice.NoticeType, |
|
Channel: notice.Channel, |
|
CDate: time.Now().Unix(), |
|
}) |
|
} |
|
} |
|
//fmt.Println("notice.NoticeId: ", notice.Id) |
|
} |
|
return |
|
} |
|
|
|
func (c *gameCronImpl) AddMailCorn(req entity.Mail) { |
|
for i := len(mails) - 1; i >= 0; i-- { |
|
notice := mails[i] |
|
if notice.Id == req.Id { |
|
notice.ServerId = req.ServerId |
|
notice.Send = req.Send |
|
notice.Time = req.Time |
|
//notice.To = req.To |
|
notice.From = req.From // "陶渊明" |
|
notice.Type = req.Type |
|
notice.Expired = req.Expired |
|
notice.ValidDay = req.ValidDay |
|
notice.NewGet = req.NewGet |
|
notice.Title = req.Title |
|
//notice.Greetings = req.Greetings |
|
notice.Content = req.Content |
|
notice.Drops = req.Drops |
|
notice.OwnerId = req.OwnerId |
|
fmt.Println("AddMailCor same: ", notice.Id) |
|
return |
|
} |
|
} |
|
fmt.Println("AddMailCorn null: ", req.Id) |
|
mails = append(mails, req) |
|
return |
|
} |
|
|
|
func (c *gameCronImpl) DelMailCron(id string) { |
|
for i := len(mails) - 1; i >= 0; i-- { |
|
notice := mails[i] |
|
if notice.Id == id { |
|
mails = append(mails[:i], mails[i+1:]...) |
|
return |
|
} |
|
} |
|
return |
|
} |
|
|
|
func (c *gameCronImpl) MailCron(sTime int64) { |
|
for i := len(mails) - 1; i >= 0; i-- { |
|
notice := mails[i] |
|
if notice.Time <= sTime { |
|
mails = append(mails[:i], mails[i+1:]...) |
|
req := new(game.MailSendReq) |
|
req.ServerId = notice.ServerId |
|
req.Send = notice.Send |
|
req.Type = notice.Type |
|
req.NewGet = notice.NewGet |
|
req.ValidDay = notice.ValidDay |
|
req.Time = notice.Time |
|
req.Drops = notice.Drops |
|
//req.Greetings = notice.Greetings |
|
req.Content = notice.Content |
|
req.From = notice.From |
|
req.Title = notice.Title |
|
req.Expired = notice.Expired |
|
req.OwnerId = notice.OwnerId |
|
//req.To = notice.To |
|
ctx := context.TODO() |
|
_, err := internal.SendMailToC(ctx, req) |
|
req1 := entity.Mail{Id: notice.Id, Send: consts.Mail_Send_Time_Sended} |
|
_ = internal.UpdateMail(ctx, req1, err.Error()) |
|
} |
|
//fmt.Println("notice.NoticeId: ", notice.Id, notice.Time) |
|
} |
|
return |
|
} |
|
|
|
func (c *gameCronImpl) CronAdvertisement(ctx context.Context, sTime int64) { |
|
rdbTop := internal.RedisDbList[0] |
|
rdbTop.Do(ctx, "select", 0) |
|
redisRes := rdbTop.HGet(ctx, consts.ADVERTISE_DATA_SAVE, consts.ADVERTISE_TOKEN_REFRESH_TIME) |
|
if redisRes.Val() == "" { |
|
return |
|
} |
|
advertiseTime, _ = time.ParseInLocation(consts.TIME_FORMAT, redisRes.Val(), time.Local) |
|
now := advertiseTime.Unix() + 23*60*60 |
|
redisSign := rdbTop.HGet(ctx, consts.ADVERTISE_DATA_SAVE, consts.ADVERTISE_TOKEN_REFRESH_SIGN) |
|
if redisSign.Val() == "1" { |
|
return |
|
} |
|
if now <= sTime { |
|
rdbTop.HSet(ctx, consts.ADVERTISE_DATA_SAVE, consts.ADVERTISE_TOKEN_REFRESH_SIGN, 1) |
|
internal.RefreshAccessToken(ctx) |
|
rdbTop.HSet(ctx, consts.ADVERTISE_DATA_SAVE, consts.ADVERTISE_TOKEN_REFRESH_SIGN, 0) |
|
} |
|
}
|
|
|