/
status.go
122 lines (114 loc) · 2.38 KB
/
status.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
var Message struct {
Status string
Result []MsgSub
}
type MsgSub struct {
Id int
ContestId int
CreationTimeSeconds int
RelativeTimeSeconds int
Problem struct {
ContestId int
Index string
Name string
Points float64
Tags []string
}
Author struct {
ContestId int
Members []struct {
Handle string
}
ParticipantType string
Ghost bool
StartTimeSeconds int
}
ProgrammingLanguage string
Verdict string
Testset string
PassedTestCount int
TimeConsumedMillis int
MemoryConsumedBytes int
}
func (cf *cf) status(prob probCode) {
urlBase := CFURL + "/api/user.status?handle=" + cf.config["user"] + "&count="
url := urlBase + "1"
subCnt := 1
var err error
var resp *http.Response
var body []byte
spin := `|/-\`
empty := strings.Repeat(" ", 80)
var subID int
sleepTimeInc := time.Second / 5
sleepTime := sleepTimeInc
for i := 0; ; i++ {
time.Sleep(sleepTime)
sleepTime += sleepTimeInc
resp, err = http.Get(url)
if err != nil {
break
}
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
break
}
resp.Body.Close()
err = json.Unmarshal(body, &Message)
if len(Message.Result) == 0 {
break
}
var m *MsgSub
for _, msg := range Message.Result {
if (subID == 0 || subID == msg.Id) && msg.ContestId == prob.contest && msg.Problem.Index == prob.task {
m = &msg
subID = m.Id
break
}
}
if m == nil {
if subCnt > 100 {
if subID != 0 {
err = fmt.Errorf("could not find submission %d in last %d submissions", subID, subCnt)
} else {
err = fmt.Errorf("could not find submission for problem %d%s", prob.contest, prob.task)
}
break
}
subCnt *= 2
url = fmt.Sprintf("%s%d", urlBase, subCnt)
continue
}
buf := &bytes.Buffer{}
fmt.Fprintf(buf, "\rid=%v problem=%v%v ... %v %s%s", m.Id, m.ContestId, prob.task, string(spin[i%len(spin)]), color(m.Verdict), empty)
fmt.Print(buf.String()[:80])
if m.Verdict != "TESTING" && m.Verdict != "" {
break
}
}
fmt.Println()
if err != nil {
log.Println("status:", err)
}
}
func color(s string) string {
var c int
if s == "OK" {
c = 32
s = "ACCEPTED"
} else if s != "TESTING" {
c = 31
}
return fmt.Sprintf("\033[%dm%s\033[0m", c, s)
}