battle_wait.go 3.0 KB

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