Trying to figure out this slice issue...

This commit is contained in:
Brian Buller 2014-12-23 11:16:37 -06:00
parent 6cc194a5d1
commit 581dc01338
3 changed files with 128 additions and 70 deletions

View File

@ -6,13 +6,10 @@ import (
"log"
"os"
"os/exec"
"strings"
//"strings"
)
func main() {
// Load the Config
util.LoadConfig()
xmxVal := "1024M"
xmsVal := "1024M"
args := os.Args[1:]
@ -72,8 +69,9 @@ func main() {
close(ch)
}()
listen_for := ""
listen_for_user := ""
// Load the Config
mm := util.NewManager(stdin)
util.LoadConfig(&mm)
// The forever loop to monitor the channel
loop:
@ -83,52 +81,7 @@ loop:
break loop
}
m := util.NewMessage(s)
// First check for anything we're listening for
if listen_for != "" && strings.Contains(m.Text, listen_for) {
r := strings.Split(m.Text, listen_for)
if len(r) > 0 {
p_str := r[1]
p_str = strings.Replace(p_str, ",", "", -1)
util.SetHome(listen_for_user, p_str)
stdin.Write([]byte("tell " + listen_for_user + " Set your home to " + p_str))
listen_for = ""
listen_for_user = ""
}
} else {
if m.IsStopRequest() {
stdin.Write([]byte("stop\n"))
} else if m.IsHomeRequest() {
home_str, found := util.GetHome(m.User.Name)
if found {
stdin.Write([]byte("tp " + m.User.Name + " " + home_str + "\n"))
}
} else if m.IsSetHomeRequest() {
listen_for = "Teleported " + m.User.Name + " to "
listen_for_user = m.User.Name
stdin.Write([]byte("tp " + m.User.Name + " ~ ~ ~\n"))
} else if m.IsVisitRequest() {
visiting_user, err := m.VisitingUser()
if !err {
fmt.Printf("\x1b[31;1m%s requested a tp visit to %s\x1b[0m\n", m.User.Name, visiting_user)
porch_str, found := util.GetPorch(visiting_user)
fmt.Printf(">>> Porch String: " + porch_str)
if found {
stdin.Write([]byte("tp " + m.User.Name + " " + porch_str + "\n"))
} else {
stdin.Write([]byte("tell " + m.User.Name + " Couldn't find that user's porch 1\n"))
}
} else {
stdin.Write([]byte("tell " + m.User.Name + " Couldn't find that user's porch 2\n"))
}
} else if m.IsSetPorchRequest() {
listen_for = "Teleported " + m.User.Name + " to "
listen_for_user = m.User.Name
stdin.Write([]byte("tp " + m.User.Name + " ~ ~ ~\n"))
} else {
// fmt.Printf("\x1b[34;1m%s\x1b[0m", m.Output())
}
}
fmt.Printf("\x1b[34;1m%s\x1b[0m", m.Output())
mm.ProcessMessage(s)
}
}

View File

@ -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))
}
}

View File

@ -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) {
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
}