You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
857 B

  1. package services
  2. import (
  3. lucifer3 "git.aiterp.net/lucifer3/server"
  4. "git.aiterp.net/lucifer3/server/events"
  5. "time"
  6. )
  7. func NewTicker() lucifer3.Service {
  8. return &tickerService{}
  9. }
  10. type tickerService struct{}
  11. func (s *tickerService) Active() bool {
  12. return true
  13. }
  14. func (s *tickerService) HandleEvent(bus *lucifer3.EventBus, event lucifer3.Event) {
  15. if _, ok := event.(events.Started); ok {
  16. go func() {
  17. now := time.Now()
  18. bus.RunEvent(events.Time{
  19. Hour: now.Hour(),
  20. Minute: now.Minute(),
  21. })
  22. time.Sleep(time.Now().Truncate(time.Minute).Add(time.Minute).Sub(time.Now()))
  23. now = time.Now()
  24. bus.RunEvent(events.Time{
  25. Hour: now.Hour(),
  26. Minute: now.Minute(),
  27. })
  28. for t := range time.NewTicker(time.Minute).C {
  29. bus.RunEvent(events.Time{
  30. Hour: t.Hour(),
  31. Minute: t.Minute(),
  32. })
  33. }
  34. }()
  35. }
  36. }