battle_wait.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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(сам, "http://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. if стрВрем > "01:00:00" {
  49. time.Sleep(time.Hour * 1)
  50. continue
  51. }
  52. if "00:00:05" < стрВрем && стрВрем < "00:00:59" {
  53. time.Sleep(time.Second * 5)
  54. continue
  55. }
  56. time.Sleep(time.Second * 1)
  57. }
  58. }
  59. // Ждёт пока время не обнулится
  60. func (сам *СражениеОжидание) ждать() string {
  61. if err := сам.net.UpdateLst(); err != nil { // Здесь может уже обратный отсчёт перед сражением
  62. return ""
  63. }
  64. var (
  65. strOut string
  66. lstBattle = сам.СписПолучить()
  67. isFind bool
  68. )
  69. for _, strOut = range lstBattle {
  70. if strings.Contains(strOut, `<span>до начала `) {
  71. isFind = true
  72. break
  73. }
  74. // if strings.Contains(strOut, `>ОБЫЧНЫЕ<`) { // Это уже битва
  75. // if len(сам.chBattle) == 0 {
  76. // сам.chBattle <- 1
  77. // }
  78. // return
  79. // }
  80. }
  81. if !isFind { // Сражение уже идёт
  82. return ""
  83. }
  84. // Найдена строка ожидания начала сражения
  85. lstTime := strings.Split(strOut, `<span>до начала `)
  86. strTime := lstTime[1]
  87. lstTime = strings.Split(strTime, ` (`)
  88. strTime = lstTime[0]
  89. if err := сам.ParseCountDown(strTime); err != nil { // Возможно уже всё
  90. // log._rintf("WARN BattleWait.Wait(): при установке времени ожидания сражения(%v)\n\terr=%v\n", strTime, err)
  91. return ""
  92. }
  93. return strTime
  94. }