// package battle_wait -- заставляет ожидать начало битвы package battle_wait import ( "fmt" // "log" "strings" "time" "wartank/pkg/components/section" "wartank/pkg/components/sectionnet" "wartank/pkg/types" ) // BattleWait -- ожидатель начала битвы type BattleWait struct { *section.Section bot types.IBot net *sectionnet.SectionNet } // NewBattleWait -- возвращает новый ожидатель битвы func NewBattleWait(bot types.IBot) (*BattleWait, error) { section, err := section.NewSection(bot, "Ожидание сражения", `Сражения`) if err != nil { return nil, fmt.Errorf("NewBattleWait(): in create ISection, err=\n\t%w", err) } sf := &BattleWait{ Section: section, bot: bot, } sf.net, err = sectionnet.NewSectionNet(sf, "http://wartank.ru/pve") if err != nil { return nil, fmt.Errorf("NewBattleWait(): in create *SectionNet, err=\n\t%w", err) } return sf, nil } // Wait -- ожидает начало сражения func (sf *BattleWait) Wait() { if err := sf.net.UpdateLst(); err != nil { // Здесь может уже обратный отсчёт перед сражением return } var ( strOut string lstBattle = sf.GetLst() isFind bool ) for _, strOut = range lstBattle { if strings.Contains(strOut, `до начала `) { isFind = true break } // if strings.Contains(strOut, `>ОБЫЧНЫЕ<`) { // Это уже битва // if len(sf.chBattle) == 0 { // sf.chBattle <- 1 // } // return // } } if !isFind { // Сражение уже идёт return } // Найдена строка ожидания начала сражения lstTime := strings.Split(strOut, `до начала `) strTime := lstTime[1] lstTime = strings.Split(strTime, ` (`) strTime = lstTime[0] if err := sf.ParseCountDown(strTime); err != nil { // log._rintf("WARN BattleWait.Wait(): при установке времени ожидания сражения(%v)\n\terr=%v\n", strTime, err) return } // Зайти в цикложидания сражения for { countTime := sf.CountDown().Get() if countTime > 0 { time.Sleep(time.Millisecond * 500) // log.Printf("BattleWait.Wait(): countTime=%v\n", sf.CountDown().String()) continue } for len(sf.CountDown().ChanSig()) > 0 { <-sf.CountDown().ChanSig() } return } }