game_timer.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. 只适用于一天24小时开奖的情况,0->1
  3. **/
  4. package gametimer
  5. import (
  6. "errors"
  7. "fmt"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/gookit/event"
  12. "github.com/sirupsen/logrus"
  13. "gogs.daxia.dev/huanan/pkg.daxia.dev.git/intutils"
  14. "gogs.daxia.dev/huanan/pkg.daxia.dev.git/time_utils"
  15. )
  16. const (
  17. EventTimerStart = "EventTimerStart"
  18. EventTimerStop = "EventTimerStop"
  19. )
  20. type GameTimer struct {
  21. baseTime time.Time
  22. baseName string
  23. totalSecond int
  24. stopSecond int
  25. currentEvent string
  26. }
  27. func NewGameTimer(baseName string, baseTime time.Time, totalSecond, stopSecond int) *GameTimer {
  28. if stopSecond > totalSecond {
  29. panic("stop second > total second")
  30. }
  31. if (1440*60)%totalSecond != 0 {
  32. panic("total second不能被整除")
  33. }
  34. instance := &GameTimer{
  35. baseTime: baseTime,
  36. baseName: baseName,
  37. totalSecond: totalSecond,
  38. stopSecond: stopSecond,
  39. }
  40. instance.currentEvent = ""
  41. go instance.start()
  42. return instance
  43. }
  44. func (p *GameTimer) start() {
  45. for {
  46. <-time.After(1 * time.Second)
  47. err := p.doWork()
  48. if err != nil {
  49. logrus.Error(err)
  50. }
  51. }
  52. }
  53. func (p *GameTimer) doWork() error {
  54. currentEvent := p.GetCurrentEvent()
  55. if p.currentEvent == currentEvent {
  56. return nil
  57. }
  58. p.currentEvent = currentEvent
  59. event.MustFire(p.currentEvent, event.M{})
  60. return nil
  61. }
  62. func (p *GameTimer) GetCurrentEvent() string {
  63. secondFromBase := time_utils.TimeNowInCN().Unix() - p.baseTime.Unix()
  64. //180 [0 - 180]
  65. secondInRange := secondFromBase % int64(p.totalSecond)
  66. if secondInRange < int64(p.stopSecond) {
  67. return p.GetEvent(EventTimerStart)
  68. }
  69. return p.GetEvent(EventTimerStop)
  70. }
  71. func (p *GameTimer) GetIssueByTime(timeIssue time.Time) int64 {
  72. secondFromBase := timeIssue.Unix() - p.baseTime.Unix()
  73. issue := secondFromBase / int64(p.totalSecond)
  74. return (issue + 1)
  75. }
  76. func (p *GameTimer) GetIssueStrByTime(timeIssue time.Time) string {
  77. return p.GetIssueStr(p.GetIssueByTime(timeIssue))
  78. }
  79. func (p *GameTimer) GetIssueStr(issue int64) string {
  80. timestamp := p.baseTime.Unix() + int64(issue-1)*int64(p.totalSecond)
  81. timeIssue := time.Unix(timestamp, 0)
  82. timeIssueCN := timeIssue.In(time_utils.GetCNZone())
  83. totalIssueOnDay := (1440 * 60) / p.totalSecond
  84. maxSize := intutils.IntSize(totalIssueOnDay)
  85. fmtStr := fmt.Sprintf("0%dd", maxSize)
  86. todayIssue := p.GetIssueFromTodayZero(timeIssueCN)
  87. return timeIssueCN.Format("2006-01-02") + " " + fmt.Sprintf("%"+fmtStr, todayIssue)
  88. }
  89. func (p *GameTimer) GetIssueFromStr(issueStr string) (int64, error) {
  90. timeIssue, err := p.GetIssueTime(issueStr)
  91. if err != nil {
  92. return 0, err
  93. }
  94. fmt.Println(timeIssue)
  95. fmt.Println(p.baseTime)
  96. return 1 + ((timeIssue.Unix() - p.baseTime.Unix()) / int64(p.totalSecond)), nil
  97. }
  98. func (p *GameTimer) GetIssueTime(issueStr string) (*time.Time, error) {
  99. issueTimeSplit := strings.Split(issueStr, " ")
  100. if len(issueTimeSplit) != 2 {
  101. return nil, errors.New("期号格式错误")
  102. }
  103. timeIssue, err := time.ParseInLocation("2006-01-02", issueTimeSplit[0], time_utils.GetCNZone())
  104. if err != nil {
  105. return nil, err
  106. }
  107. numStr := strings.TrimLeft(issueTimeSplit[1], "0")
  108. issueInt, err := strconv.Atoi(numStr)
  109. if err != nil {
  110. return nil, err
  111. }
  112. timeIssue = timeIssue.Add(time.Duration(issueInt-1) * time.Duration(p.totalSecond) * time.Second)
  113. return &timeIssue, nil
  114. }
  115. func (p *GameTimer) GetIssueTimeInt(issue int64) (*time.Time, error) {
  116. issueStr := p.GetIssueStr(issue)
  117. return p.GetIssueTime(issueStr)
  118. }
  119. func (p *GameTimer) GetBaseTime() time.Time {
  120. return p.baseTime
  121. }
  122. func (p *GameTimer) GetIssueFromTodayZero(timeIssue time.Time) int64 {
  123. secondFromBase := timeIssue.Unix() - time_utils.GetZeroTime(timeIssue).Unix()
  124. issue := secondFromBase / int64(p.totalSecond)
  125. return issue + 1
  126. }
  127. func (p GameTimer) GetEvent(eventName string) string {
  128. return p.baseName + eventName
  129. }