package timer import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "time" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/cron" "google.golang.org/protobuf/types/known/anypb" ) var ( game_chatsystem = "game_chatsystem.json" ) /* 聊天系统 推送系统公告 */ type ChatComp struct { modules.MCompConfigure service core.IService module *Timer takes []cron.EntryID } //组件初始化接口 func (this *ChatComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.MCompConfigure.Init(service, module, comp, options) this.service = service this.module = module.(*Timer) this.takes = make([]cron.EntryID, 0) return } func (this *ChatComp) Start() (err error) { err = this.MCompConfigure.Start() configure.RegisterConfigure(game_chatsystem, cfg.NewGameChatSystem, func() { for _, v := range this.takes { //移除前面的定时任务 cron.Remove(v) } if v, err := this.GetConfigure(game_chatsystem); err != nil { this.module.Errorf("err:%v", err) return } else { var id cron.EntryID this.takes = make([]cron.EntryID, 0) for _, v := range v.(*cfg.GameChatSystem).GetDataList() { if v.Type == int32(comm.TimedNotice) { //处理定时任务 weekStr := "" for _, w := range v.Week { weekStr += fmt.Sprintf("%d,", w) } if len(weekStr) > 0 && len(weekStr) < 7 { weekStr = weekStr[0 : len(weekStr)-1] } else { weekStr = "*" } cronStr := fmt.Sprintf("* %d %d * * %s", v.TimeM, v.TimeH, weekStr) if id, err = cron.AddFunc(cronStr, this.chatNoticen(v)); err != nil { this.module.Errorf("cron.AddFunc:%s err:%v", cronStr, err) continue } this.takes = append(this.takes, id) } } //测试代码 // if id, err = cron.AddFunc("0 */1 * * * ?", this.chatNoticen(&cfg.GameChatSystemData{Text: "测试公告系统"})); err != nil { // this.module.Errorf("cron.AddFunc:%s err:%v", "0 */1 * * * ?", err) // } } }) return } func (this *ChatComp) chatNoticen(n *cfg.GameChatSystemData) func() { return func() { msg := &pb.DBChat{ Channel: pb.ChatChannel_System, Stag: this.service.GetTag(), Ctime: time.Now().Unix(), Content: n.Text, } data, _ := anypb.New(&pb.ChatMessagePush{Chat: msg}) if err := this.module.service.AcrossClusterBroadcast(context.Background(), msg.Stag, comm.Service_Gateway, string(comm.Rpc_GatewaySendRadioMsg), pb.UserMessage{ MainType: string(comm.ModuleChat), SubType: "message", Data: data, }, nil); err != nil { this.module.Errorf("err:%v", err) } } }