/
race.go
72 lines (61 loc) · 1.37 KB
/
race.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package client
import (
"bytes"
"errors"
"fmt"
"regexp"
"strconv"
"time"
"github.com/xalanq/cf-tool/util"
"github.com/fatih/color"
ansi "github.com/k0kubun/go-ansi"
)
func findCountdown(body []byte) (int, error) {
reg := regexp.MustCompile(`class=["']countdown["'][\s\S]*?(\d+):(\d+):(\d+)`)
tmp := reg.FindSubmatch(body)
if tmp == nil {
return 0, errors.New("Cannot find any countdown")
}
h, _ := strconv.Atoi(string(tmp[1]))
m, _ := strconv.Atoi(string(tmp[2]))
s, _ := strconv.Atoi(string(tmp[3]))
return h*60*60 + m*60 + s, nil
}
// RaceContest wait for contest starting
func (c *Client) RaceContest(info Info) (err error) {
color.Cyan("Race " + info.Hint())
URL, err := info.ProblemSetURL(c.host)
if err != nil {
return
}
if info.ProblemType == "acmsguru" {
return errors.New(ErrorNotSupportAcmsguru)
}
URL = URL + "/countdown"
body, err := util.GetBody(c.client, URL)
if err != nil {
return
}
_, err = findHandle(body)
if err != nil {
return
}
if !bytes.Contains(body, []byte(`Go!</a>`)) {
count, err := findCountdown(body)
if err != nil {
return err
}
color.Green("Countdown: ")
for count > 0 {
h := count / 60 / 60
m := count/60 - h*60
s := count - h*60*60 - m*60
fmt.Printf("%02d:%02d:%02d\n", h, m, s)
ansi.CursorUp(1)
count--
time.Sleep(time.Second)
}
time.Sleep(900 * time.Millisecond)
}
return
}