battle_wait.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // package battle_wait -- заставляет ожидать начало битвы
  2. package battle_wait
  3. import (
  4. "fmt"
  5. // "log"
  6. "strings"
  7. "time"
  8. "wartank/pkg/components/section"
  9. "wartank/pkg/components/sectionnet"
  10. "wartank/pkg/types"
  11. )
  12. // СражениеОжидание -- ожидатель начала битвы
  13. type СражениеОжидание struct {
  14. *section.Section
  15. bot types.ИБот
  16. net *sectionnet.SectionNet
  17. }
  18. // НовСражениеОжидание -- возвращает новый ожидатель битвы
  19. func НовСражениеОжидание(bot types.ИБот) (*СражениеОжидание, error) {
  20. section, err := section.NewSection(bot, "Ожидание сражения", `<title>Сражения</title>`)
  21. if err != nil {
  22. return nil, fmt.Errorf("NewBattleWait(): in create ISection, err=\n\t%w", err)
  23. }
  24. сам := &СражениеОжидание{
  25. Section: section,
  26. bot: bot,
  27. }
  28. сам.net, err = sectionnet.NewSectionNet(сам, "https://wartank.ru/pve")
  29. if err != nil {
  30. return nil, fmt.Errorf("NewBattleWait(): in create *SectionNet, err=\n\t%w", err)
  31. }
  32. return сам, nil
  33. }
  34. // Ожидать -- ожидает начало сражения
  35. func (сам *СражениеОжидание) Ожидать() {
  36. // Зайти в цикл ожидания сражения
  37. for {
  38. // countTime := сам.ВремяОпрос().Получ()
  39. // if countTime > 0 {
  40. // time.Sleep(time.Millisecond * 500)
  41. // // log.Printf("BattleWait.Wait(): countTime=%v\n", сам.CountDown().String())
  42. // continue
  43. // }
  44. стрВрем := сам.ждать()
  45. if стрВрем == "" {
  46. return
  47. }
  48. лстВрем := strings.Split(стрВрем, ":")
  49. стрЧас := лстВрем[0]
  50. if стрЧас > "00" {
  51. time.Sleep(time.Hour * 1)
  52. continue
  53. }
  54. стрМин := лстВрем[1]
  55. if стрМин > "10" {
  56. time.Sleep(time.Minute * 10)
  57. continue
  58. }
  59. if стрМин > "01" {
  60. time.Sleep(time.Minute * 1)
  61. continue
  62. }
  63. if "00:00:05" < стрВрем && стрВрем < "00:00:59" {
  64. time.Sleep(time.Second * 5)
  65. continue
  66. }
  67. time.Sleep(time.Second * 1)
  68. }
  69. }
  70. // Ждёт пока время не обнулится
  71. func (сам *СражениеОжидание) ждать() string {
  72. if err := сам.net.UpdateLst(); err != nil { // Здесь может уже обратный отсчёт перед сражением
  73. return ""
  74. }
  75. var (
  76. strOut string
  77. lstBattle = сам.СписПолучить()
  78. isFind bool
  79. )
  80. for _, strOut = range lstBattle {
  81. if strings.Contains(strOut, `<span>до начала `) {
  82. isFind = true
  83. break
  84. }
  85. // if strings.Contains(strOut, `>ОБЫЧНЫЕ<`) { // Это уже битва
  86. // if len(сам.chBattle) == 0 {
  87. // сам.chBattle <- 1
  88. // }
  89. // return
  90. // }
  91. }
  92. if !isFind { // Сражение уже идёт
  93. return ""
  94. }
  95. // Найдена строка ожидания начала сражения
  96. lstTime := strings.Split(strOut, `<span>до начала `)
  97. strTime := lstTime[1]
  98. lstTime = strings.Split(strTime, ` (`)
  99. strTime = lstTime[0]
  100. if err := сам.ParseCountDown(strTime); err != nil { // Возможно уже всё
  101. // log._rintf("WARN BattleWait.Wait(): при установке времени ожидания сражения(%v)\n\terr=%v\n", strTime, err)
  102. return ""
  103. }
  104. return strTime
  105. }