From c38d8810589f2a05d0a3b73cac2fbf95225fb430 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Mon, 15 Aug 2022 20:08:11 +0200 Subject: [PATCH] clean up resolver a bit. --- services/resolver.go | 98 +++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/services/resolver.go b/services/resolver.go index 6ac83f0..de4af63 100644 --- a/services/resolver.go +++ b/services/resolver.go @@ -5,6 +5,7 @@ import ( "git.aiterp.net/lucifer3/server/commands" "git.aiterp.net/lucifer3/server/events" "git.aiterp.net/lucifer3/server/internal/gentools" + "reflect" "strings" ) @@ -46,20 +47,56 @@ func (r *resolver) HandleEvent(_ *lucifer3.EventBus, event lucifer3.Event) { } } +func (r *resolver) rerun(bus *lucifer3.EventBus, resolveList *[]string, command interface{}) { + newList := make([]string, 0, 16) + + for _, id := range *resolveList { + switch { + case strings.HasPrefix(id, "lucifer:tag:"): + tag := id[12:] + if len(newList) == 0 { + newList = append(newList, r.tags[tag]...) + } else { + gentools.AddUniques(&newList, r.tags[tag]...) + } + case strings.HasPrefix(id, "lucifer:name:"): + name := id[13:] + if strings.HasSuffix(name, "*") { + prefix := name[:len(name)-1] + for name, ids := range r.names { + if strings.HasPrefix(name, prefix) { + gentools.AddUniques(&newList, ids...) + } + } + } else if strings.HasPrefix(name, "*") { + suffix := name[1:] + for name, ids := range r.names { + if strings.HasSuffix(name, suffix) { + gentools.AddUniques(&newList, ids...) + } + } + } else { + gentools.AddUniques(&newList, r.names[name]...) + } + } + } + + if len(newList) > 0 { + *resolveList = newList + + bus.RunCommand(reflect.Indirect(reflect.ValueOf(command)).Interface().(lucifer3.Command)) + } +} + func (r *resolver) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command) { - var resolveList []string - var resolveCB func(newIDs []string) lucifer3.Command switch command := command.(type) { case commands.Assign: - resolveList = command.IDs - resolveCB = func(newIDs []string) lucifer3.Command { command.IDs = newIDs; return command } + r.rerun(bus, &command.IDs, &command) case commands.ReplaceScene: - resolveList = command.IDs - resolveCB = func(newIDs []string) lucifer3.Command { command.IDs = newIDs; return command } + r.rerun(bus, &command.IDs, &command) case commands.ClearScene: - resolveList = command.IDs - resolveCB = func(newIDs []string) lucifer3.Command { command.IDs = newIDs; return command } + r.rerun(bus, &command.IDs, &command) case commands.SetName: if !strings.HasPrefix(command.ID, "lucifer:") { for name := range r.names { @@ -88,8 +125,7 @@ func (r *resolver) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Comman } } - resolveList = command.IDs - resolveCB = func(newIDs []string) lucifer3.Command { command.IDs = newIDs; return command } + r.rerun(bus, &command.IDs, &command) case commands.RemoveTag: for _, id := range command.IDs { if strings.HasPrefix(id, "lucifer:") { @@ -104,46 +140,6 @@ func (r *resolver) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Comman } } - resolveList = command.IDs - resolveCB = func(newIDs []string) lucifer3.Command { command.IDs = newIDs; return command } - } - - if resolveList != nil && resolveCB != nil { - newList := make([]string, 0, 16) - - for _, id := range resolveList { - switch { - case strings.HasPrefix(id, "lucifer:tag:"): - tag := id[12:] - if len(newList) == 0 { - newList = append(newList, r.tags[tag]...) - } else { - gentools.AddUniques(&newList, r.tags[tag]...) - } - case strings.HasPrefix(id, "lucifer:name:"): - name := id[13:] - if strings.HasSuffix(name, "*") { - prefix := name[:len(name)-1] - for name, ids := range r.names { - if strings.HasPrefix(name, prefix) { - gentools.AddUniques(&newList, ids...) - } - } - } else if strings.HasPrefix(name, "*") { - suffix := name[1:] - for name, ids := range r.names { - if strings.HasSuffix(name, suffix) { - gentools.AddUniques(&newList, ids...) - } - } - } else { - gentools.AddUniques(&newList, r.names[name]...) - } - } - } - - if len(newList) > 0 { - bus.RunCommand(resolveCB(newList)) - } + r.rerun(bus, &command.IDs, &command) } }