From a06412e8334be8b3e073d1bfa07f79e5bafaa17f Mon Sep 17 00:00:00 2001 From: aynakeya Date: Sun, 23 Jun 2024 15:22:57 +0800 Subject: [PATCH] update single user max diange count --- assets/translation.json | 4 ++++ go.mod | 1 + pkg/miaosic | 2 +- plugin/diange/diange.go | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/assets/translation.json b/assets/translation.json index f15e2ef..75716f0 100644 --- a/assets/translation.json +++ b/assets/translation.json @@ -384,6 +384,10 @@ "en": "User", "zh-CN": "普通用户" }, + "plugin.diange.user_max": { + "en": "User Maximum queue", + "zh-CN": "单个用户最大点歌数" + }, "plugin.maxduration.description": { "en": "Set the maximum duration of a song", "zh-CN": "设置歌曲最长能播多久" diff --git a/go.mod b/go.mod index cfe02c8..71510a5 100644 --- a/go.mod +++ b/go.mod @@ -62,6 +62,7 @@ require ( github.com/mattn/go-isatty v0.0.19 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sahilm/fuzzy v0.1.0 // indirect + github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect github.com/spf13/cast v1.5.1 // indirect github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c // indirect github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef // indirect diff --git a/pkg/miaosic b/pkg/miaosic index ceabb30..10ba44e 160000 --- a/pkg/miaosic +++ b/pkg/miaosic @@ -1 +1 @@ -Subproject commit ceabb3009f2f192dcae48f88e53ff53c87114afe +Subproject commit 10ba44e33aae2a6921a1972898d2a4217bc44d09 diff --git a/plugin/diange/diange.go b/plugin/diange/diange.go index 35ff756..fc44c5d 100644 --- a/plugin/diange/diange.go +++ b/plugin/diange/diange.go @@ -18,6 +18,7 @@ import ( "golang.org/x/exp/slices" "sort" "strings" + "sync" "time" ) @@ -35,6 +36,7 @@ type Diange struct { MedalName string MedalPermission int QueueMax int + UserMax int UserCoolDown int CustomCMD string SourceConfigPath string @@ -46,6 +48,7 @@ type Diange struct { sourceConfigs map[string]*sourceConfig blacklist []blacklistItem cooldowns map[string]int + userCount sync.Map panel fyne.CanvasObject log logger.ILogger } @@ -59,6 +62,7 @@ func NewDiange() *Diange { AdminPermission: true, QueueMax: 128, UserCoolDown: -1, + UserMax: 128, CustomCMD: "点歌", SourceConfigPath: "./config/diange.json", BlackListItemPath: "./config/diange_blacklist.json", @@ -92,6 +96,7 @@ func NewDiange() *Diange { }, }, cooldowns: make(map[string]int), + userCount: sync.Map{}, log: global.Logger.WithPrefix("Plugin.Diange"), } return diange @@ -124,6 +129,18 @@ func (d *Diange) Enable() error { "plugin.diange.queue.update", func(event *event.Event) { d.currentQueueLength = len(event.Data.(events.PlaylistDetailUpdateEvent).Medias) + medias := event.Data.(events.PlaylistDetailUpdateEvent).Medias + tmpUserCount := make(map[string]int) + for _, media := range medias { + _, ok := tmpUserCount[media.ToUser().Name] + if !ok { + tmpUserCount[media.ToUser().Name] = 0 + } + tmpUserCount[media.ToUser().Name]++ + } + for user, count := range tmpUserCount { + d.userCount.Store(user, count) + } }) global.EventManager.RegisterA( events.PlayerPlayingUpdate, @@ -186,6 +203,18 @@ func (d *Diange) handleMessage(event *event.Event) { return } + //check user max + if d.UserMax > 0 { + userCount, ok := d.userCount.Load(message.User.Username) + if !ok { + userCount = 0 + } + if userCount.(int) >= d.UserMax { + d.log.Infof("User %s(%s) exceed max diange count, ignore", message.User.Username, message.User.Uid) + return + } + } + // if in user cool down, return ct := int(time.Now().Unix()) if (ct - d.cooldowns[message.User.Uid]) <= d.UserCoolDown { @@ -326,6 +355,10 @@ func (d *Diange) CreatePanel() fyne.CanvasObject { widget.NewLabel(i18n.T("plugin.diange.queue_max")), nil, widget.NewEntryWithData(binding.IntToString(binding.BindInt(&d.QueueMax))), ) + dgUserMax := container.NewBorder(nil, nil, + widget.NewLabel(i18n.T("plugin.diange.user_max")), nil, + widget.NewEntryWithData(binding.IntToString(binding.BindInt(&d.UserMax))), + ) dgCoolDown := container.NewBorder(nil, nil, widget.NewLabel(i18n.T("plugin.diange.cooldown")), nil, widget.NewEntryWithData(binding.IntToString(binding.BindInt(&d.UserCoolDown))), @@ -355,6 +388,6 @@ func (d *Diange) CreatePanel() fyne.CanvasObject { dgSourceCMD := container.NewBorder( nil, nil, widget.NewLabel(i18n.T("plugin.diange.source_cmd")), nil, container.NewVBox(sourceCfgs...)) - d.panel = container.NewVBox(dgPerm, dgMdPerm, dgQueue, dgCoolDown, dgShortCut, skipPlaylist, dgSourceCMD) + d.panel = container.NewVBox(dgPerm, dgMdPerm, dgQueue, dgUserMax, dgCoolDown, dgShortCut, skipPlaylist, dgSourceCMD) return d.panel }