Skip to content

Commit

Permalink
Fix race command: change countdown URL and open all problems of the c…
Browse files Browse the repository at this point in the history
…ontest. Close #21
  • Loading branch information
xalanq committed Jul 23, 2019
1 parent 2c446a4 commit 4ee78e8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 30 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ Examples:
cf stand Use default web browser to open the standing page.
cf sid 52531875 Use default web browser to open the submission 52531875's page.
cf sid Open the last submission's page.
cf race 1136 If the contest 1136 has not started yet, it will countdown. After the
countdown ends, it will run 'cf open 1136 a', 'cf open 1136 b', ...,
'cf open 1136 e', 'cf parse 1136'.
cf race 1136 If the contest 1136 has not started yet, it will countdown. When the
countdown ends, it will open all problems' pages and parse samples.
cf pull 100 Pull all problems' latest codes of contest 100 into "./100/<problem-id>".
cf pull 100 a Pull the latest code of problem "a" of contest 100 into "./100/<problem-id>".
cf pull ac 100 a Pull the "Accepted" or "Pretests passed" code of problem "a" of contest 100.
Expand Down
8 changes: 4 additions & 4 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ $ go build -ldflags "-s -w" cf.go
cf parse 获取当前比赛的当前题目到当前文件夹下。
cf gen 用默认的模板生成一份代码到当前文件夹下。
cf gen cpp 用名字为 "cpp" 的模板来生成一份代码到当前文件夹下。
cf test 在当前目录下执行模板里的命令,并测试全部样例。如果你想加一组新的测试数据,新建两个
文件 "inK.txt" 和 "ansK.txt" 即可,其中 K 是包含 0~9 的字符串。
cf test 在当前目录下执行模板里的命令,并测试全部样例。如果你想加一组新的测试数据,
新建两个文件 "inK.txt" 和 "ansK.txt" 即可,其中 K 是包含 0~9 的字符串。
cf watch 查看自己在当前比赛的最后 10 次提交结果。
cf watch all 查看自己在当前比赛的全部提交结果
cf open 1136 a 用默认的浏览器打开比赛 id 为 1136 的题目 a。
cf open 1136 用默认的浏览器打开比赛 id 为 1136 的总览页面。
cf stand 用默认的浏览器打开当前比赛的榜单。
cf sid 52531875 用默认的浏览器打开 52531875 这个提交页面。
cf sid 打开最后一次提交的页面。
cf race 1136 如果比赛还未开始且进入倒计时,则该命令会倒计时。当倒计时完后,会自动执行
'cf open 1136 a', 'cf open 1136 b', ..., 'cf open 1136 e', 'cf parse 1136'
cf race 1136 如果比赛还未开始且进入倒计时,则该命令会倒计时。倒计时完后,会自动打开所有
题目页面并拉取样例。
cf pull 100 拉取比赛 id 为 100 每道题的最新代码到文件夹 "./100/<problem-id>" 下。
cf pull 100 a 拉取比赛 id 为 100 的题目 a 的最新代码到文件夹 "./100/a" 下。
cf pull ac 100 a 拉取比赛 id 为 100 的题目 a 的 AC 代码。
Expand Down
5 changes: 2 additions & 3 deletions cf.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ Examples:
cf stand Use default web browser to open the standing page.
cf sid 52531875 Use default web browser to open the submission 52531875's page.
cf sid Open the last submission's page.
cf race 1136 If the contest 1136 has not started yet, it will countdown. After the
countdown ends, it will run 'cf open 1136 a', 'cf open 1136 b', ...,
'cf open 1136 e', 'cf parse 1136'.
cf race 1136 If the contest 1136 has not started yet, it will countdown. When the
countdown ends, it will open all problems' pages and parse samples.
cf pull 100 Pull all problems' latest codes of contest 100 into "./100/<problem-id>".
cf pull 100 a Pull the latest code of problem "a" of contest 100 into "./100/<problem-id>".
cf pull ac 100 a Pull the "Accepted" or "Pretests passed" code of problem "a" of contest 100.
Expand Down
8 changes: 7 additions & 1 deletion client/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"

"github.com/fatih/color"
"github.com/skratchdot/open-golang/open"
)

// ToGym if length of contestID >= 6, replace contest to gym
Expand Down Expand Up @@ -96,11 +97,16 @@ func (c *Client) ParseContestProblem(contestID, problemID, path string) (samples
}

// ParseContest parse for contest
func (c *Client) ParseContest(contestID, rootPath string) (err error) {
func (c *Client) ParseContest(contestID, rootPath string, race bool) (err error) {
problems, err := c.StatisContest(contestID)
if err != nil {
return
}
if race {
for _, problem := range problems {
open.Run(ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/problem/%v", contestID, problem.ID), contestID))
}
}
wg := sync.WaitGroup{}
wg.Add(len(problems))
mu := sync.Mutex{}
Expand Down
26 changes: 9 additions & 17 deletions client/race.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
Expand All @@ -10,7 +11,6 @@ import (

"github.com/fatih/color"
ansi "github.com/k0kubun/go-ansi"
"github.com/skratchdot/open-golang/open"
)

func findCountdown(body []byte) (int, error) {
Expand All @@ -25,18 +25,11 @@ func findCountdown(body []byte) (int, error) {
return h*60*60 + m*60 + s, nil
}

func raceContest(contestID string) (err error) {
for _, problemID := range []string{"A", "B", "C", "D", "E"} {
open.Run(ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/problem/%v", contestID, problemID), contestID))
}
return nil
}

// RaceContest wait for contest starting
func (c *Client) RaceContest(contestID string) (err error) {
color.Cyan(ToGym("Race for contest %v\n", contestID), contestID)

URL := ToGym(fmt.Sprintf("https://codeforces.com/contest/%v", contestID), contestID)
URL := ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/countdown", contestID), contestID)
resp, err := c.client.Get(URL)
if err != nil {
return
Expand All @@ -52,24 +45,23 @@ func (c *Client) RaceContest(contestID string) (err error) {
return
}

_, err = findStatisBlock(body)
if err != nil {
if !bytes.Contains(body, []byte(`Go!</a>`)) {
count, err := findCountdown(body)
if err != nil {
return err
}
color.Green("Count down: ")
count--
time.Sleep(900 * time.Millisecond)
color.Green("Countdown: ")
for count > 0 {
time.Sleep(time.Second)
count--
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 raceContest(contestID)

return
}
4 changes: 2 additions & 2 deletions cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Parse(args map[string]interface{}) error {
var ok bool
if contestID, ok = args["<contest-id>"].(string); ok {
if problemID, ok = args["<problem-id>"].(string); !ok {
return cln.ParseContest(contestID, filepath.Join(currentPath, contestID))
return cln.ParseContest(contestID, filepath.Join(currentPath, contestID), args["race"].(bool))
}
problemID = strings.ToLower(problemID)
path = filepath.Join(currentPath, contestID, problemID)
Expand All @@ -39,7 +39,7 @@ func Parse(args map[string]interface{}) error {
return err
}
if problemID == contestID {
return cln.ParseContest(contestID, currentPath)
return cln.ParseContest(contestID, currentPath, args["race"].(bool))
}
}
samples, err := cln.ParseContestProblem(contestID, problemID, path)
Expand Down

0 comments on commit 4ee78e8

Please sign in to comment.