package main import ( "encoding/json" "fmt" "io/ioutil" "os" "time" "github.com/robfig/cron/v3" "github.com/tidwall/pretty" ) func startCron() *cron.Cron { c := cron.New(cron.WithLocation(time.UTC)) c.AddFunc("58 6,14,22 * * *", cronSetDef) c.AddFunc("00 0 * * *", cronUpdateAngryBirbs) c.AddFunc("01 * * * *", cronScrubData) c.AddFunc("02 1,3,5,9,11,13,17,19,21 * * *", cronGetHammerTime) c.AddFunc("12 7,15,23 * * *", cronGetHammerTime) c.AddFunc("13 3,7,11,15,19,23 * * *", cronTribute) c.AddFunc("59 6,14,22 * * *", cronCheckVaultLimit) c.AddFunc("15 7,15,23 * * *", cronSendWarReport) c.AddFunc("53 6,14,22 * * *", cronAutoDeposit) c.AddFunc("@every 1m", cronSaveClients) c.Start() return c } func stopCron(c *cron.Cron) { c.Stop() return } func cronUpdateAngryBirbs() { muxClients.RLock() for _, c := range clients { if c.Active { p := JobPayloadMsgFwd{ ChatID64: cfg.Bot.AngryBirbs, } b, _ := json.Marshal(&p) err := createJobCallback(cacheObjSubType[`job_msg_fwd`], c.TGUserID64, cacheObjSubType[`msg_me_ack`], b, 1*time.Minute) logOnError(err, "cronUpdateAngryBirbs : createJobCallback") clientSendCWMsgDelay(c.TGUserID64, `/me`, 0) } } muxClients.RUnlock() return } func cronScrubData() { cleanupJobData() } func cronSendWarReport() { muxClients.RLock() for _, c := range clients { if c.Active { p := JobPayloadMsgFwd{ ChatID64: cfg.Bot.Reportchat, } b, _ := json.Marshal(&p) err := createJobCallback(cacheObjSubType[`job_msg_fwd`], c.TGUserID64, cacheObjSubType[`msg_report_ack`], b, 1*time.Minute) logOnError(err, "cronSendWarReport : createJobCallback") clientSendCWMsgDelay(c.TGUserID64, `/report`, 0) clientSendCWMsgDelay(c.TGUserID64, `/g_report`, 4) } } muxClients.RUnlock() return } func cronTribute() { muxClients.RLock() for _, c := range clients { if c.Active { if c.CWClass == `Knight` || c.CWClass2 == `Knight` { clientSendCWMsgDelay(c.TGUserID64, `/tributes`, 0) } } } muxClients.RUnlock() return } func cronSetDef() { muxClients.RLock() for _, c := range clients { if c.Active { p := JobPayloadSetDef{} b, _ := json.Marshal(&p) err := createJobCallback(cacheObjSubType[`job_set_def`], c.TGUserID64, cacheObjSubType[`msg_me_ack`], b, 1*time.Minute) logOnError(err, "cronSetDef : createJobCallback") clientSendCWMsgDelay(c.TGUserID64, `🏅Me`, 0) } } muxClients.RUnlock() return } func cronGetHammerTime() { clt, err := getLockedRandomClient() logOnError(err, "cronGetHammerTime : getLockedRandomClient") if err != nil { return } clt.Mux.Unlock() p := JobPayloadGetHammerTime{} b, _ := json.Marshal(&p) err = createJobCallback(cacheObjSubType[`job_get_hammer_time`], clt.TGUserID64, cacheObjSubType[`msg_time_ack`], b, 1*time.Minute) logOnError(err, "cronGetHammerTime : createJobCallback") clientSendCWMsgDelay(clt.TGUserID64, `/time`, 0) } func cronSaveClients() { var clts []ChirpClient muxClients.RLock() for _, c := range clients { if c.Active { clts = append(clts, *c) } } muxClients.RUnlock() for k, _ := range clts { clts[k].Active = false } b, err := json.Marshal(clts) logOnError(err, "cronSaveClients : Marshal") pb := pretty.Pretty(b) err = ioutil.WriteFile("./data/clients_new.json", pb, 0644) logOnError(err, "cronSaveClients : WriteFile") err = os.Remove("./data/clients.json") logOnError(err, "cronSaveClients : Remove") err = os.Rename("./data/clients_new.json", "./data/clients.json") logOnError(err, "cronSaveClients : Rename") } func cronCheckVaultLimit() { clt, err := getLockedRandomClient() logOnError(err, "cronCheckVaultLimit : getLockedRandomClient") if err != nil { return } clt.Mux.Unlock() p := JobPayloadCheckVaultLimit{ Status: 0, } b, _ := json.Marshal(p) t := time.Now().UTC().Add(1 * time.Second) _, err = createJob(cacheObjSubType[`job_check_vault_limit`], objJobPriority, clt.TGUserID64, 0, t, b) } func cronAutoDeposit() { muxClients.RLock() for _, c := range clients { if c.Active && c.Config.AutoDeposit { p := JobPayloadGDeposit{ MsgID64: 0, ChatID64: c.TGUserID64, ResObjID64: nil, Status: 0, } for _, v := range c.Config.AutoDepositItems { p.ResObjID64 = append(p.ResObjID64, getObjItemID(v, ``)) } for _, v := range c.Config.AutoDepositTypes { list := getSQLListID64(fmt.Sprintf("select o.id from obj o where o.obj_sub_type_id = %d;", cacheObjSubType[v])) p.ResObjID64 = append(p.ResObjID64, list...) } b, _ := json.Marshal(p) t := time.Now().UTC() t.Add(time.Duration(RndIntn(300)) * time.Second) _, err := createJob(cacheObjSubType[`job_gdeposit`], objJobPriority, c.TGUserID64, 0, t, b) logOnError(err, "cronAutoDeposit : createJob") } } muxClients.RUnlock() }