chirpnest/cron.go

86 lines
2.0 KiB
Go
Raw Normal View History

2019-12-12 10:42:05 +01:00
package main
import (
2019-12-12 11:25:26 +01:00
"encoding/json"
2020-01-15 11:22:41 +01:00
//"log"
2019-12-12 10:45:27 +01:00
"time"
2019-12-12 10:44:18 +01:00
//"github.com/robfig/cron/v3"
"gopkg.in/robfig/cron.v3"
2019-12-12 10:42:05 +01:00
)
2019-12-12 10:45:27 +01:00
func startCron() *cron.Cron {
2019-12-12 10:42:05 +01:00
c := cron.New(cron.WithLocation(time.UTC))
2019-12-13 09:34:32 +01:00
c.AddFunc("15 7,15,23 * * *", cronSendWarReport)
c.AddFunc("58 6,14,22 * * *", cronSetDef)
2019-12-18 03:46:58 +01:00
c.AddFunc("02 1,3,5,9,11,13,17,19,21 * * *", cronGetHammerTime)
c.AddFunc("10 7,15,23 * * *", cronGetHammerTime)
2020-01-09 09:14:37 +01:00
c.AddFunc("13 3,7,11,15,19,23 * * *", cronTribute)
2019-12-12 10:42:05 +01:00
c.Start()
2019-12-12 10:49:18 +01:00
return c
2019-12-12 10:42:05 +01:00
}
2019-12-12 10:45:27 +01:00
func stopCron(c *cron.Cron) {
2019-12-12 10:42:05 +01:00
c.Stop()
return
}
2019-12-12 11:24:12 +01:00
func cronSendWarReport() {
2019-12-12 10:42:05 +01:00
muxClients.RLock()
2019-12-12 10:51:01 +01:00
for _, c := range clients {
2019-12-12 10:42:05 +01:00
if c.Active {
2020-01-08 10:21:00 +01:00
p := JobPayloadMsgFwd{
2019-12-12 11:24:12 +01:00
ChatID64: cfg.Bot.Reportchat,
}
2019-12-12 11:25:06 +01:00
b, _ := json.Marshal(&p)
2020-01-02 13:04:12 +01:00
err := createJobCallback(cacheObjSubType[`job_msg_fwd`], c.TGUserID64, cacheObjSubType[`msg_report_ack`], b, 1*time.Minute)
2019-12-12 11:24:12 +01:00
logOnError(err, "cronSendWarReport : createJobCallback")
2019-12-13 09:34:32 +01:00
clientSendCWMsgDelay(c.TGUserID64, `/report`, 0)
}
}
muxClients.RUnlock()
return
}
2020-01-06 07:55:40 +01:00
func cronTribute() {
2020-01-06 07:55:13 +01:00
muxClients.RLock()
for _, c := range clients {
2020-01-09 09:14:37 +01:00
if c.Active {
if c.CWClass == `Knight` {
clientSendCWMsgDelay(c.TGUserID64, `/tributes`, 0)
}
2020-01-06 07:55:13 +01:00
}
}
muxClients.RUnlock()
return
}
2019-12-13 09:34:32 +01:00
func cronSetDef() {
muxClients.RLock()
for _, c := range clients {
if c.Active {
p := JobPayloadSetDef{}
b, _ := json.Marshal(&p)
2020-01-02 13:04:12 +01:00
err := createJobCallback(cacheObjSubType[`job_set_def`], c.TGUserID64, cacheObjSubType[`msg_me_ack`], b, 1*time.Minute)
2019-12-13 09:34:32 +01:00
logOnError(err, "cronSetDef : createJobCallback")
clientSendCWMsgDelay(c.TGUserID64, `🏅Me`, 0)
2019-12-12 10:42:05 +01:00
}
}
muxClients.RUnlock()
return
}
2019-12-16 11:23:46 +01:00
func cronGetHammerTime() {
2019-12-18 03:45:10 +01:00
clt, err := getLockedRandomClient()
2019-12-19 04:31:28 +01:00
logOnError(err, "cronGetHammerTime : getLockedRandomClient")
2019-12-18 03:46:04 +01:00
if err != nil {
2019-12-18 03:45:10 +01:00
return
}
2019-12-18 03:46:40 +01:00
clt.Mux.Unlock()
2019-12-19 04:34:11 +01:00
p := JobPayloadGetHammerTime{}
2019-12-19 04:31:28 +01:00
b, _ := json.Marshal(&p)
2020-01-02 13:04:12 +01:00
err = createJobCallback(cacheObjSubType[`job_get_hammer_time`], clt.TGUserID64, cacheObjSubType[`msg_time_ack`], b, 1*time.Minute)
2019-12-19 04:31:28 +01:00
logOnError(err, "cronGetHammerTime : createJobCallback")
2019-12-18 03:48:12 +01:00
clientSendCWMsgDelay(clt.TGUserID64, `/time`, 0)
2019-12-16 11:23:46 +01:00
}