新增小店抢购报告导出并优化停止条件
This commit is contained in:
@@ -39,6 +39,7 @@ type roundOutcome struct {
|
||||
stoppedAccounts map[string]string
|
||||
stoppedProducts map[string]string
|
||||
cooldownAccounts map[string]time.Time
|
||||
results map[string]types.ExchangeResult
|
||||
attempts int
|
||||
}
|
||||
|
||||
@@ -72,6 +73,9 @@ type Scheduler struct {
|
||||
resultsMu sync.RWMutex
|
||||
results map[string]string
|
||||
|
||||
reportMu sync.RWMutex
|
||||
latestXiaodianReport *types.XiaodianRunReport
|
||||
|
||||
runMu sync.Mutex
|
||||
running bool
|
||||
stopRun chan struct{}
|
||||
@@ -125,7 +129,7 @@ func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, con
|
||||
if attempts <= 0 {
|
||||
attempts = constants.DefaultAttempts
|
||||
}
|
||||
return &Scheduler{
|
||||
scheduler := &Scheduler{
|
||||
client: cli,
|
||||
paths: paths,
|
||||
font: font,
|
||||
@@ -145,6 +149,8 @@ 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.loadLatestXiaodianReport()
|
||||
return scheduler
|
||||
}
|
||||
|
||||
// Close 关闭调度器的后台资源。
|
||||
@@ -617,17 +623,25 @@ func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, del
|
||||
settings := s.settingsSnapshot()
|
||||
attempts, delay := normalizeXiaodianRun(attempts, delayMs, settings.attempts)
|
||||
accounts := make([]types.Account, 0, len(accountProducts))
|
||||
products := make(map[string]types.Product, len(accountProducts))
|
||||
for name := range accountProducts {
|
||||
cookie, ok := s.AccountCookieByName(name)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
productID := accountProducts[name]
|
||||
product := s.productForCommodityID(productID)
|
||||
if product.CommodityID == "" {
|
||||
product.CommodityID = productID
|
||||
}
|
||||
accounts = append(accounts, types.Account{Name: name, Cookie: cookie})
|
||||
products[name] = product
|
||||
}
|
||||
if len(accounts) == 0 {
|
||||
logger.LogEvent("抢购", "warn", "和平小店抢购没有可执行账号")
|
||||
return
|
||||
}
|
||||
s.startXiaodianReport(accounts, products)
|
||||
|
||||
stoppedAccounts := make(map[string]string)
|
||||
cooldownAccounts := make(map[string]time.Time)
|
||||
@@ -652,11 +666,7 @@ func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, del
|
||||
if stoppedAccounts[account.Name] != "" {
|
||||
continue
|
||||
}
|
||||
productID := accountProducts[account.Name]
|
||||
product := s.productForCommodityID(productID)
|
||||
if product.CommodityID == "" {
|
||||
product.CommodityID = productID
|
||||
}
|
||||
product := products[account.Name]
|
||||
roundAttempts = append(roundAttempts, types.ExchangeAttempt{
|
||||
Account: account,
|
||||
Product: product,
|
||||
@@ -681,6 +691,7 @@ func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, del
|
||||
}
|
||||
|
||||
outcome := s.executeXiaodianRound(roundAttempts, settings)
|
||||
s.updateXiaodianReport(outcome.results)
|
||||
for name := range outcome.successAccounts {
|
||||
stoppedAccounts[name] = "已抢到"
|
||||
}
|
||||
@@ -690,6 +701,7 @@ func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, del
|
||||
logger.LogAccountEvent("抢购", "warn", name, "账号停止重试: 账号=%s 原因=%s", name, reason)
|
||||
}
|
||||
}
|
||||
s.stopXiaodianReportAccounts(outcome.stoppedAccounts)
|
||||
for name, until := range outcome.cooldownAccounts {
|
||||
if stoppedAccounts[name] != "" || !until.After(cooldownAccounts[name]) {
|
||||
continue
|
||||
@@ -715,6 +727,7 @@ func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, del
|
||||
|
||||
logger.LogEvent("抢购", "info", "和平小店抢购结束: 成功或停止=%d/%d,耗时=%v",
|
||||
len(stoppedAccounts), len(accounts), utils.RoundDuration(time.Since(start)))
|
||||
s.finishXiaodianReport(s.runStopped())
|
||||
}
|
||||
|
||||
func normalizeXiaodianRun(attempts, delayMs, fallbackAttempts int) (int, time.Duration) {
|
||||
@@ -824,6 +837,7 @@ func (s *Scheduler) executeRoundWithRunner(
|
||||
stoppedAccounts: make(map[string]string),
|
||||
stoppedProducts: make(map[string]string),
|
||||
cooldownAccounts: make(map[string]time.Time),
|
||||
results: make(map[string]types.ExchangeResult),
|
||||
}
|
||||
var mu sync.Mutex
|
||||
|
||||
@@ -867,6 +881,7 @@ func (s *Scheduler) executeRoundWithRunner(
|
||||
|
||||
mu.Lock()
|
||||
outcome.attempts++
|
||||
outcome.results[attempt.Account.Name] = result
|
||||
if result.OK {
|
||||
outcome.successAccounts[attempt.Account.Name] = true
|
||||
} else {
|
||||
@@ -960,6 +975,9 @@ func classifyRetryStop(message string) (account bool, product bool, reason strin
|
||||
|
||||
func classifyXiaodianRetryStop(message string) (account bool, product bool, reason string) {
|
||||
switch {
|
||||
case strings.Contains(message, "Livelink登录态校验失败"),
|
||||
strings.Contains(message, "Livelink") && strings.Contains(message, "登录态校验失败"):
|
||||
return true, false, "和平小店绑定已失效,需重新绑定"
|
||||
case strings.Contains(message, "余额不足"),
|
||||
strings.Contains(message, "点券不足"):
|
||||
return true, false, "点券余额不足"
|
||||
|
||||
@@ -128,6 +128,7 @@ func TestClassifyXiaodianRetryStop(t *testing.T) {
|
||||
product bool
|
||||
reason string
|
||||
}{
|
||||
{"对不起,Livelink登录态校验失败!", true, false, "和平小店绑定已失效,需重新绑定"},
|
||||
{"不好意思,您的余额不足~", true, false, "点券余额不足"},
|
||||
{"点券不足,请充值后重试", true, false, "点券余额不足"},
|
||||
{"未绑定账号", true, false, "账号未绑定角色"},
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
package seckill
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"douyu_go/internal/logger"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
const xiaodianReportFileName = "last_xiaodian_report.json"
|
||||
|
||||
func (s *Scheduler) startXiaodianReport(accounts []types.Account, products map[string]types.Product) {
|
||||
report := types.XiaodianRunReport{
|
||||
ReportID: fmt.Sprintf("xiaodian_%s", time.Now().Format("20060102_150405.000")),
|
||||
StartedAt: time.Now(),
|
||||
Accounts: make([]types.XiaodianRunReportAccount, 0, len(accounts)),
|
||||
}
|
||||
for _, account := range accounts {
|
||||
product := products[account.Name]
|
||||
report.Accounts = append(report.Accounts, types.XiaodianRunReportAccount{
|
||||
AccountName: account.Name,
|
||||
ProductID: product.CommodityID,
|
||||
ProductName: product.CommodityName,
|
||||
ProductPrice: product.Score,
|
||||
PayType: product.PayType,
|
||||
PayLabel: product.PayLabel,
|
||||
Status: "待执行",
|
||||
})
|
||||
}
|
||||
sort.Slice(report.Accounts, func(i, j int) bool {
|
||||
return report.Accounts[i].AccountName < report.Accounts[j].AccountName
|
||||
})
|
||||
|
||||
s.reportMu.Lock()
|
||||
s.latestXiaodianReport = &report
|
||||
s.reportMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Scheduler) updateXiaodianReport(results map[string]types.ExchangeResult) {
|
||||
if len(results) == 0 {
|
||||
return
|
||||
}
|
||||
s.reportMu.Lock()
|
||||
defer s.reportMu.Unlock()
|
||||
if s.latestXiaodianReport == nil {
|
||||
return
|
||||
}
|
||||
for accountName, result := range results {
|
||||
for i := range s.latestXiaodianReport.Accounts {
|
||||
account := &s.latestXiaodianReport.Accounts[i]
|
||||
if account.AccountName != accountName {
|
||||
continue
|
||||
}
|
||||
account.Attempts++
|
||||
account.LastMessage = result.Message
|
||||
account.RoleName = result.RoleName
|
||||
if result.OK {
|
||||
account.Status = "成功"
|
||||
account.FailureReason = ""
|
||||
} else {
|
||||
account.Status = "失败"
|
||||
account.FailureReason = reportFailureReason(result.Message)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scheduler) stopXiaodianReportAccounts(stopped map[string]string) {
|
||||
if len(stopped) == 0 {
|
||||
return
|
||||
}
|
||||
s.reportMu.Lock()
|
||||
defer s.reportMu.Unlock()
|
||||
if s.latestXiaodianReport == nil {
|
||||
return
|
||||
}
|
||||
for i := range s.latestXiaodianReport.Accounts {
|
||||
account := &s.latestXiaodianReport.Accounts[i]
|
||||
if reason := stopped[account.AccountName]; reason != "" && account.Status != "成功" {
|
||||
account.Status = "失败"
|
||||
account.FailureReason = reason
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Scheduler) finishXiaodianReport(stopped bool) {
|
||||
s.reportMu.Lock()
|
||||
if s.latestXiaodianReport == nil {
|
||||
s.reportMu.Unlock()
|
||||
return
|
||||
}
|
||||
report := s.latestXiaodianReport
|
||||
report.FinishedAt = time.Now()
|
||||
report.Stopped = stopped
|
||||
if stopped {
|
||||
for i := range report.Accounts {
|
||||
account := &report.Accounts[i]
|
||||
if account.Status == "待执行" {
|
||||
account.Status = "已停止"
|
||||
account.FailureReason = "任务已停止"
|
||||
}
|
||||
}
|
||||
}
|
||||
snapshot := cloneXiaodianReport(*report)
|
||||
s.reportMu.Unlock()
|
||||
|
||||
if err := s.saveXiaodianReport(snapshot); err != nil {
|
||||
logger.LogEvent("导出", "warn", "保存和平小店任务报告失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// LatestXiaodianReport 返回最近一次和平小店任务报告的副本。
|
||||
func (s *Scheduler) LatestXiaodianReport() (types.XiaodianRunReport, bool) {
|
||||
s.reportMu.RLock()
|
||||
defer s.reportMu.RUnlock()
|
||||
if s.latestXiaodianReport == nil {
|
||||
return types.XiaodianRunReport{}, false
|
||||
}
|
||||
return cloneXiaodianReport(*s.latestXiaodianReport), true
|
||||
}
|
||||
|
||||
func (s *Scheduler) loadLatestXiaodianReport() {
|
||||
data, err := os.ReadFile(s.xiaodianReportPath())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var report types.XiaodianRunReport
|
||||
if err := json.Unmarshal(data, &report); err != nil || report.ReportID == "" || len(report.Accounts) == 0 {
|
||||
return
|
||||
}
|
||||
s.reportMu.Lock()
|
||||
s.latestXiaodianReport = &report
|
||||
s.reportMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Scheduler) saveXiaodianReport(report types.XiaodianRunReport) error {
|
||||
path := s.xiaodianReportPath()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.MarshalIndent(report, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tempPath := path + ".tmp"
|
||||
if err := os.WriteFile(tempPath, data, 0600); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tempPath, path)
|
||||
}
|
||||
|
||||
func (s *Scheduler) xiaodianReportPath() string {
|
||||
return filepath.Join(s.paths.DataDir, "reports", xiaodianReportFileName)
|
||||
}
|
||||
|
||||
func cloneXiaodianReport(report types.XiaodianRunReport) types.XiaodianRunReport {
|
||||
report.Accounts = append([]types.XiaodianRunReportAccount(nil), report.Accounts...)
|
||||
return report
|
||||
}
|
||||
|
||||
func reportFailureReason(message string) string {
|
||||
if accountStopped, _, reason := classifyXiaodianRetryStop(message); accountStopped {
|
||||
return reason
|
||||
}
|
||||
reason := strings.TrimSpace(strings.TrimPrefix(message, "失败|"))
|
||||
return strings.TrimSpace(strings.TrimPrefix(reason, "和平小店兑换失败:"))
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package seckill
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestReportFailureReasonUsesXiaodianStopReason(t *testing.T) {
|
||||
message := "失败|和平小店兑换失败: 对不起,Livelink登录态校验失败!"
|
||||
if got := reportFailureReason(message); got != "和平小店绑定已失效,需重新绑定" {
|
||||
t.Fatalf("reportFailureReason() = %q", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user