From 2b8ce20bb9f2e5fe0c0072af3d03f19907621213 Mon Sep 17 00:00:00 2001 From: qydysky Date: Mon, 5 Aug 2024 20:29:21 +0800 Subject: [PATCH] =?utf8?q?Improve=20=E7=B2=89=E4=B8=9D=E7=89=8C=E7=82=B9?= =?utf8?q?=E4=BA=AE=E7=AD=96=E7=95=A5=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Reply/F/keepMedalLight/keepMedalLight.go | 103 ++++++----------------- 1 file changed, 28 insertions(+), 75 deletions(-) diff --git a/Reply/F/keepMedalLight/keepMedalLight.go b/Reply/F/keepMedalLight/keepMedalLight.go index 10ae42f..05cb652 100644 --- a/Reply/F/keepMedalLight/keepMedalLight.go +++ b/Reply/F/keepMedalLight/keepMedalLight.go @@ -2,7 +2,6 @@ package keepMedalLight import ( "context" - "errors" "sync/atomic" "time" @@ -10,18 +9,14 @@ import ( p "github.com/qydysky/part" comp "github.com/qydysky/part/component" - reqf "github.com/qydysky/part/reqf" log "github.com/qydysky/part/log" ) var ( - Main = comp.NewComp(main) - rand = p.Rand() - roomI = make(map[int]*room) - skip atomic.Bool - ErrNotFoundRoom = errors.New(`ErrNotFoundRoom`) - ErrNotLight = errors.New(`ErrNotLight`) + Main = comp.NewComp(main) + rand = p.Rand() + skip atomic.Bool ) type Func struct { @@ -32,13 +27,6 @@ type Func struct { PreferDanmu []any } -type room struct { - medalID int - targetID int - danmu int - like int -} - func main(ctx context.Context, ptr Func) (ret any, err error) { if !skip.CompareAndSwap(false, true) { return @@ -54,27 +42,32 @@ func main(ctx context.Context, ptr Func) (ret any, err error) { }() if e, list := ptr.BiliApi.GetFansMedal(0, 0); e != nil { - return nil, e + ptr.Logg.L(`E: `, e) } else { - for _, v := range list { - // 熄灭的徽章只能通过送礼物点亮 - if v.TodayFeed > 0 { - delete(roomI, v.RoomID) - continue - } else if v.IsLighted == 1 { - roomI[v.RoomID] = &room{ - targetID: v.TargetID, - medalID: v.MedalID, + for _, i := range list { + + select { + case <-ctx.Done(): + return + case <-t.C: + } + + if len(ptr.PreferDanmu) > 0 { + if s, ok := ptr.PreferDanmu[rand.MixRandom(0, int64(len(ptr.PreferDanmu)-1))].(string); ok { + if e := ptr.SendDanmu(s, i.RoomID); e != nil { + ptr.Logg.L(`E: `, e) + } } + } else if e := ptr.SendDanmu(`点赞`, i.RoomID); e != nil { + ptr.Logg.L(`E: `, e) } } } - ptr.Logg.L(`I: `, "等待保持点亮数量", len(roomI)) - - for len(roomI) != 0 && err == nil { - // deal roomI - for roomid, v := range roomI { + if e, list := ptr.BiliApi.GetFansMedal(0, 0); e != nil { + ptr.Logg.L(`E: `, e) + } else { + for _, i := range list { select { case <-ctx.Done(): @@ -82,52 +75,12 @@ func main(ctx context.Context, ptr Func) (ret any, err error) { case <-t.C: } - if e, tmp := ptr.BiliApi.GetFansMedal(roomid, v.targetID); e != nil && !reqf.IsTimeout(e) { - err = e - } else if len(tmp) == 0 { - err = ErrNotFoundRoom - } else if tmp[0].TodayFeed > 0 { - delete(roomI, roomid) - ptr.Logg.L(`I: `, roomid, "已获得亲密度", tmp[0].TodayFeed, "剩余", len(roomI)) - continue - } else if tmp[0].LivingStatus == 1 { - if e := ptr.BiliApi.LikeReport(15, ptr.Uid, roomid, v.targetID); e == nil { - v.like += 15 - } else if !reqf.IsTimeout(e) { - err = e - } - } else { - if len(ptr.PreferDanmu) > 0 { - if s, ok := ptr.PreferDanmu[rand.MixRandom(0, int64(len(ptr.PreferDanmu)-1))].(string); ok { - if e := ptr.SendDanmu(s, roomid); e == nil { - v.danmu += 1 - } else if !reqf.IsTimeout(e) { - err = e - } - } - } else if e := ptr.SendDanmu(`点赞`, roomid); e == nil { - v.danmu += 1 - } else if !reqf.IsTimeout(e) { - err = e - } - } - - if v.danmu < 10 && v.like < 50 { - // 发送弹幕:每日首次发送弹幕达10条可获得70亲密度 - // 给主播点赞:每日首次点满50个赞可获得50亲密度 - continue - } else if v.danmu > 25 || v.like > 70 { - delete(roomI, roomid) - ptr.Logg.L(`I: `, roomid, "未获得亲密度") - break - } else { - // 发送了足量弹幕/点赞仍不能获得,尝试发送历史弹幕 - if e, his := ptr.BiliApi.GetHisDanmu(roomid); e != nil { - err = e + if i.IsLighted == 0 { + if e, his := ptr.BiliApi.GetHisDanmu(i.RoomID); e != nil { + ptr.Logg.L(`E: `, e) } else if len(his) > 0 { - v.danmu += 1 - if e := ptr.SendDanmu(his[0], roomid); e != nil { - err = e + if e := ptr.SendDanmu(his[0], i.RoomID); e != nil { + ptr.Logg.L(`E: `, e) } } } -- 2.39.2