chirpnest/cron.go

190 lines
4.7 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-06-16 17:29:07 +02:00
"fmt"
2021-04-06 13:57:31 +02:00
"io/ioutil"
2020-01-16 10:15:09 +01:00
"os"
2019-12-12 10:45:27 +01:00
"time"
2021-04-06 13:57:31 +02:00
"github.com/robfig/cron/v3"
2020-01-16 10:14:37 +01:00
"github.com/tidwall/pretty"
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))
2020-01-26 08:08:00 +01:00
c.AddFunc("58 6,14,22 * * *", cronSetDef)
2020-03-01 07:13:33 +01:00
c.AddFunc("00 0 * * *", cronUpdateAngryBirbs)
2020-10-20 11:48:07 +02:00
c.AddFunc("01 * * * *", cronScrubData)
2019-12-18 03:46:58 +01:00
c.AddFunc("02 1,3,5,9,11,13,17,19,21 * * *", cronGetHammerTime)
2020-02-03 03:54:34 +01:00
c.AddFunc("12 7,15,23 * * *", cronGetHammerTime)
2020-01-09 09:14:37 +01:00
c.AddFunc("13 3,7,11,15,19,23 * * *", cronTribute)
2020-05-31 10:51:00 +02:00
c.AddFunc("59 6,14,22 * * *", cronCheckVaultLimit)
2020-02-03 03:54:34 +01:00
c.AddFunc("15 7,15,23 * * *", cronSendWarReport)
2020-06-17 12:06:05 +02:00
c.AddFunc("53 6,14,22 * * *", cronAutoDeposit)
2020-01-16 08:46:42 +01:00
c.AddFunc("@every 1m", cronSaveClients)
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
}
2020-03-01 07:13:33 +01:00
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
}
2020-10-11 12:53:15 +02:00
func cronScrubData() {
cleanupJobData()
}
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)
2020-04-07 04:43:14 +02:00
clientSendCWMsgDelay(c.TGUserID64, `/g_report`, 4)
2019-12-13 09:34:32 +01:00
}
}
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 {
2020-10-20 11:07:52 +02:00
if c.CWClass == `Knight` || c.CWClass2 == `Knight` {
2020-01-09 09:14:37 +01:00
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
}
2020-01-16 07:24:52 +01:00
func cronSaveClients() {
2020-01-16 08:37:15 +01:00
var clts []ChirpClient
2020-01-16 07:24:52 +01:00
2020-01-16 08:37:15 +01:00
muxClients.RLock()
for _, c := range clients {
if c.Active {
2020-01-16 08:38:10 +01:00
clts = append(clts, *c)
2020-01-16 08:37:15 +01:00
}
}
2020-01-16 08:38:25 +01:00
muxClients.RUnlock()
2020-01-16 08:42:20 +01:00
2020-01-16 14:28:34 +01:00
for k, _ := range clts {
clts[k].Active = false
}
2020-01-16 08:43:48 +01:00
b, err := json.Marshal(clts)
2020-01-16 08:44:39 +01:00
logOnError(err, "cronSaveClients : Marshal")
2020-01-16 10:14:37 +01:00
pb := pretty.Pretty(b)
2021-10-26 16:24:14 +02:00
err = ioutil.WriteFile("./data/clients_new.json", pb, 0644)
2020-01-16 10:14:37 +01:00
logOnError(err, "cronSaveClients : WriteFile")
2021-10-26 16:24:14 +02:00
err = os.Remove("./data/clients.json")
2020-01-16 10:14:37 +01:00
logOnError(err, "cronSaveClients : Remove")
2021-10-26 16:24:14 +02:00
err = os.Rename("./data/clients_new.json", "./data/clients.json")
2020-01-16 10:14:37 +01:00
logOnError(err, "cronSaveClients : Rename")
2020-01-16 08:43:48 +01:00
2020-01-16 07:24:52 +01:00
}
2020-02-03 03:54:34 +01:00
func cronCheckVaultLimit() {
clt, err := getLockedRandomClient()
2020-05-19 18:56:03 +02:00
logOnError(err, "cronCheckVaultLimit : getLockedRandomClient")
2020-02-03 03:54:34 +01:00
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)
}
2020-06-16 17:28:28 +02:00
func cronAutoDeposit() {
muxClients.RLock()
for _, c := range clients {
if c.Active && c.Config.AutoDeposit {
p := JobPayloadGDeposit{
MsgID64: 0,
2020-06-17 11:51:07 +02:00
ChatID64: c.TGUserID64,
2020-06-16 17:28:28 +02:00
ResObjID64: nil,
Status: 0,
}
for _, v := range c.Config.AutoDepositItems {
2020-06-17 11:46:17 +02:00
p.ResObjID64 = append(p.ResObjID64, getObjItemID(v, ``))
2020-06-16 17:28:28 +02:00
}
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()
2020-06-19 16:44:44 +02:00
t.Add(time.Duration(RndIntn(300)) * time.Second)
2020-06-17 11:46:17 +02:00
_, err := createJob(cacheObjSubType[`job_gdeposit`], objJobPriority, c.TGUserID64, 0, t, b)
2020-06-16 17:29:47 +02:00
logOnError(err, "cronAutoDeposit : createJob")
2020-06-16 17:28:28 +02:00
}
}
muxClients.RUnlock()
}