Browse Source

clean up resolver a bit.

beelzebub
Gisle Aune 2 years ago
parent
commit
c38d881058
  1. 98
      services/resolver.go

98
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)
}
}
Loading…
Cancel
Save