抢购后自动核对积分余额(基线在导入账号时获取),发布0.3.5
兑换成功会扣积分,所以"余额确实少了多少"是一个独立于接口返回的确认证据—— 2026-09-30 出现过订单实际已支付、程序却报成失败的情况,只看接口返回不够。 基线的获取时机刻意放在抢购链路之外:轮次/预热期间任何额外请求都会挤占锁单, 账号越多越明显。基线只在三个时机更新: - 导入账号后自动查一次(后台执行,不阻塞导入返回) - 用户手动点"查询积分"时 - 每次抢购核对之后把最新余额存回,下一次抢购的基线就是上一次的终值,差值才准 账号被编辑(改名/换 Cookie)时清掉旧基线,删除账号时一并清理。 - 新增 ScoreBaselineStore(scores.json,与账号库同目录,原子写盘): 记录每个账号上次已知的积分余额与时间;账号库路径为空时退化为纯内存,不写工作目录 - 核对输出示例: "积分已扣减,兑换确认: 账号=a 抢购前=1000 抢购后=800 扣减=200" 并把"|积分-200"附到结果集,账号表里也能直接看到确认信息 - 没有基线时只记录余额并提示"下次抢购即可比对",不会误报"未扣分"; 积分接口失败只记警告,不改变"抢到"这个结论 - 抢购进行中调用导入不会去查询积分(避免与锁单抢连接),提示稍后手动查询 - 预热日志措辞修正:"开始预热: N 个请求" → "N 个账号"(每账号实际不止一个请求) - 新增测试:TestVerifyRunResultsConfirmsScoreDeduction、TestVerifyRunResultsWithoutBaseline、 TestVerifyRunResultsIgnoresScoreFailure、TestScoreBaselineStorePersists、 TestFetchScoreBaselinesRecordsValues
This commit is contained in:
@@ -2,6 +2,7 @@ package seckill
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -70,6 +71,8 @@ type Scheduler struct {
|
||||
|
||||
accounts *AccountStore
|
||||
|
||||
scoreBaselines *ScoreBaselineStore
|
||||
|
||||
resultsMu sync.RWMutex
|
||||
results map[string]string
|
||||
|
||||
@@ -130,10 +133,11 @@ func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, con
|
||||
attempts = constants.DefaultAttempts
|
||||
}
|
||||
scheduler := &Scheduler{
|
||||
client: cli,
|
||||
paths: paths,
|
||||
font: font,
|
||||
accounts: NewAccountStore(paths.AccountFile, paths.LegacyAccountFile),
|
||||
client: cli,
|
||||
paths: paths,
|
||||
font: font,
|
||||
accounts: NewAccountStore(paths.AccountFile, paths.LegacyAccountFile),
|
||||
scoreBaselines: NewScoreBaselineStore(scoreBaselinePath(paths.AccountFile)),
|
||||
successImages: image.NewSuccessImageSaver(
|
||||
font,
|
||||
paths.SuccessDir,
|
||||
@@ -149,6 +153,7 @@ func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, con
|
||||
products: make(map[string]types.Product),
|
||||
calibrationOffsets: make([]time.Duration, 0, calibrationHistorySize),
|
||||
}
|
||||
scheduler.scoreBaselines.Load()
|
||||
scheduler.loadLatestXiaodianReport()
|
||||
return scheduler
|
||||
}
|
||||
@@ -455,9 +460,158 @@ func (s *Scheduler) RunNow(accountProducts map[string]string) {
|
||||
wg.Wait()
|
||||
|
||||
s.logRunSummary(outcomes, ctl, time.Since(start))
|
||||
// 先核对积分(会产生"积分余额"请求,一并计入后面的接口统计)。
|
||||
s.verifyRunResults(outcomes, ctl, s.clientSnapshot())
|
||||
s.logNetworkSummary(s.clientSnapshot())
|
||||
}
|
||||
|
||||
// verifyRunResults 抢购结束后对成功账号核对积分余额。
|
||||
//
|
||||
// 兑换成功会扣积分,所以"余额确实少了多少"是一个独立于接口返回的确认证据:
|
||||
// 2026-09-30 就出现过订单实际已支付、程序却报成失败的情况,只看接口返回不够。
|
||||
// 核对在任务结束之后进行,不影响抢购本身;单个账号查询失败只记警告,
|
||||
// 不改变"抢到"这个结论。
|
||||
func (s *Scheduler) verifyRunResults(outcomes []accountOutcome, ctl *runControl, cli *client.DouyuClient) {
|
||||
if cli == nil || cli.Activity.ScoreURL == "" {
|
||||
return
|
||||
}
|
||||
successes := make([]accountOutcome, 0, len(outcomes))
|
||||
for _, out := range outcomes {
|
||||
if out.kind == outcomeSuccess {
|
||||
successes = append(successes, out)
|
||||
}
|
||||
}
|
||||
if len(successes) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
logger.LogEvent("核对", "info", "开始核对积分余额: %d 个成功账号", len(successes))
|
||||
var mu sync.Mutex
|
||||
confirmed := 0
|
||||
var wg sync.WaitGroup
|
||||
for _, out := range successes {
|
||||
wg.Add(1)
|
||||
go func(out accountOutcome) {
|
||||
defer wg.Done()
|
||||
cookie, ok := s.AccountCookieByName(out.account)
|
||||
if !ok || cookie == "" {
|
||||
logger.LogAccountEvent("核对", "warn", out.account, "积分核对跳过: 账号=%s 没有可用 Cookie", out.account)
|
||||
return
|
||||
}
|
||||
if ctl != nil {
|
||||
if !ctl.enter() {
|
||||
return
|
||||
}
|
||||
defer ctl.leave()
|
||||
}
|
||||
score, err := cli.Score(cookie)
|
||||
if err != nil {
|
||||
logger.LogAccountEvent("核对", "warn", out.account, "积分核对失败: 账号=%s err=%v", out.account, err)
|
||||
return
|
||||
}
|
||||
|
||||
before, hasBefore, baselineAt := s.ScoreBaseline(out.account)
|
||||
// 无论能否比对,都把最新余额存回基线:下一次抢购的"抢购前"就是这一次的终值。
|
||||
s.RecordScoreBaseline(out.account, score)
|
||||
if !hasBefore {
|
||||
logger.LogAccountEvent("核对", "info", out.account,
|
||||
"抢购后积分余额: 账号=%s 余额=%d(该账号还没有基线,本次先记录,下次抢购即可比对)", out.account, score)
|
||||
return
|
||||
}
|
||||
if score >= before {
|
||||
logger.LogAccountEvent("核对", "warn", out.account,
|
||||
"积分未见减少,请核实兑换记录: 账号=%s 抢购前=%d 抢购后=%d 基线时间=%s",
|
||||
out.account, before, score, baselineAt.In(constants.CST).Format("01-02 15:04:05"))
|
||||
return
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
confirmed++
|
||||
mu.Unlock()
|
||||
logger.LogAccountEvent("核对", "success", out.account,
|
||||
"积分已扣减,兑换确认: 账号=%s 抢购前=%d 抢购后=%d 扣减=%d", out.account, before, score, before-score)
|
||||
// 把扣分结果附到结果集,账号表里也能直接看到确认信息。
|
||||
key := resultKey(types.ExchangeAttempt{Account: types.Account{Name: out.account}})
|
||||
if current := s.Results()[key]; current != "" {
|
||||
s.setResult(key, fmt.Sprintf("%s|积分-%d", current, before-score))
|
||||
}
|
||||
}(out)
|
||||
}
|
||||
wg.Wait()
|
||||
logger.LogEvent("核对", "info", "积分核对完成: %d/%d 个账号确认扣分", confirmed, len(successes))
|
||||
}
|
||||
|
||||
// ScoreBaseline 返回该账号上次已知的积分余额及其记录时间。
|
||||
func (s *Scheduler) ScoreBaseline(accountName string) (int, bool, time.Time) {
|
||||
if s.scoreBaselines == nil {
|
||||
return 0, false, time.Time{}
|
||||
}
|
||||
return s.scoreBaselines.Get(accountName)
|
||||
}
|
||||
|
||||
// RecordScoreBaseline 记录账号的最新积分余额(落盘)。
|
||||
func (s *Scheduler) RecordScoreBaseline(accountName string, score int) {
|
||||
if s.scoreBaselines != nil {
|
||||
s.scoreBaselines.Set(accountName, score)
|
||||
}
|
||||
}
|
||||
|
||||
// ForgetScoreBaselines 删除已移除账号的基线。
|
||||
func (s *Scheduler) ForgetScoreBaselines(accountNames []string) {
|
||||
if s.scoreBaselines != nil {
|
||||
s.scoreBaselines.Forget(accountNames)
|
||||
}
|
||||
}
|
||||
|
||||
// FetchScoreBaselines 并发查询并记录这批账号的积分基线。
|
||||
//
|
||||
// 只在"添加账号"这类抢购之外的操作里调用:账号多的时候,
|
||||
// 抢购链路(预热或触发时刻)绝不能承担这批额外请求。
|
||||
func (s *Scheduler) FetchScoreBaselines(accounts []types.Account) {
|
||||
cli := s.clientSnapshot()
|
||||
if cli == nil || cli.Activity.ScoreURL == "" || len(accounts) == 0 {
|
||||
return
|
||||
}
|
||||
if s.IsRunning() {
|
||||
logger.LogEvent("账号", "warn", "抢购进行中,跳过 %d 个账号的积分基线查询(可稍后手动查询积分)", len(accounts))
|
||||
return
|
||||
}
|
||||
settings := s.settingsSnapshot()
|
||||
limit := make(chan struct{}, settings.concurrency)
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
mu sync.Mutex
|
||||
collected = make(map[string]int, len(accounts))
|
||||
)
|
||||
for _, account := range accounts {
|
||||
if account.Cookie == "" {
|
||||
continue
|
||||
}
|
||||
wg.Add(1)
|
||||
go func(account types.Account) {
|
||||
defer wg.Done()
|
||||
limit <- struct{}{}
|
||||
defer func() { <-limit }()
|
||||
score, err := cli.Score(account.Cookie)
|
||||
if err != nil {
|
||||
logger.LogAccountEvent("账号", "warn", account.Name, "积分基线查询失败: 账号=%s err=%v", account.Name, err)
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
collected[account.Name] = score
|
||||
mu.Unlock()
|
||||
}(account)
|
||||
}
|
||||
wg.Wait()
|
||||
if len(collected) == 0 {
|
||||
return
|
||||
}
|
||||
if s.scoreBaselines != nil {
|
||||
s.scoreBaselines.SetMany(collected)
|
||||
}
|
||||
logger.LogEvent("账号", "success", "已记录 %d 个账号的积分基线(用于抢购后核对是否扣分)", len(collected))
|
||||
}
|
||||
|
||||
// logNetworkSummary 输出本次任务期间各类接口的聚合统计,替代逐条刷屏的网络日志。
|
||||
func (s *Scheduler) logNetworkSummary(cli *client.DouyuClient) {
|
||||
if cli == nil {
|
||||
@@ -520,6 +674,7 @@ const stockOutWarnAfter = 8
|
||||
// accountOutcome 是单个账号的最终结果。
|
||||
type accountOutcome struct {
|
||||
account string
|
||||
product types.Product
|
||||
kind string
|
||||
reason string
|
||||
locked bool
|
||||
@@ -528,7 +683,7 @@ type accountOutcome struct {
|
||||
// runAccountLoop 是单个账号的独立重试循环:锁单失败按配置节奏重试,
|
||||
// 达到连续被拒上限或账号级终止条件时退出。
|
||||
func (s *Scheduler) runAccountLoop(task types.ExchangeAttempt, settings runSettings, ctl *runControl) accountOutcome {
|
||||
out := accountOutcome{account: task.Account.Name}
|
||||
out := accountOutcome{account: task.Account.Name, product: task.Product}
|
||||
// rejectStreak 只统计"该商品当前拿不到"的连续次数,用于停止判定;
|
||||
// failStreak 统计所有连续失败,用于放大退避(两者混用会让频控/传输层错误
|
||||
// 永远不递增,一路用配置里的最快节奏猛打)。
|
||||
@@ -1120,3 +1275,16 @@ func (s *Scheduler) enqueueSuccessImage(account types.Account, product types.Pro
|
||||
}
|
||||
s.successImages.Enqueue(account, product, roleName)
|
||||
}
|
||||
|
||||
// scoreBaselinePath 返回积分基线文件路径:与账号库同目录。
|
||||
// 账号库路径为空时返回空串,基线退化为纯内存,避免写到当前工作目录。
|
||||
func scoreBaselinePath(accountFile string) string {
|
||||
if strings.TrimSpace(accountFile) == "" {
|
||||
return ""
|
||||
}
|
||||
dir := filepath.Dir(accountFile)
|
||||
if dir == "" {
|
||||
dir = "."
|
||||
}
|
||||
return filepath.Join(dir, scoreBaselineFileName)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user