From d1558cf352f24c311b8505661143973d11d9edba Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Fri, 21 Jul 2023 15:56:34 +0200 Subject: backend: support an incremental time bonus --- TODO.md | 1 + config.go | 1 + main.go | 2 ++ playlist.go | 8 ++++++++ wrms.go | 7 ++++++- 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 38baf55..2448bf4 100644 --- a/TODO.md +++ b/TODO.md @@ -18,6 +18,7 @@ - [X] support incremental search results - [X] distinguish different search queries - [X] do not apply obsolete events +- [ ] support timeBonus ## Web Backend diff --git a/config.go b/config.go index 2483079..32b94ca 100644 --- a/config.go +++ b/config.go @@ -22,6 +22,7 @@ type Config struct { Admins []uuid.UUID `yaml:"admins"` Spotify *SpotifyConfig `yaml:"spotify"` HasUpload bool + timeBonus float64 `yaml:time-bonus` } func defaultConfig() Config { diff --git a/main.go b/main.go index ec0b20a..a5cc794 100644 --- a/main.go +++ b/main.go @@ -268,6 +268,7 @@ func main() { flag.StringVar(&config.LogLevel, "loglevel", config.LogLevel, "log level") flag.IntVar(&config.Port, "port", config.Port, "port to listen to") + flag.Float64Var(&config.timeBonus, "time-bonus", config.timeBonus, "weight bonus granted over time") backends := flag.String("backends", "", "music backend to use") flag.StringVar(&config.LocalMusicDir, "serve-music-dir", config.LocalMusicDir, "local music directory to serve") @@ -281,6 +282,7 @@ func main() { llog.SetLogLevelFromString(config.LogLevel) config.HasUpload = slices.Contains(config.Backends, "upload") + llog.Info("%v", config) wrms = NewWrms(config) setupRoutes() diff --git a/playlist.go b/playlist.go index 1fbd344..aeb1297 100644 --- a/playlist.go +++ b/playlist.go @@ -74,3 +74,11 @@ func (pl *Playlist) OrderedList() []*Song { return songs } + +func (pl *Playlist) applyTimeBonus(timeBonus float64) { + for _, s := range *pl { + s.Weight += timeBonus + } + heap.Init(pl) + llog.DDebug("applying time bonus to the playlist") +} diff --git a/wrms.go b/wrms.go index a345767..1f1332e 100644 --- a/wrms.go +++ b/wrms.go @@ -18,7 +18,7 @@ type Song struct { Artist string `json:"artist"` Source string `json:"source"` Uri string `json:"uri"` - Weight int `json:"weight"` + Weight float64 `json:"weight"` Album string `json:"album"` Year int `json:"year"` index int `json:"-"` // used by heap.Interface @@ -296,6 +296,11 @@ func (wrms *Wrms) AddSong(song Song) { s := &wrms.Songs[len(wrms.Songs)-1] wrms.queue.Add(s) + if wrms.Config.timeBonus != 0 { + llog.Info("Apply time bonus %v", wrms.Config.timeBonus) + wrms.queue.applyTimeBonus(wrms.Config.timeBonus) + } + wrms.rwlock.Unlock() llog.Info("Added song %s (ptr=%p) to Songs", s.Uri, s) -- cgit v1.2.3