start hammer time

This commit is contained in:
shoopea 2019-12-18 10:45:10 +08:00
parent aa4755fc22
commit 40d0e41f2a
2 changed files with 30 additions and 1 deletions

View File

@ -8,6 +8,28 @@ import (
"time"
)
func getLockedRandomClient() (*ChirpClient, error) {
muxClients.RLock()
ids := make([]int64, 0)
for _, c := range clients {
if c.Active {
ids = append(ids, c.TGUserID64)
}
}
muxClients.RUnlock()
if len(ids) == 0 {
return nil, errors.New("No active client.")
}
RndMux.Lock()
id := RndSrc.Intn(len(ids))
RndMux.Unlock()
clients[ids[id]].Mux.Lock()
return clients[ids[id]], nil
}
func setClientBusy(userID64 int64, from time.Time, duration time.Duration) error {
if from.UTC().Add(duration).After(time.Now().UTC()) {
if clt, ok := getLockedClient(userID64, false); ok {

View File

@ -12,6 +12,8 @@ func startCron() *cron.Cron {
c := cron.New(cron.WithLocation(time.UTC))
c.AddFunc("15 7,15,23 * * *", cronSendWarReport)
c.AddFunc("58 6,14,22 * * *", cronSetDef)
c.AddFunc("02 1,3,5,9,11,13,17,19,21 * * *", cronHammerTime)
c.AddFunc("10 7,15,23 * * *", cronHammerTime)
c.Start()
return c
}
@ -54,5 +56,10 @@ func cronSetDef() {
}
func cronGetHammerTime() {
clt, err := getLockedRandomClient()
logOnError(err, "cronGetHammerTime")
if err != {
return
}
clientSendCWMsg(clt.TGUserID64, `/time`, 0)
}