GUI 日志落盘,修复同一奖品重复兑换与监控结束竞态
- 日志落盘:新增 internal/gui/logfile.go,写到 <用户配置目录>/huya-go/logs/huya-go.log,单文件 5MB 后滚动保留 5 个备份;「日志」页签顶部显示路径并支持打开目录/复制路径;写盘失败自动降级为仅界面输出 - 修复重复兑换:界面选品会同时提交 pid、名称与 targetText 里的同一 pid,normalizeTargets 现按 pid/名称去重;runState 再按「账号+奖品」记账,多个 target 命中同一奖品时只兑换一次(新增 skip_duplicate 结果) - 修复 use of closed network connection:成功后的异步刷新改为 runState.refresh 等待,RunTargets 返回前不再提前关闭连接 - queryHttpDns 增加 single-flight 与失败冷却,多账号并发建连只解析一次 - 新增 internal/gui/logfile_test.go 与 sniper/huya 回归测试;README 补日志说明
This commit is contained in:
@@ -91,6 +91,10 @@ type runState struct {
|
||||
mu sync.Mutex
|
||||
accounts map[int64][]targetState
|
||||
targetDone []bool
|
||||
// exchanged 记录每个账号已成功兑换的奖品,避免多个 target 命中同一奖品时重复兑换。
|
||||
exchanged map[int64]map[int64]bool
|
||||
// refresh 用于等待成功后的异步刷新,确保 RunTargets 返回前不会提前释放连接。
|
||||
refresh sync.WaitGroup
|
||||
}
|
||||
|
||||
type targetState struct {
|
||||
@@ -128,6 +132,8 @@ func (r *Runner) RunTargets(ctx context.Context, accounts []Account, targets []T
|
||||
}
|
||||
|
||||
state := newRunState(accounts, len(targets))
|
||||
// 成功后的刷新是异步的,等它们结束再返回,避免调用方提前关闭连接。
|
||||
defer state.refresh.Wait()
|
||||
for loop := 1; loop <= cfg.Loops; loop++ {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
@@ -223,12 +229,20 @@ func (r *Runner) runAccountLoop(ctx context.Context, client huya.ActivityClient,
|
||||
for _, index := range active {
|
||||
target := targets[index]
|
||||
if shouldDirectExchange(target, cfg) {
|
||||
// 直兑路径没有奖品列表,用 target.PID 判断是否已经兑换过同一奖品。
|
||||
if state.prizeExchanged(account.UID, int64(target.PID)) {
|
||||
emitSkippedDuplicate(emit, loop, account.UID, index, target)
|
||||
continue
|
||||
}
|
||||
result := r.exchangeTarget(ctx, client, account, target, index, loop, cfg.SID, nil)
|
||||
result = r.applyExchangeDecision(state, account, index, cfg, result, cancel)
|
||||
if result.Category == CategorySuccess {
|
||||
state.markPrizeExchanged(account.UID, int64(target.PID))
|
||||
}
|
||||
if emit != nil {
|
||||
emit(result)
|
||||
}
|
||||
r.refreshAfterSuccess(client, account, target, index, loop, cfg, result, emit)
|
||||
r.refreshAfterSuccess(state, client, account, target, index, loop, cfg, result, emit)
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
@@ -289,18 +303,42 @@ func (r *Runner) runAccountLoop(ctx context.Context, client huya.ActivityClient,
|
||||
}
|
||||
continue
|
||||
}
|
||||
// 多个 target 可能命中同一个奖品(例如按不同名称匹配),已成功兑换过就跳过。
|
||||
if state.prizeExchanged(account.UID, prize.ID) {
|
||||
emitSkippedDuplicate(emit, loop, account.UID, index, target)
|
||||
continue
|
||||
}
|
||||
result := r.exchangeTarget(ctx, client, account, target, index, loop, cfg.SID, prize)
|
||||
result = r.applyExchangeDecision(state, account, index, cfg, result, cancel)
|
||||
if result.Category == CategorySuccess {
|
||||
state.markPrizeExchanged(account.UID, prize.ID)
|
||||
}
|
||||
if emit != nil {
|
||||
emit(result)
|
||||
}
|
||||
r.refreshAfterSuccess(client, account, target, index, loop, cfg, result, emit)
|
||||
r.refreshAfterSuccess(state, client, account, target, index, loop, cfg, result, emit)
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// emitSkippedDuplicate 在跳过同一奖品的重复兑换时给出明确结果,便于界面观察。
|
||||
func emitSkippedDuplicate(emit func(Result), loop int, uid int64, index int, target Target) {
|
||||
if emit == nil {
|
||||
return
|
||||
}
|
||||
emit(Result{
|
||||
Loop: loop,
|
||||
UID: uid,
|
||||
TargetIndex: index,
|
||||
Target: target,
|
||||
Action: "skip_duplicate",
|
||||
Category: CategoryAlreadyDone,
|
||||
Message: "该奖品本账号已兑换成功,跳过重复兑换",
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Runner) exchangeTarget(ctx context.Context, client huya.ActivityClient, account Account, target Target, index int, loop int, sid int32, prize *huya.PrizeInfo) Result {
|
||||
pid := target.PID
|
||||
if prize != nil {
|
||||
@@ -365,11 +403,13 @@ func (r *Runner) applyExchangeDecision(state *runState, account Account, index i
|
||||
return result
|
||||
}
|
||||
|
||||
func (r *Runner) refreshAfterSuccess(client huya.ActivityClient, account Account, target Target, index int, loop int, cfg Config, result Result, emit func(Result)) {
|
||||
func (r *Runner) refreshAfterSuccess(state *runState, client huya.ActivityClient, account Account, target Target, index int, loop int, cfg Config, result Result, emit func(Result)) {
|
||||
if !cfg.RefreshAfterSuccess || emit == nil || result.Category != CategorySuccess {
|
||||
return
|
||||
}
|
||||
state.refresh.Add(1)
|
||||
go func() {
|
||||
defer state.refresh.Done()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
@@ -447,6 +487,7 @@ func newRunState(accounts []Account, targetCount int) *runState {
|
||||
state := &runState{
|
||||
accounts: make(map[int64][]targetState, len(accounts)),
|
||||
targetDone: make([]bool, targetCount),
|
||||
exchanged: make(map[int64]map[int64]bool, len(accounts)),
|
||||
}
|
||||
for _, account := range accounts {
|
||||
state.accounts[account.UID] = make([]targetState, targetCount)
|
||||
@@ -454,6 +495,28 @@ func newRunState(accounts []Account, targetCount int) *runState {
|
||||
return state
|
||||
}
|
||||
|
||||
// prizeExchanged 判断该账号是否已经成功兑换过这个奖品。
|
||||
func (s *runState) prizeExchanged(uid, prizeID int64) bool {
|
||||
if prizeID <= 0 {
|
||||
return false
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.exchanged[uid][prizeID]
|
||||
}
|
||||
|
||||
func (s *runState) markPrizeExchanged(uid, prizeID int64) {
|
||||
if prizeID <= 0 {
|
||||
return
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.exchanged[uid] == nil {
|
||||
s.exchanged[uid] = map[int64]bool{}
|
||||
}
|
||||
s.exchanged[uid][prizeID] = true
|
||||
}
|
||||
|
||||
func (s *runState) activeTargets(uid int64, targetCount int, now time.Time) []int {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
@@ -545,19 +608,37 @@ func normalizeConfig(cfg Config) Config {
|
||||
return cfg
|
||||
}
|
||||
|
||||
// normalizeTargets 去掉空目标与重复目标。
|
||||
//
|
||||
// 除了完全相同的 target,还按 pid / 名称去重:界面选品会同时提交 pid、名称,
|
||||
// 以及 targetText 里的同一个 pid,不去重就会对同一奖品重复兑换(实测踩过)。
|
||||
func normalizeTargets(targets []Target) []Target {
|
||||
out := make([]Target, 0, len(targets))
|
||||
seen := map[string]bool{}
|
||||
seenKey := map[string]bool{}
|
||||
seenPID := map[int32]bool{}
|
||||
seenName := map[string]bool{}
|
||||
for _, target := range targets {
|
||||
target.Name = strings.TrimSpace(target.Name)
|
||||
if target.PID <= 0 && target.Name == "" && !target.Auto {
|
||||
continue
|
||||
}
|
||||
key := targetKey(target)
|
||||
if seen[key] {
|
||||
if target.PID > 0 && seenPID[target.PID] {
|
||||
continue
|
||||
}
|
||||
seen[key] = true
|
||||
if target.Name != "" && seenName[target.Name] {
|
||||
continue
|
||||
}
|
||||
key := targetKey(target)
|
||||
if seenKey[key] {
|
||||
continue
|
||||
}
|
||||
seenKey[key] = true
|
||||
if target.PID > 0 {
|
||||
seenPID[target.PID] = true
|
||||
}
|
||||
if target.Name != "" {
|
||||
seenName[target.Name] = true
|
||||
}
|
||||
out = append(out, target)
|
||||
}
|
||||
return out
|
||||
|
||||
@@ -317,3 +317,96 @@ func TestRunTargetsConditionGateKeepsRetrying(t *testing.T) {
|
||||
t.Fatalf("exchangeCount=%d, want 3", exchangeCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeTargetsDropsDuplicatePrizeTargets(t *testing.T) {
|
||||
targets := normalizeTargets([]Target{
|
||||
{PID: 13409, Name: "幸运币礼包(小)"},
|
||||
{PID: 13409},
|
||||
{Name: "幸运币礼包(小)"},
|
||||
{PID: 12889, Name: "套装-潮萌小黄鸭"},
|
||||
})
|
||||
if len(targets) != 2 {
|
||||
t.Fatalf("targets=%d, want 2: %+v", len(targets), targets)
|
||||
}
|
||||
if targets[0].PID != 13409 || targets[1].PID != 12889 {
|
||||
t.Fatalf("unexpected targets: %+v", targets)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunTargetsExchangesPrizeOnceWhenTargetsDuplicated(t *testing.T) {
|
||||
client := &fakeActivityClient{}
|
||||
runner := Runner{Client: client}
|
||||
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{
|
||||
{PID: 13409, Name: "幸运币礼包(小)"},
|
||||
{PID: 13409},
|
||||
}, Config{
|
||||
SID: 2277,
|
||||
Loops: 3,
|
||||
Delay: time.Millisecond,
|
||||
StopAccountAfterSuccess: true,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, exchangeCount, _ := client.counts()
|
||||
if exchangeCount != 1 {
|
||||
t.Fatalf("exchangeCount=%d, want 1(同一奖品不应重复兑换)", exchangeCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunTargetsWaitsForRefreshBeforeReturn(t *testing.T) {
|
||||
client := &fakeActivityClient{}
|
||||
runner := Runner{Client: client}
|
||||
|
||||
var mu sync.Mutex
|
||||
seen := map[string]bool{}
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{{PID: 13409}}, Config{
|
||||
SID: 2277,
|
||||
Loops: 1,
|
||||
StopAccountAfterSuccess: true,
|
||||
RefreshAfterSuccess: true,
|
||||
}, func(result Result) {
|
||||
mu.Lock()
|
||||
seen[result.Action] = true
|
||||
mu.Unlock()
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if !seen["refresh_score"] || !seen["refresh_records"] {
|
||||
t.Fatalf("RunTargets 返回时异步刷新还没结束: %v", seen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunTargetsDoesNotExchangeSamePrizeTwiceViaNameMatch(t *testing.T) {
|
||||
client := &fakeActivityClient{
|
||||
listResp: &huya.GetActPrizeListResp{
|
||||
Status: 200,
|
||||
Prizes: []huya.PrizeInfo{{ID: 13409, Name: "幸运币礼包(小)", NewScore: 20}},
|
||||
},
|
||||
}
|
||||
runner := Runner{Client: client}
|
||||
|
||||
// 两个不同名称的 target 都会匹配到同一个奖品。
|
||||
err := runner.RunTargets(context.Background(), []Account{{UID: 1, Cookie: "yyuid=1"}}, []Target{
|
||||
{Name: "幸运币"},
|
||||
{Name: "礼包"},
|
||||
}, Config{
|
||||
SID: 2277,
|
||||
Loops: 2,
|
||||
Delay: time.Millisecond,
|
||||
DirectExchange: false,
|
||||
StopAccountAfterSuccess: true,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, exchangeCount, _ := client.counts()
|
||||
if exchangeCount != 1 {
|
||||
t.Fatalf("exchangeCount=%d, want 1(同一奖品不应重复兑换)", exchangeCount)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user