diff --git a/client.go b/client.go index dbb4c70..df66355 100644 --- a/client.go +++ b/client.go @@ -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 { diff --git a/cron.go b/cron.go index b0c7af9..8d8ebe0 100644 --- a/cron.go +++ b/cron.go @@ -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) }