Trying to figure out this slice issue...
This commit is contained in:
		@@ -22,24 +22,69 @@ type Config struct {
 | 
			
		||||
 | 
			
		||||
var c *Config
 | 
			
		||||
 | 
			
		||||
func LoadConfig() {
 | 
			
		||||
func LoadConfig(mm *MessageManager) {
 | 
			
		||||
	c = new(Config)
 | 
			
		||||
	config_str, err := ioutil.ReadFile("mc_man.config")
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		j, _ := jason.NewObjectFromBytes(config_str)
 | 
			
		||||
		o, _ := j.GetObjectArray("options")
 | 
			
		||||
 | 
			
		||||
		// Add the "Stop" listener
 | 
			
		||||
		mm.listeners = append(mm.listeners, func(i *Message) bool {
 | 
			
		||||
			if i.User.IsOp && i.Text == "!stop\n" {
 | 
			
		||||
				mm.Output("stop")
 | 
			
		||||
				return true
 | 
			
		||||
			}
 | 
			
		||||
			return false
 | 
			
		||||
		})
 | 
			
		||||
 | 
			
		||||
		for _, option := range o {
 | 
			
		||||
			opt_name, _ := option.GetString("name")
 | 
			
		||||
			opt_enabled, _ := option.GetBoolean("enabled")
 | 
			
		||||
			if opt_name == "home" {
 | 
			
		||||
				c.FeatureTPHome = opt_enabled
 | 
			
		||||
				// Add the listener for "!set home"
 | 
			
		||||
				// Add !set home listener
 | 
			
		||||
				mm.listeners = append(mm.listeners, func(i *Message) bool {
 | 
			
		||||
					if i.User.Name != "" && i.Text == "!set home\n" {
 | 
			
		||||
						mm.tempListeners = append(mm.tempListeners, func(inp *Message) bool {
 | 
			
		||||
							listen_for := "Teleported " + i.User.Name + " to "
 | 
			
		||||
							if inp.User.Name == "" && strings.Contains(inp.Text, listen_for) {
 | 
			
		||||
								// Found the text
 | 
			
		||||
								r := strings.Split(inp.Text, listen_for)
 | 
			
		||||
								if len(r) > 0 {
 | 
			
		||||
									p_str := r[1]
 | 
			
		||||
									p_str = strings.Replace(p_str, ",", "", -1)
 | 
			
		||||
									SetHome(i.User.Name, p_str)
 | 
			
		||||
									mm.Output("tell " + i.User.Name + " Set your home to " + p_str)
 | 
			
		||||
									return true
 | 
			
		||||
								}
 | 
			
		||||
							}
 | 
			
		||||
							return false
 | 
			
		||||
						})
 | 
			
		||||
						mm.Output("tp " + i.User.Name + " ~ ~ ~")
 | 
			
		||||
						return true
 | 
			
		||||
					}
 | 
			
		||||
					return false
 | 
			
		||||
				})
 | 
			
		||||
				// Add !home listener
 | 
			
		||||
				mm.listeners = append(mm.listeners, func(i *Message) bool {
 | 
			
		||||
					if i.User.Name != "" && i.Text == "!home\n" {
 | 
			
		||||
						home_str, found := GetHome(i.User.Name)
 | 
			
		||||
						if found {
 | 
			
		||||
							mm.Output("tp " + i.User.Name + " " + home_str)
 | 
			
		||||
						} else {
 | 
			
		||||
							mm.Output("tell " + i.User.Name + " I don't know where your home is. Set it to your current position by typing '!set home'")
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
					return false
 | 
			
		||||
				})
 | 
			
		||||
			} else if opt_name == "visit" {
 | 
			
		||||
				c.FeatureTPVisit = opt_enabled
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		c.Users = make([]*User, 1)
 | 
			
		||||
		c.Users = make([]*User, 0, 10)
 | 
			
		||||
		u, _ := j.GetObjectArray("users")
 | 
			
		||||
		for _, user := range u {
 | 
			
		||||
			user_name, err := user.GetString("name")
 | 
			
		||||
@@ -54,7 +99,7 @@ func LoadConfig() {
 | 
			
		||||
				c.U = *us
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Printf("Loaded %d Users", len(c.Users))
 | 
			
		||||
		fmt.Printf("Loaded %d Users\n", len(c.Users))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,15 +1,12 @@
 | 
			
		||||
package util
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type MessageManager struct {
 | 
			
		||||
	/* Process takes a string and returns whether
 | 
			
		||||
	 * we did anything with that string or not
 | 
			
		||||
	 */
 | 
			
		||||
	Process func(inp string) bool
 | 
			
		||||
 | 
			
		||||
	/* listeners is an array of functions that
 | 
			
		||||
	 * tell the manager how to listen for specific
 | 
			
		||||
	 * text and what to do if we receive it.
 | 
			
		||||
@@ -17,25 +14,88 @@ type MessageManager struct {
 | 
			
		||||
	 * "consumed" (i.e. - Don't send to any more
 | 
			
		||||
	 * listeners)
 | 
			
		||||
	 */
 | 
			
		||||
	listeners []func(inp string) bool
 | 
			
		||||
	listeners []func(inp *Message) bool
 | 
			
		||||
 | 
			
		||||
	/* tempListeners is an array of functions that
 | 
			
		||||
	 * work the same as 'listeners', but these are
 | 
			
		||||
	 * just temporary and higher priority than
 | 
			
		||||
	 * 'listeners'
 | 
			
		||||
	 */
 | 
			
		||||
	tempListeners []func(inp string) bool
 | 
			
		||||
	tempListeners []func(inp *Message) bool
 | 
			
		||||
 | 
			
		||||
	/* finishedTempListener is an index of a
 | 
			
		||||
	 * tempListener to be removed
 | 
			
		||||
	 */
 | 
			
		||||
	finishedTempListener int
 | 
			
		||||
 | 
			
		||||
	output io.WriteCloser
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewManager() *MessageManager {
 | 
			
		||||
	mm := new(MessageManager)
 | 
			
		||||
	mm.Process = func(inp string) bool {
 | 
			
		||||
		// TODO: send to temp listeners
 | 
			
		||||
		for i, listener := range listeners {
 | 
			
		||||
			if listener(inp) {
 | 
			
		||||
				return true
 | 
			
		||||
			}
 | 
			
		||||
func (mm MessageManager) Output(o string) {
 | 
			
		||||
	if !strings.HasSuffix("\n", o) {
 | 
			
		||||
		o = o + "\n"
 | 
			
		||||
	}
 | 
			
		||||
	mm.output.Write([]byte(o))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (mm MessageManager) ProcessMessage(inp string) bool {
 | 
			
		||||
	// First of all, create the message from inp
 | 
			
		||||
	m := NewMessage(inp)
 | 
			
		||||
	tempListeners := mm.tempListeners
 | 
			
		||||
	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
 | 
			
		||||
	for i := range tempListeners {
 | 
			
		||||
		fmt.Printf("Processing message in tempListener %d\n", i)
 | 
			
		||||
		// Pop the listener off of the stack
 | 
			
		||||
		consumed := tempListeners[i](m)
 | 
			
		||||
		if consumed {
 | 
			
		||||
			// When a temp listener is consumed, we delete it
 | 
			
		||||
			fmt.Println("Consumed Event from Temporary Listener.")
 | 
			
		||||
			mm.finishedTempListener = i
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	// and run the message through all of mm's listeners
 | 
			
		||||
	for i := range listeners {
 | 
			
		||||
		fmt.Printf("Processing message in listener %d\n", i)
 | 
			
		||||
		consumed := listeners[i](m)
 | 
			
		||||
		if consumed {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	// Message not consumed, return false
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (mm MessageManager) AddListener(l func(*Message) bool) {
 | 
			
		||||
	mm.listeners = append(mm.listeners, l)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (mm MessageManager) AddTempListener(l func(*Message) bool) {
 | 
			
		||||
	mm.tempListeners = append(mm.tempListeners, l)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (mm MessageManager) RemoveListener(i int) {
 | 
			
		||||
	if i > -1 && i < len(mm.listeners) {
 | 
			
		||||
		mm.listeners = append(mm.listeners[:i], mm.listeners[i+1:]...)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
func (mm MessageManager) RemoveTempListener(i int) {
 | 
			
		||||
	if i > -1 && i < len(mm.tempListeners) {
 | 
			
		||||
		fmt.Printf("Removing Temp Listener %d\n", i)
 | 
			
		||||
		fmt.Printf("Before: %d\n", len(mm.tempListeners))
 | 
			
		||||
		mm.tempListeners = append(mm.tempListeners[:i], mm.tempListeners[i+1:]...)
 | 
			
		||||
		fmt.Printf("After: %d\n", len(mm.tempListeners))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewManager(o io.WriteCloser) MessageManager {
 | 
			
		||||
	mm := MessageManager{
 | 
			
		||||
		//		listeners:     make(([]func(*Message) bool), 0, 10),
 | 
			
		||||
		//		tempListeners: make(([]func(*Message) bool), 0, 10),
 | 
			
		||||
		finishedTempListener: -1,
 | 
			
		||||
		output:               o,
 | 
			
		||||
	}
 | 
			
		||||
	return mm
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user