TempListener Removal is working now.

This commit is contained in:
Brian Buller 2014-12-23 11:53:04 -06:00
parent 5e2e0bdcaa
commit 01f9aaa035
2 changed files with 32 additions and 27 deletions

View File

@ -30,7 +30,8 @@ func LoadConfig(mm *MessageManager) {
o, _ := j.GetObjectArray("options") o, _ := j.GetObjectArray("options")
// Add the "Stop" listener // Add the "Stop" listener
mm.listeners = append(mm.listeners, func(i *Message) bool { AddListener(func(i *Message) bool {
//listeners = append(mm.listeners, func(i *Message) bool {
if i.User.IsOp && i.Text == "!stop\n" { if i.User.IsOp && i.Text == "!stop\n" {
mm.Output("stop") mm.Output("stop")
return true return true
@ -45,9 +46,11 @@ func LoadConfig(mm *MessageManager) {
c.FeatureTPHome = opt_enabled c.FeatureTPHome = opt_enabled
// Add the listener for "!set home" // Add the listener for "!set home"
// Add !set home listener // Add !set home listener
mm.listeners = append(mm.listeners, func(i *Message) bool { AddListener(func(i *Message) bool {
// mm.listeners = append(mm.listeners, func(i *Message) bool {
if i.User.Name != "" && i.Text == "!set home\n" { if i.User.Name != "" && i.Text == "!set home\n" {
mm.tempListeners = append(mm.tempListeners, func(inp *Message) bool { AddTempListener(func(inp *Message) bool {
//mm.tempListeners = append(mm.tempListeners, func(inp *Message) bool {
listen_for := "Teleported " + i.User.Name + " to " listen_for := "Teleported " + i.User.Name + " to "
if inp.User.Name == "" && strings.Contains(inp.Text, listen_for) { if inp.User.Name == "" && strings.Contains(inp.Text, listen_for) {
// Found the text // Found the text
@ -68,7 +71,8 @@ func LoadConfig(mm *MessageManager) {
return false return false
}) })
// Add !home listener // Add !home listener
mm.listeners = append(mm.listeners, func(i *Message) bool { AddListener(func(i *Message) bool {
//mm.listeners = append(mm.listeners, func(i *Message) bool {
if i.User.Name != "" && i.Text == "!home\n" { if i.User.Name != "" && i.Text == "!home\n" {
home_str, found := GetHome(i.User.Name) home_str, found := GetHome(i.User.Name)
if found { if found {

View File

@ -31,6 +31,9 @@ type MessageManager struct {
output io.WriteCloser output io.WriteCloser
} }
var Listeners []func(inp *Message) bool
var TempListeners []func(inp *Message) bool
func (mm MessageManager) Output(o string) { func (mm MessageManager) Output(o string) {
if !strings.HasSuffix("\n", o) { if !strings.HasSuffix("\n", o) {
o = o + "\n" o = o + "\n"
@ -41,25 +44,23 @@ func (mm MessageManager) Output(o string) {
func (mm MessageManager) ProcessMessage(inp string) bool { func (mm MessageManager) ProcessMessage(inp string) bool {
// First of all, create the message from inp // First of all, create the message from inp
m := NewMessage(inp) m := NewMessage(inp)
tempListeners := mm.tempListeners fmt.Printf("[%d]l, [%d]tmpl: Processing Message: >>> %s\n", len(Listeners), len(TempListeners), strings.Replace(inp, "\n", "", -1))
listeners := mm.listeners
fmt.Printf("[%d]l, [%d]tmpl: Processing Message: >>> %s\n", len(listeners), len(tempListeners), strings.Replace(inp, "\n", "", -1))
// Now run the message through all of mm's tempListeners // Now run the message through all of mm's tempListeners
for i := range tempListeners { for i := range TempListeners {
fmt.Printf("Processing message in tempListener %d\n", i) fmt.Printf("Processing message in tempListener %d\n", i)
// Pop the listener off of the stack // Pop the listener off of the stack
consumed := tempListeners[i](m) consumed := TempListeners[i](m)
if consumed { if consumed {
// When a temp listener is consumed, we delete it // When a temp listener is consumed, we delete it
fmt.Println("Consumed Event from Temporary Listener.") fmt.Println("Consumed Event from Temporary Listener.")
mm.RemoveTempListener(i) RemoveTempListener(i)
return true return true
} }
} }
// and run the message through all of mm's listeners // and run the message through all of mm's listeners
for i := range listeners { for i := range Listeners {
fmt.Printf("Processing message in listener %d\n", i) fmt.Printf("Processing message in listener %d\n", i)
consumed := listeners[i](m) consumed := Listeners[i](m)
if consumed { if consumed {
return true return true
} }
@ -68,25 +69,26 @@ func (mm MessageManager) ProcessMessage(inp string) bool {
return false return false
} }
func (mm MessageManager) AddListener(l func(*Message) bool) { func AddListener(l func(*Message) bool) {
mm.listeners = append(mm.listeners, l) Listeners = append(Listeners, l)
} }
func (mm MessageManager) AddTempListener(l func(*Message) bool) { func AddTempListener(l func(*Message) bool) {
mm.tempListeners = append(mm.tempListeners, l) TempListeners = append(TempListeners, l)
} }
func (mm MessageManager) RemoveListener(i int) { func RemoveListener(i int) {
if i > -1 && i < len(mm.listeners) { if i > -1 && i < len(Listeners) {
mm.listeners = append(mm.listeners[:i], mm.listeners[i+1:]...) t := append(Listeners[:i], Listeners[i+1:]...)
Listeners = make(([]func(*Message) bool), len(t))
copy(LIsteners, t)
} }
} }
func (mm MessageManager) RemoveTempListener(i int) { func RemoveTempListener(i int) {
if i > -1 && i < len(mm.tempListeners) { if i > -1 && i < len(TempListeners) {
fmt.Printf("Removing Temp Listener %d\n", i) t := append(TempListeners[:i], TempListeners[i+1:]...)
fmt.Printf("Before: %d\n", len(mm.tempListeners)) TempListeners = make(([]func(*Message) bool), len(t))
mm.tempListeners = append(mm.tempListeners[:i], mm.tempListeners[i+1:]...) copy(TempListeners, t)
fmt.Printf("After: %d\n", len(mm.tempListeners))
} }
} }
@ -94,8 +96,7 @@ func NewManager(o io.WriteCloser) MessageManager {
mm := MessageManager{ mm := MessageManager{
// listeners: make(([]func(*Message) bool), 0, 10), // listeners: make(([]func(*Message) bool), 0, 10),
// tempListeners: make(([]func(*Message) bool), 0, 10), // tempListeners: make(([]func(*Message) bool), 0, 10),
finishedTempListener: -1, output: o,
output: o,
} }
return mm return mm
} }