Skipping now working(?)

This commit is contained in:
Deluan 2016-03-28 17:51:10 -04:00
parent 1e17efc729
commit c8184da0ed
2 changed files with 41 additions and 16 deletions

View File

@ -11,7 +11,7 @@ import (
)
const (
minSkipped = time.Duration(3) * time.Second
minSkipped = time.Duration(5) * time.Second
maxSkipped = time.Duration(20) * time.Second
)
@ -31,22 +31,37 @@ type scrobbler struct {
}
func (s *scrobbler) detectSkipped(playerId int, trackId string) {
for {
size, _ := s.npRepo.Count(playerId)
np, err := s.npRepo.Tail(playerId)
if err != nil || np == nil || (size == 1 && np.TrackId != trackId) {
break
size, _ := s.npRepo.Count(playerId)
switch size {
case 0:
return
case 1:
np, _ := s.npRepo.Tail(playerId)
if np.TrackId != trackId {
return
}
s.npRepo.Dequeue(playerId)
if np.TrackId == trackId {
break
}
err = s.itunes.MarkAsSkipped(np.TrackId, np.Start.Add(time.Duration(1)*time.Minute))
if err != nil {
beego.Warn("Error skipping track", np.TrackId)
} else {
beego.Debug("Skipped track", np.TrackId)
default:
prev, _ := s.npRepo.Dequeue(playerId)
for {
if prev.TrackId == trackId {
break
}
np, err := s.npRepo.Dequeue(playerId)
if np == nil || err != nil {
break
}
diff := np.Start.Sub(prev.Start)
if diff < minSkipped || diff > maxSkipped {
prev = np
continue
}
err = s.itunes.MarkAsSkipped(prev.TrackId, prev.Start.Add(time.Duration(1)*time.Minute))
if err != nil {
beego.Warn("Error skipping track", prev.TrackId)
} else {
beego.Debug("Skipped track", prev.TrackId)
}
}
}
}

View File

@ -109,7 +109,17 @@ func TestSkipping(t *testing.T) {
So(err, ShouldBeNil)
})
})
SkipConvey("And I skip it after 20 seconds", func() {
Convey("And I skip it after 5 seconds", func() {
npRepo.OverrideNow(start.Add(time.Duration(3) * time.Second))
scrobbler.NowPlaying(1, "DSub", "2", "deluan")
Convey("Then the first song should be marked as skipped", func() {
mf, err := scrobbler.Register(1, "2", start.Add(time.Duration(3)*time.Minute))
So(mf.Id, ShouldEqual, "2")
So(itCtrl.skipped, ShouldBeEmpty)
So(err, ShouldBeNil)
})
})
Convey("And I skip it after 20 seconds", func() {
npRepo.OverrideNow(start.Add(time.Duration(30) * time.Second))
scrobbler.NowPlaying(1, "DSub", "2", "deluan")
Convey("Then the first song should be marked as skipped", func() {