Skip to content

Commit d0080fc

Browse files
committed
pre release
1 parent 59a6e10 commit d0080fc

File tree

6 files changed

+120
-19
lines changed

6 files changed

+120
-19
lines changed

app/contest.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414
)
1515

1616
// FindContest will find the latest contest in the contests array
17-
func FindContest(contests []contest.Contest) (int, error) {
17+
func FindContest(contests []contest.Contest) (contest.Contest, error) {
1818
contestsSiz := len(contests)
1919

2020
if contestsSiz <= 0 {
21-
return 0, errors.New("[Info] Contests is an empty field")
21+
return contest.Contest{}, errors.New("[Info] Contests is an empty field")
2222
}
2323

2424
var lastContestTime int64 = 100000000
@@ -39,30 +39,30 @@ func FindContest(contests []contest.Contest) (int, error) {
3939
}
4040

4141
if lastContestIndex == -1 {
42-
return 0, errors.New("[Info] No vaild contest")
42+
return contest.Contest{}, errors.New("[Info] No vaild contest")
4343
}
4444

4545
fmt.Println("[Info] The current contest is " + contests[lastContestIndex].Name)
4646

4747
openHackingPhase := currentTime - contests[lastContestIndex].StartTimeSeconds - contests[lastContestIndex].DurationSeconds
4848

49-
if openHackingPhase > 12*3600 {
50-
return 0, errors.New("[Info] Open hacking phase finished")
49+
if openHackingPhase < 12*3600 {
50+
return contest.Contest{}, errors.New("[Info] Open hacking phase finished")
5151
}
5252

5353
fmt.Println("[Info] Open hacking phase running")
5454

55-
return contests[lastContestIndex].ID, nil
55+
return contests[lastContestIndex], nil
5656
}
5757

5858
// ChooseProblem will fetch the problem in the contest and read user's input from stdin
59-
func ChooseProblem(ContestID int, cookie *[]*http.Cookie) (int, error) {
59+
func ChooseProblem(ContestID int, cookie *[]*http.Cookie) (string, error) {
6060
fmt.Println("[Info] Fetching Problems...")
6161

6262
problems, e := contest.GetProblems(ContestID, cookie)
6363

6464
if e != nil {
65-
return 0, e
65+
return "", e
6666
}
6767

6868
fmt.Println("")
@@ -83,7 +83,7 @@ func ChooseProblem(ContestID int, cookie *[]*http.Cookie) (int, error) {
8383
content, e := myReader.ReadString('\n')
8484

8585
if e != nil {
86-
return 0, e
86+
return "", e
8787
}
8888

8989
fields := strings.Fields(content)
@@ -102,7 +102,7 @@ func ChooseProblem(ContestID int, cookie *[]*http.Cookie) (int, error) {
102102
content, e = myReader.ReadString('\n')
103103

104104
if e != nil {
105-
return 0, e
105+
return "", e
106106
}
107107

108108
fields = strings.Fields(content)
@@ -115,5 +115,5 @@ func ChooseProblem(ContestID int, cookie *[]*http.Cookie) (int, error) {
115115
choose, e = strconv.Atoi(fields[0])
116116
}
117117

118-
return choose, nil
118+
return problems[choose-1].Index, nil
119119
}

app/contest_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ func Test_FindContest(t *testing.T) {
3232
res = contest.Contests{}
3333
e = json.Unmarshal([]byte(data2Contest), &res)
3434

35-
id, e := FindContest(res.Result)
35+
resp, e := FindContest(res.Result)
3636

3737
if e != nil {
3838
t.Error(e)
39-
} else if id != 1148 {
39+
} else if resp.ID != 1148 {
4040
t.Error("App Contest failed in finding contest")
4141
}
4242

app/judge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func runCode(SubmissionID int, Language string, customDiff bool) (bool, error) {
2424
}
2525

2626
if e.Error() == "Compile Error" {
27-
fmt.Println("[Warning] Code " + strconv.Itoa(SubmissionID) + " Compile Failed")
27+
fmt.Println("[Info] Code " + strconv.Itoa(SubmissionID) + " Compile Failed")
2828
}
2929

3030
if e.Error() == "Runtime Error" {

app/submission.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package app
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/hytzongxuan/Codeforces-Hacker/module/contest"
7+
"github.com/hytzongxuan/Codeforces-Hacker/module/submission"
8+
)
9+
10+
func GetSubmission(cookie *[]*http.Cookie, contestInfo contest.Contest, problem string) []submission.Submission {
11+
data, _ := submission.GetSubmissionArray(contestInfo.ID, cookie, contestInfo.StartTimeSeconds, contestInfo.DurationSeconds, problem)
12+
return data
13+
}

main.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
"os"
7+
"strconv"
68
"time"
79

810
"github.com/hytzongxuan/Codeforces-Hacker/app"
@@ -17,6 +19,7 @@ var Cookie []*http.Cookie
1719

1820
func finish() {
1921
time.Sleep(time.Millisecond * 5000)
22+
os.Exit(0)
2023
}
2124

2225
func main() {
@@ -29,14 +32,14 @@ func main() {
2932
finish()
3033
}
3134

32-
contestID, e := app.FindContest(Contest)
35+
contestInfo, e := app.FindContest(Contest)
3336

3437
if e != nil {
3538
fmt.Println(e)
3639
finish()
3740
}
3841

39-
id, e := app.ChooseProblem(contestID, &Cookie)
42+
choose, e := app.ChooseProblem(contestInfo.ID, &Cookie)
4043

4144
if e != nil {
4245
fmt.Println(e)
@@ -50,19 +53,43 @@ func main() {
5053
finish()
5154
}
5255

56+
var hackChoose bool
57+
5358
if loginChoose == true {
54-
hackChoose, e := app.QueryHackChoose()
59+
hackChoose, e = app.QueryHackChoose()
5560

5661
if e != nil {
5762
fmt.Println(e)
5863
finish()
5964
}
6065

6166
app.Login(&Cookie, CSRF)
62-
fmt.Println(hackChoose)
6367
}
6468

6569
app.SaveData()
66-
fmt.Println(id)
6770

71+
fmt.Println("[Info] Fetching submissions...")
72+
submit := app.GetSubmission(&Cookie, contestInfo, choose)
73+
74+
fmt.Println(" ")
75+
76+
if hackChoose == true {
77+
fmt.Println("[Info] Not Support auto hack")
78+
}
79+
80+
for i := 0; i < len(submit); i++ {
81+
flag, _ := app.TestCode(submit[i].SubmissionID, submit[i].Language, false, &Cookie, CSRF)
82+
83+
if flag == true {
84+
fmt.Println("[Info] Submission " + strconv.Itoa(submit[i].SubmissionID) + " Accepted")
85+
}
86+
87+
if e != nil {
88+
if hackChoose == true {
89+
90+
} else {
91+
92+
}
93+
}
94+
}
6895
}

module/submission/submission.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package submission
2+
3+
import (
4+
"errors"
5+
"net/http"
6+
"strconv"
7+
8+
"github.com/bitly/go-simplejson"
9+
"github.com/hytzongxuan/Codeforces-Hacker/module/conn"
10+
)
11+
12+
func querySubmission(contestID int, cookie *[]*http.Cookie) ([]byte, error) {
13+
res, err := conn.HTTPGetByte("http://codeforces.com/api/contest.status?contestId="+strconv.Itoa(contestID)+"&from=1&count=100000000", cookie, map[string]string{"HOST": "codeforces.com"})
14+
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
return res, nil
20+
}
21+
22+
type Submission struct {
23+
SubmissionID int
24+
Language string
25+
}
26+
27+
func GetSubmissionArray(contestID int, cookie *[]*http.Cookie, startTime int64, duringTime int64, problem string) ([]Submission, error) {
28+
resp, e := querySubmission(contestID, cookie)
29+
30+
if e != nil {
31+
return nil, e
32+
}
33+
34+
js, e := simplejson.NewJson(resp)
35+
36+
status, e := js.Get("status").String()
37+
38+
if status != "OK" {
39+
return nil, errors.New("Codeforces Return Error Response")
40+
}
41+
42+
submissions, e := js.Get("result").Array()
43+
44+
data := []Submission{}
45+
46+
for i := 0; i < len(submissions); i++ {
47+
submission := js.Get("result").GetIndex(i)
48+
49+
verdict := submission.Get("verdict").MustString()
50+
submissionID := submission.Get("id").MustInt()
51+
language := submission.Get("programmingLanguage").MustString()
52+
problemIndex := submission.Get("problem").Get("index").MustString()
53+
submitTime := submission.Get("creationTimeSeconds").MustInt64()
54+
55+
if verdict == "OK" && problemIndex == problem && submitTime > startTime && submitTime <= startTime+duringTime {
56+
data = append(data, Submission{submissionID, language})
57+
}
58+
}
59+
60+
return data, nil
61+
}

0 commit comments

Comments
 (0)