Skip to content

Commit

Permalink
新增蹲课保护 修复有课但蹲课时无法选中的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
LeafYeeXYZ committed Jun 21, 2024
1 parent 6269a13 commit 756db70
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
| 密码 | 你的密码, 所有信息都保存在你的设备本地 |
| 课程代码 | 你要抢的课程代码, 多门课程请用空格隔开 |
| 上课班号 | 你要抢的上课班号, 多门课程请用空格隔开 |
| 记住密码 | 勾选后, 下次打开程序会自动填写密码 |
| 显示浏览器 | 勾选后, 会显示浏览器窗口, 用于调试 |
| 蹲课保护 | **仅对蹲课生效**. 勾选后, 发生任何错误都会强制重启<br>可以避免一些网络错误导致的蹲课中断 |

### 抢课模式说明
| 模式 | 开启教务页面数 | 如果系统未开启 | 如果可选人数为零 | 多个课程中一个成功 | 多个课程中一个失败 | 速度 |
| 模式 | 开启教务页面数 | 如果系统未开启 | 如果可选人数为零 | 多个课程中一个成功 | 多个课程中一个出错 | 速度 |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| 抢课 | 每个课程一个 | 刷新 | 退出 | 继续剩余课程 | 继续剩余课程 ||
| 多线程蹲课 | 每个课程一个 | 退出 | 刷新 | 继续剩余课程 | 退出 ||
Expand Down
Binary file modified README.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion catch.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ func (a *App) CatchCoursePub(speed int, studentID string, password string, cours
}

// 成功
return fmt.Errorf("抢课完成, 请手动确认结果")
runtime.EventsEmit(a.ctx, "currentStatus", "抢课完成, 请手动确认结果")
return nil
}

func (a *App) CatchCourseMaj(speed int, studentID string, password string, tpcourseID []string, tpclassID []string, headless bool) error {
Expand Down
37 changes: 36 additions & 1 deletion frontend/src/components/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,25 @@ export function Content({ browserStatus, systemStatus, currentStatus }: ContentP
EventsEmit('systemStatus', '单线程蹲课中')
}
// 抢课函数
await funcs[value.courseType][value.mode](value.speed, value.studentID, value.password, courseID, classID, localStorage.getItem('isHeadless') !== 'no')
if ((value.mode === 'WatchCourseSync' || value.mode === 'WatchCourse') && localStorage.getItem('isProtect') === 'yes') {
// 蹲课保护: Promise 被拒绝时, 会自动重试
const autoRetry = async (func: typeof WatchCoursePub | typeof WatchCoursePubSync, speed: number, studentID: string, password: string, courseID: string[], classID: string[], isHeadless: boolean) => {
// eslint-disable-next-line no-constant-condition
while (true) {
try {
await func(speed, studentID, password, courseID, classID, isHeadless)
break
} catch (err) {
EventsEmit('currentStatus', `检测到发生错误: ${err}`)
EventsEmit('currentStatus', '蹲课保护已启动, 重试 (如需退出, 直接关闭小鸦抢课即可)')
}
}
}
await autoRetry(funcs[value.courseType][value.mode], value.speed, value.studentID, value.password, courseID, classID, localStorage.getItem('isHeadless') !== 'no')
} else {
// 关闭蹲课保护或抢课
await funcs[value.courseType][value.mode](value.speed, value.studentID, value.password, courseID, classID, localStorage.getItem('isHeadless') !== 'no')
}
} catch (err) {
EventsEmit('currentStatus', err || '选课失败, 未知错误')
} finally {
Expand Down Expand Up @@ -296,6 +314,23 @@ export function Content({ browserStatus, systemStatus, currentStatus }: ContentP
}
}}
/>
<Switch
style={{
float: 'right',
opacity: 0.8,
marginRight: 10,
}}
checkedChildren='蹲课保护'
unCheckedChildren='蹲课保护'
defaultChecked={localStorage.getItem('isProtect') === 'yes'}
onChange={checked => {
if (checked) {
localStorage.setItem('isProtect', 'yes')
} else {
localStorage.setItem('isProtect', 'no')
}
}}
/>
</Form.Item>

</Form>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function Header({ systemStatus }: HeaderProps ) {
<p
className='header-title'
>
小鸦抢课 v1.3.0 - {systemStatus}
小鸦抢课 v1.4.0 - {systemStatus}
</p>

<button
Expand Down
24 changes: 20 additions & 4 deletions watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func (a *App) WatchCoursePub(speed int, studentID string, password string, cours
case err := <-ch:
if err != nil {
return err
} else {
return nil
}
default:
continue
Expand All @@ -43,6 +45,8 @@ func (a *App) WatchCoursePubSync(speed int, studentID string, password string, c
case err := <-ch:
if err != nil {
return err
} else {
return nil
}
default:
continue
Expand Down Expand Up @@ -185,13 +189,18 @@ func (a *App) watchCoursePubCore(speed int, studentID string, password string, c
runtime.EventsEmit(a.ctx, "currentStatus", fmt.Sprintf("检索课程 %s", courseID))
for {
ele = iiframe.Locator("#tr0_xz a")
// 延时
time.Sleep(time.Duration(speed) * time.Millisecond)
// 等待加载
iframe.WaitForLoadState(playwright.FrameWaitForLoadStateOptions{
State: playwright.LoadStateNetworkidle,
})
if exists, _ := ele.IsVisible(); exists {
err = ele.Click()
if err != nil { errCh <- err; return }
break
} else {
runtime.EventsEmit(a.ctx, "currentStatus", fmt.Sprintf("未找到课程 %s, 重新检索 (关闭小鸦抢课即可停止)", courseID))
time.Sleep(time.Duration(speed) * time.Millisecond)
// 点击 "检索"
ele = iframe.Locator("#btnQry")
err = ele.Click()
Expand Down Expand Up @@ -242,7 +251,8 @@ func (a *App) watchCoursePubCore(speed int, studentID string, password string, c
}

// 成功
ch <- fmt.Errorf("全部课程蹲课完成, 请手动确认结果")
runtime.EventsEmit(a.ctx, "currentStatus", "全部课程蹲课完成, 请手动确认结果")
ch <- nil
}

// 单线程蹲课模式主函数
Expand Down Expand Up @@ -380,13 +390,18 @@ func (a *App) watchCoursePubSyncCore(speed int, studentID string, password strin
// 点击 "选择"
runtime.EventsEmit(a.ctx, "currentStatus", fmt.Sprintf("检索课程 %s", courseID[index]))
ele = iiframe.Locator("#tr0_xz a")
// 延时
time.Sleep(time.Duration(speed) * time.Millisecond)
// 等待加载
iframe.WaitForLoadState(playwright.FrameWaitForLoadStateOptions{
State: playwright.LoadStateNetworkidle,
})
if exists, _ := ele.IsVisible(); exists {
err = ele.Click()
if err != nil { ch <- err; return }
break LOOP
} else {
runtime.EventsEmit(a.ctx, "currentStatus", fmt.Sprintf("未找到课程 %s, 重新检索 (关闭小鸦抢课即可停止)", courseID[index]))
time.Sleep(time.Duration(speed) * time.Millisecond)
// 更新索引
if index < len(courseID) - 1 {
index++
Expand All @@ -408,7 +423,8 @@ func (a *App) watchCoursePubSyncCore(speed int, studentID string, password strin
time.Sleep(2 * time.Second)

// 成功
ch <- fmt.Errorf("某个课程蹲课成功, 请手动确认结果")
runtime.EventsEmit(a.ctx, "currentStatus", "某个课程蹲课完成, 请手动确认结果")
ch <- nil
}

func (a *App) WatchCourseMaj(speed int, studentID string, password string, tpcourseID []string, tpclassID []string, headless bool) error {
Expand Down

0 comments on commit 756db70

Please sign in to comment.