battle_wait.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // BattleWait -- ожидатель начала битвы
  13. type BattleWait struct {
  14. *section.Section
  15. bot types.IBot
  16. net *sectionnet.SectionNet
  17. }
  18. // NewBattleWait -- возвращает новый ожидатель битвы
  19. func NewBattleWait(bot types.IBot) (*BattleWait, error) {
  20. if bot == nil {
  21. return nil, fmt.Errorf("NewBattleWait():IServBot == nil")
  22. }
  23. sf := &BattleWait{
  24. bot: bot,
  25. }
  26. var err error
  27. { // ISection (ожидание)
  28. sf.Section, err = section.NewSection(bot, `<title>Сражения</title>`)
  29. if err != nil {
  30. return nil, fmt.Errorf("NewBattleWait(): in create *Section, err=\n\t%w", err)
  31. }
  32. }
  33. return sf, nil
  34. }
  35. // Run -- запускает ожидание в работу
  36. func (sf *BattleWait) Run() {
  37. sf.net = sectionnet.NewSectionNet(sf.bot, sf, "http://wartank.ru/pve")
  38. }
  39. // Wait -- ожидает начало сражения
  40. func (sf *BattleWait) Wait() {
  41. if err := sf.net.UpdateLst("Ближайшее сражение"); err != nil { // Здесь может уже обратный отсчёт перед сражением
  42. return
  43. }
  44. var (
  45. strOut string
  46. lstBattle = sf.GetLst()
  47. isFind bool
  48. )
  49. for _, strOut = range lstBattle {
  50. if strings.Contains(strOut, `<span>до начала `) {
  51. isFind = true
  52. break
  53. }
  54. // if strings.Contains(strOut, `>ОБЫЧНЫЕ<`) { // Это уже битва
  55. // if len(sf.chBattle) == 0 {
  56. // sf.chBattle <- 1
  57. // }
  58. // return
  59. // }
  60. }
  61. if !isFind { // Сражение уже идёт
  62. return
  63. }
  64. // Найдена строка ожидания начала сражения
  65. lstTime := strings.Split(strOut, `<span>до начала `)
  66. strTime := lstTime[1]
  67. lstTime = strings.Split(strTime, ` (`)
  68. strTime = lstTime[0]
  69. if err := sf.CountDown().Parse(strTime); err != nil {
  70. // log._rintf("WARN BattleWait.Wait(): при установке времени ожидания сражения(%v)\n\terr=%v\n", strTime, err)
  71. return
  72. }
  73. // Зайти в цикложидания сражения
  74. for {
  75. countTime := sf.CountDown().Get()
  76. if countTime > 0 {
  77. time.Sleep(time.Millisecond * 500)
  78. // log.Printf("BattleWait.Wait(): countTime=%v\n", sf.CountDown().String())
  79. continue
  80. }
  81. for len(sf.CountDown().ChanSig()) > 0 {
  82. <-sf.CountDown().ChanSig()
  83. }
  84. return
  85. }
  86. }