runner.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package main
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "math/rand"
  5. "sync/atomic"
  6. "time"
  7. )
  8. const (
  9. MAX_RUN = 3
  10. MIN_RUN = 1
  11. TIME_CHECK = 10 * 60 * time.Second // 十分钟检查一次
  12. //TIME_CHECK = 60 * time.Second // 十分钟检查一次
  13. TIME_RESET_TOTAL_BET = 24 * 3600 * time.Second
  14. //TIME_RESET_TOTAL_BET = 300 * time.Second
  15. )
  16. var BotQueue *queue
  17. var Working int64
  18. var TotalBet int64
  19. var MaxBet int64
  20. func init() {
  21. BotQueue = newQueue()
  22. }
  23. func Run(bots []Account) {
  24. AccountShuffle(bots)
  25. for _, bot := range bots {
  26. BotQueue.rPush(bot)
  27. }
  28. logrus.Info("bots queue init")
  29. ticker := time.NewTicker(TIME_CHECK)
  30. defer ticker.Stop()
  31. dayTicker := time.NewTicker(TIME_RESET_TOTAL_BET)
  32. defer dayTicker.Stop()
  33. botCh := make(chan Account)
  34. resetBet()
  35. master(botCh)
  36. for {
  37. select {
  38. case <-ticker.C:
  39. master(botCh)
  40. case <-dayTicker.C:
  41. resetBet()
  42. case exitBot := <-botCh:
  43. logrus.Warnf("bot: %s exit!", exitBot.Player)
  44. atomic.AddInt64(&Working, -1)
  45. BotQueue.rPush(exitBot) // 退出机器人重新加到队尾
  46. }
  47. }
  48. }
  49. func master(botCh chan<- Account) {
  50. logrus.Info("check running worker")
  51. totalBet := atomic.LoadInt64(&TotalBet)
  52. if totalBet >= MaxBet {
  53. logrus.Infof("TotalBet:%d >= MaxBet:%d, stop work!", totalBet, MaxBet)
  54. return
  55. }
  56. running := atomic.LoadInt64(&Working)
  57. logrus.Infof("running %d bots, queue len: %d", running, BotQueue.getLen())
  58. if running <= MIN_RUN {
  59. need := MAX_RUN - running
  60. var i int64
  61. for ; i < need; i++ {
  62. bot := BotQueue.lPop()
  63. if bot == nil {
  64. logrus.Error("bot queue empty!!!")
  65. break
  66. }
  67. go worker(bot.(Account), botCh)
  68. }
  69. }
  70. }
  71. func worker(bot Account, botCh chan<- Account) {
  72. logrus.Infof("bot: %s running~", bot.Player)
  73. atomic.AddInt64(&Working, 1)
  74. for {
  75. bet, err := Transfer(bot.Player, bot.StarId)
  76. if err != nil { // 下注失败退出
  77. logrus.WithField("error", err).Warnf("bot: %s transfer error", bot.Player)
  78. break
  79. }
  80. if atomic.AddInt64(&TotalBet, bet) >= atomic.LoadInt64(&MaxBet) {
  81. logrus.Infof("TotalBet >= MaxBet, bot: %s stop", bot.Player)
  82. break
  83. }
  84. rand.Seed(time.Now().UnixNano())
  85. c := rand.Intn(6) + 15
  86. time.Sleep(time.Duration(c) * time.Second)
  87. }
  88. botCh <- bot
  89. }
  90. func resetBet() {
  91. rand.Seed(time.Now().UnixNano())
  92. max := rand.Int63n(15000001) + 35000000 // [3500, 5000] EOS
  93. //max := rand.Int63n(5000) + 10000 // [3500, 5000] EOS
  94. atomic.StoreInt64(&MaxBet, max)
  95. atomic.StoreInt64(&TotalBet, 0)
  96. logrus.Infof("Reset MaxBet:%d, TotalBet:%d", max, 0)
  97. }